netdef package¶
netdef.__main__¶
-
netdef.__main__.
cli
()[source]¶ entrypoint for use in
setup.py
:entry_points={ 'console_scripts': [ '{NAME}={MAIN_PACKAGE}.__main__:cli'.format(NAME=NAME, MAIN_PACKAGE=MAIN_PACKAGE), ], },
-
netdef.__main__.
create_project
(proj_path, template_config_callback)[source]¶ Create project structure in given folder. Add content from
template_config_callback
intoconfig/default.ini
Parameters: - proj_path (str) – project folder
- template_config_callback (str) – config text
-
netdef.__main__.
entrypoint
(run_callback, template_config_callback)[source]¶ Entrypoint to be used in your application. Parses Command line arguments and dispatch functions.
Example from
First-App/first_app/__main__.py
:from netdef.__main__ import entrypoint def run_app(): from . import main def get_template_config(): from . import defaultconfig return defaultconfig.template_config_string def cli(): # entrypoint: console_scripts entrypoint(run_app, get_template_config) if __name__ == '__main__': # entrypoint: python -m console_scripts entrypoint(run_app, get_template_config)
-
netdef.__main__.
framework_entrypoint
()[source]¶ The main entrypoint for the netdef package. Used by
cli()
.Parses command line arguments and dispatch functions
netdef.service¶
-
netdef.service.
get_service
(*args, **kwargs)[source]¶ Note
This function is only implemented for Windows and Systemd based linux distributions
Returns the Service-class to use as argument in
run_service()
Parameters: - svc_name – name of the service
- exe_name – filename of the service
- app_callback – a function that will start your application
- template_callback – a function that returns template config
Returns: GenericApplicationService
Example:
from netdef.service import get_service, run_service def run_app(): from . import main def get_template_config(): from . import defaultconfig return defaultconfig.template_config_string application_service = get_service("First-App", "First-App-Service", run_app, get_template_config) run_service(application_service)
-
netdef.service.
run_service
(*args, **kwargs)[source]¶ Note
This function is only implemented for Windows and Systemd based linux distributions
Parameters: app_service_class – service class from get_service()
Create an instance of
app_service_class
and run as serviceExample:
from netdef.service import get_service, run_service def run_app(): from . import main def get_template_config(): from . import defaultconfig return defaultconfig.template_config_string application_service = get_service("First-App", "First-App-Service", run_app, get_template_config) run_service(application_service)
netdef.windows_service¶
-
class
netdef.windows_service.
GenericApplicationService
(args)[source]¶ Bases:
sphinx.ext.autodoc.importer._MockObject
-
application
= None¶
-
-
netdef.windows_service.
get_service
(svc_name, exe_name, app_callback, template_callback=None)[source]¶ Note
This function is only implemented for Windows and Systemd based linux distributions
Returns the Service-class to use as argument in
run_service()
Parameters: - svc_name – name of the service
- exe_name – filename of the service
- app_callback – a function that will start your application
- template_callback – a function that returns template config
Returns: Example:
from netdef.service import get_service, run_service def run_app(): from . import main def get_template_config(): from . import defaultconfig return defaultconfig.template_config_string application_service = get_service("First-App", "First-App-Service", run_app, get_template_config) run_service(application_service)
-
netdef.windows_service.
run_service
(app_service_class)[source]¶ Note
This function is only implemented for Windows and Systemd based linux distributions
Parameters: app_service_class – service class from get_service()
Create an instance of
app_service_class
and run as serviceExample:
from netdef.service import get_service, run_service def run_app(): from . import main def get_template_config(): from . import defaultconfig return defaultconfig.template_config_string application_service = get_service("First-App", "First-App-Service", run_app, get_template_config) run_service(application_service)
netdef.systemd_service¶
netdef.systemd_service
can also be invoked directly using the -m switch of
the interpreter with proj_path as argument.
This example installs the project in current directory as a service:
$ python -m netdef.systemd_service -i .
-
class
netdef.systemd_service.
ApplicationService
(svc_name, exe_name, app_callback, template_callback)¶ Bases:
tuple
-
app_callback
¶ Alias for field number 2
-
exe_name
¶ Alias for field number 1
-
svc_name
¶ Alias for field number 0
-
template_callback
¶ Alias for field number 3
-
-
netdef.systemd_service.
get_service
(svc_name, exe_name, app_callback, template_callback)[source]¶ Note
This function is only implemented for Windows and Systemd based linux distributions
Returns the Service-class to use as argument in
run_service()
Parameters: - svc_name – name of the service
- exe_name – filename of the service
- app_callback – a function that will start your application
- template_callback – a function that returns template config
Returns: GenericApplicationService
Example:
from netdef.service import get_service, run_service def run_app(): from . import main def get_template_config(): from . import defaultconfig return defaultconfig.template_config_string application_service = get_service("First-App", "First-App-Service", run_app, get_template_config) run_service(application_service)
-
netdef.systemd_service.
install_service
(proj_path, service_file, svc_name, user)[source]¶ Note
This function is only implemented for Systemd based linux distributions
Creates a systemd service file in /etc/systemd/system/
-
netdef.systemd_service.
run_service
(app_service_class)[source]¶ Note
This function is only implemented for Windows and Systemd based linux distributions
Parameters: app_service_class – service class from get_service()
Create an instance of
app_service_class
and run as serviceExample:
from netdef.service import get_service, run_service def run_app(): from . import main def get_template_config(): from . import defaultconfig return defaultconfig.template_config_string application_service = get_service("First-App", "First-App-Service", run_app, get_template_config) run_service(application_service)
netdef.utils¶
-
netdef.utils.
handle_restart
(shared, engine)[source]¶ By calling this function your application will restart on SystemExit if shared.restart_on_exit is True.
Parameters: - shared – instance of
netdef.Shared.Shared
- engine – instance or subclass of
netdef.Engines.BaseEngine.BaseEngine
Example:
from netdef.utils import handle_restart ... engine.init() engine.start() engine.block() # until ctrl-c or SIG_TERM engine.stop() handle_restart(shared, engine)
- shared – instance of
-
netdef.utils.
setup_logging
(config)[source]¶ Parse the config file for:
[logging] logglevel loggformat loggdatefmt loggfile to_console to_file
Then the logging module is set according to the configs
Parameters: config – instance of netdef.Shared.SharedConfig.Config
Example:
... from netdef.Shared import Shared from netdef.utils import setup_logging shared = Shared.Shared("First-App", install_path, proj_path, config_string) setup_logging(shared.config) ...
netdef.testutils¶
-
class
netdef.testutils.
MockExpression
(**kwargs)[source]¶ Bases:
object
Example:
from netdef.testutils import MockExpression def test_hello(): mock = MockExpression( module="config/command_rule.py", intern=InternalSource("generic"), cmd=CmdSource("echo hello") ) mock.intern.update_value(None, stat_init=True) mock.cmd.assert_called_once_with("world") mock.intern.assert_not_called()
Bases:
netdef.Shared.Shared.Shared