Commit 96c04cbe493c88a826f0ff746a884b47228a7aaf
1 parent
5fcaeae1e9
Exists in
master
refactory of the env name. It's more about persistency data
Showing 2 changed files with 90 additions and 90 deletions Inline Diff
volia/core/env.py
| 1 | import json | File was deleted | |
| 2 | |||
| 3 | """ | ||
| 4 | This module aims to manage environement variables local to the application. | ||
| 5 | These variables are persitent and located in a json file. | ||
| 6 | """ | ||
| 7 | |||
| 8 | class Env(object): | ||
| 9 | """Initialize with an persistent file (json) | ||
| 10 | |||
| 11 | You can access to environment variables using the brackets operators. | ||
| 12 | You can also check with the 'in' operator if a key is present among | ||
| 13 | the variables. | ||
| 14 | |||
| 15 | example code: | ||
| 16 | ``` | ||
| 17 | env = Env("myfile.json") | ||
| 18 | |||
| 19 | if "MyVariable" in env: | ||
| 20 | print(env["MyVariable"]) | ||
| 21 | else: | ||
| 22 | env["MyVariable"] = "New value" | ||
| 23 | ``` | ||
| 24 | |||
| 25 | """ | ||
| 26 | def __init__(self, file: str): | ||
| 27 | self.__variables = {} | ||
| 28 | self.file = file | ||
| 29 | self.load() | ||
| 30 | |||
| 31 | @property | ||
| 32 | def variables(self): | ||
| 33 | return self.__variables | ||
| 34 | |||
| 35 | @property | ||
| 36 | def file(self): | ||
| 37 | return self.__file | ||
| 38 | |||
| 39 | @file.setter | ||
| 40 | def file(self, file): | ||
| 41 | self.__file = file | ||
| 42 | |||
| 43 | |||
| 44 | def set(self, key: str, value: str): | ||
| 45 | """Modify an environment variable | ||
| 46 | |||
| 47 | Args: | ||
| 48 | key (str): [description] | ||
| 49 | value ([type]): [description] | ||
| 50 | """ | ||
| 51 | self.__variables[str] | ||
| 52 | self.save() | ||
| 53 | |||
| 54 | |||
| 55 | def get(self, key: str) -> str: | ||
| 56 | """Get an environment variable. | ||
| 57 | |||
| 58 | Args: | ||
| 59 | key (str): [description] | ||
| 60 | |||
| 61 | Returns: | ||
| 62 | [str]: [description] | ||
| 63 | """ | ||
| 64 | return self.variables[key] | ||
| 65 | |||
| 66 | def __getitem__(self, key): | ||
| 67 | print(key) | ||
| 68 | return self.variables[key] | ||
| 69 | |||
| 70 | def __setitem__(self, key, value): | ||
| 71 | self.variables[key] = value | ||
| 72 | |||
| 73 | def __delitem__(self, key): | ||
| 74 | del self.variables[key] | ||
| 75 | |||
| 76 | def __contains__(self, key): | ||
| 77 | return key in self.variables | ||
| 78 | |||
| 79 | def load(self): | ||
| 80 | """Load the variables from the json file. | ||
| 81 | """ | ||
| 82 | with open(self.file, "r") as f: | ||
| 83 | self.__variables = json.load(f) | ||
| 84 | |||
| 85 | |||
| 86 | def save(self): | ||
| 87 | """Save the variables into the json file. | ||
| 88 | """ | ||
| 89 | with open(self.file, "w") as f: | ||
| 90 | json.dump(self.variables, f) | ||
| 91 | 1 | import json |
volia/core/persistency.py
| File was created | 1 | import json | |
| 2 | |||
| 3 | """ | ||
| 4 | This module aims to manage environement variables local to the application. | ||
| 5 | These variables are persitent and located in a json file. | ||
| 6 | """ | ||
| 7 | |||
| 8 | class PersistencyData(object): | ||
| 9 | """Initialize with an persistent file (json) | ||
| 10 | |||
| 11 | You can access to environment variables using the brackets operators. | ||
| 12 | You can also check with the 'in' operator if a key is present among | ||
| 13 | the variables. | ||
| 14 | |||
| 15 | example code: | ||
| 16 | ``` | ||
| 17 | env = Env("myfile.json") | ||
| 18 | |||
| 19 | if "MyVariable" in env: | ||
| 20 | print(env["MyVariable"]) | ||
| 21 | else: | ||
| 22 | env["MyVariable"] = "New value" | ||
| 23 | ``` | ||
| 24 | |||
| 25 | """ | ||
| 26 | def __init__(self, file: str): | ||
| 27 | self.__variables = {} | ||
| 28 | self.file = file | ||
| 29 | self.load() | ||
| 30 | |||
| 31 | @property | ||
| 32 | def variables(self): | ||
| 33 | return self.__variables | ||
| 34 | |||
| 35 | @property | ||
| 36 | def file(self): | ||
| 37 | return self.__file | ||
| 38 | |||
| 39 | @file.setter | ||
| 40 | def file(self, file): | ||
| 41 | self.__file = file | ||
| 42 | |||
| 43 | |||
| 44 | def set(self, key: str, value: str): | ||
| 45 | """Modify an environment variable | ||
| 46 | |||
| 47 | Args: | ||
| 48 | key (str): [description] | ||
| 49 | value ([type]): [description] | ||
| 50 | """ | ||
| 51 | self.__variables[str] | ||
| 52 | self.save() | ||
| 53 | |||
| 54 | |||
| 55 | def get(self, key: str) -> str: | ||
| 56 | """Get an environment variable. | ||
| 57 | |||
| 58 | Args: | ||
| 59 | key (str): [description] | ||
| 60 | |||
| 61 | Returns: | ||
| 62 | [str]: [description] | ||
| 63 | """ | ||
| 64 | return self.variables[key] | ||
| 65 | |||
| 66 | def __getitem__(self, key): | ||
| 67 | print(key) | ||
| 68 | return self.variables[key] | ||
| 69 | |||
| 70 | def __setitem__(self, key, value): | ||
| 71 | self.variables[key] = value | ||
| 72 | |||
| 73 | def __delitem__(self, key): | ||
| 74 | del self.variables[key] | ||
| 75 | |||
| 76 | def __contains__(self, key): | ||
| 77 | return key in self.variables | ||
| 78 | |||
| 79 | def load(self): | ||
| 80 | """Load the variables from the json file. | ||
| 81 | """ | ||
| 82 | with open(self.file, "r") as f: | ||
| 83 | self.__variables = json.load(f) | ||
| 84 | |||
| 85 | |||
| 86 | def save(self): | ||
| 87 | """Save the variables into the json file. | ||
| 88 | """ | ||
| 89 | with open(self.file, "w") as f: | ||
| 90 | json.dump(self.variables, f) | ||
| 91 |