forked from Sidnioulz/PolicyAnalysis
/
LibraryManager.py
273 lines (227 loc) · 10.1 KB
/
LibraryManager.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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
"""Load libraries and verify if Files are part of libraries."""
from File import File, FileAccess, EventFileFlags
from Application import Application
from UserConfigLoader import UserConfigLoader
import sys
import math
class LibraryManager(object):
"""Load libraries and verify if Files are part of libraries."""
"""A service to store File instances."""
__lib_mgr = None
# Lists of libraries supported in each library mode.
DefaultList = ['documents', 'image', 'music', 'video', 'removable']
CompoundList = DefaultList
CustomList = ['documents', 'image', 'music', 'video', 'removable',
'downloads', 'desktop', 'programming', 'ebooks', 'scores']
# Supported library modes.
Default = 0
Compound = 1
Custom = 2
@staticmethod
def get():
"""Return the LibraryManager for the entire application."""
if LibraryManager.__lib_mgr is None:
LibraryManager.__lib_mgr = LibraryManager()
return LibraryManager.__lib_mgr
@staticmethod
def reset():
LibraryManager.__lib_mgr = None
def __init__(self):
"""Construct a LibraryManager."""
super(LibraryManager, self).__init__()
self.initDefaults()
def initDefaults(self):
"""Initialise folder lists for all 3 library configurations."""
# Caches for library locations.
self.defaults = dict()
self.compounds = dict()
self.customs = dict()
# Roots of all libraries, reverse-sorted by length.
self.defaultRoots = list()
self.compoundRoots = list()
self.customRoots = list()
# Map of paths to library names, to use with sorted root lists.
self.defaultMap = dict()
self.compoundMap = dict()
self.customMap = dict()
self.configCosts = dict()
# Cache for application policies.
self.appPolicyCache = dict()
self.appPolicyCacheCustom = dict()
# Cache for files.
self.fileCacheDefault = dict()
self.fileCacheCompound = dict()
self.fileCacheCustom = dict()
# Loading default libraries.
userConf = UserConfigLoader.get()
self.userHome = userConf.getHomeDir()
for lib in LibraryManager.DefaultList:
fD = dict()
fD[userConf.getSetting('Xdg%sDir' % lib.capitalize())] = 0
self.defaults[lib] = fD
self.configCosts[LibraryManager.Default] = 0
# Loading compound libraries.
confCost = 0
for lib in LibraryManager.CompoundList:
fD = dict()
fD[userConf.getSetting('Xdg%sDir' % lib.capitalize())] = 0
for d in userConf.getSetting('Extra%sDirs' % lib.capitalize(),
defaultValue=[],
type='string list'):
fD[d] = 1
confCost += 1
self.compounds[lib] = fD
self.configCosts[LibraryManager.Compound] = confCost
# Loading custom libraries.
confCost = 0
for lib in LibraryManager.CustomList:
fD = dict()
defaultKey = userConf.getSetting('Xdg%sDir' % lib.capitalize())
if defaultKey:
fD[defaultKey] = 0
for d in userConf.getSetting('Extra%sDirs' % lib.capitalize(),
defaultValue=[],
type='string list'):
fD[d] = 1
confCost += 1
if fD:
self.customs[lib] = fD
self.configCosts[LibraryManager.Custom] = confCost
self.defaultRoots = self.getAllLibraryRoots(LibraryManager.Default,
addXdgRoots=False,
mapToFill=self.defaultMap)
self.compoundRoots = self.getAllLibraryRoots(LibraryManager.Compound,
addXdgRoots=False,
mapToFill=self.compoundMap)
self.customRoots = self.getAllLibraryRoots(LibraryManager.Custom,
addXdgRoots=False,
mapToFill=self.customMap)
def getAppPolicy(self, actor: Application, libMod: int):
"""Return the library capabilities policy for one Application."""
if libMod == LibraryManager.Custom:
if actor.desktopid not in self.appPolicyCacheCustom:
policies = actor.getSetting('LibraryCapsCustom',
type='string list') or \
self.getAppPolicy(actor, LibraryManager.Default)
self.appPolicyCacheCustom[actor.desktopid] = policies
return self.appPolicyCacheCustom[actor.desktopid]
else:
if actor.desktopid not in self.appPolicyCache:
policies = actor.getSetting('LibraryCaps',
type='string list') or []
self.appPolicyCache[actor.desktopid] = policies
return self.appPolicyCache[actor.desktopid]
def getLibraryForFile(self, f: File, libMod: int):
"""Return the name of the library a File belongs to."""
if libMod == LibraryManager.Default:
fileCache = self.fileCacheDefault
roots = self.defaultRoots
libMap = self.defaultMap
elif libMod == LibraryManager.Compound:
fileCache = self.fileCacheCompound
roots = self.compoundRoots
libMap = self.compoundMap
elif libMod == LibraryManager.Custom:
fileCache = self.fileCacheCustom
roots = self.customRoots
libMap = self.customMap
else:
raise AttributeError("Invalid library mode '%d'." % libMod)
if f not in fileCache:
val = None
for path in roots:
if(f.path.startswith(path)):
val = libMap[path]
break
# Non-library file, distinguish user documents.
if not val:
if f.isUserDocument(userHome=self.userHome,
allowHiddenFiles=True):
val = "UnclassifiedUserDocument"
else:
val = "Unclassified"
fileCache[f] = val
return fileCache[f]
def getRemovableMediaDir(self, libMod: int):
"""Return the root directory for the user's removable media."""
if libMod == LibraryManager.Default:
libraries = self.defaults
elif libMod == LibraryManager.Compound:
libraries = self.compounds
elif libMod == LibraryManager.Custom:
libraries = self.customs
else:
raise AttributeError("Invalid library mode '%d'." % libMod)
return libraries['removable']
def getAllLibraryRoots(self, libMod: int,
addXdgRoots: bool=True,
mapToFill: dict=None):
"""Return all the root folders for libraries, and for XDG folders."""
if libMod == LibraryManager.Default:
libraries = self.defaults
elif libMod == LibraryManager.Compound:
libraries = self.compounds
elif libMod == LibraryManager.Custom:
libraries = self.customs
else:
raise AttributeError("Invalid library mode '%d'." % libMod)
userConf = UserConfigLoader.get()
rootSet = set()
for (libName, lib) in libraries.items():
for (path, cost) in lib.items():
rootSet.add(path)
if mapToFill is not None:
mapToFill[path] = libName
if addXdgRoots:
desk = userConf.getSetting('XdgDesktopDir') or \
'%s/Desktop' % self.userHome
down = userConf.getSetting('XdgDownloadsDir') or \
'%s/Downloads' % self.userHome
cfg = '%s/.config' % self.userHome
cache = '%s/.cache' % self.userHome
data = '%s/.local/share' % self.userHome
rootSet = rootSet.union([desk, down, self.userHome,
cfg, cache, data])
rootList = list(rootSet)
rootList.sort(key=len, reverse=True)
return rootList
def getLibraryRoots(self,
supportedLibraries: list,
libMod: int,
addXdgRoots: bool=True,
mapToFill: dict=None):
"""Return all the root folders for libraries, and for XDG folders."""
if libMod == LibraryManager.Default:
libraries = self.defaults
elif libMod == LibraryManager.Compound:
libraries = self.compounds
elif libMod == LibraryManager.Custom:
libraries = self.customs
else:
raise AttributeError("Invalid library mode '%d'." % libMod)
userConf = UserConfigLoader.get()
rootSet = set()
for libName in supportedLibraries:
if libName in libraries:
for (path, cost) in libraries[libName].items():
rootSet.add(path)
if mapToFill is not None:
mapToFill[path] = libName
if addXdgRoots:
if 'desktop' in supportedLibraries:
rootSet.add(userConf.getSetting('XdgDesktopDir') or \
'%s/Desktop' % self.userHome)
if 'downloads' in supportedLibraries:
rootSet.add(userConf.getSetting('XdgDownloadsDir') or \
'%s/Downloads' % self.userHome)
if 'config' in supportedLibraries:
rootSet.add('%s/.config' % self.userHome)
if 'cache' in supportedLibraries:
rootSet.add('%s/.cache' % self.userHome)
if 'data' in supportedLibraries:
rootSet.add('%s/.local/share' % self.userHome)
if 'home' in supportedLibraries:
rootSet.add(self.userHome)
rootList = list(rootSet)
rootList.sort(key=len, reverse=True)
return rootList