Passed
Push — main ( 64214e...7b8dcf )
by Sat CFDI
01:49
created

satdigitalinvoice.utils   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 13
eloc 48
dl 0
loc 66
rs 10
c 0
b 0
f 0

7 Functions

Rating   Name   Duplication   Size   Complexity  
A find_best_match() 0 8 5
A parse_ym_date() 0 2 1
A to_uuid() 0 5 2
A add_file_handler() 0 10 1
A cert_info() 0 14 2
A random_string() 0 3 1
A convert_ans1_date() 0 2 1
1
import logging
2
import random
3
from datetime import datetime
4
from uuid import UUID
5
6
from satcfdi import Signer
7
8
9
def parse_ym_date(periodo):
10
    return datetime.strptime(periodo, '%Y-%m')
11
12
13
def to_uuid(s):
14
    try:
15
        return UUID(s)
16
    except ValueError:
17
        return None
18
19
20
def random_string():
21
    chars = "0123456789abcdefghijklmnopqrstuvwxzyABCDEFGHIJKLMNOPQRSTUVWXZY"
22
    return "".join(random.choice(chars) for _ in range(32))
23
24
25
def add_file_handler():
26
    fh = logging.FileHandler(
27
        f'.data/errors.log',
28
        mode='a',
29
        encoding='utf-8',
30
    )
31
    fh.setLevel(logging.ERROR)
32
    formatter = logging.Formatter('%(asctime)s - %(message)s')
33
    fh.setFormatter(formatter)
34
    logging.root.addHandler(fh)
35
36
37
def convert_ans1_date(ans1_date):
38
    return datetime.strptime(ans1_date.decode('utf-8'), '%Y%m%d%H%M%SZ')
39
40
41
def cert_info(signer: Signer):
42
    if signer:
43
        return {
44
            "NoCertificado": signer.certificate_number,
45
            "Tipo": str(signer.type),
46
47
            "organizationName": signer.certificate.get_subject().O,
48
            "x500UniqueIdentifier": signer.certificate.get_subject().x500UniqueIdentifier,
49
            "serialNumber": signer.certificate.get_subject().serialNumber,
50
            "organizationUnitName": signer.certificate.get_subject().OU,
51
            "emailAddress": signer.certificate.get_subject().emailAddress,
52
53
            "notAfter": convert_ans1_date(signer.certificate.get_notAfter()),
54
            "notBefore": convert_ans1_date(signer.certificate.get_notBefore()),
55
        }
56
57
58
def find_best_match(cases, emission_date):
59
    fk, fv = (None, None)
60
    for k, v in cases.items():
61
        k = parse_ym_date(k)
62
        if k <= emission_date:
63
            if fk is None or k > fk:
64
                fk, fv = k, v
65
    return fv
66