Passed
Push — main ( 8825b4...ea8bbb )
by Sat CFDI
01:44
created

satdigitalinvoice.utils   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 35
eloc 90
dl 0
loc 136
rs 9.6
c 0
b 0
f 0

15 Functions

Rating   Name   Duplication   Size   Complexity  
A find_best_match() 0 8 5
A to_uuid() 0 5 2
A clear_directory() 0 3 1
A random_string() 0 3 1
A to_int() 0 5 2
A cert_info() 0 14 2
A convert_ans1_date() 0 2 1
A add_month() 0 4 1
A first_duplicate() 0 7 3
A months_between() 0 2 1
A load_certificate() 0 8 2
A to_datetime() 0 5 2
A estado_to_estatus() 0 6 3
B to_date_period() 0 14 6
A open_file() 0 6 3
1
import os
2
import random
3
import shutil
4
import subprocess
5
import sys
6
from datetime import datetime
7
from uuid import UUID
8
9
from satcfdi import Signer, DatePeriod
10
from satcfdi.accounting.models import EstadoComprobante
11
12
13
def to_date_period(periodo):
14
    if not periodo:
15
        return DatePeriod(year=None)
16
17
    for f in ('%Y', '%Y-%m', '%Y-%m-%d'):
18
        try:
19
            d = datetime.strptime(periodo, f)
20
            return DatePeriod(
21
                d.year,
22
                d.month if '%Y-%m' in f else None,
23
                d.day if f == '%Y-%m-%d' else None
24
            )
25
        except ValueError:
26
            pass
27
28
29
def to_uuid(s):
30
    try:
31
        return UUID(s)
32
    except ValueError:
33
        return None
34
35
36
def to_int(s):
37
    try:
38
        return int(s)
39
    except ValueError:
40
        return None
41
42
43
def random_string():
44
    chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
45
    return "".join(random.choice(chars) for _ in range(32))
46
47
48
def convert_ans1_date(ans1_date):
49
    return datetime.strptime(ans1_date.decode('utf-8'), '%Y%m%d%H%M%SZ')
50
51
52
def cert_info(signer: Signer):
53
    if signer:
54
        return {
55
            "NoCertificado": signer.certificate_number,
56
            # "Tipo": str(signer.type),
57
            #
58
            # "organizationName": signer.certificate.get_subject().O,
59
            # "x500UniqueIdentifier": signer.certificate.get_subject().x500UniqueIdentifier,
60
            # "serialNumber": signer.certificate.get_subject().serialNumber,
61
            # "organizationUnitName": signer.certificate.get_subject().OU,
62
            # "emailAddress": signer.certificate.get_subject().emailAddress,
63
            #
64
            "Expira": convert_ans1_date(signer.certificate.get_notAfter()),
65
            "Creado": convert_ans1_date(signer.certificate.get_notBefore()),
66
        }
67
68
69
def find_best_match(cases, dp: DatePeriod) -> (datetime, object):
70
    fk, fv = (None, None)
71
    for k, v in cases.items():
72
        k = datetime.strptime(k, '%Y-%m')
73
        if k <= dp:
74
            if fk is None or k > fk:
75
                fk, fv = k, v
76
    return fk, fv
77
78
79
def clear_directory(directory):
80
    shutil.rmtree(directory, ignore_errors=True)
81
    os.makedirs(directory, exist_ok=True)
82
83
84
# calculate the number of months between two dates
85
def months_between(d1, d2):
86
    return (d1.year - d2.year) * 12 + d1.month - d2.month
87
88
89
def add_month(dp: DatePeriod, months):
90
    year, month = divmod(dp.year * 12 + dp.month + months - 1, 12)
91
    month += 1
92
    return DatePeriod(year, month)
93
94
95
def load_certificate(data):
96
    if 'data' in data:
97
        return Signer.load_pkcs12(
98
            **data,
99
        )
100
    else:
101
        return Signer.load(
102
            **data,
103
        )
104
105
106
def first_duplicate(seq):
107
    seen = set()
108
    for x in seq:
109
        if x in seen:
110
            return x
111
        seen.add(x)
112
    return None
113
114
115
def estado_to_estatus(estatus):
116
    if estatus == 'Vigente':
117
        return EstadoComprobante.Vigente.value
118
    elif estatus == 'Cancelado':
119
        return EstadoComprobante.Cancelado.value
120
    raise ValueError(f"Unknown status: {estatus}")
121
122
123
def to_datetime(s):
124
    try:
125
        return datetime.fromisoformat(s, )
126
    except ValueError:
127
        return None
128
129
130
def open_file(filename):
131
    if sys.platform == "win32":
132
        os.startfile(filename)
133
    else:
134
        opener = "open" if sys.platform == "darwin" else "xdg-open"
135
        subprocess.call([opener, filename])
136