logpyros.py
7.33 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
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
import os
import math
import tempfile
# --- update the path of Python in case to test this file alone
py_path = os.sys.path
py_pwd = os.path.normpath(os.getcwd() + "/../../../..")
if (py_pwd not in py_path):
(os.sys.path).append(py_pwd)
# --- import celestial mechanics
import src.core.celme as celme
class LogPyros:
# === Constants
NO_ERROR = 0
ERR_ALIAS_NAME_NOT_DEFINED = 10
ERR_FILE_NOT_EXISTS = 101
ERR_PATH_NOT_EXISTS = 102
ERR_EMPTY_FILENAME = 103
ERR_EMPTY_PATHNAME = 104
# === Private variables
_last_errno = NO_ERROR
_path_data = ''
_agent_alias = ''
_path_data_log = ''
_path_wwwpyros = ''
_date = None
_home = None
_noon_hour = 12
# =====================================================================
# =====================================================================
# Private methods
# =====================================================================
# =====================================================================
def _mkdir_path_log(self):
# ---
if self._agent_alias == "":
self._last_errno = self.ERR_EMPTY_FILENAME
else:
self._path_www_log = self._path_wwwpyros + "/logs/" + self._agent_alias
os.mkdir(self._path_www_log)
# ---
if self._path_data == "":
self._last_errno = self.ERR_EMPTY_PATHNAME
else:
self._path_data_log = self._path_data + "/logs/" + self._agent_alias
os.mkdir(self._path_data_log)
def _jd2datedigit(self, jd):
""" Convert a julian day into a string of YYYYMMDD
"""
datetmp = self._date.date(jd);
datedigit = (datetmp.digits(0))[0:8]
return datedigit
def _date2night(self, date):
# night is the truncated part of the date of the previous noon
self._date.date(date)
jd = self._date.jd()
jd0 = math.floor(jd) - self._noon_hour/24
d = celme.Date(jd0)
djd = jd-jd0
# print(f"jd0 = {d.iso()} djd={djd}")
if djd>=1:
jd0 += 1
djd = jd-jd0
if djd>=1:
jd0 += 1
d = celme.Date(jd0)
night = d.digits(0)[0:8]
return night
def _night2dates(self, night):
"""
jd_noon0 = previous noon
jd_midnight = midnight instant
jd_noon1 = next noon
"""
d = celme.Date(night)
d_noon0 = d + self._noon_hour/24
jd_noon0 = d_noon0.jd()
jd_midnight = jd_noon0 + 0.5
jd_noon1 = jd_noon0 + 1.0
return jd_noon0, jd_midnight, jd_noon1
# =====================================================================
# =====================================================================
# Methods for experimented users
# =====================================================================
# =====================================================================
# =====================================================================
# =====================================================================
# Methods for users
# =====================================================================
# =====================================================================
def set_agent_alias(self, agent_alias:str):
if agent_alias=="":
self._last_errno = self.ERR_ALIAS_NAME_NOT_DEFINED
raise Exception(f"Agent alias must be not be empty string.")
self._agent_alias = agent_alias
return self.NO_ERROR
def get_agent_alias(self):
return self._agent_alias
def set_path_data(self, path_data:str):
if not os.path.exists(path_data):
self._last_errno = self.ERR_PATH_NOT_EXISTS
raise Exception(f"Path '{path_data}' for data does not exists. Create it first manually.")
self._path_data = path_data
return self.NO_ERROR
def get_path_data(self):
return self._path_data
def set_path_wwwpyros(self, path_wwwpyros:str):
if not os.path.exists(path_wwwpyros):
self._path_wwwpyros = self.ERR_PATH_NOT_EXISTS
raise Exception(f"Path '{path_wwwpyros}' for wwwpyros does not exists. Create it first manually.")
self._path_wwwpyros = path_wwwpyros
return self.NO_ERROR
def get_path_wwwpyros(self):
return self._path_wwwpyros
def set_home(self, home:str):
self._home.home(home)
longitude = self._home.longitude
self._noon_hour = (180 - longitude)/15.0
# - place noon_hour in the range [0 24[
if self._noon_hour < 0:
self._noon_hour += 24
def get_home(self):
return self._home.gps
def print(self, msg):
# --- classical print
print(msg)
# --- no more if the agent name is not defined
if self._agent_alias=="":
return
# ---
self.file(msg)
def file(self, msg):
# --- no more if the agent name is not defined
if self._agent_alias=="":
return
# --- check the path to write the log file
path = os.path.normpath(self._path_data + os.sep + "logs" + os.sep + self._agent_alias)
if not os.path.exists(path):
try:
os.makedirs(path)
except:
p = f"Cannot create path {path}"
raise Exception(p)
# --- compute the current night digital date
night = self._date2night("now")
# --- build the log file name
file = os.path.normpath(path + os.sep + self._agent_alias + '_' + night + '.log')
# --- prepare the super_msg
self._date.date("NOW")
super_msg = f"{self._date.iso()} {msg}"
# --- write super_msg in the log file
with open(file,'a') as fic:
fic.write(super_msg+"\n")
def db(self, msg):
self._date.date("now");
print(f"{self._date.iso()} {msg}")
# =====================================================================
# =====================================================================
# Special methods
# =====================================================================
# =====================================================================
def __init__(self, agent_alias:str = "", path_data:str = "", path_wwwpyros:str = ""):
self._last_errno = self.NO_ERROR
# ---
if agent_alias != "":
self.set_agent_alias(agent_alias)
# ---
if path_data != "":
self.set_path_data(path_data)
else:
self.set_path_data(tempfile.gettempdir())
# ---
if path_wwwpyros != "":
self.set_path_wwwpyros(path_wwwpyros)
self._date = celme.Date()
self._home = celme.Home()
self._noon_hour = 12
# =====================================================================
# =====================================================================
# Test if main
# =====================================================================
# =====================================================================
if __name__ == "__main__":
agent_alias = "test"
path_data = "c:/srv/work/pyros"
log = LogPyros()
log.set_agent_alias(agent_alias)
log.set_path_data(path_data)
log.set_home("GPS 2 E 43 148")
a = 2
b = 'tutu'
c = f"a={a} b={b}"
log.print(c)