annotate babel/util.py @ 13:b6c0de43fa40

Extended and documented `LazyProxy`.
author cmlenz
date Thu, 31 May 2007 10:27:47 +0000
parents f71ca60f2a4a
children da1c9610e751
rev   line source
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
1 # -*- coding: utf-8 -*-
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
2 #
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
3 # Copyright (C) 2007 Edgewall Software
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
4 # All rights reserved.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
5 #
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
6 # This software is licensed as described in the file COPYING, which
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
7 # you should have received as part of this distribution. The terms
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
8 # are also available at http://babel.edgewall.org/wiki/License.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
9 #
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
10 # This software consists of voluntary contributions made by many
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
11 # individuals. For the exact contribution history, see the revision
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
12 # history and logs, available at http://babel.edgewall.org/log/.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
13
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
14 """Various utility classes and functions."""
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
15
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
16 import os
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
17 import re
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
18
13
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
19 __all__ = ['default_locale', 'extended_glob', 'LazyProxy']
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
20 __docformat__ = 'restructuredtext en'
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
21
13
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
22 def default_locale(kind=None):
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
23 """Returns the default locale for a given category, based on environment
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
24 variables.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
25
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
26 :param kind: one of the ``LC_XXX`` environment variable names
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
27 :return: the value of the variable, or any of the fallbacks (``LC_ALL`` and
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
28 ``LANG``)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
29 :rtype: `str`
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
30 """
13
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
31 for name in filter(None, (kind, 'LC_ALL', 'LANG')):
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
32 locale = os.getenv(name)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
33 if locale is not None:
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
34 return locale
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
35
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
36 def extended_glob(pattern, dirname=''):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
37 """Extended pathname pattern expansion.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
38
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
39 This function is similar to what is provided by the ``glob`` module in the
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
40 Python standard library, but also supports a convenience pattern ("**") to
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
41 match files at any directory level.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
42
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
43 :param pattern: the glob pattern
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
44 :param dirname: the path to the directory in which to search for files
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
45 matching the given pattern
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
46 :return: an iterator over the absolute filenames of any matching files
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
47 :rtype: ``iterator``
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
48 """
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
49 symbols = {
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
50 '?': '[^/]',
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
51 '?/': '[^/]/',
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
52 '*': '[^/]+',
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
53 '*/': '[^/]+/',
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
54 '**': '(?:.+/)*?',
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
55 '**/': '(?:.+/)*?',
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
56 }
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
57 buf = []
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
58 for idx, part in enumerate(re.split('([?*]+/?)', pattern)):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
59 if idx % 2:
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
60 buf.append(symbols[part])
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
61 elif part:
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
62 buf.append(re.escape(part))
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
63 regex = re.compile(''.join(buf) + '$')
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
64
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
65 absname = os.path.abspath(dirname)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
66 for root, dirnames, filenames in os.walk(absname):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
67 for subdir in dirnames:
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
68 if subdir.startswith('.') or subdir.startswith('_'):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
69 dirnames.remove(subdir)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
70 for filename in filenames:
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
71 filepath = relpath(
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
72 os.path.join(root, filename).replace(os.sep, '/'),
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
73 dirname
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
74 )
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
75 if regex.match(filepath):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
76 yield filepath
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
77
13
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
78 class LazyProxy(object):
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
79 """Class for proxy objects that delegate to a specified function to evaluate
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
80 the actual object.
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
81
13
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
82 >>> def greeting(name='world'):
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
83 ... return 'Hello, %s!' % name
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
84 >>> lazy_greeting = LazyProxy(greeting, name='Joe')
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
85 >>> print lazy_greeting
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
86 Hello, Joe!
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
87 >>> u' ' + lazy_greeting
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
88 u' Hello, Joe!'
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
89 >>> u'(%s)' % lazy_greeting
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
90 u'(Hello, Joe!)'
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
91
13
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
92 This can be used, for example, to implement lazy translation functions that
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
93 delay the actual translation until the string is actually used. The
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
94 rationale for such behavior is that the locale of the user may not always
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
95 be available. In web applications, you only know the locale when processing
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
96 a request.
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
97
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
98 The proxy implementation attempts to be as complete as possible, so that
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
99 the lazy objects should mostly work as expected, for example for sorting:
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
100
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
101 >>> greetings = [
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
102 ... LazyProxy(greeting, 'world'),
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
103 ... LazyProxy(greeting, 'Joe'),
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
104 ... LazyProxy(greeting, 'universe'),
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
105 ... ]
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
106 >>> greetings.sort()
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
107 >>> for greeting in greetings:
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
108 ... print greeting
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
109 Hello, Joe!
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
110 Hello, universe!
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
111 Hello, world!
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
112 """
13
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
113 __slots__ = ['_func', '_args', '_kwargs', '_value']
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
114
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
115 def __init__(self, func, *args, **kwargs):
13
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
116 # Avoid triggering our own __setattr__ implementation
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
117 object.__setattr__(self, '_func', func)
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
118 object.__setattr__(self, '_args', args)
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
119 object.__setattr__(self, '_kwargs', kwargs)
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
120 object.__setattr__(self, '_value', None)
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
121
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
122 def value(self):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
123 if self._value is None:
13
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
124 value = self._func(*self._args, **self._kwargs)
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
125 object.__setattr__(self, '_value', value)
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
126 return self._value
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
127 value = property(value)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
128
13
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
129 def __contains__(self, key):
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
130 return key in self.value
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
131
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
132 def __nonzero__(self):
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
133 return bool(self.value)
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
134
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
135 def __dir__(self):
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
136 return dir(self.value)
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
137
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
138 def __iter__(self):
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
139 return iter(self.value)
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
140
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
141 def __len__(self):
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
142 return len(self.value)
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
143
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
144 def __str__(self):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
145 return str(self.value)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
146
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
147 def __unicode__(self):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
148 return unicode(self.value)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
149
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
150 def __add__(self, other):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
151 return self.value + other
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
152
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
153 def __radd__(self, other):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
154 return other + self.value
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
155
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
156 def __mod__(self, other):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
157 return self.value % other
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
158
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
159 def __rmod__(self, other):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
160 return other % self.value
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
161
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
162 def __mul__(self, other):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
163 return self.value * other
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
164
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
165 def __rmul__(self, other):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
166 return other * self.value
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
167
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
168 def __call__(self, *args, **kwargs):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
169 return self.value(*args, **kwargs)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
170
13
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
171 def __lt__(self, other):
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
172 return self.value < other
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
173
13
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
174 def __le__(self, other):
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
175 return self.value <= other
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
176
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
177 def __eq__(self, other):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
178 return self.value == other
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
179
13
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
180 def __ne__(self, other):
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
181 return self.value != other
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
182
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
183 def __gt__(self, other):
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
184 return self.value > other
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
185
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
186 def __ge__(self, other):
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
187 return self.value >= other
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
188
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
189 def __delattr__(self, name):
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
190 delattr(self.value, name)
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
191
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
192 def __getattr__(self, name):
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
193 return getattr(self.value, name)
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
194
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
195 def __setattr__(self, key, value):
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
196 setattr(self.value, name, value)
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
197
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
198 def __delitem__(self, key):
13
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
199 del self.value[key]
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
200
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
201 def __getitem__(self, key):
13
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
202 return self.value[key]
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
203
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
204 def __setitem__(self, key, value):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
205 self.value[name] = value
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
206
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
207
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
208 try:
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
209 relpath = os.path.relpath
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
210 except AttributeError:
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
211 def relpath(path, start='.'):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
212 start_list = os.path.abspath(start).split(os.sep)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
213 path_list = os.path.abspath(path).split(os.sep)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
214
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
215 # Work out how much of the filepath is shared by start and path.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
216 i = len(os.path.commonprefix([start_list, path_list]))
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
217
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
218 rel_list = [os.path.pardir] * (len(start_list) - i) + path_list[i:]
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
219 return os.path.join(*rel_list)
Copyright (C) 2012-2017 Edgewall Software