Source code for yaml2archiving.v1.script

import json
import logging

from ska_ser_logging import configure_logging

from .action import get_actions, perform_actions, show_actions
from .archiver import get_current_attributes
from .config import (
    get_desired_attributes,
    get_tango_database,
    load_configuration,
)

try:
    from ._version import version  # type: ignore
except ImportError:
    version = "unknown"

configure_logging()
logger = logging.getLogger("script")


[docs] def configure( yamlfile: str, update: bool, write: bool, show: bool, delay: float = 0, use_old_format: bool = False, logger=logger, ): config = load_configuration(yamlfile) if show: logger.debug(json.dumps(config, indent=4)) return db = get_tango_database(config["db"]) # Get the current archiving configuration. This consists of # all archiving related settings for each attribute currently # archived by the archiver. current = get_current_attributes(config["archiver"], delay=delay) # Build the configuration described by the config file + reality desired = get_desired_attributes(config["db"], config["configuration"], db) # Calculate what is needed to bring the current config to the desired state actions = get_actions(current, desired, use_old_format) if any(actions.values()): output = show_actions(actions, update=update) if output: logger.debug("--- Required actions ---") logger.debug("\n".join(output)) if write: logger.debug("--- Applying actions... ---") failed = perform_actions( config["manager"], config["archiver"], actions, update=update, delay=delay, use_old_format=use_old_format, ) failed_output = [] for key, attrs in failed.items(): if attrs: failed_output.append(key.upper()) for attr, error in attrs: line_error = error.replace("\n", " ") failed_output.append(f"\t{attr}: {line_error}") if failed_output: logger.debug( "!!! Actions applied, but the" " following actions failed !!!" ) logger.debug("\n".join(failed_output)) actions["failed"] = [] actions["failed"].append("\n".join(failed_output)) else: logger.debug( "*** Successfully applied the actions" " to the archiving system! ***" ) if update: logger.debug( "Warning: since the '-u/--update' flag was used," " the archiver may not be" ) logger.debug("in sync with the YAML file!") else: logger.debug( "=== No actions made, try the" " '--write' option to apply. ===" ) else: logger.debug("--- Nothing to do! ---") # if actions["broken"] or actions["pre_broken"]: # print("!!! Errors may have prevented some actions; check log messages !!!") else: logger.debug("--- Nothing to do! ---") return actions