Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(694)

Side by Side Diff: third_party/logilab/logilab/common/__init__.py

Issue 1920403002: [content/test/gpu] Run pylint check of gpu tests in unittest instead of PRESUBMIT (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Update path to LICENSE.txt of logilab/README.chromium Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 # copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
3 #
4 # This file is part of logilab-common.
5 #
6 # logilab-common is free software: you can redistribute it and/or modify it unde r
7 # the terms of the GNU Lesser General Public License as published by the Free
8 # Software Foundation, either version 2.1 of the License, or (at your option) an y
9 # later version.
10 #
11 # logilab-common is distributed in the hope that it will be useful, but WITHOUT
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
14 # details.
15 #
16 # You should have received a copy of the GNU Lesser General Public License along
17 # with logilab-common. If not, see <http://www.gnu.org/licenses/>.
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 from six.moves import range
30
31 from logilab.common.__pkginfo__ import version as __version__
32
33 STD_BLACKLIST = ('CVS', '.svn', '.hg', 'debian', 'dist', 'build')
34
35 IGNORED_EXTENSIONS = ('.pyc', '.pyo', '.elc', '~', '.swp', '.orig')
36
37 # set this to False if you've mx DateTime installed but you don't want your db
38 # adapter to use it (should be set before you got a connection)
39 USE_MX_DATETIME = True
40
41
42 class attrdict(dict):
43 """A dictionary for which keys are also accessible as attributes."""
44 def __getattr__(self, attr):
45 try:
46 return self[attr]
47 except KeyError:
48 raise AttributeError(attr)
49
50 class dictattr(dict):
51 def __init__(self, proxy):
52 self.__proxy = proxy
53
54 def __getitem__(self, attr):
55 try:
56 return getattr(self.__proxy, attr)
57 except AttributeError:
58 raise KeyError(attr)
59
60 class nullobject(object):
61 def __repr__(self):
62 return '<nullobject>'
63 def __bool__(self):
64 return False
65 __nonzero__ = __bool__
66
67 class tempattr(object):
68 def __init__(self, obj, attr, value):
69 self.obj = obj
70 self.attr = attr
71 self.value = value
72
73 def __enter__(self):
74 self.oldvalue = getattr(self.obj, self.attr)
75 setattr(self.obj, self.attr, self.value)
76 return self.obj
77
78 def __exit__(self, exctype, value, traceback):
79 setattr(self.obj, self.attr, self.oldvalue)
80
81
82
83 # flatten -----
84 # XXX move in a specific module and use yield instead
85 # do not mix flatten and translate
86 #
87 # def iterable(obj):
88 # try: iter(obj)
89 # except: return False
90 # return True
91 #
92 # def is_string_like(obj):
93 # try: obj +''
94 # except (TypeError, ValueError): return False
95 # return True
96 #
97 #def is_scalar(obj):
98 # return is_string_like(obj) or not iterable(obj)
99 #
100 #def flatten(seq):
101 # for item in seq:
102 # if is_scalar(item):
103 # yield item
104 # else:
105 # for subitem in flatten(item):
106 # yield subitem
107
108 def flatten(iterable, tr_func=None, results=None):
109 """Flatten a list of list with any level.
110
111 If tr_func is not None, it should be a one argument function that'll be call ed
112 on each final element.
113
114 :rtype: list
115
116 >>> flatten([1, [2, 3]])
117 [1, 2, 3]
118 """
119 if results is None:
120 results = []
121 for val in iterable:
122 if isinstance(val, (list, tuple)):
123 flatten(val, tr_func, results)
124 elif tr_func is None:
125 results.append(val)
126 else:
127 results.append(tr_func(val))
128 return results
129
130
131 # XXX is function below still used ?
132
133 def make_domains(lists):
134 """
135 Given a list of lists, return a list of domain for each list to produce all
136 combinations of possibles values.
137
138 :rtype: list
139
140 Example:
141
142 >>> make_domains(['a', 'b'], ['c','d', 'e'])
143 [['a', 'b', 'a', 'b', 'a', 'b'], ['c', 'c', 'd', 'd', 'e', 'e']]
144 """
145 domains = []
146 for iterable in lists:
147 new_domain = iterable[:]
148 for i in range(len(domains)):
149 domains[i] = domains[i]*len(iterable)
150 if domains:
151 missing = (len(domains[0]) - len(iterable)) / len(iterable)
152 i = 0
153 for j in range(len(iterable)):
154 value = iterable[j]
155 for dummy in range(missing):
156 new_domain.insert(i, value)
157 i += 1
158 i += 1
159 domains.append(new_domain)
160 return domains
161
162
163 # private stuff ################################################################
164
165 def _handle_blacklist(blacklist, dirnames, filenames):
166 """remove files/directories in the black list
167
168 dirnames/filenames are usually from os.walk
169 """
170 for norecurs in blacklist:
171 if norecurs in dirnames:
172 dirnames.remove(norecurs)
173 elif norecurs in filenames:
174 filenames.remove(norecurs)
175
OLDNEW
« no previous file with comments | « third_party/logilab/logilab/common/README.chromium ('k') | third_party/logilab/logilab/common/__pkginfo__.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698