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
-
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.
-
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>}¶
-
-
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
log_stdout¶ (bool) – Whether to capture stdout and log it in the pyfileconf logger
log_folder¶ (Optional[str]) – The folder in which logs should be stored
log_file_rollover_freq¶ – How often to roll over to a new log file, see
logging.handlers.TimedRotatingFileHandler
when optionlog_file_num_keep¶ – Number of log files to keep, see
logging.handlers.TimedRotatingFileHandler
backupCount option
-
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')])