~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/rules.py

  • Committer: Ian Clatworthy
  • Date: 2008-05-28 07:01:06 UTC
  • mto: (3515.1.1 ianc-integration)
  • mto: This revision was merged to the branch mainline in revision 3516.
  • Revision ID: ian.clatworthy@canonical.com-20080528070106-6w0oc8zat4bs29g1
make iter_search_rules a tree method

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
from bzrlib.util.configobj import configobj
28
28
 
29
29
 
 
30
# Name of the file holding rules in a tree
 
31
RULES_TREE_FILENAME = ".bzrrules"
 
32
 
 
33
 
30
34
class _RulesSearcher(object):
31
35
    """An object that provides rule-based preferences."""
32
36
 
48
52
    def __init__(self, inifile):
49
53
        """Construct a _RulesSearcher based on an ini file.
50
54
 
 
55
        The content will be decoded as utf-8.
 
56
 
51
57
        :param inifile: the name of the file or a sequence of lines.
52
58
        """
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()
55
62
        if patterns:
56
63
            self._globster = globbing._OrderedGlobster(patterns)
97
104
 
98
105
# The object providing default rules
99
106
_per_user_searcher = _IniBasedRulesSearcher(rules_filename())
100
 
 
101
 
 
102
 
# The cache of branch-specific rule searchers
103
 
_branch_searchers = {}
104
 
 
105
 
 
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.
109
 
 
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.
117
 
    """
118
 
    # Get the searcher
119
 
    if branch is None:
120
 
        searcher = _default_searcher
121
 
    else:
122
 
        branch_searcher = _branch_searchers.get(branch)
123
 
        if branch_searcher is None:
124
 
            # Create and cache the branch searcher
125
 
            branch.lock_read()
126
 
            try:
127
 
                ini_file = branch.control_files.get('branch.rules')
128
 
            finally:
129
 
                branch.unlock()
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
135
 
        else:
136
 
            searcher = _StackedRulesSearcher(
137
 
                [branch_searcher, _default_searcher])
138
 
 
139
 
    # Do the searching
140
 
    for path in path_names:
141
 
        yield searcher.get_items(path, pref_names)