/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 breezy/rules.py

  • Committer: Jelmer Vernooij
  • Date: 2020-04-05 19:11:34 UTC
  • mto: (7490.7.16 work)
  • mto: This revision was merged to the branch mainline in revision 7501.
  • Revision ID: jelmer@jelmer.uk-20200405191134-0aebh8ikiwygxma5
Populate the .gitignore file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2008 Canonical Ltd
 
1
# Copyright (C) 2008, 2009, 2010 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 bzrlib import (
23
 
    config,
 
22
from __future__ import absolute_import
 
23
 
 
24
import configobj
 
25
 
 
26
from . import (
 
27
    bedding,
24
28
    cmdline,
25
29
    errors,
26
30
    globbing,
27
31
    osutils,
28
32
    )
29
 
from bzrlib.util.configobj import configobj
30
33
 
31
34
 
32
35
# Name of the file holding rules in a tree
40
43
_per_user_searcher = None
41
44
 
42
45
 
 
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
 
43
54
class _RulesSearcher(object):
44
55
    """An object that provides rule-based preferences."""
45
56
 
64
75
        """
65
76
        raise NotImplementedError(self.get_selected_items)
66
77
 
 
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
 
67
87
 
68
88
class _IniBasedRulesSearcher(_RulesSearcher):
69
89
 
74
94
 
75
95
        :param inifile: the name of the file or a sequence of lines.
76
96
        """
77
 
        options = {'encoding': 'utf-8'}
78
 
        self._cfg = configobj.ConfigObj(inifile, options=options)
 
97
        self._cfg = configobj.ConfigObj(inifile, encoding='utf-8')
79
98
        sections = self._cfg.keys()
80
99
        patterns = []
81
100
        self.pattern_to_section = {}
87
106
                    self.pattern_to_section[fp] = s
88
107
        if len(patterns) < len(sections):
89
108
            unknowns = [s for s in sections
90
 
                if not s.startswith(FILE_PREFS_PREFIX)]
91
 
            raise errors.UnknownRules(unknowns)
 
109
                        if not s.startswith(FILE_PREFS_PREFIX)]
 
110
            raise UnknownRules(unknowns)
92
111
        elif patterns:
93
112
            self._globster = globbing._OrderedGlobster(patterns)
94
113
        else:
143
162
        return ()
144
163
 
145
164
 
146
 
def rules_filename():
147
 
    """Return the default rules filename."""
148
 
    return osutils.pathjoin(config.config_dir(), 'rules')
 
165
def rules_path():
 
166
    """Return the default rules file path."""
 
167
    return osutils.pathjoin(bedding.config_dir(), 'rules')
149
168
 
150
169
 
151
170
def reset_rules():
152
171
    global _per_user_searcher
153
 
    _per_user_searcher = _IniBasedRulesSearcher(rules_filename())
 
172
    _per_user_searcher = _IniBasedRulesSearcher(rules_path())
 
173
 
154
174
 
155
175
reset_rules()