Utils

Timers

Timer decorators

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

Print timing to log

Parameters:
  • self (object) – class instance with logger

  • func (callable) – measured function

  • duration (float) – calling duration of function in secs

Return type:

None

luna_api.crutches_on_wheels.cow.utils.timer.timer(func)[source]

Decorator for function work time estimation.

Parameters:

func (callable) – decorating function

Return type:

Callable

Returns:

decorated function.

Common regexps

luna_api.crutches_on_wheels.cow.utils.regexps.REQUEST_ID_REGEXP

compiled regexp for request id string

Loggers

Loggers module.

luna_api.crutches_on_wheels.cow.utils.log.logger = <Logger  (DEBUG)>

disable-msg=C0103

Type:

pylint

Module realizes getting and checking request id

luna_api.crutches_on_wheels.cow.utils.rid.checkRequestId(requestId)[source]

Validate request id str

Parameters:

requestId (str) – str for checking

Return type:

bool

Returns:

True if requestId match with REQUEST_ID_REGEXP otherwise False

luna_api.crutches_on_wheels.cow.utils.rid.generateRequestId(timeIsLocal)[source]

Generate correct request id.

Parameters:

timeIsLocal (bool) – timestamp generate in localtime or not

Return type:

str

Returns:

standard request id string.

Module realizes useful function.

async luna_api.crutches_on_wheels.cow.utils.functions.addCallBack(callableFunction, sleepTime=0)[source]

Function creates infinity loop, where call function

Parameters:
  • callableFunction (Callable) – function to call

  • sleepTime (int) – time in secods if callback is periodic, in seconds

Return type:

None

luna_api.crutches_on_wheels.cow.utils.functions.convertDateTimeToCurrentFormatDateTime(dateTimeString, currentTimeFormat)[source]

Convert datetime string from UTC or LOCAL to datetime

Parameters:
  • dateTimeString (str) – str with datetime

  • currentTimeFormat (str) – LOCAL or UTC

Return type:

datetime

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
luna_api.crutches_on_wheels.cow.utils.functions.convertDateTimeToCurrentFormatStr(dateTimeString, currentTimeFormat)[source]

Convert datetime string from UTC or LOCAL to string, considering current STORAGE_TIME

Parameters:
  • dateTimeString (str) – str with datetime

  • currentTimeFormat (str) – LOCAL or UTC

Return type:

str

Returns:

str with datetime in current format

luna_api.crutches_on_wheels.cow.utils.functions.convertEnumToSnakeDict(enum)[source]

Convert enum with camelCase name to dict with snake_case keys

Parameters:

enum (Type[Enum]) – enum

Return type:

dict

Returns:

dict

luna_api.crutches_on_wheels.cow.utils.functions.currentDateTime(currentTimeFormat)[source]

Function get current date-time in ISO format.

Parameters:

currentTimeFormat (str) – LOCAL or UTC

Returns:

20:30.45+01:00)

Return type:

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

luna_api.crutches_on_wheels.cow.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 (str) – LOCAL or UTC, LOCAL for default

  • dbType (str) – db type (oracle/postgres), postgres for default

Returns:

%M:%S.%fZ’

Return type:

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

async luna_api.crutches_on_wheels.cow.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 (Union[str, URL]) – image url

  • logger (Logger) – logger

  • timeout (ClientTimeout) – client timeout

  • accountId (Optional[str]) – account id

Return type:

tuple[bytes, str]

Returns:

tuple with image bytes and its content type

Raises:

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

luna_api.crutches_on_wheels.cow.utils.functions.getAnnotationWithNullable(value)[source]

Get python annotation with allowed nullable value

Notes

annotation contains 2 parts: - first part is tuple(…) + None - required for good validation error messages - second part | None - required to allow None as value

Parameters:

value (EnumType | dict | list | tuple) – incoming value

Return type:

type[Literal[]]

Returns:

annotation

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

Calculate count of pages with adjusted page size and object count

Parameters:
  • objectCount (int) – count of objects

  • pageSize (int) – page size

Return type:

int

Returns:

count of pages

luna_api.crutches_on_wheels.cow.utils.functions.workingDirectory(path)[source]

Changes working directory and returns to previous on exit.

Parameters:

path (Path) – temporary working directory