1
|
|
|
import logging |
2
|
|
|
import os |
3
|
|
|
import random |
4
|
|
|
import shutil |
5
|
|
|
from datetime import datetime |
6
|
|
|
from uuid import UUID |
7
|
|
|
|
8
|
|
|
from satcfdi import Signer |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
def parse_ym_date(periodo): |
12
|
|
|
return datetime.strptime(periodo, '%Y-%m') |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
def to_uuid(s): |
16
|
|
|
try: |
17
|
|
|
return UUID(s) |
18
|
|
|
except ValueError: |
19
|
|
|
return None |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
def random_string(): |
23
|
|
|
chars = "0123456789abcdefghijklmnopqrstuvwxzyABCDEFGHIJKLMNOPQRSTUVWXZY" |
24
|
|
|
return "".join(random.choice(chars) for _ in range(32)) |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
def add_file_handler(): |
28
|
|
|
fh = logging.FileHandler( |
29
|
|
|
f'.data/errors.log', |
30
|
|
|
mode='a', |
31
|
|
|
encoding='utf-8', |
32
|
|
|
) |
33
|
|
|
fh.setLevel(logging.ERROR) |
34
|
|
|
formatter = logging.Formatter('%(asctime)s - %(message)s') |
35
|
|
|
fh.setFormatter(formatter) |
36
|
|
|
logging.root.addHandler(fh) |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
def convert_ans1_date(ans1_date): |
40
|
|
|
return datetime.strptime(ans1_date.decode('utf-8'), '%Y%m%d%H%M%SZ') |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
def cert_info(signer: Signer): |
44
|
|
|
if signer: |
45
|
|
|
return { |
46
|
|
|
"NoCertificado": signer.certificate_number, |
47
|
|
|
"Tipo": str(signer.type), |
48
|
|
|
|
49
|
|
|
"organizationName": signer.certificate.get_subject().O, |
50
|
|
|
"x500UniqueIdentifier": signer.certificate.get_subject().x500UniqueIdentifier, |
51
|
|
|
"serialNumber": signer.certificate.get_subject().serialNumber, |
52
|
|
|
"organizationUnitName": signer.certificate.get_subject().OU, |
53
|
|
|
"emailAddress": signer.certificate.get_subject().emailAddress, |
54
|
|
|
|
55
|
|
|
"notAfter": convert_ans1_date(signer.certificate.get_notAfter()), |
56
|
|
|
"notBefore": convert_ans1_date(signer.certificate.get_notBefore()), |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
def find_best_match(cases, emission_date): |
61
|
|
|
fk, fv = (None, None) |
62
|
|
|
for k, v in cases.items(): |
63
|
|
|
k = parse_ym_date(k) |
64
|
|
|
if k <= emission_date: |
65
|
|
|
if fk is None or k > fk: |
66
|
|
|
fk, fv = k, v |
67
|
|
|
return fk, fv |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
def clear_directory(directory): |
71
|
|
|
shutil.rmtree(directory, ignore_errors=True) |
72
|
|
|
os.makedirs(directory, exist_ok=True) |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
# calculate the number of months between two dates |
76
|
|
|
def months_between(d1, d2): |
77
|
|
|
return (d1.year - d2.year) * 12 + d1.month - d2.month |
78
|
|
|
|