27
27
from bzrlib.util.configobj import configobj
30
# Name of the file holding rules in a tree
31
RULES_TREE_FILENAME = ".bzrrules"
30
34
class _RulesSearcher(object):
31
35
"""An object that provides rule-based preferences."""
48
52
def __init__(self, inifile):
49
53
"""Construct a _RulesSearcher based on an ini file.
55
The content will be decoded as utf-8.
51
57
:param inifile: the name of the file or a sequence of lines.
53
self._cfg = configobj.ConfigObj(inifile)
59
options = {'encoding': 'utf-8'}
60
self._cfg = configobj.ConfigObj(inifile, options=options)
54
61
patterns = self._cfg.keys()
56
63
self._globster = globbing._OrderedGlobster(patterns)
98
105
# The object providing default rules
99
106
_per_user_searcher = _IniBasedRulesSearcher(rules_filename())
102
# The cache of branch-specific rule searchers
103
_branch_searchers = {}
106
def iter_search_rules(branch, path_names, pref_names=None,
107
_default_searcher=_per_user_searcher):
108
"""Find the preferences for filenames in a branch.
110
:param branch: the branch, or None to only search the per user preferences
111
:param path_names: an iterable of paths to find attributes for.
112
Paths are given relative to the root of the tree.
113
:param pref_names: the list of preferences to lookup - None for all
114
:param _default_searcher: private parameter to assist testing - don't use
115
:return: an iterator of tuple sequences, one per path-name.
116
See _RulesSearcher.get_items for details on the tuple sequence.
120
searcher = _default_searcher
122
branch_searcher = _branch_searchers.get(branch)
123
if branch_searcher is None:
124
# Create and cache the branch searcher
127
ini_file = branch.control_files.get('branch.rules')
130
branch_searcher = _IniBasedRulesSearcher(ini_file)
131
_branch_searchers[branch] = branch_searcher
132
# If branch.rules is missing or empty, skip searching it
133
if branch_searcher._globster is None:
134
searcher = _default_searcher
136
searcher = _StackedRulesSearcher(
137
[branch_searcher, _default_searcher])
140
for path in path_names:
141
yield searcher.get_items(path, pref_names)