39
39
class _RulesSearcher(object):
40
40
"""An object that provides rule-based preferences."""
42
def get_items(self, path, names=None):
43
"""Return the preferences for a path as a sequence of name,value tuples.
42
def get_items(self, path):
43
"""Return the preferences for a path as name,value tuples.
45
45
:param path: tree relative path
46
:param names: the list of preferences to lookup - None for all
47
:return: None if no rule matched, otherwise a sequence of name,value
48
tuples. If names is not None, the sequence is the same length as
49
names, tuple order matches the order in names, and undefined
50
preferences are given the value None.
46
:return: [] if no rule matched, otherwise a sequence of name,value
52
49
raise NotImplementedError(self.get_items)
51
def get_selected_items(self, path, names):
52
"""Return selected preferences for a path as name,value tuples.
54
:param path: tree relative path
55
:param names: the list of preferences to lookup
56
:return: [] if no rule matched, otherwise a sequence of name,value
57
tuples. The sequence is the same length as names,
58
tuple order matches the order in names, and
59
undefined preferences are given the value None.
61
raise NotImplementedError(self.get_selected_items)
55
64
class _IniBasedRulesSearcher(_RulesSearcher):
76
85
self._globster = None
78
def get_items(self, path, names=None):
87
def get_items(self, path):
79
88
"""See _RulesSearcher.get_items."""
80
89
if self._globster is None:
82
pat = self._globster.match(path)
86
all = self._cfg[FILE_PREFS_PREFIX + pat]
88
return tuple(all.items())
90
return tuple((k, all.get(k)) for k in names)
91
pat = self._globster.match(path)
95
all = self._cfg[FILE_PREFS_PREFIX + pat]
96
return tuple(all.items())
98
def get_selected_items(self, path, names):
99
"""See _RulesSearcher.get_selected_items."""
100
if self._globster is None:
102
pat = self._globster.match(path)
106
all = self._cfg[FILE_PREFS_PREFIX + pat]
107
return tuple((k, all.get(k)) for k in names)
93
110
class _StackedRulesSearcher(_RulesSearcher):
100
117
self.searchers = searchers
102
def get_items(self, path, names=None):
119
def get_items(self, path):
103
120
"""See _RulesSearcher.get_items."""
104
121
for searcher in self.searchers:
105
result = searcher.get_items(path, names)
106
if result is not None:
122
result = searcher.get_items(path)
127
def get_selected_items(self, path, names):
128
"""See _RulesSearcher.get_selected_items."""
129
for searcher in self.searchers:
130
result = searcher.get_selected_items(path, names)
111
136
def rules_filename():