-
Notifications
You must be signed in to change notification settings - Fork 15
/
utils.py
79 lines (60 loc) · 2.24 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
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
import os
# Escape ansi from stdout
import sys
from datetime import datetime
def is_test(name):
if len(sys.argv) > 1 and (name in sys.argv):
return True
else:
return False
def app_has_extra_directories(app):
# remove all directories under /apps
root_directories = []
for directory in app["subdirectories"]:
if "/apps" in directory:
pass
else:
root_directories.append(directory)
if len(root_directories) > 0:
return False
else:
return True
# Get resource when frozen with PyInstaller
def resource_path(relative_path):
if hasattr(sys, '_MEIPASS'):
return os.path.join(sys._MEIPASS, relative_path)
return os.path.join(os.path.dirname(os.path.abspath(__file__)), relative_path)
# Returns readable file size from file length
def file_size(length):
for unit in ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi']:
if abs(length) < 1024.0:
return "%3.1f%s%s" % (length, unit, "B")
length /= 1024.0
return "%.1f%s%s" % (length, 'Yi', "B")
# Returns mount point for a path in a given device
def get_mount_point(path):
path = os.path.abspath(path)
while not os.path.ismount(path):
path = os.path.dirname(path)
return path
# check if the app has a birthday
def app_birthday_string(app):
# Apparently some apps have negative release dates?? (sigh)
if app["release_date"] < 0:
return None
if datetime.fromtimestamp(int(app["release_date"])).strftime('%m%d') == datetime.now().strftime('%m%d'):
# verify that it was not added today
if datetime.fromtimestamp(int(app["release_date"])).strftime('%Y%m%d') != datetime.now().strftime('%Y%m%d'):
# determine app age
age = int((datetime.now().timestamp() - int(app["release_date"])) / 31536000)
# determine st/nd/rd/th
if age % 10 == 1 and age % 100 != 11:
age = str(age) + "st"
elif age % 10 == 2 and age % 100 != 12:
age = str(age) + "nd"
elif age % 10 == 3 and age % 100 != 13:
age = str(age) + "rd"
else:
age = str(age) + "th"
return f"Happy {age} Birthday!"
return None