def define_publisher(module_path=None, **kwargs): module_path = module_path or publishers.__name__ module_labels = labels.make_labels(module_path, *PUBLISHER_LABEL_NAMES) setup_publisher( module_labels, parameters.define(module_path, make_publisher_params(**kwargs)), ) return module_labels
def define_client(module_path=None, **kwargs): module_path = module_path or clients.__name__ module_labels = labels.make_labels(module_path, *CLIENT_LABEL_NAMES) setup_client( module_labels, parameters.define(module_path, make_client_params(**kwargs)), ) return module_labels
def define_server(module_path=None, **kwargs): module_path = module_path or servers.__name__ module_labels = labels.make_labels(module_path, *SERVER_LABEL_NAMES) setup_server( module_labels, parameters.define(module_path, make_server_params(**kwargs)), ) return module_labels
def define_console(module_path=None, **kwargs): module_path = module_path or __name__ module_labels = labels.make_labels(module_path, *CONSOLE_LABEL_NAMES) setup_console( module_labels, parameters.define(module_path, make_console_params(**kwargs)), ) return module_labels
def define_executor(module_path=None, **kwargs): """Define an executor under ``module_path``.""" module_path = module_path or executors.__name__ module_labels = labels.make_labels(module_path, *EXECUTOR_LABEL_NAMES) setup_executor( module_labels, parameters.define(module_path, make_executor_params(**kwargs)), ) return module_labels
def define_create_engine(module_path=None, **kwargs): """Define a database engine under ``module_path``.""" module_path = module_path or __package__ module_labels = labels.make_labels(module_path, *DATABASE_LABEL_NAMES) setup_create_engine( module_labels, parameters.define(module_path, make_create_engine_params(**kwargs)), ) return module_labels
def define_session(module_path=None, **kwargs): """Define a session object under ``module_path``.""" module_path = module_path or clients.__name__ module_labels = labels.make_labels(module_path, *SESSION_LABEL_NAMES) setup_session( module_labels, parameters.define( module_path, make_session_params(**kwargs), ), ) return module_labels
def test_make_labels(self): names = labels.make_labels('foo.bar', 'x', 'y', z='p.q') self.assertEqual( names._asdict(), { 'x': labels.Label('foo.bar', 'x'), 'y': labels.Label('foo.bar', 'y'), 'z': labels.Label('foo.bar', 'p.q'), }, ) self.assertEqual(names.x, labels.Label('foo.bar', 'x')) self.assertEqual(names.y, labels.Label('foo.bar', 'y')) self.assertEqual(names.z, labels.Label('foo.bar', 'p.q')) n2 = labels.make_labels('spam.egg', 'p', **names._asdict()) self.assertEqual( n2._asdict(), { 'p': labels.Label('spam.egg', 'p'), 'x': labels.Label('foo.bar', 'x'), 'y': labels.Label('foo.bar', 'y'), 'z': labels.Label('foo.bar', 'p.q'), }, )
"""Demonstrate ``g1.apps.parameters``.""" from startup import startup from g1.apps import bases from g1.apps import parameters from g1.apps import utils from g1.bases import labels LABELS = labels.make_labels( __name__, 'f', 'x', ) PARAMS = parameters.define( __name__, parameters.Namespace(x=parameters.Parameter(0)), ) utils.depend_parameter_for(LABELS.x, PARAMS.x) def square(x): return x * x @startup def bind(x: LABELS.x) -> LABELS.f: x = x.get() return lambda: square(x)
from startup import startup from g1.apps import bases from g1.apps import parameters from g1.bases import labels from .. import scripts # pylint: disable=relative-beyond-top-level LABELS = labels.make_labels( 'g1.scripts', 'setup', ) PARAMS = parameters.define( 'g1.scripts', parameters.Namespace( dry_run=parameters.Parameter(False, 'whether to dry-run commands'), ), ) @startup def setup( exit_stack: bases.LABELS.exit_stack, _: parameters.LABELS.parameters, ) -> LABELS.setup: exit_stack.enter_context(scripts.doing_dry_run(PARAMS.dry_run.get()))
except ImportError: yaml = None from startup import startup from g1.bases import labels from g1.bases.assertions import ASSERT from g1.bases.collections import Namespace as _Namespace from . import bases LOG = logging.getLogger(__name__) LABELS = labels.make_labels( __name__, 'parameters', 'root_namespaces', 'parameter_table', ) INITIALIZED = False # This will be nullified when ``index_root_namespaces`` is called (and # thus you cannot call ``define`` after that). ROOT_NAMESPACES = {} # # Public interface. # def define(module_path, namespace):
from startup import startup from g1.apps import parameters from g1.apps import utils from g1.asyncs.bases import locks from g1.asyncs.bases import queues from g1.asyncs.bases import tasks from g1.bases import labels from .. import agents # pylint: disable=relative-beyond-top-level LABELS = labels.make_labels( agents.__name__, # supervise_agents. 'supervise_agents', 'agent_queue', 'graceful_exit', 'grace_period', # shutdown_agents. 'shutdown_queue', ) PARAMS = parameters.define( agents.__name__, parameters.Namespace(grace_period=parameters.Parameter(4, type=(int, float), unit='seconds'), ), ) startup.set(LABELS.agent_queue, tasks.CompletionQueue()) startup.set(LABELS.graceful_exit, locks.Event()) startup.set(LABELS.shutdown_queue, queues.Queue())
from startup import startup from g1.bases import labels from g1.bases.assertions import ASSERT from . import loggers LABELS = labels.make_labels( __name__, 'args', 'args_not_validated', 'argv', 'parser', # Although not all applications need an ``ExitStack``, I find it # very useful in almost all use cases, and so it is provided. 'exit_stack', # In case you want to change the main function dynamically... 'main', # Labels for sequencing application startup. 'parse', 'validate_args', ) # # Application startup. # @startup def parse_argv(
"""Define a global task queue for background jobs.""" import logging from startup import startup import g1.asyncs.agents.parts from g1.bases import labels from g1.asyncs.bases import tasks from g1.asyncs.bases import timers LABELS = labels.make_labels(__name__, 'queue') @startup def make_queue( agent_queue: g1.asyncs.agents.parts.LABELS.agent_queue, shutdown_queue: g1.asyncs.agents.parts.LABELS.shutdown_queue, ) -> LABELS.queue: bg = BackgroundTasks() agent_queue.spawn(bg.supervise) shutdown_queue.put_nonblocking(bg.shutdown) return bg.queue LOG = logging.getLogger(__name__) NON_GRACE_PERIOD = 0.1 # Unit: seconds. class BackgroundTasks:
def define_server(module_path=None): module_path = module_path or inprocs.__name__ module_labels = labels.make_labels(module_path, *SERVER_LABEL_NAMES) setup_server(module_labels) return module_labels
"""Demonstrate executor.""" import random import time from g1.apps import bases from g1.apps import parameters from g1.apps import utils from g1.bases import labels from g1.threads import futures import g1.threads.parts LABELS = labels.make_labels( __name__, 'executor_params', 'executor', ) utils.depend_parameter_for( LABELS.executor_params, parameters.define( __name__, g1.threads.parts.make_executor_params(), ), ) utils.define_maker( g1.threads.parts.make_executor, { 'params': LABELS.executor_params,