misc.py 2.59 KB
Newer Older
Vladislav Rykov's avatar
Vladislav Rykov committed
1
from app import app
2
3
from binascii import hexlify
import os
Vladislav Rykov's avatar
Vladislav Rykov committed
4
import psycopg2
5
6
7
8
9
10

def rand_str(length):
    if length % 2 == 0:
        return hexlify(os.urandom(length//2))
    else:
        return hexlify(os.urandom(length//2 + 1))
Vladislav Rykov's avatar
Vladislav Rykov committed
11
12

def prep_id_range(devlist):
13
    r = list(range(1,255))
Vladislav Rykov's avatar
Vladislav Rykov committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

    for dev in devlist:
        del r[r.index(dev[1])]

    s = ''
    inr = False
    for i in range(len(r)-1):
        if r[i+1] - r[i] > 1:
            if inr:
                s += str(r[i])+'], '
                inr = False
            else:
                s += str(r[i])+', '
        else:
            if not inr:
                s += '['+str(r[i])+'-'
                inr = True
   
    if r[-1] - r[-2] > 1:
        if inr:
            s += str(r[-1])+']'
        else:
            s += str(r[-1])
    else:
        if not inr:
            s += str(r[-1])
        else:
            s += str(r[-1])+']'

    return s
Vladislav Rykov's avatar
Vladislav Rykov committed
44
45
46

def with_psql(f):
    def _with_psql(*args, **kwargs):
Vladislav Rykov's avatar
Vladislav Rykov committed
47
        conn = psycopg2.connect(
48
49
50
51
52
                database = app.config['DB_NAME'], 
                user = app.config['DB_USERNAME'],
                password = app.config['DB_PASSWORD'],
                host = app.config['DB_HOST'],
                port = app.config['DB_PORT']
Vladislav Rykov's avatar
Vladislav Rykov committed
53
            )
Vladislav Rykov's avatar
Vladislav Rykov committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
        cur = conn.cursor()

        try:
            res = f(cur, *args, **kwargs)
        except (Exception, psycopg2.DatabaseError) as error:
            conn.rollback()
            res = (False, error)
        else:
            conn.commit()
        finally:
            cur.close()
            conn.close()
    
        return res
    return _with_psql
69
70
71
72
73
74
75
76

def clean_data_folder():
    try:
        filelist = [f for f in os.listdir(app.config['DATA_DOWNLOAD_DIR_OS'])]
        for f in filelist:
            os.remove(app.config['DATA_DOWNLOAD_DIR_OS']+'/'+f)
    except OSError:
        pass
Vladislav Rykov's avatar
Vladislav Rykov committed
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106

def paging(cur_pg, ent_cnt, max_ent, max_pg):
    npg = int(ent_cnt/max_ent)+1
    ps = int(max_pg/2)

    # next and previous pages
    pp = False
    np = False
    pr = None # current pages range

    if npg < max_pg:
        # 1, ... , npg
        pr = [1, npg+1]
    else:
        if cur_pg - ps <= 1:
            # 1, 2, ..., MAX_PG >>
            pr = [1, max_pg+1]
            np = cur_pg + max_pg + ps
        else:
            if cur_pg + ps >= npg:
                # << npg-MAX_PG-1, ..., npg
                pp = cur_pg - max_pg
                pr = [npg-max_pg-1, npg+1]
            else:
                # << cur_pg-ps, ... , cur_pg + ps >>
                pp = cur_pg - max_pg
                np = cur_pg + max_pg
                pr = [cur_pg-ps, cur_pg+ps+1]

    return [pp, pr, np]