1
# Copyright (C) 2008 Canonical Ltd
 
 
3
# This program is free software; you can redistribute it and/or modify
 
 
4
# it under the terms of the GNU General Public License as published by
 
 
5
# the Free Software Foundation; either version 2 of the License, or
 
 
6
# (at your option) any later version.
 
 
8
# This program is distributed in the hope that it will be useful,
 
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
 
11
# GNU General Public License for more details.
 
 
13
# You should have received a copy of the GNU General Public License
 
 
14
# along with this program; if not, write to the Free Software
 
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 
17
"""Rule-based definition of preferences for selected files in selected branches.
 
 
19
See ``bzr help rules`` for details.
 
 
28
from bzrlib.util.configobj import configobj
 
 
31
# Name of the file holding rules in a tree
 
 
32
RULES_TREE_FILENAME = ".bzrrules"
 
 
34
# Namespace prefix for per file preferences
 
 
35
FILE_PREFS_PREFIX = 'name '
 
 
36
FILE_PREFS_PREFIX_LEN = len(FILE_PREFS_PREFIX)
 
 
39
class _RulesSearcher(object):
 
 
40
    """An object that provides rule-based preferences."""
 
 
42
    def get_items(self, path):
 
 
43
        """Return the preferences for a path as name,value tuples.
 
 
45
        :param path: tree relative path
 
 
46
        :return: [] if no rule matched, otherwise a sequence of name,value
 
 
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)
 
 
64
class _IniBasedRulesSearcher(_RulesSearcher):
 
 
66
    def __init__(self, inifile):
 
 
67
        """Construct a _RulesSearcher based on an ini file.
 
 
69
        The content will be decoded as utf-8.
 
 
71
        :param inifile: the name of the file or a sequence of lines.
 
 
73
        options = {'encoding': 'utf-8'}
 
 
74
        self._cfg = configobj.ConfigObj(inifile, options=options)
 
 
75
        sections = self._cfg.keys()
 
 
76
        patterns = [s[FILE_PREFS_PREFIX_LEN:] for s in sections
 
 
77
            if s.startswith(FILE_PREFS_PREFIX)]
 
 
78
        if len(patterns) < len(sections):
 
 
79
            unknowns = [s for s in sections
 
 
80
                if not s.startswith(FILE_PREFS_PREFIX)]
 
 
81
            raise errors.UnknownRules(unknowns)
 
 
83
            self._globster = globbing._OrderedGlobster(patterns)
 
 
87
    def get_items(self, path):
 
 
88
        """See _RulesSearcher.get_items."""
 
 
89
        if self._globster is None:
 
 
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)
 
 
110
class _StackedRulesSearcher(_RulesSearcher):
 
 
112
    def __init__(self, searchers):
 
 
113
        """Construct a _RulesSearcher based on a stack of other ones.
 
 
115
        :param searchers: a sequence of searchers.
 
 
117
        self.searchers = searchers
 
 
119
    def get_items(self, path):
 
 
120
        """See _RulesSearcher.get_items."""
 
 
121
        for searcher in self.searchers:
 
 
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)
 
 
136
def rules_filename():
 
 
137
    """Return the default rules filename."""
 
 
138
    return osutils.pathjoin(config.config_dir(), 'rules')
 
 
141
# The object providing default rules
 
 
142
_per_user_searcher = _IniBasedRulesSearcher(rules_filename())