forked from Sidnioulz/PolicyAnalysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserConfigLoader.py
135 lines (108 loc) · 4.34 KB
/
UserConfigLoader.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
"""UserConfigLoader loads settings related to the user being analysed."""
from xdg import IniFile
from constants import USERCFG_VERSION
class UserConfigLoader(object):
"""UserConfigLoader loads settings related to the user being analysed."""
__loader = None
@staticmethod
def get(path: str=None):
"""Return the UserConfigLoader for the entire application."""
if not UserConfigLoader.__loader:
if path:
UserConfigLoader.__loader = UserConfigLoader(path)
else:
raise ValueError("UserConfigLoader is not initialised yet and "
"needs a config file path.")
return UserConfigLoader.__loader
@staticmethod
def reset():
UserConfigLoader.__loader = None
def __init__(self, path: str):
"""Construct a UserConfigLoader."""
super(UserConfigLoader, self).__init__()
self.ini = IniFile.IniFile()
try:
self.ini.parse(path)
except(IniFile.ParsingError) as e:
raise ValueError("Current user's config file could not be parsed.")
else:
vs = self.ini.get(key='Version',
group='User Config',
type='numeric')
assert(vs == USERCFG_VERSION), ("Error: User config file version "
"mismatch: expected %f, was %f" % (
USERCFG_VERSION,
vs or -1.0))
def getHomeDir(self):
"""Get the user's home directory."""
return self.getSetting("HomeDir")
def getSetting(self, key: str, defaultValue=None, type: str="string"):
"""Get a stored setting relative to the current participant."""
if not self.ini:
return defaultValue
isList = False
if type.endswith(" list"):
isList = True
type = type[:-5]
return self.ini.get(key,
group='User Config',
type=type,
list=isList) or defaultValue
def getExcludedHomeDirs(self):
def _get(self):
"""Get the list of directories excluded from analysis."""
if not self.ini:
return []
return self.ini.get('HomeExclDirs',
group='User Config',
type='string', list=True) or []
l = _get(self)
l.append("/srv/")
l.append("/run/")
l.append("/proc/")
l.append("/dev/")
l.append("/usr/")
l.append("/tmp/")
return l
def getSecurityExclusionLists(self):
"""Get the security exclusion lists setting."""
if not self.ini:
return dict()
def _parseVals(key):
vals = self.ini.get(key,
group='User Config',
type='string', list=True) or []
result = []
for value in vals:
excls = value.strip('|').split('||')
if not excls:
raise ValueError("Syntax error in user configuration's "
"SecurityExclusionLists on bit '%s'" %
value)
else:
result.append(excls)
return result
exclLists = dict()
for key in ['ExplicitExclusion',
'WorkPersonalSeparation',
'ProjectSeparation']:
exclLists[key] = _parseVals(key)
return exclLists
def getDefinedSecurityExclusionLists(self):
"""Get the security exclusion lists setting."""
if not self.ini:
return []
exclLists = self.getSecurityExclusionLists()
return list(key for (key, dic) in exclLists.items() if len(dic))
def getProjects(self):
"""Get the projects of a participant."""
if not self.ini:
return list()
projs = self.ini.get('Projects',
group='User Config',
type='string', list=True) or ''
result = []
for project in projs:
projectLocations = project.split('|')
result.append(projectLocations)
return result