Passed
Push — main ( f35407...df09ae )
by Sat CFDI
01:46
created

satdigitalinvoice.utils   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 25
dl 0
loc 34
rs 10
c 0
b 0
f 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
A to_uuid() 0 5 2
A add_file_handler() 0 9 1
A random_string() 0 4 1
A convert_ans1_date() 0 2 1
1
import base64
2
import logging
3
import random
4
from datetime import datetime
5
from uuid import UUID
6
7
8
def to_uuid(s):
9
    try:
10
        return UUID(s)
11
    except ValueError:
12
        return None
13
14
15
def random_string():
16
    hash = random.randbytes(32)
17
    res = base64.urlsafe_b64encode(hash).decode()
18
    return res.rstrip("=")
19
20
21
def add_file_handler():
22
    fh = logging.FileHandler(
23
        f'.data/errors.log',
24
        mode='a'
25
    )
26
    fh.setLevel(logging.ERROR)
27
    formatter = logging.Formatter('%(asctime)s - %(message)s')
28
    fh.setFormatter(formatter)
29
    logging.root.addHandler(fh)
30
31
32
def convert_ans1_date(ans1_date):
33
    return datetime.strptime(ans1_date.decode('utf-8'), '%Y%m%d%H%M%SZ')