annotate babel/util.py @ 30:6b388baae20c

Add missing import.
author cmlenz
date Mon, 04 Jun 2007 10:54:26 +0000
parents da1c9610e751
children 3b314a78015d
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
30
6b388baae20c Add missing import.
cmlenz
parents: 29
diff changeset
16 from datetime import timedelta, tzinfo
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
17 import os
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
18 import re
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
19
29
da1c9610e751 More work on timezones.
cmlenz
parents: 13
diff changeset
20 __all__ = ['default_locale', 'extended_glob', 'relpath', 'LazyProxy', 'UTC']
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
21 __docformat__ = 'restructuredtext en'
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
22
13
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
23 def default_locale(kind=None):
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
24 """Returns the default locale for a given category, based on environment
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
25 variables.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
26
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
27 :param kind: one of the ``LC_XXX`` environment variable names
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
28 :return: the value of the variable, or any of the fallbacks (``LC_ALL`` and
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
29 ``LANG``)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
30 :rtype: `str`
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
31 """
13
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
32 for name in filter(None, (kind, 'LC_ALL', 'LANG')):
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
33 locale = os.getenv(name)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
34 if locale is not None:
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
35 return locale
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
36
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
37 def extended_glob(pattern, dirname=''):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
38 """Extended pathname pattern expansion.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
39
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
40 This function is similar to what is provided by the ``glob`` module in the
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
41 Python standard library, but also supports a convenience pattern ("**") to
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
42 match files at any directory level.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
43
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
44 :param pattern: the glob pattern
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
45 :param dirname: the path to the directory in which to search for files
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
46 matching the given pattern
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
47 :return: an iterator over the absolute filenames of any matching files
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
48 :rtype: ``iterator``
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
49 """
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
50 symbols = {
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 }
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
58 buf = []
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
59 for idx, part in enumerate(re.split('([?*]+/?)', pattern)):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
60 if idx % 2:
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
61 buf.append(symbols[part])
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
62 elif part:
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
63 buf.append(re.escape(part))
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
64 regex = re.compile(''.join(buf) + '$')
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
65
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
66 absname = os.path.abspath(dirname)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
67 for root, dirnames, filenames in os.walk(absname):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
68 for subdir in dirnames:
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
69 if subdir.startswith('.') or subdir.startswith('_'):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
70 dirnames.remove(subdir)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
71 for filename in filenames:
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
72 filepath = relpath(
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
73 os.path.join(root, filename).replace(os.sep, '/'),
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
74 dirname
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
75 )
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
76 if regex.match(filepath):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
77 yield filepath
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
78
29
da1c9610e751 More work on timezones.
cmlenz
parents: 13
diff changeset
79
13
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
80 class LazyProxy(object):
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
81 """Class for proxy objects that delegate to a specified function to evaluate
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
82 the actual object.
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
83
13
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
84 >>> def greeting(name='world'):
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
85 ... return 'Hello, %s!' % name
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
86 >>> lazy_greeting = LazyProxy(greeting, name='Joe')
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
87 >>> print lazy_greeting
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
88 Hello, Joe!
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
89 >>> u' ' + lazy_greeting
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
90 u' Hello, Joe!'
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
91 >>> u'(%s)' % lazy_greeting
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
92 u'(Hello, Joe!)'
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
93
13
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
94 This can be used, for example, to implement lazy translation functions that
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
95 delay the actual translation until the string is actually used. The
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
96 rationale for such behavior is that the locale of the user may not always
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
97 be available. In web applications, you only know the locale when processing
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
98 a request.
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
99
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
100 The proxy implementation attempts to be as complete as possible, so that
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
101 the lazy objects should mostly work as expected, for example for sorting:
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
102
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
103 >>> greetings = [
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
104 ... LazyProxy(greeting, 'world'),
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
105 ... LazyProxy(greeting, 'Joe'),
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
106 ... LazyProxy(greeting, 'universe'),
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
107 ... ]
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
108 >>> greetings.sort()
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
109 >>> for greeting in greetings:
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
110 ... print greeting
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
111 Hello, Joe!
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
112 Hello, universe!
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
113 Hello, world!
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
114 """
13
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
115 __slots__ = ['_func', '_args', '_kwargs', '_value']
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
116
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
117 def __init__(self, func, *args, **kwargs):
13
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
118 # Avoid triggering our own __setattr__ implementation
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
119 object.__setattr__(self, '_func', func)
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
120 object.__setattr__(self, '_args', args)
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
121 object.__setattr__(self, '_kwargs', kwargs)
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
122 object.__setattr__(self, '_value', None)
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
123
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
124 def value(self):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
125 if self._value is None:
13
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
126 value = self._func(*self._args, **self._kwargs)
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
127 object.__setattr__(self, '_value', value)
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
128 return self._value
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
129 value = property(value)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
130
13
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
131 def __contains__(self, key):
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
132 return key in self.value
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
133
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
134 def __nonzero__(self):
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
135 return bool(self.value)
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
136
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
137 def __dir__(self):
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
138 return dir(self.value)
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
139
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
140 def __iter__(self):
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
141 return iter(self.value)
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
142
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
143 def __len__(self):
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
144 return len(self.value)
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
145
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
146 def __str__(self):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
147 return str(self.value)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
148
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
149 def __unicode__(self):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
150 return unicode(self.value)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
151
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
152 def __add__(self, other):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
153 return self.value + other
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
154
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
155 def __radd__(self, other):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
156 return other + self.value
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
157
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
158 def __mod__(self, other):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
159 return self.value % other
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
160
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
161 def __rmod__(self, other):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
162 return other % self.value
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
163
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
164 def __mul__(self, other):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
165 return self.value * other
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
166
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
167 def __rmul__(self, other):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
168 return other * self.value
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
169
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
170 def __call__(self, *args, **kwargs):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
171 return self.value(*args, **kwargs)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
172
13
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
173 def __lt__(self, other):
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
174 return self.value < other
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
175
13
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
176 def __le__(self, other):
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
177 return self.value <= other
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
178
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
179 def __eq__(self, other):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
180 return self.value == other
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
181
13
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
182 def __ne__(self, other):
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
183 return self.value != other
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
184
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
185 def __gt__(self, other):
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
186 return self.value > other
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
187
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
188 def __ge__(self, other):
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
189 return self.value >= other
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
190
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
191 def __delattr__(self, name):
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
192 delattr(self.value, name)
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
193
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
194 def __getattr__(self, name):
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
195 return getattr(self.value, name)
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
196
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
197 def __setattr__(self, key, value):
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
198 setattr(self.value, name, value)
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
199
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
200 def __delitem__(self, key):
13
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
201 del self.value[key]
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
202
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
203 def __getitem__(self, key):
13
b6c0de43fa40 Extended and documented `LazyProxy`.
cmlenz
parents: 1
diff changeset
204 return self.value[key]
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
205
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
206 def __setitem__(self, key, value):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
207 self.value[name] = value
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
208
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
209
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
210 try:
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
211 relpath = os.path.relpath
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
212 except AttributeError:
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
213 def relpath(path, start='.'):
29
da1c9610e751 More work on timezones.
cmlenz
parents: 13
diff changeset
214 """Compute the relative path to one path from another.
da1c9610e751 More work on timezones.
cmlenz
parents: 13
diff changeset
215
da1c9610e751 More work on timezones.
cmlenz
parents: 13
diff changeset
216 :return: the relative path
da1c9610e751 More work on timezones.
cmlenz
parents: 13
diff changeset
217 :rtype: `basestring`
da1c9610e751 More work on timezones.
cmlenz
parents: 13
diff changeset
218 """
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
219 start_list = os.path.abspath(start).split(os.sep)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
220 path_list = os.path.abspath(path).split(os.sep)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
221
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
222 # Work out how much of the filepath is shared by start and path.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
223 i = len(os.path.commonprefix([start_list, path_list]))
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
224
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
225 rel_list = [os.path.pardir] * (len(start_list) - i) + path_list[i:]
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
226 return os.path.join(*rel_list)
29
da1c9610e751 More work on timezones.
cmlenz
parents: 13
diff changeset
227
da1c9610e751 More work on timezones.
cmlenz
parents: 13
diff changeset
228 try:
da1c9610e751 More work on timezones.
cmlenz
parents: 13
diff changeset
229 from pytz import UTC
da1c9610e751 More work on timezones.
cmlenz
parents: 13
diff changeset
230 except ImportError:
da1c9610e751 More work on timezones.
cmlenz
parents: 13
diff changeset
231 ZERO = timedelta(0)
da1c9610e751 More work on timezones.
cmlenz
parents: 13
diff changeset
232
da1c9610e751 More work on timezones.
cmlenz
parents: 13
diff changeset
233 class UTC(tzinfo):
da1c9610e751 More work on timezones.
cmlenz
parents: 13
diff changeset
234 """Simple `tzinfo` implementation for UTC."""
da1c9610e751 More work on timezones.
cmlenz
parents: 13
diff changeset
235
da1c9610e751 More work on timezones.
cmlenz
parents: 13
diff changeset
236 def __repr__(self):
da1c9610e751 More work on timezones.
cmlenz
parents: 13
diff changeset
237 return '<UTC>'
da1c9610e751 More work on timezones.
cmlenz
parents: 13
diff changeset
238
da1c9610e751 More work on timezones.
cmlenz
parents: 13
diff changeset
239 def __str__(self):
da1c9610e751 More work on timezones.
cmlenz
parents: 13
diff changeset
240 return 'UTC'
da1c9610e751 More work on timezones.
cmlenz
parents: 13
diff changeset
241
da1c9610e751 More work on timezones.
cmlenz
parents: 13
diff changeset
242 def utcoffset(self, dt):
da1c9610e751 More work on timezones.
cmlenz
parents: 13
diff changeset
243 return ZERO
da1c9610e751 More work on timezones.
cmlenz
parents: 13
diff changeset
244
da1c9610e751 More work on timezones.
cmlenz
parents: 13
diff changeset
245 def tzname(self, dt):
da1c9610e751 More work on timezones.
cmlenz
parents: 13
diff changeset
246 return 'UTC'
da1c9610e751 More work on timezones.
cmlenz
parents: 13
diff changeset
247
da1c9610e751 More work on timezones.
cmlenz
parents: 13
diff changeset
248 def dst(self, dt):
da1c9610e751 More work on timezones.
cmlenz
parents: 13
diff changeset
249 return ZERO
da1c9610e751 More work on timezones.
cmlenz
parents: 13
diff changeset
250
da1c9610e751 More work on timezones.
cmlenz
parents: 13
diff changeset
251 UTC = UTC()
da1c9610e751 More work on timezones.
cmlenz
parents: 13
diff changeset
252 """`tzinfo` object for UTC (Universal Time).
da1c9610e751 More work on timezones.
cmlenz
parents: 13
diff changeset
253
da1c9610e751 More work on timezones.
cmlenz
parents: 13
diff changeset
254 :type: `tzinfo`
da1c9610e751 More work on timezones.
cmlenz
parents: 13
diff changeset
255 """
Copyright (C) 2012-2017 Edgewall Software