-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodules.py
278 lines (215 loc) · 8.17 KB
/
modules.py
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import os
import tomli
import tomli_w
import re
import json
class ConfigLoader:
"""
A class for loading and retrieving configuration settings from a TOML file.
Args:
file_path (str): The path to the TOML configuration file. Default is "config.toml".
Raises:
FileNotFoundError: If the specified configuration file does not exist.
KeyError: If the required configuration settings are not found in the file.
"""
def __init__(self, file_path="config.toml"):
self.file_path = file_path
def load_config(self):
"""
Loads the configuration settings from the TOML file.
Returns:
dict: The loaded configuration settings.
Raises:
FileNotFoundError: If the specified configuration file does not exist.
"""
if os.path.exists(self.file_path):
with open(self.file_path, mode="rb") as fp:
config = tomli.load(fp)
return config
else:
raise FileNotFoundError(
f"Config file '{self.file_path}' not found. Create it and add astrometry token"
)
def get_astrometry_key(self):
"""
Retrieves the astrometry token from the configuration settings.
Returns:
str: The astrometry token.
Raises:
KeyError: If the astrometry token is not found in the configuration settings.
"""
return self.get_value_from_data("token", "astrometry")
def get_home_dir(self):
"""
Retrieves the home directory from the configuration settings.
Returns:
str: The home directory.
Raises:
KeyError: If the home directory is not found in the configuration settings.
"""
return self.get_value_from_data("home_dir")
def get_value_from_data(self, key="home_dir", category="data"):
"""
Retrieves a value from the data section of the configuration settings.
Args:
key (str): The key of the value to retrieve.
Returns:
str: The value associated with the specified key.
Raises:
KeyError: If the specified key is not found in the data section of the configuration settings.
"""
config = self.load_config()
if category in config and key in config[category]:
return config[category][key]
else:
raise KeyError(f"Key '{key}' or Category '{category}' not found in the data section of config.toml")
class EditConfig:
"""
A class for editing the configuration settings in a TOML file.
Args:
file_path (str): The path to the TOML configuration file. Default is "config.toml".
Raises:
FileNotFoundError: If the specified configuration file does not exist.
"""
def __init__(self, file_path="config.toml"):
self.file_path = file_path
def load_config(self):
"""
Loads the configuration settings from the TOML file.
Returns:
dict: The loaded configuration settings.
Raises:
FileNotFoundError: If the specified configuration file does not exist.
"""
if os.path.exists(self.file_path):
with open(self.file_path, mode="rb") as fp:
config = tomli.load(fp)
return config
else:
raise FileNotFoundError(
f"Config file '{self.file_path}' not found. Create it and add astrometry token"
)
def save_config(self, config):
"""
Saves the configuration settings to the TOML file.
Args:
config (dict): The configuration settings to save.
"""
with open(self.file_path, mode="wb") as fp:
tomli_w.dump(config, fp)
def remove_key(self, key, category="data"):
"""
Removes a key from the configuration settings.
Args:
key (str): The key to remove.
"""
config = self.load_config()
if key in config[category]:
del config[category][key]
self.save_config(config)
else:
print(f"Key '{key}' not found in the configuration settings.")
def set_value(self, key, value, category="data"):
"""
Sets a value in the configuration settings.
Args:
key (str): The key to set.
value (str): The value to associate with the key.
"""
config = self.load_config()
config[category][key] = value
self.save_config(config)
class ParseData:
"""Parse from data.txt file, meteor and stars coordinates
Args:
data_path (str): data.txt file path
Usage:
Create ParseData object and call get_meteor_start_end_coordinates() and get_stars_coordinates() methods
```python
PD = ParseData("data/data.txt")
meteor = PD.get_meteor_start_end_coordinates()
stars = PD.get_stars_coordinates()```
Returns:
list[float]: Meteor and stars coordinates
list[float]: Stars coordinates
"""
def __init__ (self, data_path):
self.data_path = data_path
self.data = self._read_file()
self.parsed_data = self.parse_data()
def parse_data(self):
self.get_meteor_start_end_coordinates()
self.get_stars_coordinates()
def get_meteor_start_end_coordinates(self) -> list[float]:
"""Get meteor coordinates from data.txt file
Args:
path (str): data.txt file path
Returns:
tuple(): Meteor coordinates
"""
file = open(self.data_path, 'r')
for line in file:
if line.startswith("#Meteor 1:"):
match = re.search(r'start \(([^)]+)\) end \(([^)]+)\)', line)
if match:
start = tuple(map(float, match.group(1).split(', ')))
end = tuple(map(float, match.group(2).split(', ')))
return start, end
return None, None # Return the coordinates of the meteor
def get_stars_coordinates(self):
star_pattern = re.compile(r"#\d+ position \((\d+\.?\d*), (\d+\.?\d*)\)")
stars = star_pattern.findall(self.data)
return [(float(x), float(y)) for x, y in stars]
def _read_file(self):
if not os.path.exists(self.data_path):
raise FileNotFoundError(f"File {self.data_path} not found")
with open(self.data_path, 'r') as f:
data = f.read()
return data
class cache():
def __init__(self):
self.dirname = os.path.dirname(__file__)
self.cache_path = os.path.join(self.dirname, "cache")
self.cache_file = os.path.join(self.cache_path, "cache.json")
if not os.path.exists(self.cache_path):
self.create_cache()
self.load_cache()
def create_cache(self):
if not os.path.exists(self.cache_path):
os.mkdir(self.cache_path)
self.cache = {}
self.save_cache()
else:
print("Cache already exists")
def load_cache(self):
if os.path.exists(self.cache_file):
with open(self.cache_file, "r") as file:
self.cache = json.load(file)
else:
self.cache = {}
def save_cache(self):
with open(self.cache_file, "w") as file:
json.dump(self.cache, file)
def get(self, key):
return self.cache.get(key)
def search(self, ident, obs):
for key, value in self.cache.items():
if value[0] == ident and value[1] == obs:
return key
return None
def set_key(self, key, value, observatory):
# each key have two values, id and observatory
self.cache[key] = (value, observatory)
self.save_cache()
def delete(self, key):
if self.cache.get(key):
del self.cache[key]
self.save_cache()
def print_cache(self):
print(self.cache)
def clear_cache(self):
self.cache = {}
self.save_cache()
if __name__ == "__main__":
print(ParseData("data/data.txt").get_meteor_start_end_coordinates())
print(ParseData("data/data.txt").get_stars_coordinates())