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 from logilab.common.__pkginfo__ import version as __version__
29
30 STD_BLACKLIST = ('CVS', '.svn', '.hg', 'debian', 'dist', 'build')
31
32 IGNORED_EXTENSIONS = ('.pyc', '.pyo', '.elc', '~', '.swp', '.orig')
33
34
35
36 USE_MX_DATETIME = True
37
38
40 """A dictionary for which keys are also accessible as attributes."""
42 try:
43 return self[attr]
44 except KeyError:
45 raise AttributeError(attr)
46
50
52 try:
53 return getattr(self.__proxy, attr)
54 except AttributeError:
55 raise KeyError(attr)
56
62
65 self.obj = obj
66 self.attr = attr
67 self.value = value
68
70 self.oldvalue = getattr(self.obj, self.attr)
71 setattr(self.obj, self.attr, self.value)
72 return self.obj
73
74 - def __exit__(self, exctype, value, traceback):
75 setattr(self.obj, self.attr, self.oldvalue)
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104 -def flatten(iterable, tr_func=None, results=None):
105 """Flatten a list of list with any level.
106
107 If tr_func is not None, it should be a one argument function that'll be called
108 on each final element.
109
110 :rtype: list
111
112 >>> flatten([1, [2, 3]])
113 [1, 2, 3]
114 """
115 if results is None:
116 results = []
117 for val in iterable:
118 if isinstance(val, (list, tuple)):
119 flatten(val, tr_func, results)
120 elif tr_func is None:
121 results.append(val)
122 else:
123 results.append(tr_func(val))
124 return results
125
126
127
128
129 -def make_domains(lists):
130 """
131 Given a list of lists, return a list of domain for each list to produce all
132 combinations of possibles values.
133
134 :rtype: list
135
136 Example:
137
138 >>> make_domains(['a', 'b'], ['c','d', 'e'])
139 [['a', 'b', 'a', 'b', 'a', 'b'], ['c', 'c', 'd', 'd', 'e', 'e']]
140 """
141 domains = []
142 for iterable in lists:
143 new_domain = iterable[:]
144 for i in range(len(domains)):
145 domains[i] = domains[i]*len(iterable)
146 if domains:
147 missing = (len(domains[0]) - len(iterable)) / len(iterable)
148 i = 0
149 for j in range(len(iterable)):
150 value = iterable[j]
151 for dummy in range(missing):
152 new_domain.insert(i, value)
153 i += 1
154 i += 1
155 domains.append(new_domain)
156 return domains
157
158
159
160
162 """remove files/directories in the black list
163
164 dirnames/filenames are usually from os.walk
165 """
166 for norecurs in blacklist:
167 if norecurs in dirnames:
168 dirnames.remove(norecurs)
169 elif norecurs in filenames:
170 filenames.remove(norecurs)
171