pyfileconf.opts package

Options system for pyfileconf

To add a new option, the attribute and type hint to PyfileconfOptions, add it to PyfileconfOptions.option_attrs, add it to init and initialize it with the default, and add a param docstring for it to PyfileconfOptionsManager.

Optionally, functions can be registered in PyfileconfOptions.option_callbacks to perform an action upon setting or resetting the option. The callbacks should accept the name of the option as the first argument and the value being set as the second argument.

class pyfileconf.opts.PyfileconfOption(name, callback=None)[source]

Bases: object

__init__(name, callback=None)[source]

Initialize self. See help(type(self)) for accurate signature.

property default_value
Return type

Any

reset()[source]
property value
Return type

Any

class pyfileconf.opts.PyfileconfOptions(log_stdout=False, log_folder=None, log_file_rollover_freq='D', log_file_num_keep=0)[source]

Bases: object

__init__(log_stdout=False, log_folder=None, log_file_rollover_freq='D', log_file_num_keep=0)[source]

Initialize self. See help(type(self)) for accurate signature.

log_file_num_keep: int
log_file_rollover_freq: str
log_folder: Optional[str]
log_stdout: bool
option_attrs: Tuple[str, …] = ('log_stdout', 'log_folder', 'log_file_rollover_freq', 'log_file_num_keep')
option_callbacks: Dict[str, Callable[[str, Any], None]] = {'log_folder': <function add_file_handler_if_log_folder_exists_else_remove_handler>}
update(opts)[source]
class pyfileconf.opts.PyfileconfOptionsManager[source]

Bases: object

Allows setting options for the pyfileconf library

Usage

>>> import pyfileconf

Use as a context manager with a single change:

>>> with pyfileconf.options.set_option('log_stdout', True):
>>>      # Do something
>>> # Now options have been reset

Use as a context manager with multiple individual changes:

>>> with pyfileconf.options:
>>>     # Change using set_option and the attribute name
>>>     pyfileconf.options.set_option('log_stdout', True)
>>>     # Change using option value attribute
>>>     pyfileconf.options.log_stdout = True
>>>     # More options changes, then desired operations
>>> # Now options have been reset

Use as a context manager with multiple changes at once:

>>> with pyfileconf.options.set_options([('log_stdout', True), ('log_folder', 'MyLogs')]):
>>>     # Operations needing options
>>> # Now options have been reset

Usage without context manager. The following three are equivalent to set options:

>>> pyfileconf.options.set_option("log_stdout", True)
>>> pyfileconf.options.set_options([("log_stdout", True)])
>>> pyfileconf.options.log_stdout.value = True

When using without the context manager, options will last until they are reset. Options can be reset individually, as a group, or all at once.

>>> pyfileconf.options.log_stdout.reset()  # reset only the log_stdout option
>>> pyfileconf.options.reset_option('log_stdout')  # reset only the log_stdout option
# reset only the log_stdout option, but other options could be included
>>> pyfileconf.options.reset_options(['log_stdout'])
>>> pyfileconf.options.reset()  # reset all the options
Available Options

Parameters
__init__()[source]

Initialize self. See help(type(self)) for accurate signature.

reset()[source]

Undo all changes made through the options interface

Returns

reset_option(attr)[source]

Reset a particular option by name

Parameters

attr (str) – Name of option

Returns

reset_options(attrs)[source]

Reset multiple options at once by their names

Parameters

attrs (Iterable[str]) – Option names

Returns

set_option(attr, value)[source]

Set a particular option by its name

Parameters
  • attr (str) – Option name

  • value (Any) – New option value

Returns

set_options(updates)[source]

Set multiple options at once by their name :type _sphinx_paramlinks_pyfileconf.opts.PyfileconfOptionsManager.set_options.updates: Iterable[Tuple[str, Any]] :param _sphinx_paramlinks_pyfileconf.opts.PyfileconfOptionsManager.set_options.updates: Multiple option updates :return:

Examples

>>> import pyfileconf
>>> pyfileconf.options.set_options([('log_stdout', True), ('log_folder', 'MyLogs')])

Submodules

pyfileconf.opts.filelog module

pyfileconf.opts.filelog.add_file_handler_if_log_folder_exists_else_remove_handler(attr_name, log_folder)[source]