encommon.config package#

Submodules#

encommon.config.config module#

class encommon.config.config.Config(files: PATHABLE | None = None, *, paths: PATHABLE | None = None, cargs: dict[str, Any] | None = None, sargs: dict[str, Any] | None = None, valid: Callable | None = None)[source]#

Bases: object

Contain the configurations from the arguments and files.

Note

Configuration loaded from files is validated with the Pydantic model encommon.config.Params.

Example#

>>> config = Config()
>>> config.config
{'enconfig': None, 'encrypts': None, 'enlogger': None}
param files:

Complete or relative path to config files.

param paths:

Complete or relative path to config paths.

param cargs:

Configuration arguments in dictionary form, which will override contents from the config files.

param sargs:

Additional arguments on the command line.

param valid:

Override default config validation model.

property files: ConfigFiles#

Return the value for the attribute from class instance.

Returns:

Value for the attribute from class instance.

property paths: ConfigPaths#

Return the value for the attribute from class instance.

Returns:

Value for the attribute from class instance.

property cargs: dict[str, Any]#

Return the value for the attribute from class instance.

Returns:

Value for the attribute from class instance.

property sargs: dict[str, Any]#

Return the value for the attribute from class instance.

Returns:

Value for the attribute from class instance.

property basic: dict[str, Any]#

Return the configuration source loaded from the objects.

Returns:

Configuration source loaded from the objects.

property merge: dict[str, Any]#

Return the configuration source loaded from the objects.

Returns:

Configuration source loaded from the objects.

property valid: Callable#

Return the value for the attribute from class instance.

Returns:

Value for the attribute from class instance.

property params: Params#

Return the Pydantic model containing the configuration.

Returns:

Pydantic model containing the configuration.

property config: dict[str, Any]#

Return the configuration dumped from the Pydantic model.

Returns:

Configuration dumped from the Pydantic model.

property logger: Logger#

Initialize the Python logging library using parameters.

Returns:

Instance of Python logging library created.

property crypts: Crypts#

Initialize the encryption instance using the parameters.

Returns:

Instance of the encryption instance created.

property jinja2: Jinja2#

Return the value for the attribute from class instance.

Returns:

Value for the attribute from class instance.

encommon.config.files module#

class encommon.config.files.ConfigFile(path: str | Path)[source]#

Bases: object

Contain the configuration content from filesystem path.

Parameters:

path – Complete or relative path to configuration.

path: Path#
config: dict[str, Any]#
class encommon.config.files.ConfigFiles(paths: PATHABLE, force: bool = False)[source]#

Bases: object

Enumerate files and store the contents on relative path.

Note

Class can be empty in order to play nice with parent.

Parameters:
  • paths – Complete or relative path to config files.

  • force – Force the merge on earlier files by later.

paths: tuple[Path, ...]#
config: dict[str, ConfigFile]#
property merge: dict[str, Any]#

Return the configuration in dictionary format for files.

Returns:

Configuration in dictionary format for files.

encommon.config.logger module#

class encommon.config.logger.Message(level: Literal['critical', 'debug', 'error', 'info', 'warning'], time: PARSABLE | None = None, **kwargs: Any)[source]#

Bases: object

Format the provided keyword arguments for logging output.

Note

Log messages are expected to contain string or numeric.

Example#

>>> message = Message('info', '1970-01-01', foo='bar')
>>> strip_ansi(message.stdo_output)
'level="info" time="1970-01-01T00:00:00Z" foo="bar"'
param level:

Severity which log message is classified.

param time:

What time the log message actually occurred.

param kwargs:

Keyword arguments for populating message.

property level: Literal['critical', 'debug', 'error', 'info', 'warning']#

Return the value for the attribute from class instance.

Returns:

Value for the attribute from class instance.

property time: Time#

Return the value for the attribute from class instance.

Returns:

Value for the attribute from class instance.

property fields: dict[str, str]#

Return the value for the attribute from class instance.

Returns:

Value for the attribute from class instance.

property stdo_output: str#

Format keyword arguments for writing to standard output.

Returns:

String representation for the standard output.

property file_output: str#

Format keyword arguments for writing to filesystem path.

Returns:

String representation for the filesystem path.

class encommon.config.logger.FileFormatter(fmt=None, datefmt=None, style='%', validate=True, *, defaults=None)[source]#

Bases: Formatter

Supplement class for built-in logger exception formatter.

Note

Input parameters are not defined, check parent class.

formatException(ei: Any) str[source]#

Specifically overrides method for formatting exceptions.

Parameters:

ei – Exception information provided by the logger.

Returns:

String representation for the filesystem path.

class encommon.config.logger.Logger(params: LoggerParams | None = None, *, stdo_level: Literal['critical', 'debug', 'error', 'info', 'warning'] | None = None, file_level: Literal['critical', 'debug', 'error', 'info', 'warning'] | None = None, file_path: str | Path | None = None)[source]#

Bases: object

Manage the file and standard output with logging library.

Note

Uses keyword name for levels in Pyton logging library.

Numeric

Keyword

10

debug

20

info

30

warning

40

error

50

critical

Example#

>>> logger = Logger(stdo_level='info')
>>> logger.start()
>>> logger.log_i(message='testing')
param stdo_level:

Minimum level for the message to pass.

param file_level:

Minimum level for the message to pass.

param file_path:

Enables writing to the filesystem path.

param params:

Parameters used to instantiate the class.

property params: LoggerParams#

Return the Pydantic model containing the configuration.

Returns:

Pydantic model containing the configuration.

property stdo_level: Literal['critical', 'debug', 'error', 'info', 'warning'] | None#

Return the value for the attribute from class instance.

Returns:

Value for the attribute from class instance.

property file_level: Literal['critical', 'debug', 'error', 'info', 'warning'] | None#

Return the value for the attribute from class instance.

Returns:

Value for the attribute from class instance.

property file_path: Path | None#

Return the value for the attribute from class instance.

Returns:

Value for the attribute from class instance.

property started: bool#

Return the value for the attribute from class instance.

Returns:

Value for the attribute from class instance.

property logger_stdo: Logger#

Return the value for the attribute from class instance.

Returns:

Value for the attribute from class instance.

property logger_file: Logger#

Return the value for the attribute from class instance.

Returns:

Value for the attribute from class instance.

start() None[source]#

Initialize the Python logging library using parameters.

stop() None[source]#

Deinitialize the Python logging library using parameters.

log(level: Literal['critical', 'debug', 'error', 'info', 'warning'], *, exc_info: Exception | None = None, **kwargs: Any) None[source]#

Prepare keyword arguments and log to configured output.

Parameters:
  • exc_info – Optional exception included with trace.

  • kwargs – Keyword arguments for populating message.

log_c(**kwargs: Any) None[source]#

Prepare keyword arguments and log to configured output.

Parameters:

kwargs – Keyword arguments for populating message.

log_d(**kwargs: Any) None[source]#

Prepare keyword arguments and log to configured output.

Parameters:

kwargs – Keyword arguments for populating message.

log_e(**kwargs: Any) None[source]#

Prepare keyword arguments and log to configured output.

Parameters:

kwargs – Keyword arguments for populating message.

log_i(**kwargs: Any) None[source]#

Prepare keyword arguments and log to configured output.

Parameters:

kwargs – Keyword arguments for populating message.

log_w(**kwargs: Any) None[source]#

Prepare keyword arguments and log to configured output.

Parameters:

kwargs – Keyword arguments for populating message.

encommon.config.params module#

class encommon.config.params.ConfigParams[source]#

Bases: BaseModel

Process and validate the core configuration parameters.

Fields#

Field

Type

Required

Default

Constraints

paths

list[str] | None

No

None

min_length=1

paths: Annotated[list[str] | None, FieldInfo(annotation=NoneType, required=False, default=None, description='Location of configuration files', metadata=[MinLen(min_length=1)])]#
class encommon.config.params.LoggerParams[source]#

Bases: BaseModel

Process and validate the core configuration parameters.

Fields#

Field

Type

Required

Default

Constraints

file_level

Literal[‘critical’, ‘debug’, ‘error’, ‘info’, ‘warning’] | None

No

None

min_length=1

file_path

str | None

No

None

min_length=1

stdo_level

Literal[‘critical’, ‘debug’, ‘error’, ‘info’, ‘warning’] | None

No

None

min_length=1

stdo_level: Annotated[Literal['critical', 'debug', 'error', 'info', 'warning'] | None, FieldInfo(annotation=NoneType, required=False, default=None, description='Minimum logging message level', metadata=[MinLen(min_length=1)])]#
file_level: Annotated[Literal['critical', 'debug', 'error', 'info', 'warning'] | None, FieldInfo(annotation=NoneType, required=False, default=None, description='Minimum logging message level', metadata=[MinLen(min_length=1)])]#
file_path: Annotated[str | None, FieldInfo(annotation=NoneType, required=False, default=None, description='Enable output to the log file', metadata=[MinLen(min_length=1)])]#
class encommon.config.params.Params[source]#

Bases: BaseModel

Process and validate the core configuration parameters.

Fields#

Field

Type

Required

Default

enconfig

ConfigParams | None

No

None

encrypts

CryptsParams | None

No

None

enlogger

LoggerParams | None

No

None

enconfig: Annotated[ConfigParams | None, FieldInfo(annotation=NoneType, required=False, default=None, description='Parameters for Config instance')]#
enlogger: Annotated[LoggerParams | None, FieldInfo(annotation=NoneType, required=False, default=None, description='Parameters for Logger instance')]#
encrypts: Annotated[CryptsParams | None, FieldInfo(annotation=NoneType, required=False, default=None, description='Parameters for Crypts instance')]#

encommon.config.paths module#

class encommon.config.paths.ConfigPath(path: str | Path)[source]#

Bases: object

Contain the configuration content from filesystem path.

Parameters:

path – Complete or relative path to configuration.

path: Path#
config: dict[str, ConfigFile]#
class encommon.config.paths.ConfigPaths(paths: PATHABLE, force: bool = False)[source]#

Bases: object

Enumerate paths and store the contents on relative path.

Note

Class can be empty in order to play nice with parent.

Parameters:
  • paths – Complete or relative path to config paths.

  • force – Force the merge on earlier files by later.

paths: tuple[Path, ...]#
config: dict[str, ConfigPath]#
property merge: dict[str, Any]#

Return the configuration in dictionary format for paths.

Returns:

Configuration in dictionary format for paths.

encommon.config.utils module#

encommon.config.utils.config_load(path: str | Path) dict[str, Any][source]#

Load configuration using the directory or file provided.

Parameters:

path – Complete or relative path to configuration.

Returns:

New resolved filesystem path object instance.

encommon.config.utils.config_path(path: str | Path) Path[source]#

Resolve the provided path replacing the magic keywords.

Note

This function simply wraps one from utils subpackage.

Parameters:

path – Complete or relative path for processing.

Returns:

New resolved filesystem path object instance.

encommon.config.utils.config_paths(paths: PATHABLE) tuple[Path, ...][source]#

Resolve the provided paths replacing the magic keywords.

Note

This function simply wraps one from utils subpackage.

Parameters:

paths – Complete or relative paths for processing.

Returns:

New resolved filesystem path object instances.

Module contents#

class encommon.config.Config(files: PATHABLE | None = None, *, paths: PATHABLE | None = None, cargs: dict[str, Any] | None = None, sargs: dict[str, Any] | None = None, valid: Callable | None = None)[source]#

Bases: object

Contain the configurations from the arguments and files.

Note

Configuration loaded from files is validated with the Pydantic model encommon.config.Params.

Example#

>>> config = Config()
>>> config.config
{'enconfig': None, 'encrypts': None, 'enlogger': None}
param files:

Complete or relative path to config files.

param paths:

Complete or relative path to config paths.

param cargs:

Configuration arguments in dictionary form, which will override contents from the config files.

param sargs:

Additional arguments on the command line.

param valid:

Override default config validation model.

property files: ConfigFiles#

Return the value for the attribute from class instance.

Returns:

Value for the attribute from class instance.

property paths: ConfigPaths#

Return the value for the attribute from class instance.

Returns:

Value for the attribute from class instance.

property cargs: dict[str, Any]#

Return the value for the attribute from class instance.

Returns:

Value for the attribute from class instance.

property sargs: dict[str, Any]#

Return the value for the attribute from class instance.

Returns:

Value for the attribute from class instance.

property basic: dict[str, Any]#

Return the configuration source loaded from the objects.

Returns:

Configuration source loaded from the objects.

property merge: dict[str, Any]#

Return the configuration source loaded from the objects.

Returns:

Configuration source loaded from the objects.

property valid: Callable#

Return the value for the attribute from class instance.

Returns:

Value for the attribute from class instance.

property params: Params#

Return the Pydantic model containing the configuration.

Returns:

Pydantic model containing the configuration.

property config: dict[str, Any]#

Return the configuration dumped from the Pydantic model.

Returns:

Configuration dumped from the Pydantic model.

property logger: Logger#

Initialize the Python logging library using parameters.

Returns:

Instance of Python logging library created.

property crypts: Crypts#

Initialize the encryption instance using the parameters.

Returns:

Instance of the encryption instance created.

property jinja2: Jinja2#

Return the value for the attribute from class instance.

Returns:

Value for the attribute from class instance.

class encommon.config.ConfigFile(path: str | Path)[source]#

Bases: object

Contain the configuration content from filesystem path.

Parameters:

path – Complete or relative path to configuration.

path: Path#
config: dict[str, Any]#
class encommon.config.ConfigFiles(paths: PATHABLE, force: bool = False)[source]#

Bases: object

Enumerate files and store the contents on relative path.

Note

Class can be empty in order to play nice with parent.

Parameters:
  • paths – Complete or relative path to config files.

  • force – Force the merge on earlier files by later.

paths: tuple[Path, ...]#
config: dict[str, ConfigFile]#
property merge: dict[str, Any]#

Return the configuration in dictionary format for files.

Returns:

Configuration in dictionary format for files.

class encommon.config.ConfigParams[source]#

Bases: BaseModel

Process and validate the core configuration parameters.

Fields#

Field

Type

Required

Default

Constraints

paths

list[str] | None

No

None

min_length=1

paths: Annotated[list[str] | None, FieldInfo(annotation=NoneType, required=False, default=None, description='Location of configuration files', metadata=[MinLen(min_length=1)])]#
class encommon.config.ConfigPath(path: str | Path)[source]#

Bases: object

Contain the configuration content from filesystem path.

Parameters:

path – Complete or relative path to configuration.

path: Path#
config: dict[str, ConfigFile]#
class encommon.config.ConfigPaths(paths: PATHABLE, force: bool = False)[source]#

Bases: object

Enumerate paths and store the contents on relative path.

Note

Class can be empty in order to play nice with parent.

Parameters:
  • paths – Complete or relative path to config paths.

  • force – Force the merge on earlier files by later.

paths: tuple[Path, ...]#
config: dict[str, ConfigPath]#
property merge: dict[str, Any]#

Return the configuration in dictionary format for paths.

Returns:

Configuration in dictionary format for paths.

class encommon.config.Logger(params: LoggerParams | None = None, *, stdo_level: Literal['critical', 'debug', 'error', 'info', 'warning'] | None = None, file_level: Literal['critical', 'debug', 'error', 'info', 'warning'] | None = None, file_path: str | Path | None = None)[source]#

Bases: object

Manage the file and standard output with logging library.

Note

Uses keyword name for levels in Pyton logging library.

Numeric

Keyword

10

debug

20

info

30

warning

40

error

50

critical

Example#

>>> logger = Logger(stdo_level='info')
>>> logger.start()
>>> logger.log_i(message='testing')
param stdo_level:

Minimum level for the message to pass.

param file_level:

Minimum level for the message to pass.

param file_path:

Enables writing to the filesystem path.

param params:

Parameters used to instantiate the class.

property params: LoggerParams#

Return the Pydantic model containing the configuration.

Returns:

Pydantic model containing the configuration.

property stdo_level: Literal['critical', 'debug', 'error', 'info', 'warning'] | None#

Return the value for the attribute from class instance.

Returns:

Value for the attribute from class instance.

property file_level: Literal['critical', 'debug', 'error', 'info', 'warning'] | None#

Return the value for the attribute from class instance.

Returns:

Value for the attribute from class instance.

property file_path: Path | None#

Return the value for the attribute from class instance.

Returns:

Value for the attribute from class instance.

property started: bool#

Return the value for the attribute from class instance.

Returns:

Value for the attribute from class instance.

property logger_stdo: Logger#

Return the value for the attribute from class instance.

Returns:

Value for the attribute from class instance.

property logger_file: Logger#

Return the value for the attribute from class instance.

Returns:

Value for the attribute from class instance.

start() None[source]#

Initialize the Python logging library using parameters.

stop() None[source]#

Deinitialize the Python logging library using parameters.

log(level: Literal['critical', 'debug', 'error', 'info', 'warning'], *, exc_info: Exception | None = None, **kwargs: Any) None[source]#

Prepare keyword arguments and log to configured output.

Parameters:
  • exc_info – Optional exception included with trace.

  • kwargs – Keyword arguments for populating message.

log_c(**kwargs: Any) None[source]#

Prepare keyword arguments and log to configured output.

Parameters:

kwargs – Keyword arguments for populating message.

log_d(**kwargs: Any) None[source]#

Prepare keyword arguments and log to configured output.

Parameters:

kwargs – Keyword arguments for populating message.

log_e(**kwargs: Any) None[source]#

Prepare keyword arguments and log to configured output.

Parameters:

kwargs – Keyword arguments for populating message.

log_i(**kwargs: Any) None[source]#

Prepare keyword arguments and log to configured output.

Parameters:

kwargs – Keyword arguments for populating message.

log_w(**kwargs: Any) None[source]#

Prepare keyword arguments and log to configured output.

Parameters:

kwargs – Keyword arguments for populating message.

class encommon.config.LoggerParams[source]#

Bases: BaseModel

Process and validate the core configuration parameters.

Fields#

Field

Type

Required

Default

Constraints

file_level

Literal[‘critical’, ‘debug’, ‘error’, ‘info’, ‘warning’] | None

No

None

min_length=1

file_path

str | None

No

None

min_length=1

stdo_level

Literal[‘critical’, ‘debug’, ‘error’, ‘info’, ‘warning’] | None

No

None

min_length=1

stdo_level: Annotated[Literal['critical', 'debug', 'error', 'info', 'warning'] | None, FieldInfo(annotation=NoneType, required=False, default=None, description='Minimum logging message level', metadata=[MinLen(min_length=1)])]#
file_level: Annotated[Literal['critical', 'debug', 'error', 'info', 'warning'] | None, FieldInfo(annotation=NoneType, required=False, default=None, description='Minimum logging message level', metadata=[MinLen(min_length=1)])]#
file_path: Annotated[str | None, FieldInfo(annotation=NoneType, required=False, default=None, description='Enable output to the log file', metadata=[MinLen(min_length=1)])]#
class encommon.config.Message(level: Literal['critical', 'debug', 'error', 'info', 'warning'], time: PARSABLE | None = None, **kwargs: Any)[source]#

Bases: object

Format the provided keyword arguments for logging output.

Note

Log messages are expected to contain string or numeric.

Example#

>>> message = Message('info', '1970-01-01', foo='bar')
>>> strip_ansi(message.stdo_output)
'level="info" time="1970-01-01T00:00:00Z" foo="bar"'
param level:

Severity which log message is classified.

param time:

What time the log message actually occurred.

param kwargs:

Keyword arguments for populating message.

property level: Literal['critical', 'debug', 'error', 'info', 'warning']#

Return the value for the attribute from class instance.

Returns:

Value for the attribute from class instance.

property time: Time#

Return the value for the attribute from class instance.

Returns:

Value for the attribute from class instance.

property fields: dict[str, str]#

Return the value for the attribute from class instance.

Returns:

Value for the attribute from class instance.

property stdo_output: str#

Format keyword arguments for writing to standard output.

Returns:

String representation for the standard output.

property file_output: str#

Format keyword arguments for writing to filesystem path.

Returns:

String representation for the filesystem path.

class encommon.config.Params[source]#

Bases: BaseModel

Process and validate the core configuration parameters.

Fields#

Field

Type

Required

Default

enconfig

ConfigParams | None

No

None

encrypts

CryptsParams | None

No

None

enlogger

LoggerParams | None

No

None

enconfig: Annotated[ConfigParams | None, FieldInfo(annotation=NoneType, required=False, default=None, description='Parameters for Config instance')]#
enlogger: Annotated[LoggerParams | None, FieldInfo(annotation=NoneType, required=False, default=None, description='Parameters for Logger instance')]#
encrypts: Annotated[CryptsParams | None, FieldInfo(annotation=NoneType, required=False, default=None, description='Parameters for Crypts instance')]#
encommon.config.config_load(path: str | Path) dict[str, Any][source]#

Load configuration using the directory or file provided.

Parameters:

path – Complete or relative path to configuration.

Returns:

New resolved filesystem path object instance.

encommon.config.config_path(path: str | Path) Path[source]#

Resolve the provided path replacing the magic keywords.

Note

This function simply wraps one from utils subpackage.

Parameters:

path – Complete or relative path for processing.

Returns:

New resolved filesystem path object instance.

encommon.config.config_paths(paths: PATHABLE) tuple[Path, ...][source]#

Resolve the provided paths replacing the magic keywords.

Note

This function simply wraps one from utils subpackage.

Parameters:

paths – Complete or relative paths for processing.

Returns:

New resolved filesystem path object instances.