Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def main(ctx, config_files):
"""Shared entrypoint configuration."""
logging.getLogger('schedule').setLevel(logging.CRITICAL)
config_data = layer_loader.load_files(
config_files,
loader=yaml.load,
)
try:
config = load_config(config_data)
except ConfigError:
logger.exception("Configuration Error")
click.get_current_context().exit(1)
ctx.obj = App(config)
_validate_config(ctx.obj)
"""Shared types for state machine execution."""
from typing import Any, Dict, Callable, NamedTuple
from routemaster.app import App
from routemaster.config import State, StateMachine
Metadata = Dict[str, Any]
IsExitingCheck = Callable[[], bool]
StateProcessor = Callable[
[App, State, StateMachine, IsExitingCheck],
None,
]
class LabelRef(NamedTuple):
"""API representation of a label for the state machine."""
name: str
state_machine: str
def app_from_config(config_path):
"""
Create an `App` instance with a session from a given config path.
By default, will use the example.yaml file.
"""
config = load_config(
layer_loader.load_files(
[config_path],
loader=yaml.load,
),
)
class InteractiveApp(App):
"""
App for use in interactive shell only.
Provides a global database session.
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._session = self._sessionmaker()
@property
def session(self):
"""Return the database session."""
return self._session
return InteractiveApp(config)
get_label_metadata as get_label_metadata_internal
from routemaster.state_machine.utils import (
needs_gate_evaluation_for_metadata_change,
)
from routemaster.state_machine.exceptions import (
DeletedLabel,
UnknownLabel,
LabelAlreadyExists,
)
from routemaster.state_machine.transitions import process_transitions
# Signature of a function to gather the labels to be operated upon when
# processing a cron task. This will be called in a different transaction to
# where we iterate over the results, so to prevent confusion or the possible
# introduction of errors, we require all the data up-front.
LabelProvider = Callable[[App, StateMachine, State], List[str]]
def list_labels(app: App, state_machine: StateMachine) -> Iterable[LabelRef]:
"""
Returns a sorted iterable of labels associated with a state machine.
Labels are returned ordered alphabetically by name.
"""
for (name,) in app.session.query(Label.name).filter_by(
state_machine=state_machine.name,
deleted=False,
):
yield LabelRef(name=name, state_machine=state_machine.name)
def get_label_state(app: App, label: LabelRef) -> Optional[State]:
)
from routemaster.state_machine import (
LabelStateProcessor,
process_cron,
process_gate,
process_action,
labels_in_state,
labels_needing_metadata_update_retry_in_gate,
)
IsTerminating = Callable[[], bool]
# Note: This function will be called in a different transaction to where we
# iterate over the results, so to prevent confusion or the possible
# introduction of errors, we require all the data up-front.
LabelProvider = Callable[[App, StateMachine, State], List[str]]
class CronProcessor(Protocol):
"""Type signature for the cron processor callable."""
def __call__(
self,
*,
app: App,
state: State,
state_machine: StateMachine,
fn: LabelStateProcessor,
label_provider: LabelProvider,
) -> None:
"""Type signature for the cron processor callable."""
...
"""WSGI middlewares used in routemaster."""
from typing import Any, Dict, List, Callable, Iterable, Optional
from routemaster.app import App
from routemaster.utils import WSGICallable, StartResponse, WSGIEnvironment
WSGIMiddleware = Callable[[App, WSGICallable], WSGICallable]
ACTIVE_MIDDLEWARES: List[WSGIMiddleware]
ACTIVE_MIDDLEWARES = []
def middleware(fn: WSGIMiddleware) -> WSGIMiddleware:
"""Decorator: add `fn` to ACTIVE_MIDDLEWARES."""
ACTIVE_MIDDLEWARES.append(fn)
return fn
def wrap_application(app: App, wsgi: WSGICallable) -> WSGICallable:
"""Wrap a given WSGI callable in all active middleware."""
for middleware_instance in reversed(ACTIVE_MIDDLEWARES):
wsgi = middleware_instance(app, wsgi)
return wsgi