/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: Ian Clatworthy
  • Date: 2008-06-25 07:17:14 UTC
  • mto: (3515.1.1 ianc-integration)
  • mto: This revision was merged to the branch mainline in revision 3516.
  • Revision ID: ian.clatworthy@canonical.com-20080625071714-am1ya6mgo54ikrgv
jam feedback - make patterns a separate help topic

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2008 Canonical Ltd
 
2
#
 
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.
 
7
#
 
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.
 
12
#
 
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
 
16
 
 
17
"""Rule-based definition of preferences for selected files in selected branches.
 
18
 
 
19
See ``bzr help rules`` for details.
 
20
"""
 
21
 
 
22
from bzrlib import (
 
23
    config,
 
24
    globbing,
 
25
    osutils,
 
26
    )
 
27
from bzrlib.util.configobj import configobj
 
28
 
 
29
 
 
30
# Name of the file holding rules in a tree
 
31
RULES_TREE_FILENAME = ".bzrrules"
 
32
 
 
33
 
 
34
class _RulesSearcher(object):
 
35
    """An object that provides rule-based preferences."""
 
36
 
 
37
    def get_items(self, path, names=None):
 
38
        """Return the preferences for a path as a sequence of name,value tuples.
 
39
 
 
40
        :param path: tree relative path
 
41
        :param names: the list of preferences to lookup - None for all
 
42
        :return: None if no rule matched, otherwise a sequence of name,value
 
43
          tuples. If names is not None, the sequence is the same length as
 
44
          names, tuple order matches the order in names, and undefined
 
45
          preferences are given the value None.
 
46
        """
 
47
        raise NotImplementedError(self.get_items)
 
48
 
 
49
 
 
50
class _IniBasedRulesSearcher(_RulesSearcher):
 
51
 
 
52
    def __init__(self, inifile):
 
53
        """Construct a _RulesSearcher based on an ini file.
 
54
 
 
55
        The content will be decoded as utf-8.
 
56
 
 
57
        :param inifile: the name of the file or a sequence of lines.
 
58
        """
 
59
        options = {'encoding': 'utf-8'}
 
60
        self._cfg = configobj.ConfigObj(inifile, options=options)
 
61
        patterns = self._cfg.keys()
 
62
        if patterns:
 
63
            self._globster = globbing._OrderedGlobster(patterns)
 
64
        else:
 
65
            self._globster = None
 
66
 
 
67
    def get_items(self, path, names=None):
 
68
        """See _RulesSearcher.get_items."""
 
69
        if self._globster is None:
 
70
            return None
 
71
        pat = self._globster.match(path)
 
72
        if pat is None:
 
73
            return None
 
74
        else:
 
75
            all = self._cfg[pat]
 
76
            if names is None:
 
77
                return tuple(all.items())
 
78
            else:
 
79
                return tuple((k, all.get(k)) for k in names)
 
80
 
 
81
 
 
82
class _StackedRulesSearcher(_RulesSearcher):
 
83
 
 
84
    def __init__(self, searchers):
 
85
        """Construct a _RulesSearcher based on a stack of other ones.
 
86
 
 
87
        :param searchers: a sequence of searchers.
 
88
        """
 
89
        self.searchers = searchers
 
90
 
 
91
    def get_items(self, path, names=None):
 
92
        """See _RulesSearcher.get_items."""
 
93
        for searcher in self.searchers:
 
94
            result = searcher.get_items(path, names)
 
95
            if result is not None:
 
96
                return result
 
97
        return None
 
98
 
 
99
 
 
100
def rules_filename():
 
101
    """Return the default rules filename."""
 
102
    return osutils.pathjoin(config.config_dir(), 'rules')
 
103
 
 
104
 
 
105
# The object providing default rules
 
106
_per_user_searcher = _IniBasedRulesSearcher(rules_filename())