import pykwalify.core import yaml,sys,logging,os from pykwalify.errors import PyKwalifyException,SchemaError class ConfigPyrosV2: # (AKo) : Config file path is checked on the settings file, if the file isn't valid (i.e not found) the error will be launched by the settings file when starting the application def check_and_return_config(self,yaml_file:str,schema_file:str)->dict: """ Check if yaml_file is valid for the schema_file and return an dictionary of the config file Args: yaml_file (str): Path to the config_file to be validated schema_file (str): Path to the schema file Returns: dict: dictionary of the config file (with values) """ # disable pykwalify error to clean the output logging.disable(logging.ERROR) try: c = pykwalify.core.Core(source_file=yaml_file, schema_files=[schema_file]) return c.validate(raise_exception=True) except SchemaError: for error in c.errors: print("Error :",str(error).split(". Path")[0]) print("Path to error :",error.path) return None def read_and_check_config_file(self,yaml_file:str)->dict: """ Read the schema key of the config file to retrieve schema name and proceed to the checking of that config file Call check_and_return_config function and print its return. Args: yaml_file (str): path to the config file Returns: dict: Dictionary of the config file (with values) """ try: with open(yaml_file, 'r') as stream: config_file = yaml.safe_load(stream) self.CONFIG_PATH = yaml_file.split("config/")[0]+"config/" result = self.check_and_return_config(yaml_file,self.CONFIG_PATH+config_file["schema"]) if result is None: print("Error when reading and validating config file, please check the errors right above") return result except yaml.YAMLError as exc: print(exc) except: return None def __init__(self,config_file_name:str) -> None: """ Initiate class with the config file set content attribute to a dictionary containing all values from the config file Args: config_file_name (str): path to the config file """ obs_config = self.read_and_check_config_file(config_file_name) if obs_config is None: print(f"Error when trying to read config file (path of config file : {config_file_name}") self.content = obs_config def get_obs_name(self)->str: """ Return name of the observatory Returns: str: Name of the observatory """ return self.content["OBSERVATORY"]["_name"] def get_channels(self,unit)->dict: """ return dictionary of channels Returns: dict: [description] """ channels = {} for channel_id in range(len(unit["TOPOLOGY"]["CHANNELS"])): channel = unit["TOPOLOGY"]["CHANNELS"][channel_id]["CHANNEL"] channels[channel["_name"]] = channel return channels def get_computers(self)->dict: """ return dictionary of computers Returns: dict: [description] """ computers = {} for computer_id in range(len(self.content["OBSERVATORY"]["COMPUTERS"])): computer = self.content["OBSERVATORY"]["COMPUTERS"][computer_id]["COMPUTER"] if( "_file" in computer.keys() ): computer["computer_config"]= self.read_and_check_config_file(self.CONFIG_PATH+computer["_file"])["COMPUTER"] computers[computer["_name"]] = computer return computers def get_devices(self)->dict: """ return dictionary of devices Returns: dict: [description] """ devices = {} for device_id in range(len(self.content["OBSERVATORY"]["DEVICES"])): device = self.content["OBSERVATORY"]["DEVICES"][device_id]["DEVICE"] if( "_file" in device.keys() ): device["device_config"] = self.read_and_check_config_file(self.CONFIG_PATH+device["_file"])["DEVICE"] devices[device["_name"]] = device return devices def get_agents(self,unit)->dict: """ return dictionary of agents Returns: dict: dictionary of agents. For each agents tell the name, computer, device, protocole, etc... """ agents = {} for agent_id in range(len(unit["AGENTS"])): # Agents is a list containing dictionary that have only one key key = list(unit["AGENTS"][agent_id].keys())[0] agent = unit["AGENTS"][agent_id][key] agents[agent["_name"]] = agent return agents def get_channel_groups(self,unit:dict)->dict: """ Return dictionary of channel groups, tell the logic between groups of channels and within a group of channels Args: unit (dict): dictonary contaning all values of a unit Returns: dict: dictionary of channel groups (tell the logic and groups of channels) """ info = {} info["global_groups_logic"] = unit["TOPOLOGY"]["CHANNEL_GROUPS"]["_logic"] info["groups"] = {} for group_id in range(len(unit["TOPOLOGY"]["CHANNEL_GROUPS"]["GROUPS"])): group = unit["TOPOLOGY"]["CHANNEL_GROUPS"]["GROUPS"][group_id]["GROUP"] info["groups"][group_id] = group return info def get_channel_information(self,unit:dict,channel_name:str)->dict: """ Return information of the given channel name of a unit Args: unit (dict): dictionary representing the unit channel_name (str): name of the channel Returns: dict: dictionary containing all values that define this channel """ channels = self.get_channels(unit) return channels[channel_name] def get_topology(self,unit:dict)->dict: """ Return dictionary of the topology of the observatory Args: unit (dict): dictionary representing the unit Returns: dict: dictionary representing the topology of an unit (security, mount, channels, channel_groups) """ topology = {} for key in unit["TOPOLOGY"].keys(): branch = unit["TOPOLOGY"][key] if key == "CHANNELS": topology[key] = self.get_channels(unit) elif key == "CHANNEL_GROUPS": topology[key] = self.get_channel_groups(unit) else: topology[key] = branch return topology def get_active_agents(self,unit:dict)->list: """ Return the list of active agents (i.e. agents that have an association with a device) Args: unit (dict): dictionary representing the unit Returns: list: kist of the name of active agents """ return list(self.get_agents(unit).keys()) def get_units(self)->dict: """ Return all units sort by name defined in the config file Returns: dict: dictionary giving for a unit_name, his content (name,database,topology,agents,...) """ result = {} units = self.content["OBSERVATORY"]["UNITS"] for unit in units: unit = unit["UNIT"] result[unit["_name"]] = unit return result def get_components_agents(self,unit:dict)->dict: """ Return dictionary of component_agents of the given unit Args: unit (dict): dictionary representing the unit Returns: dict: dictionary sort by component name giving the associated agent (agent name) """ components_agents = {} topology = self.get_topology(unit) for element in topology: if element in ("SECURITY","MOUNT","CHANNELS"): if(element != "CHANNELS"): for component_agent in topology[element]["COMPONENT_AGENTS"]: component_name = list(component_agent.keys())[0] components_agents[component_name] = component_agent[component_name] else: for channel in topology[element]: for component_agent in topology[element][channel]["COMPONENT_AGENTS"]: component_name = list(component_agent.keys())[0] components_agents[component_name] = component_agent[component_name] return components_agents def get_units_name(self)->list: """ Return list of units names Returns: [list]: names of units """ return list(self.get_units().keys()) def get_unit_by_name(self,name:str)->dict: """ Return dictionary containing definition of the unit that match the given name Args: name (str): name of the unit Returns: dict: dictonary representing the unit """ return self.get_units()[name] def get_agents_per_computer(self,unit:dict)->dict: """ Return dictionary that give for each computer, what are the associated agents to it as a list Args: unit (dict): dictonary representing the unit Returns: dict: dictionary that give for each computer, what are the associated agents to it as a list """ agents_per_computer = {} agents = self.get_agents(unit) for agent in agents: computer_name = agents[agent]["_computer"] if(agents[agent]["_computer"] not in agents_per_computer.keys()): agents_per_computer[computer_name] = [agent] else: agents_per_computer[computer_name].append(agent) return agents_per_computer def get_agents_per_device(self,unit:dict)->dict: """ Return dictionary that give for each device, what are the associated agents to it as a list Args: unit (dict): dictonary representing the unit Returns: dict: dictionary that give for each device, what are the associated agents to it as a list """ agents_per_device = {} agents = self.get_agents(unit) for agent in agents: if("_device" in agents[agent].keys()): device_name = agents[agent]["_device"] if(agents[agent]["_device"] not in agents_per_device.keys()): agents_per_device[device_name] = [agent] else: agents_per_device[device_name].append(agent) return agents_per_device def get_active_devices(self)->list: """ Return a list of active device names Returns: list: list of active device names """ active_devices = [] for unit_name in self.get_units(): unit = self.get_unit_by_name(unit_name) for device in self.get_agents_per_device(unit): active_devices.append(device) return active_devices def get_active_computers(self)->list: """ Return a list of active computer names Returns: list: list of active computer names """ active_computers = [] for unit_name in self.get_units(): unit = self.get_unit_by_name(unit_name) for computer in self.get_agents_per_computer(unit): active_computers.append(computer) return active_computers def get_agent_information(self,unit:dict,agent_name:str)->dict: """ Give the dictionary of attributes of the agent for an unit. Args: unit (dict): dictonary representing the unit agent_name (str): agent name Returns: dict: dictionary containing attributes of the agent """ return self.get_agents(unit)[agent_name] def get_device_information(self,device_name:str)->dict: """ Give the dictionary of the attributes of the device Args: device_name (str): device name Returns: dict: dictionary containing attributes of the device """ return self.get_devices()[device_name] def get_database_for_unit(self,unit_name:str)->dict: """ Return dictionary of attributes of the database for an unit Args: unit_name (str): unit name Returns: dict: dictionary of attributes of the database for an unit """ return self.get_unit_by_name(unit_name)["DATABASE"] def get_device_for_agent(self,unit:dict,agent_name:str)->str: """ Return device name associated to the agent Args: unit (dict): dictonary representing the unit agent_name (str): agent name Returns: str: device name associated to this agent """ agents_per_device = self.get_agents_per_device(unit) for device in agents_per_device: if agent_name in agents_per_device[device]: return self.get_device_information(device) def get_unit_of_computer(self,computer_name:str)->str: """ Return the name of the unit where the computer is used Args: computer_name (str): computer name Returns: str: unit name """ for unit_name in self.get_units(): unit = self.get_unit_by_name(unit_name) if(computer_name in self.get_agents_per_computer(unit)): return unit_name def get_unit_of_device(self,device_name:str)->str: """ Return the name of the unit where the device is used Args: device_name (str): device name Returns: str: unit name """ for unit_name in self.get_units(): unit = self.get_unit_by_name(unit_name) if(device_name in self.get_agents_per_device(unit)): return unit_name