Passed
Push — main ( 01ebda...dde71f )
by Sat CFDI
01:47
created

satdigitalinvoice.utils.to_int()   A

Complexity

Conditions 2

Size

Total Lines 5
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 2
nop 1
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 open_file(filename):
124
    match OS.get_os():
125
        case OS.WINDOWS:
126
            os.startfile(filename)
127
        case OS.LINUX:
128
            subprocess.call(['xdg-open', filename])
129
        case OS.MACOS:
130
            subprocess.call(['open', filename])
131
        case _:
132
            raise NotImplementedError(f"Unknown OS: {OS.get_os()}")
133
134
135
# Enum for operative Systems
136
class OS:
137
    WINDOWS = 'Windows'
138
    LINUX = 'Linux'
139
    MACOS = 'MacOS'
140
    UNKNOWN = 'Unknown'
141
142
    @staticmethod
143
    def get_os():
144
        if sys.platform.startswith('win'):
145
            return OS.WINDOWS
146
        elif sys.platform.startswith('linux'):
147
            return OS.LINUX
148
        elif sys.platform.startswith('darwin'):
149
            return OS.MACOS
150
        else:
151
            return OS.UNKNOWN
152