-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
106 lines (82 loc) · 2.72 KB
/
utils.py
File metadata and controls
106 lines (82 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
from datetime import datetime
from functools import wraps, update_wrapper
import logging
import math
from subprocess import Popen, PIPE
import uuid
from flask import make_response
import pymysql
main_cursor = None
HOST = "dodb"
conn = None
LOG = logging.getLogger(__name__)
IntegrityError = pymysql.err.IntegrityError
def runproc(cmd):
proc = Popen([cmd], shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE,
close_fds=True)
stdout_text, stderr_text = proc.communicate()
return stdout_text, stderr_text
def _parse_creds():
with open("/home/ed/projects/leafe/.dbcreds") as ff:
lines = ff.read().splitlines()
ret = {}
for ln in lines:
key, val = ln.split("=")
ret[key] = val
return ret
def connect():
cls = pymysql.cursors.DictCursor
creds = _parse_creds()
ret = pymysql.connect(host=HOST, user=creds["DB_USERNAME"],
passwd=creds["DB_PWD"], db=creds["DB_NAME"], charset="utf8",
cursorclass=cls)
return ret
def gen_uuid():
return str(uuid.uuid4())
def get_cursor():
global conn, main_cursor
if not (conn and conn.open):
LOG.debug("No DB connection")
main_cursor = None
conn = connect()
if not main_cursor:
LOG.debug("No cursor")
main_cursor = conn.cursor(pymysql.cursors.DictCursor)
return main_cursor
def commit():
conn.commit()
def debugout(*args):
with open("/tmp/debugout", "a") as ff:
ff.write("YO!")
argtxt = [str(arg) for arg in args]
msg = " ".join(argtxt) + "\n"
with open("/tmp/debugout", "a") as ff:
ff.write(msg)
def nocache(view):
@wraps(view)
def no_cache(*args, **kwargs):
response = make_response(view(*args, **kwargs))
response.headers["Last-Modified"] = datetime.now()
response.headers["Cache-Control"] = "no-store, no-cache, " \
"must-revalidate, post-check=0, pre-check=0, max-age=0"
response.headers["Pragma"] = "no-cache"
response.headers["Expires"] = "-1"
return response
return update_wrapper(no_cache, view)
def human_fmt(num):
"""Human friendly file size"""
# Make sure that we get a valid input. If an invalid value is passed, we
# want the exception to be raised.
num = int(num)
units = list(zip(["bytes", "K", "MB", "GB", "TB", "PB"],
[0, 0, 1, 2, 2, 2]))
if num > 1:
exponent = min(int(math.log(num, 1024)), len(units) - 1)
quotient = float(num) / 1024**exponent
unit, num_decimals = units[exponent]
format_string = "{:.%sf} {}" % (num_decimals)
return format_string.format(quotient, unit)
if num == 0:
return "0 bytes"
if num == 1:
return "1 byte"