-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
46 lines (37 loc) · 1.22 KB
/
utils.py
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
import datetime
import os
import psutil
import sys
import re
import time
CLEANUP_FILENAME_RE = re.compile(r'[~#%&*{}:<>?+|"]')
INSTALL_FOLDER = getattr(sys, '_MEIPASS', os.path.dirname(__file__))
HOME_DIR = os.path.join(os.path.expanduser('~'), 'IKKE')
HOME_DIR_SEGMENT_COUNT = len(HOME_DIR.split(os.path.pathsep))
ITEMS_DIR = os.path.join(HOME_DIR, 'items')
FILE_DIR = os.path.join(ITEMS_DIR, 'file')
CONTACT_DIR = os.path.join(ITEMS_DIR, 'contact')
if not os.path.exists(HOME_DIR):
os.mkdir(HOME_DIR)
os.chdir(INSTALL_FOLDER)
def get_timestamp(dt=None):
try:
return float(time.mktime(dt.timetuple()))
except:
return float(time.mktime(datetime.datetime.utcnow().timetuple()))
KB = 1024
MB = 1024 * KB
GB = 1024 * MB
TB = 1024 * GB
def get_memory():
process = psutil.Process(os.getpid())
memory = process.memory_full_info()
# rss vms shared text lib data dirty uss pss swap
total = memory.rss
if total < KB: return '%d' % total
if total < MB: return '%.1dKB' % (total / KB)
if total < GB: return '%.1dMB' % (total / MB)
if total < TB: return '%.1dGB' % (total / GB)
return '%d' % total
def cleanup_filename(filename):
return re.sub(CLEANUP_FILENAME_RE, '_', filename)