Utils

Timers

Timer decorators

luna_tasks.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_tasks.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_tasks.crutches_on_wheels.utils.regexps.REQUEST_ID_REGEXP

compiled regexp for request id string

Loggers

Loggers module.

class luna_tasks.crutches_on_wheels.utils.log.Formatter(fmt=None, datefmt=None, style='%', validate=True)[source]

Log record formater

converter()
localtime([seconds]) -> (tm_year,tm_mon,tm_mday,tm_hour,tm_min,

tm_sec,tm_wday,tm_yday,tm_isdst)

Convert seconds since the Epoch to a time tuple expressing local time. When ‘seconds’ is not passed in, convert the current time instead.

class luna_tasks.crutches_on_wheels.utils.log.LogDest(value)[source]

Log destination

file = 'file'

destination is log file

stdout = 'stdout'

destination is stdout

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

Logger settings container

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

Application logger.

Global logger class. This class realize following features:

  1. Class contains global handlers. All loggers will be to have this handlers.

2. Class replaces itself default logger class from standard library (logging.Logger). All third party loggers (based on logging.Logger) will be use handlers of this class

  1. method reinitialize updates all created loggers.

All features based on class variable _handlers and property handlers. They patch attribute handlers of logger from standard library and make them globals.

final classmethod addHandler(hdlr)[source]

Add handlers to all loggers

Overwrite original method. Before it was instance method, now - global classmethod :param hdlr: log handler

Return type

None

exception(*args, **kwargs)[source]

Exception without msg support. :param *args: args :param **kwargs: kwargs

Return type

None

filter(record)[source]

Add extra data (request id and app name) to record :param record: record

Returns

result of filters

Return type

bool

static getLogLevel(logLevel)[source]

Get log level from config for logger.

Returns

if logLevel not set or incorrect will return logging.NOTSET

Return type

int

Return type

int

property handlers: List[logging.Handler]

Get list of global handlers. Rewrite local handlers from logging.Logger Not save handlers to an in instance of the Logger for correct reinitialize loggers. All created loggers have these handlers.

Returns

list of global handlers.

Return type

List[Handler]

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

property level: int

Overwrite property. Not save log level to an in instance of the Logger for correct reinitialize loggers.

Returns

class log level

Return type

int

property logTime: str

Get current log time format :returns: logTime from settings

Return type

str

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. Update time converter of global formatter :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

final classmethod removeHandler(hdlr)[source]

Remove the specified handler from all loggers. Overwrite original method. Before it was instance method, now - global classmethod

Parameters

hdlr – log handler

property requestId: str

Get current request id

Returns

  1. request id from __init__ 2) from requestIdCtx 3) default

Return type

request id. priority

Return type

str

classmethod updateDefaultPythonLoggers()[source]

setup this class as default class for 3-party loggers, which use logging.getLogger, manager.getLogger, getLoggerClass.

class luna_tasks.crutches_on_wheels.utils.log.OneLineExceptionFormatter(fmt=None, datefmt=None, style='%', validate=True)[source]

One line exception log record formatter

References

https://docs.python.org/3/howto/logging-cookbook.html#customized-exception-formatting

format(record)[source]

Format an exception so that it prints on a single line.

Return type

str

luna_tasks.crutches_on_wheels.utils.log.checkThirdPartyLoggerName(name, thirdPartyLoggerWhitList)[source]

Check third party logger name that is allowed.

Parameters
  • name – third party logger name

  • thirdPartyLoggerWhitList – third party logger whit list. allowed format: - strict name - wildcard rootname.*

Returns

true if name matches with any name from thirdPartyLoggerWhitList otherwise false

>>> checkThirdPartyLoggerName("sdkloop.facedetector", THIRD_PARTY_LOGGER_WHITE_LIST)
True
>>> checkThirdPartyLoggerName("aiohttp", THIRD_PARTY_LOGGER_WHITE_LIST)
False
Return type

bool

luna_tasks.crutches_on_wheels.utils.log.generateFileHandler(logFileName, maxSize=1024)[source]

Generate file handler :param logFileName: filename :param maxSize: max size of log file

Returns

rotating file handler if maxSize> 0 otherwise usual file handler

Return type

FileHandler

luna_tasks.crutches_on_wheels.utils.log.generateStdoutLogHandler()[source]

Generate stout log handler :returns: new stdout handler

Return type

StreamHandler

luna_tasks.crutches_on_wheels.utils.log.logHandlerLock()[source]

Contextmanager for access management to logger handlers.

All changes handlers operations must be thread safe.

luna_tasks.crutches_on_wheels.utils.log.logger

disable-msg=C0103

Type

pylint

luna_tasks.crutches_on_wheels.utils.log.setUpHandler(handler, multilineStackTrace, level)[source]

Setup log handler. set log record formatter, level :param handler: handler :param multilineStackTrace: allow a multiline line stack trace message :param level: log level

Module realizes getting and checking request id

luna_tasks.crutches_on_wheels.utils.rid.DEFAULT_REQUEST_ID = '1649354768,00000000-0000-4000-a000-000000000376'

request id for record which is initiated of the service

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

correction for getting timestamp in utc

luna_tasks.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_tasks.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_tasks.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.

async luna_tasks.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_tasks.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_tasks.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_tasks.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_tasks.crutches_on_wheels.utils.functions.currentDateTime(currentTimeFormat)[source]

Function get current date-time in ISO format.

Parameters

currentTimeFormat – LOCAL or UTC

Returns

20:30.45+01:00)

Return type

date-time in rfc3339 format (e.g. 1997-07-16T19

Return type

str

luna_tasks.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]

async luna_tasks.crutches_on_wheels.utils.functions.downloadImage(url, logger, timeout, accountId=None)[source]

Download image. Add Luna-Account-Id header for request. If request to lis, apply account id to request.

Parameters
  • url – image url

  • logger – logger

  • timeout – client timeout

  • accountId – account id

Returns

tuple with image bytes and its content type

Raises

VLException(Error.FailDownloadImage.format(url), 400, isCriticalError=False) if failed to download image

Return type

tuple[bytes, str]

luna_tasks.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

luna_tasks.crutches_on_wheels.utils.functions.workingDirectory(path)[source]

Changes working directory and returns to previous on exit. :param path: temporary working directory