Welcome to pyjld.system documentation!

pyjld.system: various system level utilities (e.g. daemon, cross-platform registry, command-line tools)

This package contains various system level utilities.

Changelog

0.4

  • Added ‘’proxy’’ module
  • Corrected “cmd_restart” from daemon module
  • Added status return code to ui.handleError method

0.3

  • Modified BaseCmdUI.handleArgument: integrates the templating for usage message

0.2

  • Added command-line related utilities
  • Added a cross-platform registry utility

pyjld.system.daemon

This module borrows heavily from python_daemon available at Pypi

class pyjld.system.daemon.base.DaemonRunner(app)

Controller class for a callable running in a separate background process

The principal methods are:

The PID lock file is derived from app.name in the following way

${app.pid_directory}/${app.name}.pid

and by default is located in the /var/run directory; this directory can be customized through the app.pid_directory attribute.

The exceptions generated by this class contain pseudo-messages which are really meant as index to human readable messages. This way, customization is easier to handle.

Exceptions raised

All exceptions specific to this class are raised with DaemonRunnerException and the following message identifiers are defined:

  • error_pidfile_locked
  • error_pidfile_not_locked
  • error_daemon_aborted
  • error_daemon_open
  • error_daemon_terminate_process
  • error_daemon_openfilepath
  • error_application_method_not_found

The parameter app must be a callable with, as minimum, the following attributes:

  • name
  • run() method
  • exit() method

Optional attributes are:

  • before_start() method
  • before_run() method

The exit() is called when the daemon is shutting down eg. it received a SIGTERM signal ( called also during the cmd_stop() ).

cmd_restart()
Restarts the daemon for (the currently running) app
cmd_start()

Starts the daemon for app

The method app.before_start() is called prior to actually daemonizing; the method can abort the process by raising ...

The method app.before_start() need not to exist (a validity check is performed).

cmd_stop()
Stops the daemon for (the currently running) app but not before calling app.stop
exception pyjld.system.daemon.base.DaemonRunnerException

DaemonRunner Exception base class

Allows for easier customization of error messages. Use duck typing to inspect the Exception for the msg_id & params attributes.

class pyjld.system.daemon.base.ReferenceApp

Used as model of an app

requiredAttributes
Generates the list of required attributes

pyjld.system.command.base

class pyjld.system.command.base.BaseCmd

Base class for command line utilities

The functionality of this class includes:

  • automatic discovery of declared commands i.e. methods with starting prefix cmd_
  • automatic generation of the help command for each discoverd commands
getConfig()
Returns a dictionary containing all the config_ attributes
itercommands()
Iterator for the discovered commands
iterconfig()

Iterator for the configuration parameters

Iterates through all the class attributes starting with config_.

safe_validateCommand(command)
Validates the specified command against the list of discovered commands i.e. methods starting with cmd_. :rtype: boolean
validateCommand(command)
Validates the specified command against the list of discovered commands i.e. methods starting with cmd_. Raises BaseCmdException if the command is not found.
exception pyjld.system.command.base.BaseCmdException
Base class for exceptions of this module
class pyjld.system.command.base.metaBaseCmd(name, bases, ns)
Meta-class used in the command discovery process

pyjld.system.command.ui

class pyjld.system.command.ui.BaseCmdUI(msgs, ref_options, logger=None, template_factory=None)

Base class for Command Line UI

Parameters:
  • msgs – messages object
  • template_factory – the factory for creating templates out of message_id

Messages object

This object can be controlled with through template_factory; the latter must implement a safe_substitute method for rendering the message_id with the given parameters to a message string.

copyOptions(target)

Copies all var entries from the source dictionary following the ref_options dictionary into the target dictionary.

Parameters:
  • source – source dictionary
  • target – target object
  • ref_options – the reference options list
dispatchCommand(cmd)
handleArguments(cmd=None, usage_msg=None, args=None, help_params=None)

Processes command line options

Parameters:
  • usage_msg – usage message string
  • config_options – the options configuration dictionary
  • args – the arguments from the command-line
  • help_params – dictionary of parameters used to render the help messages
handleError(exc)

Displays, if required, an appropriate user message corresponding to an error condition.

Parameters:
  • exc – Exception being raised
Return type:

boolean True if the exception was handled successfully i.e. a message was found and rendered correctly.

integrateRegistry(cmd, reg)

Integrate values from the registry where None values are found in the cmd object

Parameters:
  • cmd – the cmd object
Return type:

keys list for all replacement performed

popArg()
Pops one argument from the list
prepareCmd(cmd, reg)

Prepare the command cmd object

  1. Options from the command-line have precedence over any other.
  2. Options with ‘’None’’ value are updated with matching registry value
  3. The registry is updated where necessary
updateRegistry(reg)

Updates the registry from the command-line args

Goes through the args, verifies if the reg option is to be found in the ref_options dictionary and updates accordingly the registry.

Parameters:
  • reg – the dictionary registry
  • ref_options – the reference options list
  • options – the processed command-line arguments

The registry is a dictionary.

exception pyjld.system.command.ui.BaseCmdUIConfigError
Base exception class for this module
class pyjld.system.command.ui.ExOptionParser(*pargs, **kwargs)

Enhances the standard library’s OptionParser class

Adds the method ex_add_options()

ex_add_options(options_list)

Adds options through a keyword dictionary only. This constrasts to add_options() which goes through hoops to support both positional & keyword based argument list.

This method translates the options_list for the base class.

Parameters:
  • options_list – a list of dictionaries containing the options

Each dictionary entry (i.e. each list item) will be translated to comply with the standard base class OptionParser. Furthermore, only options which are supported by the base class are passed to the OptionParser: this behavior allows for extending the options_list entries (i.e. each dictionary item) with custom keys without breaking OptionParser.

[ { option } ... ]
In each option, the keys which are translated are:
  • short
  • long

Thus, a full option list can be passed to ex_add_options() without worrying about the special cases short and long positional arguments.

pyjld.system.registry.base

class pyjld.system.registry.base.Registry(file=None)

Facade for the cross-platform Registry

Can be accessed like a dictionary: for this functionality, an instance must be constructed with the ‘file’ parameter specified.

getKey(file, key)
GETS the specified key
setKey(file, key, value, cond=False)
SETS the specified key
exception pyjld.system.registry.base.RegistryException
An exception class for Registry

Indices and tables