/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: John Ferlito
  • Date: 2009-09-02 04:31:45 UTC
  • mto: (4665.7.1 serve-init)
  • mto: This revision was merged to the branch mainline in revision 4913.
  • Revision ID: johnf@inodes.org-20090902043145-gxdsfw03ilcwbyn5
Add a debian init script for bzr --serve

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
 
import configobj
23
 
 
24
 
from . import (
25
 
    bedding,
26
 
    cmdline,
 
22
from bzrlib import (
 
23
    config,
 
24
    commands,
27
25
    errors,
28
26
    globbing,
29
27
    osutils,
30
28
    )
 
29
from bzrlib.util.configobj import configobj
31
30
 
32
31
 
33
32
# Name of the file holding rules in a tree
41
40
_per_user_searcher = None
42
41
 
43
42
 
44
 
class UnknownRules(errors.BzrError):
45
 
 
46
 
    _fmt = ('Unknown rules detected: %(unknowns_str)s.')
47
 
 
48
 
    def __init__(self, unknowns):
49
 
        errors.BzrError.__init__(self, unknowns_str=", ".join(unknowns))
50
 
 
51
 
 
52
43
class _RulesSearcher(object):
53
44
    """An object that provides rule-based preferences."""
54
45
 
73
64
        """
74
65
        raise NotImplementedError(self.get_selected_items)
75
66
 
76
 
    def get_single_value(self, path, preference_name):
77
 
        """Get a single preference for a single file.
78
 
 
79
 
        :returns: The string preference value, or None.
80
 
        """
81
 
        for key, value in self.get_selected_items(path, [preference_name]):
82
 
            return value
83
 
        return None
84
 
 
85
67
 
86
68
class _IniBasedRulesSearcher(_RulesSearcher):
87
69
 
92
74
 
93
75
        :param inifile: the name of the file or a sequence of lines.
94
76
        """
95
 
        self._cfg = configobj.ConfigObj(inifile, encoding='utf-8')
 
77
        options = {'encoding': 'utf-8'}
 
78
        self._cfg = configobj.ConfigObj(inifile, options=options)
96
79
        sections = self._cfg.keys()
97
80
        patterns = []
98
81
        self.pattern_to_section = {}
99
82
        for s in sections:
100
83
            if s.startswith(FILE_PREFS_PREFIX):
101
 
                file_patterns = cmdline.split(s[FILE_PREFS_PREFIX_LEN:])
 
84
                file_patterns = commands.shlex_split_unicode(
 
85
                    s[FILE_PREFS_PREFIX_LEN:])
102
86
                patterns.extend(file_patterns)
103
87
                for fp in file_patterns:
104
88
                    self.pattern_to_section[fp] = s
105
89
        if len(patterns) < len(sections):
106
90
            unknowns = [s for s in sections
107
 
                        if not s.startswith(FILE_PREFS_PREFIX)]
108
 
            raise UnknownRules(unknowns)
 
91
                if not s.startswith(FILE_PREFS_PREFIX)]
 
92
            raise errors.UnknownRules(unknowns)
109
93
        elif patterns:
110
94
            self._globster = globbing._OrderedGlobster(patterns)
111
95
        else:
160
144
        return ()
161
145
 
162
146
 
163
 
def rules_path():
164
 
    """Return the default rules file path."""
165
 
    return osutils.pathjoin(bedding.config_dir(), 'rules')
 
147
def rules_filename():
 
148
    """Return the default rules filename."""
 
149
    return osutils.pathjoin(config.config_dir(), 'rules')
166
150
 
167
151
 
168
152
def reset_rules():
169
153
    global _per_user_searcher
170
 
    _per_user_searcher = _IniBasedRulesSearcher(rules_path())
171
 
 
 
154
    _per_user_searcher = _IniBasedRulesSearcher(rules_filename())
172
155
 
173
156
reset_rules()