luminal.managers.handler

Module Contents

Classes

Handler

Wraps an IPhoton or another inherited photon base class and provides access to local instance information.

class Handler(logging, name, filepath, checksum, instance)

Wraps an IPhoton or another inherited photon base class and provides access to local instance information.

Parameters:
property name: str

Returns the name of the current Handler instance.

Returns:

The name of the current Handler instance.

Return type:

str

property filepath: str

Returns the file path of the current Handler instance.

Returns:

The file path of the current Handler instance.

Return type:

str

property checksum: str

Returns the checksum of the original photon file.

Returns:

The checksum of the original file containing the photon.

Return type:

str

__str__()

Returns the current Handler instance as its string representation.

Returns:

A string representation of the Handler object created by the __dict__() dunder method.

Return type:

str

async static _get_checksum(filename, block=2**20)

|coro|

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:

Optional[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.

  • If the specified file cannot be found or if there are any errors while generating the checksum, returns None.

async _clear_module_references()

|coro|

Clears all references for the current handler module and its submodules in sys.modules.

Returns:

A flag value indicating whether references were cleared successfully.

Return type:

bool

Notes

  • This function clears all references to the current module and any of its submodules in sys.modules. It is used to ensure that a module can be safely deleted from memory without any leftover references. The function returns True if the operation to clear references succeeds. Default is False.

async _stop_photon_threads()

|coro|

This function stops all of the running threads that belong to the current photon’s thread group. It is typically called during a photon unload to ensure that no threads are left running that could cause errors or conflicts with a new, reloaded photon.

Returns:

A flag indicating whether all photon threads were successfully stopped. This returns True if all photon threads are successfully stopped. Default is False.

Return type:

bool

async _stop_photon_processes()

|coro|

This function is responsible for stopping and terminating all of the child processes spawned by the handler.

Returns:

A flag indicating whether any child process for the handler were stopped. Default is False.

Return type:

bool

async _force_stop()

|coro|

This function is responsible for forcefully stopping and halting all the running threads and processes of the handler. It calls the _stop_photon_threads() and _stop_photon_processes() methods to perform these tasks.

Return type:

None

async _stop(force_stop=False)

|coro|

This function stops the running IPhoton instance, halting all the child threads and processes of the instance if any exist. If force_stop is True, this function calls the _force_stop() method to stop the instance. Otherwise, it first calls the finalize() method if present in the current instance. If the finalize() method does not exist or raises an error, then the _force_stop() method is called.

Parameters:

force_stop (Optional[bool]) – A boolean flag indicating whether to forcefully stop the photon instance. Default is False.

Raises:

PhotonNotInitializedError – If the photon has not been initialized.

Return type:

None

async start()

|coro|

This function starts running the IPhoton instance by creating it if it does not exist already.

Notes

  • If already started, it checks if it’s a tuple. If the instance is not a tuple, it initializes the IPhoton instance and stores it as a tuple with a second value set to True.

Return type:

None