netdef.Shared package

Internal

class netdef.Shared.Internal.Statistics[source]

Bases: object

A singleton class to store statistics as key-value pair. Can be turned off for performance or security.

Can be imported from Rules, Controllers and Expressions.

Example:

import psutil
from netdef.Shared.Internal import Statistics
from netdef.Sources.SystemMonitorSource import bytes2human

if Statistics.on:
    uss = psutil.Process().memory_full_info().uss
    Statistics.set("process.memory.startup", bytes2human(uss))
static get(key)[source]
static get_dict()[source]
on = True
static set(key, value)[source]
statistics = {}

Shared

class netdef.Shared.Shared.Shared(identifier, install_path, proj_path, default_config_string)[source]

Bases: object

Shared memory for the application. This is the class of the shared instance that is passed to all controllers, rules, engines and expressions. You will use this class to read configs, get message queues etc.

Parameters:
  • identifier (str) – a unique identifier for this app.
  • install_path (str) – Full filepath to application package location
  • proj_path (str) – Full filepath to project location
  • default_config_string (str) – initial config text for SharedConfig.Config

SharedConfig

class netdef.Shared.SharedConfig.Config(identifier, install_path, proj_path, default_config_string, read_from_files=True)[source]

Bases: object

A wrapper class for the configparser module in standard python library.

Parameters:
  • identifier (str) – a unique identifier for this app.
  • install_path (str) – Full filepath to application package location
  • proj_path (str) – Full filepath to project location
  • default_config_string (str) – initial config text for configparser
add_section(section)[source]
config(section, key, defaultvalue=None, add_if_not_exists=True)[source]
get_dict(section)[source]
get_full_list()[source]
is_hidden_value(section, key)[source]
read_default(config_path)[source]
set_config(section, key, value)[source]
set_hidden_value(section, key)[source]
verify(proj_path, config_path)[source]

SharedExpressions

class netdef.Shared.SharedExpressions.ExpressionInstances[source]

Bases: object

add_expression(item)[source]
add_expression_in_source_ref(ref, expression)[source]
get_expressions_by_source_ref(ref)[source]
has_expression_in_source_ref(ref, expression)[source]
has_source_ref(ref)[source]
class netdef.Shared.SharedExpressions.SharedExpressions[source]

Bases: object

instances = <netdef.Shared.SharedExpressions.ExpressionInstances object>

SharedQueues

class netdef.Shared.SharedQueues.MessageType[source]

Bases: enum.Enum

An enumeration.

ADD_PARSER = 6

Instruct the controller to use the given source class as a parser

ADD_SOURCE = 2

Instruct the controller to update the given source’s value from external datasource

APP_STATE = 9

Inform the controller of application state

READ_ALL = 1

warning: Not implemented yet

READ_SOURCE = 3

warning: Not implemented yet

REMOVE_SOURCE = 7

warning: Not implemented yet

RUN_EXPRESSION = 5

Instruct the rule or engine to execute the given expression’s function

TICK = 8

Instruct the controller to send a reply

WRITE_SOURCE = 4

Instruct the controller to update external datasource from the given source’s value

class netdef.Shared.SharedQueues.AppStateType[source]

Bases: enum.Enum

An enumeration.

RUNNING = 2
SETUP = 1
class netdef.Shared.SharedQueues.SharedQueues(maxsize=0)[source]

Bases: object

Message queues for all controllers, rules and the engine

add_controller(name)[source]

Create a incoming queue for given controller’

add_rule(name)[source]

Create a incoming queue for given rule’

get_messages_to_controller(name)[source]

Returns the incoming queue for given controller

get_messages_to_engine()[source]

Returns the incoming queue for the engine

get_messages_to_rule(name)[source]

Returns the incoming queue for given rule

run_expressions_in_engine(source_instance, expressions, value, source_time, status_code)[source]

Send a RUN_EXPRESSION message to the engine.

Parameters:
  • source_instance – the source that triggered given expressions
  • expressions (list) – list of expressions
run_expressions_in_rule(source_instance)[source]

Send a RUN_EXPRESSION message to given rule.

Parameters:source_instance – the source
send_message_to_controller(messagetype, controllername, message_object)[source]

Send a message to given controller

Parameters:
  • messagetype (self.MessageType) –
  • controllername (str) –
  • message_object – usually a source instance. can also be a tuple.
send_message_to_engine(messagetype, message_object)[source]

Send a message to the engine

Parameters:
  • messagetype (self.MessageType) – probably MessageType.RUN_EXPRESSION
  • message_object – usually a source instance.
send_message_to_rule(messagetype, rule_name, message_object)[source]

Send a message to given rule

Parameters:
  • messagetype (self.MessageType) –
  • rule_name (str) –
  • message_object – usually a source instance.
send_running_state_to_controller(controllername)[source]

Send a APP_STATE message to given controller

Parameters:controllername – the controller
send_setup_state_to_controller(controllername)[source]

Send a APP_STATE message to given controller

Parameters:controllername – the controller
write_value_to_controller(source_instance, value, source_time)[source]

Send a WRITE_SOURCE message to given controller

Parameters:
  • source_instance – the source
  • value – new value. datatype have to match the given source
  • source_time (datetime.datetime) – timestamp in utc

SharedSources

class netdef.Shared.SharedSources.SharedSources[source]

Bases: object

classes contain a dict (classes.items) with uninitiated sources classes. (key is name from config, value is class) Used by rules when parsing config files and finding the right source.

instances contains a list of all sources (instances.items) instances created by the rules.

classes = <netdef.Shared.SharedSources.SourceClasses object>
instances = <netdef.Shared.SharedSources.SourceInstances object>
class netdef.Shared.SharedSources.SourceClasses[source]

Bases: object

add_item(source_name, classobj)[source]
get_item(name)[source]
init_items(items)[source]
class netdef.Shared.SharedSources.SourceInstances[source]

Bases: object

add_item(item)[source]
get_item_by_ref(ref)[source]
has_item_ref(ref)[source]