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 into config/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.__main__.generate_certificate(interactive=True)[source]

Generate ssl certificates using openssl. Files is created in project folder.

  • certificate.pem.key
  • certificate.pem
  • certificate.der.key
  • certificate.der

Prints result to stdout.

Parameters:interactive (bool) – ask for CN if True.
netdef.__main__.generate_webadmin_auth(interactive=True)[source]

Generate a user and password in ini-format. Prints result to stdout. Can be copy-pasted into config/default.conf

Parameters:interactive (bool) – ask for user/pass if True. Generate automatically if not.

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 service

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

class netdef.windows_service.GenericApplicationService(args)[source]

Bases: sphinx.ext.autodoc.importer._MockObject

SvcDoRun()[source]
SvcStop()[source]
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:

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.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 service

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

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 service

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.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:

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)
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()
get_callbacks()[source]
get_module()[source]

Returns the expression module

set_init_values(**kwargs)[source]
set_none_values(**kwargs)[source]
class netdef.testutils.MockShared(config_string='')[source]

Bases: netdef.Shared.Shared.Shared

class netdef.testutils.MockSource(expression, source)[source]

Bases: object

assert_any_call(value)[source]
assert_called()[source]
assert_called_once()[source]
assert_called_once_with(value)[source]
assert_called_with(value)[source]
assert_not_called()[source]
assert_value(value)[source]

A helper function to assert value and timestamp

call_args
call_args_list
call_count
update_value(val, stime=None, stat_none=False, stat_init=False, stat_good=False, stat_invalid=False, run_expression=True)[source]

A Helper function to update values in expression