~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/rules.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2008-07-17 07:33:12 UTC
  • mfrom: (3530.3.3 btree-graphindex)
  • Revision ID: pqm@pqm.ubuntu.com-20080717073312-reglpowwyo671081
(robertc) Intern GraphIndex strings and handle frozenset inputs to
        make_mpdiffs in the case of errors. (Robert Collins, Andrew Bennetts)

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
"""Rule-based definition of preferences for selected files in selected branches.
18
18
 
21
21
 
22
22
from bzrlib import (
23
23
    config,
24
 
    cmdline,
25
24
    errors,
26
25
    globbing,
27
26
    osutils,
36
35
FILE_PREFS_PREFIX = 'name '
37
36
FILE_PREFS_PREFIX_LEN = len(FILE_PREFS_PREFIX)
38
37
 
39
 
# The object providing default rules
40
 
_per_user_searcher = None
41
 
 
42
38
 
43
39
class _RulesSearcher(object):
44
40
    """An object that provides rule-based preferences."""
47
43
        """Return the preferences for a path as name,value tuples.
48
44
 
49
45
        :param path: tree relative path
50
 
        :return: () if no rule matched, otherwise a sequence of name,value
 
46
        :return: [] if no rule matched, otherwise a sequence of name,value
51
47
          tuples.
52
48
        """
53
49
        raise NotImplementedError(self.get_items)
57
53
 
58
54
        :param path: tree relative path
59
55
        :param names: the list of preferences to lookup
60
 
        :return: () if no rule matched, otherwise a sequence of name,value
 
56
        :return: [] if no rule matched, otherwise a sequence of name,value
61
57
          tuples. The sequence is the same length as names,
62
58
          tuple order matches the order in names, and
63
59
          undefined preferences are given the value None.
77
73
        options = {'encoding': 'utf-8'}
78
74
        self._cfg = configobj.ConfigObj(inifile, options=options)
79
75
        sections = self._cfg.keys()
80
 
        patterns = []
81
 
        self.pattern_to_section = {}
82
 
        for s in sections:
83
 
            if s.startswith(FILE_PREFS_PREFIX):
84
 
                file_patterns = cmdline.split(s[FILE_PREFS_PREFIX_LEN:])
85
 
                patterns.extend(file_patterns)
86
 
                for fp in file_patterns:
87
 
                    self.pattern_to_section[fp] = s
 
76
        patterns = [s[FILE_PREFS_PREFIX_LEN:] for s in sections
 
77
            if s.startswith(FILE_PREFS_PREFIX)]
88
78
        if len(patterns) < len(sections):
89
79
            unknowns = [s for s in sections
90
80
                if not s.startswith(FILE_PREFS_PREFIX)]
97
87
    def get_items(self, path):
98
88
        """See _RulesSearcher.get_items."""
99
89
        if self._globster is None:
100
 
            return ()
 
90
            return []
101
91
        pat = self._globster.match(path)
102
92
        if pat is None:
103
 
            return ()
 
93
            return []
104
94
        else:
105
 
            all = self._cfg[self.pattern_to_section[pat]]
 
95
            all = self._cfg[FILE_PREFS_PREFIX + pat]
106
96
            return tuple(all.items())
107
97
 
108
98
    def get_selected_items(self, path, names):
109
99
        """See _RulesSearcher.get_selected_items."""
110
100
        if self._globster is None:
111
 
            return ()
 
101
            return []
112
102
        pat = self._globster.match(path)
113
103
        if pat is None:
114
 
            return ()
 
104
            return []
115
105
        else:
116
 
            all = self._cfg[self.pattern_to_section[pat]]
 
106
            all = self._cfg[FILE_PREFS_PREFIX + pat]
117
107
            return tuple((k, all.get(k)) for k in names)
118
108
 
119
109
 
132
122
            result = searcher.get_items(path)
133
123
            if result:
134
124
                return result
135
 
        return ()
 
125
        return []
136
126
 
137
127
    def get_selected_items(self, path, names):
138
128
        """See _RulesSearcher.get_selected_items."""
140
130
            result = searcher.get_selected_items(path, names)
141
131
            if result:
142
132
                return result
143
 
        return ()
 
133
        return []
144
134
 
145
135
 
146
136
def rules_filename():
148
138
    return osutils.pathjoin(config.config_dir(), 'rules')
149
139
 
150
140
 
151
 
def reset_rules():
152
 
    global _per_user_searcher
153
 
    _per_user_searcher = _IniBasedRulesSearcher(rules_filename())
154
 
 
155
 
reset_rules()
 
141
# The object providing default rules
 
142
_per_user_searcher = _IniBasedRulesSearcher(rules_filename())