Package logilab ::
Package common
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 """Logilab common library (aka Logilab's extension to the standard library).
19
20 :type STD_BLACKLIST: tuple
21 :var STD_BLACKLIST: directories ignored by default by the functions in
22 this package which have to recurse into directories
23
24 :type IGNORED_EXTENSIONS: tuple
25 :var IGNORED_EXTENSIONS: file extensions that may usually be ignored
26 """
27 __docformat__ = "restructuredtext en"
28
29 import sys
30 import types
31 import pkg_resources
32
33 __version__ = pkg_resources.get_distribution('logilab-common').version
34
35
36 __pkginfo__ = types.ModuleType('__pkginfo__')
37 __pkginfo__.__package__ = __name__
38 __pkginfo__.version = __version__
39 sys.modules['logilab.common.__pkginfo__'] = __pkginfo__
40
41 STD_BLACKLIST = ('CVS', '.svn', '.hg', 'debian', 'dist', 'build')
42
43 IGNORED_EXTENSIONS = ('.pyc', '.pyo', '.elc', '~', '.swp', '.orig')
44
45
46
47 USE_MX_DATETIME = True
48
49
51 """A dictionary for which keys are also accessible as attributes."""
53 try:
54 return self[attr]
55 except KeyError:
56 raise AttributeError(attr)
57
61
63 try:
64 return getattr(self.__proxy, attr)
65 except AttributeError:
66 raise KeyError(attr)
67
74
77 self.obj = obj
78 self.attr = attr
79 self.value = value
80
82 self.oldvalue = getattr(self.obj, self.attr)
83 setattr(self.obj, self.attr, self.value)
84 return self.obj
85
86 - def __exit__(self, exctype, value, traceback):
87 setattr(self.obj, self.attr, self.oldvalue)
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116 -def flatten(iterable, tr_func=None, results=None):
117 """Flatten a list of list with any level.
118
119 If tr_func is not None, it should be a one argument function that'll be called
120 on each final element.
121
122 :rtype: list
123
124 >>> flatten([1, [2, 3]])
125 [1, 2, 3]
126 """
127 if results is None:
128 results = []
129 for val in iterable:
130 if isinstance(val, (list, tuple)):
131 flatten(val, tr_func, results)
132 elif tr_func is None:
133 results.append(val)
134 else:
135 results.append(tr_func(val))
136 return results
137
138
139
140
141 -def make_domains(lists):
142 """
143 Given a list of lists, return a list of domain for each list to produce all
144 combinations of possibles values.
145
146 :rtype: list
147
148 Example:
149
150 >>> make_domains(['a', 'b'], ['c','d', 'e'])
151 [['a', 'b', 'a', 'b', 'a', 'b'], ['c', 'c', 'd', 'd', 'e', 'e']]
152 """
153 from six.moves import range
154 domains = []
155 for iterable in lists:
156 new_domain = iterable[:]
157 for i in range(len(domains)):
158 domains[i] = domains[i]*len(iterable)
159 if domains:
160 missing = (len(domains[0]) - len(iterable)) / len(iterable)
161 i = 0
162 for j in range(len(iterable)):
163 value = iterable[j]
164 for dummy in range(missing):
165 new_domain.insert(i, value)
166 i += 1
167 i += 1
168 domains.append(new_domain)
169 return domains
170
171
172
173
175 """remove files/directories in the black list
176
177 dirnames/filenames are usually from os.walk
178 """
179 for norecurs in blacklist:
180 if norecurs in dirnames:
181 dirnames.remove(norecurs)
182 elif norecurs in filenames:
183 filenames.remove(norecurs)
184