/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/rules.py

  • Committer: Marius Kruger
  • Date: 2010-07-10 21:28:56 UTC
  • mto: (5384.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 5385.
  • Revision ID: marius.kruger@enerweb.co.za-20100710212856-uq4ji3go0u5se7hx
* Update documentation
* add NEWS

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2008, 2009, 2010 Canonical Ltd
 
1
# Copyright (C) 2008 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
 
"""Rule-based definition of preferences for selected files in selected branches
 
17
"""Rule-based definition of preferences for selected files in selected branches.
18
18
 
19
19
See ``bzr help rules`` for details.
20
20
"""
21
21
 
22
 
from __future__ import absolute_import
23
 
 
24
 
import configobj
25
 
 
26
 
from . import (
27
 
    bedding,
 
22
from bzrlib import (
 
23
    config,
28
24
    cmdline,
29
25
    errors,
30
26
    globbing,
31
27
    osutils,
32
28
    )
 
29
from bzrlib.util.configobj import configobj
33
30
 
34
31
 
35
32
# Name of the file holding rules in a tree
43
40
_per_user_searcher = None
44
41
 
45
42
 
46
 
class UnknownRules(errors.BzrError):
47
 
 
48
 
    _fmt = ('Unknown rules detected: %(unknowns_str)s.')
49
 
 
50
 
    def __init__(self, unknowns):
51
 
        errors.BzrError.__init__(self, unknowns_str=", ".join(unknowns))
52
 
 
53
 
 
54
43
class _RulesSearcher(object):
55
44
    """An object that provides rule-based preferences."""
56
45
 
75
64
        """
76
65
        raise NotImplementedError(self.get_selected_items)
77
66
 
78
 
    def get_single_value(self, path, preference_name):
79
 
        """Get a single preference for a single file.
80
 
 
81
 
        :returns: The string preference value, or None.
82
 
        """
83
 
        for key, value in self.get_selected_items(path, [preference_name]):
84
 
            return value
85
 
        return None
86
 
 
87
67
 
88
68
class _IniBasedRulesSearcher(_RulesSearcher):
89
69
 
94
74
 
95
75
        :param inifile: the name of the file or a sequence of lines.
96
76
        """
97
 
        self._cfg = configobj.ConfigObj(inifile, encoding='utf-8')
 
77
        options = {'encoding': 'utf-8'}
 
78
        self._cfg = configobj.ConfigObj(inifile, options=options)
98
79
        sections = self._cfg.keys()
99
80
        patterns = []
100
81
        self.pattern_to_section = {}
106
87
                    self.pattern_to_section[fp] = s
107
88
        if len(patterns) < len(sections):
108
89
            unknowns = [s for s in sections
109
 
                        if not s.startswith(FILE_PREFS_PREFIX)]
110
 
            raise UnknownRules(unknowns)
 
90
                if not s.startswith(FILE_PREFS_PREFIX)]
 
91
            raise errors.UnknownRules(unknowns)
111
92
        elif patterns:
112
93
            self._globster = globbing._OrderedGlobster(patterns)
113
94
        else:
162
143
        return ()
163
144
 
164
145
 
165
 
def rules_path():
166
 
    """Return the default rules file path."""
167
 
    return osutils.pathjoin(bedding.config_dir(), 'rules')
 
146
def rules_filename():
 
147
    """Return the default rules filename."""
 
148
    return osutils.pathjoin(config.config_dir(), 'rules')
168
149
 
169
150
 
170
151
def reset_rules():
171
152
    global _per_user_searcher
172
 
    _per_user_searcher = _IniBasedRulesSearcher(rules_path())
173
 
 
 
153
    _per_user_searcher = _IniBasedRulesSearcher(rules_filename())
174
154
 
175
155
reset_rules()