-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathreporter.py
executable file
·348 lines (300 loc) · 12.1 KB
/
reporter.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#!/usr/bin/env python3
"""
Reporter
Copyright (c) 2020-2023 Nicolas Beguier
Licensed under the MIT License
Written by Nicolas BEGUIER (nicolas_beguier@hotmail.com)
"""
# Standard library imports
from argparse import ArgumentParser
from datetime import datetime
import json
import glob
import os
import sys
from pathlib import Path
# Own library
from lib import analysis, display, reporting
try:
import settings
except ImportError:
print('You need to specify a settings.py file.')
print('$ cp settings.py.sample settings.py')
sys.exit(1)
# Debug
# from pdb import set_trace as st
VERSION = '3.1.1'
def get_sign(value):
"""
This function returns the sign of the value
"""
sign = ''
if value > 0:
sign = '+'
return sign
def is_different_and_valid(old_report, new_report, key):
"""
Returns True if the key is present in both report and valid
"""
return key in new_report and key in old_report\
and old_report[key]['v'] != new_report[key]['v'] \
and old_report[key]['v'] is not None and new_report[key]['v'] is not None
def save_report(output_dir):
"""
Save or display the report on disk
"""
parameters = {}
for _isin in settings.ISIN_SAVE:
parameters['isin'] = _isin
parameters['mic'] = 'XPAR'
if ',' in _isin:
parameters['isin'] = _isin.split(',', maxsplit=1)[0]
parameters['mic'] = _isin.split(',')[1]
report = reporting.get_report(parameters)
if report is None:
report = {}
report['isin'] = _isin
if not output_dir:
print(report)
else:
if not Path(output_dir).is_dir():
print(f'{output_dir} is not a directory...')
return
report_path = Path(f'{output_dir}/{datetime.now().strftime("%Y_%m_%d")}.txt')
with report_path.open('a', encoding ='utf-8') as report_file:
report_file.write(json.dumps(report)+'\n')
def load_report(input_file, display_report=True):
"""
Load and display the report
"""
report = {}
report_path = Path(input_file)
if not report_path.exists():
print(f'The specified path: {input_file} does not exists...')
sys.exit(1)
with report_path.open('r', encoding ='utf-8') as report_file:
for line in report_file.readlines():
sub_report = json.loads(line)
report[sub_report['isin']] = sub_report
if display_report:
display.print_report(sub_report)
return report
def report_valorisation(old_report, new_report, html_tag):
"""
Report the valorisation
"""
if is_different_and_valid(old_report, new_report, 'LVAL_NORM'):
evo_valorisation = round(100 * (-1 + \
float(new_report['LVAL_NORM']['v']) / float(old_report['LVAL_NORM']['v'])), 2)
sign = get_sign(evo_valorisation)
if sign == '+':
evo_valorisation = f'{html_tag["green_in"]}{sign}{evo_valorisation} %{html_tag["green_out"]}'
else:
evo_valorisation = f'{html_tag["red_in"]}{sign}{evo_valorisation} %{html_tag["red_out"]}'
print(f'{html_tag["li_in"]}{html_tag["bold_in"]}Evolution valorisation{html_tag["bold_out"]}: {evo_valorisation}{html_tag["li_out"]}')
currency = ''
if 'M_CUR' in new_report:
currency = ' '+new_report['M_CUR']['v']
print(f'{html_tag["li_in"]}{html_tag["bold_in"]}Evolution valorisation{html_tag["bold_out"]}: {old_report["LVAL_NORM"]["v"]} -> {new_report["LVAL_NORM"]["v"]}{currency}{html_tag["li_out"]}')
def report_per(old_report, new_report, html_tag):
"""
Report the PER
"""
if is_different_and_valid(old_report, new_report, 'PER_ANNEE_ESTIMEE'):
evo_per = round(float(new_report['PER_ANNEE_ESTIMEE']['v']) - float(old_report['PER_ANNEE_ESTIMEE']['v']), 1)
evo_per = f'{html_tag["blue_in"]}{get_sign(evo_per)}{evo_per}{html_tag["blue_out"]}'
print(f'{html_tag["li_in"]}{html_tag["bold_in"]}Evolution PER{html_tag["bold_out"]}: {evo_per}{html_tag["li_out"]}')
print(f'{html_tag["li_in"]}{html_tag["bold_in"]}Evolution PER{html_tag["bold_out"]}: {old_report["PER_ANNEE_ESTIMEE"]["v"]} -> {new_report["PER_ANNEE_ESTIMEE"]["v"]}{html_tag["li_out"]}')
if analysis.per_text(old_report['PER_ANNEE_ESTIMEE']['v']) != analysis.per_text(new_report['PER_ANNEE_ESTIMEE']['v']):
print(f'{html_tag["li_in"]}{html_tag["bold_in"]}Evolution PER{html_tag["bold_out"]}: {analysis.per_text(old_report["PER_ANNEE_ESTIMEE"]["v"])} -> {analysis.per_text(new_report["PER_ANNEE_ESTIMEE"]["v"])}{html_tag["li_out"]}')
def report_peg(old_report, new_report, html_tag):
"""
Report the PEG
"""
if is_different_and_valid(old_report, new_report, 'peg'):
evo_peg = round(float(new_report['peg']) - float(old_report['peg']), 1)
evo_peg = f'{html_tag["blue_in"]}{get_sign(evo_peg)}{evo_peg}{html_tag["blue_out"]}'
print(f'{html_tag["li_in"]}{html_tag["bold_in"]}Evolution PEG{html_tag["bold_out"]}: {evo_peg}{html_tag["li_out"]}')
print(f'{html_tag["li_in"]}{html_tag["bold_in"]}Evolution PEG{html_tag["bold_out"]}: {old_report["peg"]} -> {new_report["peg"]}{html_tag["li_out"]}')
def diff_report(oldest_file, newer_file, isin_compare, is_html):
"""
Compare two report
"""
html_tag = {
'bold_in': '',
'bold_out': '',
'h3_in': '',
'h3_out': '',
'li_in': '',
'li_out': '',
'red_in': '',
'red_out': '',
'green_in': '',
'green_out': '',
'blue_in': '',
'blue_out': '',
}
if is_html:
html_tag = {
'bold_in': '<b>',
'bold_out': '</b>',
'h3_in': '<h3>',
'h3_out': '</h3>',
'li_in': '<li>',
'li_out': '</li>',
'red_in': '<span style="color: #f00;">',
'red_out': '</span>',
'green_in': '<span style="color: #18b724;">',
'green_out': '</span>',
'blue_in': '<span style="color: #007eff;">',
'blue_out': '</span>',
}
old_reports = load_report(oldest_file, display_report=False)
new_reports = load_report(newer_file, display_report=False)
if is_html:
print('<html><body>')
else:
print('==============')
for _isin in isin_compare:
if is_html:
print('<div><ul>')
if _isin not in old_reports or _isin not in new_reports:
continue
old_report = old_reports[_isin]
new_report = new_reports[_isin]
print(f'{html_tag["h3_in"]}{new_report["DISPLAY_NAME"]["v"]} ({new_report["isin"]}){html_tag["h3_out"]}')
report_valorisation(old_report, new_report, html_tag)
report_per(old_report, new_report, html_tag)
report_peg(old_report, new_report, html_tag)
if is_html:
print('</ul></div>')
else:
print('==============')
if is_html:
print('</body></html>')
def get_negative_values(oldest_file, newer_file, isin_compare):
"""
Check for negative valorisation and return corresponding ISINs
"""
old_reports = load_report(oldest_file, display_report=False)
new_reports = load_report(newer_file, display_report=False)
negative_valorisation_isins = []
equal_valorisation_isins = []
for _isin in isin_compare:
if _isin not in old_reports or _isin not in new_reports:
continue
old_report = old_reports[_isin]
new_report = new_reports[_isin]
# Check if 'LVAL_NORM' is in report and if its value is less than 0
if 'LVAL_NORM' in new_report and new_report['LVAL_NORM']['v'] is not None:
if float(new_report['LVAL_NORM']['v']) - float(old_report['LVAL_NORM']['v']) < 0:
negative_valorisation_isins.append(new_report['isin'])
if float(new_report['LVAL_NORM']['v']) == float(old_report['LVAL_NORM']['v']):
equal_valorisation_isins.append(new_report['isin'])
return negative_valorisation_isins, equal_valorisation_isins
def diff3_report(directory, isin_compare, is_html):
"""
Compare two report
"""
html_tag = {
'bold_in': '',
'bold_out': '',
'h3_in': '',
'h3_out': '',
'li_in': '',
'li_out': '',
'red_in': '',
'red_out': '',
'green_in': '',
'green_out': '',
'blue_in': '',
'blue_out': '',
}
if is_html:
html_tag = {
'bold_in': '<b>',
'bold_out': '</b>',
'h3_in': '<h3>',
'h3_out': '</h3>',
'li_in': '<li>',
'li_out': '</li>',
'red_in': '<span style="color: #f00;">',
'red_out': '</span>',
'green_in': '<span style="color: #18b724;">',
'green_out': '</span>',
'blue_in': '<span style="color: #007eff;">',
'blue_out': '</span>',
}
# Get a list of all report files in the directory, sorted by creation time
report_files = sorted(glob.glob(os.path.join(directory, '*.txt')))
# Take only the last 7 files
report_files = report_files[-7:]
last_report = load_report(report_files[-1], display_report=False)
count_negative_isins = {}
for i in range(len(report_files) - 1):
negative_isins, equal_isins = get_negative_values(report_files[i], report_files[i+1], isin_compare)
# Initialize isin in negative_isins
for _isin in negative_isins:
if _isin not in count_negative_isins:
count_negative_isins[_isin] = 0
# Update count
for _isin in count_negative_isins:
if _isin in negative_isins:
count_negative_isins[_isin] += 1
elif _isin not in equal_isins:
count_negative_isins[_isin] = 0
if is_html:
print('<html><body>')
for _isin in count_negative_isins:
if count_negative_isins[_isin] >= 3:
print(f'{html_tag["h3_in"]}{last_report[_isin]["DISPLAY_NAME"]["v"]}: {count_negative_isins[_isin]} days in a row !{html_tag["h3_out"]}')
if is_html:
print('</body></html>')
if __name__ == '__main__':
PARSER = ArgumentParser()
SUBPARSERS = PARSER.add_subparsers(help='commands')
PARSER.add_argument('--version', action='version', version=VERSION)
# SAVE Arguments
SAVE_PARSER = SUBPARSERS.add_parser('save',\
help='Save command')
SAVE_PARSER.add_argument('-o', '--output-dir', action='store',\
help='Save report into the specified directory', default='')
# LOAD Arguments
LOAD_PARSER = SUBPARSERS.add_parser('load',\
help='Load command')
LOAD_PARSER.add_argument('inputfile', action='store',\
help='Load and display a saved report')
# DIFF Arguments
DIFF_PARSER = SUBPARSERS.add_parser('diff',\
help='Diff command')
DIFF_PARSER.add_argument('oldest_file', action='store',\
help='Compare this oldest file with...')
DIFF_PARSER.add_argument('newer_file', action='store',\
help='... this newer file')
DIFF_PARSER.add_argument('-i', '--isin', action='store',\
help='Specific ISIN to compare', default='')
DIFF_PARSER.add_argument('--html', action='store_true',\
help='Output in HTML format', default=False)
# DIFF3 Arguments
DIFF3_PARSER = SUBPARSERS.add_parser('diff3',\
help='Diff3 command')
DIFF3_PARSER.add_argument('directory', action='store',\
help='Directory with reports for comparison')
DIFF3_PARSER.add_argument('--html', action='store_true',\
help='Output in HTML format', default=False)
ARGS = PARSER.parse_args()
if len(sys.argv) == 1:
PARSER.print_help()
sys.exit(1)
if sys.argv[1] == 'save':
save_report(ARGS.output_dir)
elif sys.argv[1] == 'load':
load_report(ARGS.inputfile)
elif sys.argv[1] == 'diff':
ISIN_COMPARE = settings.ISIN_COMPARE
if ARGS.isin:
ISIN_COMPARE = [ARGS.isin]
diff_report(ARGS.oldest_file, ARGS.newer_file, ISIN_COMPARE, ARGS.html)
elif sys.argv[1] == 'diff3':
diff3_report(ARGS.directory, settings.ISIN_COMPARE, ARGS.html)
sys.exit(0)