Utils

Timers

Timer decorators

luna_backport4.crutches_on_wheels.utils.timer.printLogger(self, func, duration)[source]

Print timing to log

Parameters:
  • self – class instance with logger
  • func – measured function
  • duration – calling duration of function in secs
Return type:

None

luna_backport4.crutches_on_wheels.utils.timer.timer(func)[source]

Decorator for function work time estimation.

Parameters:func – decorating function
Returns:decorated function.
Return type:Callable

Common regexps

luna_backport4.crutches_on_wheels.utils.regexps.REQUEST_ID_REGEXP

compiled regexp for request id string

Loggers

Loggers module.

class luna_backport4.crutches_on_wheels.utils.log.LogDest[source]

Log destination

file = 'file'

destination is log file

stdout = 'stdout'

destination is stdout

class luna_backport4.crutches_on_wheels.utils.log.LogSettings(appName, logLevel, logTime, folderForLog, maxSize, logNameSuffix, logToStdOut, logToFile, multilineStackTrace)[source]

Logger settings container

class luna_backport4.crutches_on_wheels.utils.log.Logger(requestId=None, template=None)[source]

Application logger

static buildFormatString(isLogLevelDebug)[source]

Build appropriate format string for logger.

Return type:str
static generateHandler(formatString, logDestination, multilineStackTrace, logLevel, logFileName=None, maxSize=1024)[source]

Generate log handler.

Parameters:
  • logDestination – to file or to stdout
  • logLevel – log level
  • maxSize – max log file size, 1024 mb for default, 0 - disable rotate
  • multilineStackTrace – allow a multiline line stack trace message
  • logFileName – log filename
  • formatString – format string
Returns:

handler

Return type:

Handler

static getLogLevel(logLevel)[source]

Get log level from config for logger.

Returns:if logLevel not set or incorrect will return logbook.NOTSET
Return type:int
Return type:int
classmethod initiate(appName='', logLevel='DEBUG', logTime='LOCAL', folderForLog='./', maxSize=1024, logNameSuffix=None, logToStdOut=True, logToFile=True, multilineStackTrace=True)[source]

Initiate class settings.

Parameters:
  • appName – application name
  • logLevel – log level
  • logTime – time of logs
  • folderForLog – folder with log-files
  • maxSize – max log file size, 1024 mb for default, 0 - disable rotate
  • logNameSuffix – optional suffix name for log name files.
  • logToStdOut – flush log to stdout
  • logToFile – flush log to a log file
  • multilineStackTrace – allow a multiline line stack trace message
Return type:

None

classmethod reInitialize(appName='', logLevel='DEBUG', logTime='LOCAL', folderForLog='./', maxSize=1024, logNameSuffix=None, logToStdOut=True, logToFile=True, multilineStackTrace=True, force=False)[source]

Re-Initiate class settings.

Parameters:
  • appName – application name
  • logLevel – log level
  • logTime – time of logs
  • folderForLog – folder with log-files
  • maxSize – max log file size, 1024 mb for default, 0 - disable rotate
  • logNameSuffix – optional suffix name for log name files.
  • logToStdOut – flush log to stdout
  • logToFile – flush log to a log file
  • multilineStackTrace – allow a multiline line stack trace message
  • force – reinitialize all settings
Return type:

None

classmethod reInitializeFileLogging(settings)[source]

Re-Initialize file logs handlers. Closes old handlers and create new if it iis needed :param settings: settings for re-initialization

classmethod reInitializeLogTime(settings)[source]

Re Initialize log time :param settings: settings for re-initialization

classmethod reInitializeStdoutLogging(settings)[source]

Re-Initialize stdout log handlers. Closes old handlers and create new if it iis needed :param settings: settings for re-initialization

class luna_backport4.crutches_on_wheels.utils.log.OneLineExceptionFileHandler(filename, mode='a', encoding=None, level=0, format_string=None, delay=False, filter=None, bubble=False)[source]

File handler that logging exception stack-trace in one line

formatter_class

alias of OneLineExceptionFormatter

class luna_backport4.crutches_on_wheels.utils.log.OneLineExceptionFormatter(format_string)[source]

StringFormatter that logging exception stack-trace in one line

class luna_backport4.crutches_on_wheels.utils.log.OneLineExceptionRotatingHandler(filename, mode='a', encoding='utf-8', level=0, format_string=None, delay=False, max_size=1048576, backup_count=5, filter=None, bubble=False)[source]

File rotate handler that logging exception stack-trace in one line

formatter_class

alias of OneLineExceptionFormatter

class luna_backport4.crutches_on_wheels.utils.log.OneLineExceptionStreamHandler(stream, level=0, format_string=None, encoding=None, filter=None, bubble=False)[source]

Stdout handler that logs an exception Traceback on a single line

formatter_class

alias of OneLineExceptionFormatter

Module realizes getting and checking request id

luna_backport4.crutches_on_wheels.utils.rid.TIME_ZONE_DELTA

time zone of machine

luna_backport4.crutches_on_wheels.utils.rid.DEFAULT_REQUEST_ID = '1622210337,00000000-0000-4000-a000-000000247495'

request id for record which is initiated of the service

luna_backport4.crutches_on_wheels.utils.rid.TIME_ZONE_DELTA = -10800

correction for getting timestamp in utc

luna_backport4.crutches_on_wheels.utils.rid.checkRequestId(requestId)[source]

Validate request id str

Parameters:requestId – str for checking
Returns:True if requestId match with REQUEST_ID_REGEXP otherwise False
Return type:bool
luna_backport4.crutches_on_wheels.utils.rid.generateRequestId(timeIsLocal)[source]

Generate correct request id.

Parameters:timeIsLocal – timestamp generate in localtime or not
Returns:standard request id string.
Return type:str
luna_backport4.crutches_on_wheels.utils.rid.getLogTimestamp(timeIsLocal)[source]

Generate timestamp for logs

Parameters:timeIsLocal – timestamp generate in localtime or not
Returns:timestamp with
Return type:int

Module realizes useful function.

luna_backport4.crutches_on_wheels.utils.functions.addCallBack(callableFunction, sleepTime=0)[source]

Function creates infinity loop, where call function :param callableFunction: function to call :param sleepTime: time in secods if callback is periodic, in seconds

Return type:None
luna_backport4.crutches_on_wheels.utils.functions.convertDateTimeToCurrentFormatDateTime(dateTimeString, currentTimeFormat)[source]

Convert datetime string from UTC or LOCAL to datetime :param dateTimeString: str with datetime :param currentTimeFormat: LOCAL or UTC

Returns:datetime with/without timezone depends on currentTimeFormat
>>> t1 = convertDateTimeToCurrentFormatDateTime('2018-12-25 12:21:46.652495Z', 'LOCAL')
>>> print(type(t1), t1)
<class 'datetime.datetime'> 2018-12-25 12:21:46.652495
>>> t2 = convertDateTimeToCurrentFormatDateTime('2018-12-25 15:21:46.652495+03:00', 'LOCAL')
>>> print(type(t2), t2)
<class 'datetime.datetime'> 2018-12-25 12:21:46.652495
>>> t1 = convertDateTimeToCurrentFormatDateTime('2018-12-25 12:21:46.652495Z', 'UTC')
>>> print(type(t1), t1)
<class 'datetime.datetime'> 2018-12-25 12:21:46.652495+00:00
>>> t2 = convertDateTimeToCurrentFormatDateTime('2018-12-25 15:21:46.652495+03:00', 'UTC')
>>> print(type(t2), t2)
<class 'datetime.datetime'> 2018-12-25 15:21:46.652495+03:00
Return type:datetime
luna_backport4.crutches_on_wheels.utils.functions.convertDateTimeToCurrentFormatStr(dateTimeString, currentTimeFormat)[source]

Convert datetime string from UTC or LOCAL to string, considering current STORAGE_TIME :param dateTimeString: str with datetime :param currentTimeFormat: LOCAL or UTC

Returns:str with datetime in current format
Return type:str
luna_backport4.crutches_on_wheels.utils.functions.convertEnumToSnakeDict(enum)[source]

Convert enum with camelCase name to dict with snake_case keys

Parameters:enum – enum
Returns:dict
Return type:dict
luna_backport4.crutches_on_wheels.utils.functions.currentTimestamp(logTime='LOCAL', dbType='postgres')[source]

Function get time (local or UTC, depends on logTime). The function is needed to correctly fill in the fields in different databases

Parameters:
  • logTime – LOCAL or UTC, LOCAL for default
  • dbType – db type (oracle/postgres/vertica), postgres for default
Returns:

%M:%S.%fZ’

Return type:

Timestamp if format ‘%Y-%m-%dT%H

Return type:

Union[str, datetime]

luna_backport4.crutches_on_wheels.utils.functions.getPageCount(objectCount, pageSize)[source]

Calculate count of pages with adjusted page size and object count

Parameters:
  • objectCount – count of objects
  • pageSize – page size
Returns:

count of pages

Return type:

int