Commit 5fcaeae1e904cd253a5ac6fa0edca7185b7f174f
1 parent
7213a20609
Exists in
master
New way to set environment variable using core.env module. Need to define now how to store envs.
Showing 2 changed files with 98 additions and 15 deletions Side-by-side Diff
volia/core/env.py
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 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) |
volia/experiment.py
... | ... | @@ -4,8 +4,8 @@ |
4 | 4 | #from decouple import Config, RepositoryEnv |
5 | 5 | import decouple |
6 | 6 | import json |
7 | +import core.env | |
7 | 8 | |
8 | - | |
9 | 9 | # TODO: |
10 | 10 | # Load automatically if exists the persistency file |
11 | 11 | # Create a class that is a sigleton with configuration variables |
12 | 12 | |
13 | 13 | |
14 | 14 | |
15 | 15 | |
... | ... | @@ -77,25 +77,18 @@ |
77 | 77 | CONFIG = None |
78 | 78 | PERSISTENCY_DATA = None |
79 | 79 | env_file = os.path.join(VOLIA_HOME, ".env") |
80 | - if os.path.isfile(env_file): | |
81 | - # Load le fichier | |
82 | - CONFIG = decouple.AutoConfig(search_path=VOLIA_HOME) | |
83 | - # Then loading the persistency file | |
84 | - persistency_file = os.path.join(VOLIA_HOME, "persistency.json") | |
85 | 80 | |
86 | - with open(persistency_file, "r") as f: | |
87 | - PERSISTENCY_DATA = json.load(f) | |
88 | - | |
89 | - else: | |
90 | - # Volia not initialized. | |
81 | + if not os.path.isfile(env_file): | |
91 | 82 | print("VOLIA is not initialized. Initializing.") |
92 | 83 | init() |
93 | - pass | |
94 | 84 | |
95 | - print("Config: ", CONFIG) | |
96 | - print("PERSISTENCY_DATA: ", PERSISTENCY_DATA) | |
85 | + # Load le fichier | |
86 | + CONFIG = decouple.AutoConfig(search_path=VOLIA_HOME) | |
87 | + # Then loading the persistency file | |
88 | + persistency_file = os.path.join(VOLIA_HOME, "persistency.json") | |
89 | + PERSISTENCY_DATA = core.env.Env(persistency_file) | |
97 | 90 | |
98 | - | |
91 | + # PARSER | |
99 | 92 | parser = argparse.ArgumentParser(description="...") |
100 | 93 | subparsers = parser.add_subparsers(title="actions") |
101 | 94 |