luminal.tools.utils

Module Contents

Classes

SystemUtils

Encapsulation of common operating system utilities.

TextUtils

Contains a collection of text generation utilities such as random id and cid strings.

class SystemUtils

Encapsulation of common operating system utilities.

windows = 'nt'
linux = 'posix'
macos = 'posix'
cygwin = 'posix'
static get_system()

Returns the name of the currently running operating system.

Returns:

The name of the current system type.

Return type:

str

static get_system_delimiter()

Returns a delimiter character which is appropriate for the current operating system.

Returns:

The delimiter used during parsing and concatenation of strings.

Return type:

str

static get_line(filepath)

Returns the first line of a provided file in a read-only fashion.

Parameters:

filepath (str) – The path of the file to read a line.

Raises:
  • IOError – The file could not be opened or modified.

  • Exception – The file could not be written in general.

Returns:

The line that was read from the file.

Return type:

str

static read_from_file(filepath)

Returns all data of a provided file in a read-only fashion.

Parameters:

filepath (str) – The path of the file to read.

Raises:
  • IOError – The file could not be opened or modified.

  • Exception – The file could not be written in general.

Returns:

Data that was read from the file.

Return type:

str

static write_to_file(filepath, data)

Writes all data to the provided file in a truncated fashion.

Parameters:
  • filepath (str) – The path of the file to write.

  • data (str) – The data to be written to the file.

Raises:
  • IOError – The file could not be opened or modified.

  • Exception – The file could not be written in general.

Returns:

The return value of the open.write() function.

Return type:

int

static get_file_checksum(filename, block=2**20)

Generates a calculated SHA512 hash for a given file.

Parameters:
  • filename (str) – The name of the file to generate the checksum for.

  • block (Optional[int]) – Chunk size to read and hash the file in bytes. Default is 2^20.

Returns:

The calculated SHA512 hash of the file, or None if there was an error generating the checksum.

Return type:

str

Notes

  • This function generates a SHA512 hash for a given file by reading the file in blocks and hashing each block. The generated hash is a digest checksum (a unique fixed-sized representation of the file content). The file is treated as a binary file (read in rb mode) for proper handling of all types of files.

  • The reason SHA512 was chosen is purely for the lack of collisions at runtime when performing dynamic checks.

static is_submodule(parent, child)

Returns a flag if the compared child module is from the parent and contains a {parent}.{child} path structure.

Parameters:
  • parent (str) – The path of the original parent sys module.

  • child (str) – The path of the child sys module.

Returns:

A flag determining if the child is a submodule of the parent module.

Return type:

bool

async static continue_async(delay=sys.float_info.min)

|coro|

Sleeps the thread for the least possible amount of time for the system in order to allow a function to be truly async and awaitable; or a specified time delay in seconds.

Parameters:

delay (Optional[float]) – The amount of time to sleep the thread.

Return type:

None

class TextUtils

Contains a collection of text generation utilities such as random id and cid strings.

static generate_id(length=10, use_sample=False)

Returns an alphanumeric identifier based on a given length and random sampling, if desired.

Parameters:
Returns:

The generated id of a specified length.

Return type:

str

static generate_cid(length=64)

Returns a Base64 encoded cryptographically strong random identifier.

Parameters:

length (Optional[int]) – The length of the identifier to be generated.

Returns:

The generated cryptographic id of a specified length.

Return type:

str