-
Notifications
You must be signed in to change notification settings - Fork 6
/
util.py
executable file
·73 lines (61 loc) · 1.8 KB
/
util.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
# Note that these functions are taken from the xbmc / Kodi 'common' library and
# are included here to avoid import issues
try:
import cPickle as pickle
except:
import pickle
import os
import xbmc
def get_profile(addon):
'''
Returns the full path to the addon profile directory
(useful for storing files needed by the addon such as cookies).
'''
return xbmc.translatePath(addon.getAddonInfo('profile'))
def save_data(addon, filename, data):
profile_path = get_profile(addon)
try:
os.makedirs(profile_path)
except:
pass
save_path = os.path.join(profile_path, filename)
try:
pickle.dump(data, open(save_path, 'wb'))
return True
except pickle.PickleError:
return False
def load_data(addon, filename):
profile_path = get_profile(addon)
load_path = os.path.join(profile_path, filename)
print(profile_path)
if not os.path.isfile(load_path):
print('%s does not exist' % load_path)
return None
try:
data = pickle.load(open(load_path))
return data
except:
return None
def save_text(addon, filename, text):
profile_path = get_profile(addon)
try:
os.makedirs(profile_path)
except:
pass
save_path = os.path.join(profile_path, filename)
with open(save_path, 'wb') as f:
f.write(text)
return True
def load_text(addon, filename):
profile_path = get_profile(addon)
load_path = os.path.join(profile_path, filename)
print(profile_path)
if not os.path.isfile(load_path):
print('%s does not exist' % load_path)
return None
try:
with open(load_path) as f:
text = f.read()
return text
except:
return None