|
2 | 2 | import click |
3 | 3 | import requests |
4 | 4 | import difflib |
| 5 | +import os |
5 | 6 | from .utils import * |
6 | 7 | from .version import __version__ as cli_version |
7 | 8 |
|
8 | 9 |
|
9 | 10 | class Config(dict): |
10 | | - CONFIG_FILE = None |
11 | | - PROJECT_CONFIG_VERSION = None |
12 | | - LAST_VERSION = None |
| 11 | + """ |
| 12 | + Abstraction layer for managing config files. |
| 13 | + """ |
13 | 14 |
|
14 | | - def __init__(self, *args, **kwargs): |
15 | | - self.load() |
| 15 | + FILENAME = None |
| 16 | + """ |
| 17 | + The filename used for this configuration file. |
| 18 | + """ |
16 | 19 |
|
17 | | - def load(self): |
18 | | - """ |
19 | | - Load project.json and write to the global projectConfig. |
| 20 | + VERSION = None |
| 21 | + """ |
| 22 | + The current version for this configuration file, which is tied to the viur-cli package. |
| 23 | + """ |
20 | 24 |
|
21 | | - This function is responsible for loading the project.json configuration file and populating the global |
22 | | - projectConfig variable. |
23 | | - It handles error checks, such as missing or invalid JSON configuration files, and updates the project |
24 | | - configuration. |
| 25 | + def __init__(self, *, path=None): |
| 26 | + self.path = path |
25 | 27 |
|
26 | | - :param path: str, optional |
27 | | - The path to the project.json file. If not provided, the default projectConfigFilePath is used. |
| 28 | + if self.VERSION: |
| 29 | + self["format"] = self.VERSION |
28 | 30 |
|
29 | | - :return: dict |
30 | | - The project configuration loaded from the project.json file. |
| 31 | + self.load() |
| 32 | + |
| 33 | + def load(self): |
| 34 | + """ |
| 35 | + Load configuration from a file into this config object. |
31 | 36 | """ |
32 | 37 | # Search in any parent folder for a project.json, |
33 | 38 | # change working directory because subsequent commands |
34 | 39 | # require for project root folder. |
35 | 40 |
|
36 | 41 | changed = False |
37 | | - while not os.path.exists(self.CONFIG_FILE): |
| 42 | + while not os.path.exists(self.FILENAME): |
38 | 43 | os.chdir("..") |
39 | 44 | changed = True |
40 | 45 |
|
41 | 46 | if os.getcwd() == "/": |
42 | | - echo_fatal(f"{self.CONFIG_FILE} not found - please check if you are in the right folder.") |
| 47 | + |
| 48 | + if self.path: |
| 49 | + self.save() |
| 50 | + self.load() |
| 51 | + return |
| 52 | + else: |
| 53 | + echo_fatal(f"{self.FILENAME} not found - please check if you are in the right folder.") |
43 | 54 |
|
44 | 55 | if changed: |
45 | 56 | echo_info(f"Project root is {os.getcwd()}") |
46 | 57 |
|
47 | 58 | try: |
48 | | - f = open(self.CONFIG_FILE, "r") |
| 59 | + f = open(self.FILENAME, "r") |
| 60 | + self.path = os.getcwd() |
49 | 61 | self.update(json.loads(f.read())) |
50 | 62 |
|
51 | 63 |
|
52 | 64 | except FileNotFoundError: |
53 | | - echo_fatal(f"Can't open {self.CONFIG_FILE} for reading") |
| 65 | + echo_fatal(f"Can't open {self.FILENAME} for reading") |
54 | 66 |
|
55 | 67 | except json.decoder.JSONDecodeError as e: |
56 | 68 | echo_fatal( |
57 | | - f"The configuration in {self.CONFIG_FILE} contains invalid JSON: {str(e)}. Please verify right syntax.") |
| 69 | + f"The configuration in {self.FILENAME} contains invalid JSON: {str(e)}. Please verify right syntax.") |
| 70 | + |
58 | 71 | self.migrate() |
59 | 72 |
|
60 | 73 | def migrate(self): |
| 74 | + """ |
| 75 | + A hook for migrating a read config. |
| 76 | + """ |
61 | 77 | pass |
62 | 78 |
|
63 | 79 | def save(self): |
64 | 80 | """ |
65 | | - Write the current projectConfig dictionary to project.json. |
| 81 | + Write the current configuration back to the file. |
66 | 82 | """ |
67 | | - with open(self.CONFIG_FILE, "w") as f: |
| 83 | + os.chdir(self.path) |
| 84 | + with open(self.FILENAME, "w") as f: |
68 | 85 | json.dump(self, f, indent=4, sort_keys=True) |
69 | 86 | f.write('\n') |
70 | 87 |
|
71 | 88 |
|
72 | 89 | class ProjectConfig(Config): |
73 | | - CONFIG_FILE = "project.json" |
74 | | - PROJECT_CONFIG_VERSION = "2.0.0" |
75 | | - LAST_VERSION = "" |
| 90 | + FILENAME = "project.json" |
| 91 | + VERSION = "2.0.0" |
76 | 92 |
|
77 | 93 | def __init__(self): |
78 | | - |
79 | 94 | self["default"] = {} |
80 | | - self["format"] = self.PROJECT_CONFIG_VERSION |
81 | 95 | super().__init__() |
82 | 96 |
|
83 | 97 | def get_profile(self, profile): |
@@ -129,7 +143,7 @@ def migrate(self): |
129 | 143 | self["format"] = old_format |
130 | 144 | del self["default"]["format"] |
131 | 145 |
|
132 | | - assert self["format"] in ["1.0.0", "1.0.1", "1.1.0", "1.1.1", "1.2.0", self.PROJECT_CONFIG_VERSION], \ |
| 146 | + assert self["format"] in ["1.0.0", "1.0.1", "1.1.0", "1.1.1", "1.2.0", self.VERSION], \ |
133 | 147 | "Invalid formatversion, you have to fix it manually" |
134 | 148 |
|
135 | 149 | # Version 1.0.1 |
@@ -229,18 +243,18 @@ class ScriptorConfig(Config): |
229 | 243 | Manage scriptor configuration. |
230 | 244 | TODO miragte with other config |
231 | 245 | """ |
232 | | - CONFIG_FILE = "viur_scriptor_config.json" |
| 246 | + FILENAME = "viur_scriptor_config.json" |
233 | 247 | DEFAULT_BASE_URL = "http://localhost:8080" |
234 | 248 | DEFAULT_WORKING_DIR = "scripts/" |
235 | | - _instance = None |
236 | 249 |
|
237 | | - def __init__(self, *args, **kwargs): |
| 250 | + def __init__(self, **kwargs): |
238 | 251 | self.update({ |
239 | 252 | "base_url": self.DEFAULT_BASE_URL, |
240 | 253 | "working_dir": self.DEFAULT_WORKING_DIR, |
241 | 254 | }) |
242 | | - super().__init__(*args, **kwargs) |
| 255 | + super().__init__(**kwargs) |
243 | 256 |
|
244 | 257 |
|
| 258 | +# Create specific configs |
245 | 259 | config = ProjectConfig() |
246 | | -scriptor_config = ScriptorConfig() |
| 260 | +scriptor_config = ScriptorConfig(path=config.path) |
0 commit comments