14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""Properties to associate with files based on file glob patterns.
17
"""Attributes to associate with files based on file glob patterns.
19
Patterns and the properties for each are defined in ini file format in
20
BZR_HOME/fileproperties. For example::
19
Patterns and the attributes for each are defined in ini file format in
20
BZR_HOME/attributes. For example::
44
44
from bzrlib.util.configobj import configobj
47
class _PropertiesProvider(object):
48
"""An object that provides properties for a file."""
47
class _AttributesProvider(object):
48
"""An object that provides attributes for a file."""
50
def get_attributes(self, path, names=None):
51
"""Return the dictionary of attributes for a path.
53
:param path: tree relative path
54
:param names: the list of attributes to lookup - None for all
55
:return: a dictionary where:
56
the keys are the requested attribute names and
57
the values are the attribute values or None if undefined
59
raise NotImplementedError(self.get_attributes)
62
class _FileBasedAttributesProvider(_AttributesProvider):
50
64
def __init__(self, filename):
51
"""Create a provider of properties.
65
"""Create a provider of attributes.
53
67
:param filename: the ini file holding the patterns and the
54
properties that apply to each.
68
attributes that apply to each.
56
70
self._cfg = configobj.ConfigObj(filename)
57
71
patterns = self._cfg.keys()
58
72
self._globster = globbing._OrderedGlobster(patterns)
60
def get_properties(self, path, names=None):
61
"""Return the dictionary of properties for a path.
63
:param path: tree relative path
64
:param names: the list of properties to lookup - None for all
65
:return: a dictionary where:
66
the keys are the requested property names and
67
the values are the property values or None if undefined
74
def get_attributes(self, path, names=None):
75
"""See _AttributesProvider.get_attributes."""
69
76
pat = self._globster.match(path)
77
84
return dict((k, all.get(k)) for k in names)
80
def properties_filename():
81
"""Return per-user properties file filename."""
82
return osutils.pathjoin(config.config_dir(), 'fileproperties')
85
# The object providing per-user properties
86
_user_properties_provider = _PropertiesProvider(properties_filename())
87
def attributes_filename():
88
"""Return per-user attributes file filename."""
89
return osutils.pathjoin(config.config_dir(), 'attributes')
92
# The object providing per-user attributes
93
_user_attributes_provider = _FileBasedAttributesProvider(attributes_filename())