From 5fcaeae1e904cd253a5ac6fa0edca7185b7f174f Mon Sep 17 00:00:00 2001 From: Quillot Mathias Date: Wed, 5 May 2021 21:15:16 +0200 Subject: [PATCH] New way to set environment variable using core.env module. Need to define now how to store envs. --- volia/core/env.py | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++ volia/experiment.py | 23 +++++--------- 2 files changed, 98 insertions(+), 15 deletions(-) create mode 100644 volia/core/env.py diff --git a/volia/core/env.py b/volia/core/env.py new file mode 100644 index 0000000..c4a538b --- /dev/null +++ b/volia/core/env.py @@ -0,0 +1,90 @@ +import json + +""" +This module aims to manage environement variables local to the application. +These variables are persitent and located in a json file. +""" + +class Env(object): + """Initialize with an persistent file (json) + + You can access to environment variables using the brackets operators. + You can also check with the 'in' operator if a key is present among + the variables. + + example code: + ``` + env = Env("myfile.json") + + if "MyVariable" in env: + print(env["MyVariable"]) + else: + env["MyVariable"] = "New value" + ``` + + """ + def __init__(self, file: str): + self.__variables = {} + self.file = file + self.load() + + @property + def variables(self): + return self.__variables + + @property + def file(self): + return self.__file + + @file.setter + def file(self, file): + self.__file = file + + + def set(self, key: str, value: str): + """Modify an environment variable + + Args: + key (str): [description] + value ([type]): [description] + """ + self.__variables[str] + self.save() + + + def get(self, key: str) -> str: + """Get an environment variable. + + Args: + key (str): [description] + + Returns: + [str]: [description] + """ + return self.variables[key] + + def __getitem__(self, key): + print(key) + return self.variables[key] + + def __setitem__(self, key, value): + self.variables[key] = value + + def __delitem__(self, key): + del self.variables[key] + + def __contains__(self, key): + return key in self.variables + + def load(self): + """Load the variables from the json file. + """ + with open(self.file, "r") as f: + self.__variables = json.load(f) + + + def save(self): + """Save the variables into the json file. + """ + with open(self.file, "w") as f: + json.dump(self.variables, f) diff --git a/volia/experiment.py b/volia/experiment.py index 384d596..c92c89c 100644 --- a/volia/experiment.py +++ b/volia/experiment.py @@ -4,7 +4,7 @@ from utils import SubCommandRunner #from decouple import Config, RepositoryEnv import decouple import json - +import core.env # TODO: # Load automatically if exists the persistency file @@ -77,25 +77,18 @@ if __name__ == '__main__': CONFIG = None PERSISTENCY_DATA = None env_file = os.path.join(VOLIA_HOME, ".env") - if os.path.isfile(env_file): - # Load le fichier - CONFIG = decouple.AutoConfig(search_path=VOLIA_HOME) - # Then loading the persistency file - persistency_file = os.path.join(VOLIA_HOME, "persistency.json") - - with open(persistency_file, "r") as f: - PERSISTENCY_DATA = json.load(f) - else: - # Volia not initialized. + if not os.path.isfile(env_file): print("VOLIA is not initialized. Initializing.") init() - pass - print("Config: ", CONFIG) - print("PERSISTENCY_DATA: ", PERSISTENCY_DATA) - + # Load le fichier + CONFIG = decouple.AutoConfig(search_path=VOLIA_HOME) + # Then loading the persistency file + persistency_file = os.path.join(VOLIA_HOME, "persistency.json") + PERSISTENCY_DATA = core.env.Env(persistency_file) + # PARSER parser = argparse.ArgumentParser(description="...") subparsers = parser.add_subparsers(title="actions") -- 1.8.2.3