-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsettings.py
More file actions
181 lines (140 loc) · 5.81 KB
/
Copy pathsettings.py
File metadata and controls
181 lines (140 loc) · 5.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
"""User settings for the OpenHexa CLI."""
import logging
import os
from configparser import ConfigParser
import click
CONFIGFILE_PATH = os.path.expanduser("~") + "/.openhexa.ini"
def _open_config():
"""Open the local settings file using configparser.
A default settings file will be generated if the file does not exist.
"""
config = ConfigParser()
if os.path.exists(CONFIGFILE_PATH):
config.read(CONFIGFILE_PATH)
else:
config.read_string(
"""
[openhexa]
url=https://api.openhexa.org
[workspaces]
"""
)
return config
def _save_config(config: ConfigParser):
"""Save the provided configparser local settings to disk."""
with open(CONFIGFILE_PATH, "w") as configfile:
config.write(configfile)
class Settings:
"""Class that holds the user settings for the CLI."""
_file_config: ConfigParser = None
def __init__(self):
self.refresh()
@property
def debug(self):
"""Return the debug flag from the settings file or environment variables."""
return os.getenv("DEBUG") or os.getenv("HEXA_DEBUG")
@property
def api_url(self):
"""Return the API URL from the settings file or environment variables."""
url_from_env = os.getenv("HEXA_API_URL") or os.getenv("HEXA_SERVER_URL")
if url_from_env is None:
url_from_env = self._file_config["openhexa"]["url"]
return url_from_env.rstrip("/")
@property
def public_api_url(self):
"""Return the public API URL from the settings file or environment variables."""
return self.api_url.replace("api", "app")
@property
def current_workspace(self):
"""Return the current workspace from the settings file or environment variables."""
if os.getenv("HEXA_WORKSPACE"):
return os.getenv("HEXA_WORKSPACE")
try:
return self._file_config["openhexa"]["current_workspace"]
except KeyError:
return None
@property
def workspaces(self):
"""Return the workspaces from the settings file."""
return self._file_config["workspaces"]
def activate(self, workspace: str):
"""Set the current workspace in the settings file."""
if workspace not in self.workspaces:
raise ValueError(f"Workspace {workspace} does not exist.")
self._file_config["openhexa"]["current_workspace"] = workspace
self.save()
def add_workspace(self, workspace: str, token: str, enabled: bool = True):
"""Add a workspace to the settings file."""
self._file_config["workspaces"].update({workspace: token})
if enabled:
self._file_config["openhexa"]["current_workspace"] = workspace
self.save()
def remove_workspace(self, workspace: str):
"""Remove a workspace from the settings file."""
del self._file_config["workspaces"][workspace]
if self._file_config["openhexa"]["current_workspace"] == workspace:
self._file_config["openhexa"]["current_workspace"] = None
self.save()
def set_api_url(self, url: str):
"""Set the API URL in the settings file."""
self._file_config["openhexa"]["url"] = url
self.save()
@property
def access_token(self):
"""Return the access token from the settings file or environment variables."""
if os.getenv("HEXA_TOKEN"):
return os.getenv("HEXA_TOKEN")
return self._file_config["workspaces"].get(self.current_workspace)
@property
def last_version_check(self):
"""Return the last version check timestamp from the settings file."""
val = self._file_config["openhexa"].get("last_version_check", None)
if val is not None:
return int(val)
@last_version_check.setter
def last_version_check(self, value: int):
"""Set the last version check timestamp in the settings file."""
assert isinstance(value, int), "last_version_check must be an integer."
self._file_config["openhexa"]["last_version_check"] = str(value)
self.save()
@property
def last_breaking_change_check(self):
"""Return the last breaking change check timestamp from the settings file."""
val = self._file_config["openhexa"].get("last_breaking_change_check", None)
if val is not None:
return int(val)
@last_breaking_change_check.setter
def last_breaking_change_check(self, value: int):
"""Set the last breaking change check timestamp in the settings file."""
assert isinstance(value, int), "last_breaking_change_check must be an integer."
self._file_config["openhexa"]["last_breaking_change_check"] = str(value)
self.save()
def save(self):
"""Save the settings to disk."""
_save_config(self._file_config)
self.refresh()
def refresh(self):
"""Refresh the settings file."""
self._file_config = _open_config()
def __repr__(self):
"""Return a string representation of the object."""
return f"<Config(url={self.api_url}, current_workspace={self.current_workspace}, debug={self.debug})>"
class ClickEchoHandler(logging.Handler):
"""Custom logging handler that uses click.echo to print logs."""
def emit(self, record):
"""Emit the log record."""
msg = self.format(record)
click.echo(msg)
def setup_logging():
"""Set up the logging for the CLI."""
logger = logging.getLogger()
logger.setLevel(logging.INFO)
# Create a formatter
formatter = logging.Formatter("%(message)s")
# Create an instance of the custom handler
click_handler = ClickEchoHandler()
# Set the formatter for the handler
click_handler.setFormatter(formatter)
# Add the handler to the logger
logger.addHandler(click_handler)
settings = Settings()