From a2dbbde10e44f298c4aade36612d01105ea81c1a Mon Sep 17 00:00:00 2001 From: Alexis Koralewski Date: Fri, 4 Feb 2022 17:50:06 +0100 Subject: [PATCH] Adding new configpyros class, renaming previous configpyros class to obsconfig_class --- config/pyros/__init__.py | 0 config/pyros/config_pyros.py | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ config/pyros/config_pyros.yml | 12 ++++++++++++ config/pyros/schema_pyros_config.yml | 24 ++++++++++++++++++++++++ pyros.py | 17 +++++++++++++---- src/core/pyros_django/agent/Agent.py | 6 +++--- src/core/pyros_django/dashboard/views.py | 10 ++++++---- src/core/pyros_django/misc/static/media/logo_pyros.png | Bin 0 -> 60708 bytes src/core/pyros_django/misc/templates/base.html | 21 +++++++++++++++++++-- src/core/pyros_django/obsconfig/configpyros.py | 1050 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ src/core/pyros_django/obsconfig/obsconfig_class.py | 1050 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/core/pyros_django/obsconfig/templates/obsconfig/obs_astronomer_config.html | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++-- src/core/pyros_django/obsconfig/tests.py | 10 +++++----- src/core/pyros_django/obsconfig/views.py | 37 ++++++++++++++++++++----------------- src/core/pyros_django/pyros/settings.py | 19 +++++++++++++++++-- 15 files changed, 1294 insertions(+), 1089 deletions(-) create mode 100644 config/pyros/__init__.py create mode 100644 config/pyros/config_pyros.py create mode 100644 config/pyros/config_pyros.yml create mode 100644 config/pyros/schema_pyros_config.yml create mode 100644 src/core/pyros_django/misc/static/media/logo_pyros.png delete mode 100644 src/core/pyros_django/obsconfig/configpyros.py create mode 100644 src/core/pyros_django/obsconfig/obsconfig_class.py diff --git a/config/pyros/__init__.py b/config/pyros/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/config/pyros/__init__.py diff --git a/config/pyros/config_pyros.py b/config/pyros/config_pyros.py new file mode 100644 index 0000000..94aaa62 --- /dev/null +++ b/config/pyros/config_pyros.py @@ -0,0 +1,75 @@ +import yaml,os,time +import pykwalify.core +from pykwalify.errors import SchemaError + +class configpyros: + 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: + can_yaml_file_be_read = False + while can_yaml_file_be_read != True: + if os.access(yaml_file, os.R_OK): + can_yaml_file_be_read = True + else: + print(f"{yaml_file} can't be accessed, waiting for availability") + time.sleep(0.5) + + c = pykwalify.core.Core(source_file=yaml_file, schema_files=[self.config_path+"/"+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 + except IOError: + print("Error when reading the observatory config file") + + + 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: + can_config_file_be_read = False + while can_config_file_be_read != True: + + if os.access(yaml_file, os.R_OK): + can_config_file_be_read = True + else: + print(f"{yaml_file} can't be accessed, waiting for availability") + time.sleep(0.5) + with open(yaml_file, 'r') as stream: + print(f"Reading {yaml_file}") + config_file = yaml.safe_load(stream) + result = self.check_and_return_config(yaml_file,config_file["schema"]) + if result == None: + print("Error when reading and validating config file, please check the errors right above") + exit(1) + return result + + except yaml.YAMLError as exc: + print(exc) + except Exception as e: + print(e) + return None + + def __init__(self,pyros_config_file): + self.config_path = os.path.abspath(os.path.join(os.environ.get("DJANGO_PATH"),"../../../config/pyros//")) + self.pyros_config = self.read_and_check_config_file(pyros_config_file) diff --git a/config/pyros/config_pyros.yml b/config/pyros/config_pyros.yml new file mode 100644 index 0000000..50420a1 --- /dev/null +++ b/config/pyros/config_pyros.yml @@ -0,0 +1,12 @@ +schema: schema_pyros_config.yml +general: + logo: logo_pyros.png + color_theme: sienna + nb_element_per_page: 8 +USR: + nb_element_per_page: 10 +SCP: ~ +OCF: ~ + + + \ No newline at end of file diff --git a/config/pyros/schema_pyros_config.yml b/config/pyros/schema_pyros_config.yml new file mode 100644 index 0000000..1e5fac2 --- /dev/null +++ b/config/pyros/schema_pyros_config.yml @@ -0,0 +1,24 @@ +type: map +mapping: + schema: + type: str + required: True + general: + type: map + required: True + mapping: + logo: + type: str + required: True + color_theme: + type: str + required: True + nb_element_per_page: + type: int + required: True + USR: + type: any + SCP: + type: any + OCF: + type: any \ No newline at end of file diff --git a/pyros.py b/pyros.py index 6746c79..e788b00 100755 --- a/pyros.py +++ b/pyros.py @@ -21,6 +21,7 @@ import sys import time import re import glob +from config.pyros.config_pyros import configpyros from git import Repo #import logging @@ -569,6 +570,8 @@ def test(app): print("Running tests") os.environ["PATH_TO_OBSCONF_FOLDER"] = os.path.join(os.path.abspath(PYROS_DJANGO_BASE_DIR),"obsconfig/fixtures/") os.environ["unit_name"] = "" + configfile = 'config_pyros.yml' + os.environ["pyros_config_file"] = os.path.join(os.path.abspath(PYROS_DJANGO_BASE_DIR),"../../../config/pyros/", configfile) #start_dir = os.getcwd() if app == None: #apps = ['obsconfig','scientific_program','common', 'scheduler', 'routine_manager', 'user_manager', 'alert_manager.tests.TestStrategyChange'] @@ -663,7 +666,6 @@ def initdb(): return True - @pyros_launcher.command(help="Launch an agent") #@global_test_options @click.argument('agent') @@ -677,8 +679,15 @@ def start(agent:str, configfile:str, observatory:str, unit:str): log.debug("Running start command") if configfile: log.debug("With config file"+ configfile) + os.environ["pyros_config_file"] = os.path.join(os.path.abspath(PYROS_DJANGO_BASE_DIR),"../../../config/pyros/", configfile) else: - configfile = '' + configfile = 'config_pyros.yml' + os.environ["pyros_config_file"] = os.path.join(os.path.abspath(PYROS_DJANGO_BASE_DIR),"../../../config/pyros/", configfile) + + logo_name = configpyros(os.environ["pyros_config_file"]).pyros_config["general"]["logo"] + logo_path = os.path.join(os.path.abspath(PYROS_DJANGO_BASE_DIR),"../../../config/pyros/", logo_name) + media_path = os.path.join(os.path.abspath(PYROS_DJANGO_BASE_DIR),"misc/static/media",logo_name) + shutil.copy(logo_path,media_path) #if test_mode(): print("in test mode") #if verbose_mode(): print("in verbose mode") if observatory == None or len(observatory) == 0: @@ -709,8 +718,8 @@ def start(agent:str, configfile:str, observatory:str, unit:str): ''' # add path to pyros_django folder as the config class is supposed to work within this folder - #cmd_test_obs_config = f"-c \"from src.core.pyros_django.obsconfig.configpyros import ConfigPyros\nConfigPyros('{os.path.join(PYROS_DJANGO_BASE_DIR,os.environ.get('PATH_TO_OBSCONF_FILE'))}')\"" - cmd_test_obs_config = f"-c \"from src.core.pyros_django.obsconfig.configpyros import ConfigPyros\nConfigPyros('{obs_config_file_path}')\"" + #cmd_test_obs_config = f"-c \"from src.core.pyros_django.obsconfig.obsconfig_class import OBSConfig\nOBSConfig('{os.path.join(PYROS_DJANGO_BASE_DIR,os.environ.get('PATH_TO_OBSCONF_FILE'))}')\"" + cmd_test_obs_config = f"-c \"from src.core.pyros_django.obsconfig.obsconfig_class import OBSConfig\nOBSConfig('{obs_config_file_path}')\"" if not execProcessFromVenv(cmd_test_obs_config): # Observatory configuration has an issue exit(1) diff --git a/src/core/pyros_django/agent/Agent.py b/src/core/pyros_django/agent/Agent.py index 2b07978..ffaf75f 100755 --- a/src/core/pyros_django/agent/Agent.py +++ b/src/core/pyros_django/agent/Agent.py @@ -128,7 +128,7 @@ from common.models import AgentSurvey, AgentCmd, AgentLogs #from config.configpyros import ConfigPyros from config.old_config.configpyros import ConfigPyros as ConfigPyrosOld -from src.core.pyros_django.obsconfig.configpyros import ConfigPyros +from src.core.pyros_django.obsconfig.obsconfig_class import OBSConfig #from dashboard.views import get_sunelev #from devices.TelescopeRemoteControlDefault import TelescopeRemoteControlDefault @@ -460,7 +460,7 @@ class Agent: obs_config_file_path = os.environ["PATH_TO_OBSCONF_FILE"] path_to_obs_config_folder = os.environ["PATH_TO_OBSCONF_FOLDER"] unit = os.environ["unit_name"] - oc = ConfigPyros(obs_config_file_path) + oc = OBSConfig(obs_config_file_path) self.set_config(oc, obs_config_file_path, path_to_obs_config_folder, unit) #self.name = name @@ -545,7 +545,7 @@ class Agent: - def set_config(self, oc: ConfigPyros, obs_config_file_path: str, path_to_obs_config_folder: str, unit: str): + def set_config(self, oc: OBSConfig, obs_config_file_path: str, path_to_obs_config_folder: str, unit: str): self._oc = { 'config' : oc, 'env' : [ diff --git a/src/core/pyros_django/dashboard/views.py b/src/core/pyros_django/dashboard/views.py index df8c2f0..7dbd87f 100644 --- a/src/core/pyros_django/dashboard/views.py +++ b/src/core/pyros_django/dashboard/views.py @@ -25,10 +25,10 @@ from devices.CameraVISRemoteControlDefault import CameraVISRemoteControlDefault from devices.CameraNIRRemoteControlDefault import CameraNIRRemoteControlDefault from devices import PLC import time,os -from obsconfig.configpyros import ConfigPyros +from src.core.pyros_django.obsconfig.obsconfig_class import OBSConfig from django.conf import settings as pyros_settings - sys.path.append("../../..") +from config.pyros.config_pyros import configpyros #import utils.celme as celme import vendor.guitastro as guitastro @@ -41,11 +41,13 @@ MAX_LOGS_LINES = 100 log = l.setupLogger("dashboard", "dashboard") def index(request): - config = ConfigPyros(os.environ["PATH_TO_OBSCONF_FILE"],os.environ["unit_name"]) + config = OBSConfig(os.environ["PATH_TO_OBSCONF_FILE"],os.environ["unit_name"]) observatory_name = config.get_obs_name() unit_name = config.unit_name request.session["obsname"] = observatory_name+" "+unit_name - + request.session["pyros_config"] = pyros_settings.CONFIG_PYROS + logo = pyros_settings.CONFIG_PYROS.get("general").get("logo") + request.session["logo"] = "media/"+logo message = "" if request.user.is_authenticated: if SP_Period_Guest.objects.filter(email=request.user.email): diff --git a/src/core/pyros_django/misc/static/media/logo_pyros.png b/src/core/pyros_django/misc/static/media/logo_pyros.png new file mode 100644 index 0000000..7ec0162 Binary files /dev/null and b/src/core/pyros_django/misc/static/media/logo_pyros.png differ diff --git a/src/core/pyros_django/misc/templates/base.html b/src/core/pyros_django/misc/templates/base.html index b33d59e..0beaa42 100644 --- a/src/core/pyros_django/misc/templates/base.html +++ b/src/core/pyros_django/misc/templates/base.html @@ -40,6 +40,13 @@