Passed
Push — main ( 69a97f...2ac74a )
by Sat CFDI
01:49
created

satdigitalinvoice.utils.convert_ans1_date()   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 2
rs 10
c 0
b 0
f 0
cc 1
nop 1
1
import os
2
import random
3
import shutil
4
from datetime import datetime
5
from uuid import UUID
6
7
from satcfdi import Signer, DatePeriod
8
9
10
def to_date_period(periodo):
11
    if not periodo:
12
        return DatePeriod(year=None)
13
14
    for f in ('%Y', '%Y-%m', '%Y-%m-%d'):
15
        try:
16
            d = datetime.strptime(periodo, f)
17
            return DatePeriod(
18
                d.year,
19
                d.month if '%Y-%m' in f else None,
20
                d.day if f == '%Y-%m-%d' else None
21
            )
22
        except ValueError:
23
            pass
24
25
26
def to_uuid(s):
27
    try:
28
        return UUID(s)
29
    except ValueError:
30
        return None
31
32
33
def to_int(s):
34
    try:
35
        return int(s)
36
    except ValueError:
37
        return None
38
39
40
def random_string():
41
    chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
42
    return "".join(random.choice(chars) for _ in range(32))
43
44
45
def convert_ans1_date(ans1_date):
46
    return datetime.strptime(ans1_date.decode('utf-8'), '%Y%m%d%H%M%SZ')
47
48
49
def cert_info(signer: Signer):
50
    if signer:
51
        return {
52
            "NoCertificado": signer.certificate_number,
53
            # "Tipo": str(signer.type),
54
            #
55
            # "organizationName": signer.certificate.get_subject().O,
56
            # "x500UniqueIdentifier": signer.certificate.get_subject().x500UniqueIdentifier,
57
            # "serialNumber": signer.certificate.get_subject().serialNumber,
58
            # "organizationUnitName": signer.certificate.get_subject().OU,
59
            # "emailAddress": signer.certificate.get_subject().emailAddress,
60
            #
61
            "Expira": convert_ans1_date(signer.certificate.get_notAfter()),
62
            "Creado": convert_ans1_date(signer.certificate.get_notBefore()),
63
        }
64
65
66
def find_best_match(cases, dp: DatePeriod) -> (datetime, object):
67
    fk, fv = (None, None)
68
    for k, v in cases.items():
69
        k = datetime.strptime(k, '%Y-%m')
70
        if k <= dp:
71
            if fk is None or k > fk:
72
                fk, fv = k, v
73
    return fk, fv
74
75
76
def clear_directory(directory):
77
    shutil.rmtree(directory, ignore_errors=True)
78
    os.makedirs(directory, exist_ok=True)
79
80
81
# calculate the number of months between two dates
82
def months_between(d1, d2):
83
    return (d1.year - d2.year) * 12 + d1.month - d2.month
84
85
86
def add_month(dp: DatePeriod, months):
87
    year, month = divmod(dp.year * 12 + dp.month + months - 1, 12)
88
    month += 1
89
    return DatePeriod(year, month)
90
91
92
def load_certificate(data):
93
    if 'data' in data:
94
        return Signer.load_pkcs12(
95
            **data,
96
        )
97
    else:
98
        return Signer.load(
99
            **data,
100
        )
101
102
103
def first_duplicate(seq):
104
    seen = set()
105
    for x in seq:
106
        if x in seen:
107
            return x
108
        seen.add(x)
109
    return None
110
111
112
def parse_rango(rango):
113
    if rango == "":
114
        return 1, None
115
    if rango.isdigit():
116
        return int(rango), int(rango)
117
    if "-" in rango:
118
        start, end = rango.split("-")
119
        return int(start), int(end)
120