Package logilab :: Package common
[frames] | no frames]

Source Code for Package logilab.common

  1  # copyright 2003-2010 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 under 
  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) any 
  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  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  # set this to False if you've mx DateTime installed but you don't want your db 
 35  # adapter to use it (should be set before you got a connection) 
 36  USE_MX_DATETIME = True 
 37   
 38   
39 -class attrdict(dict):
40 """A dictionary for which keys are also accessible as attributes."""
41 - def __getattr__(self, attr):
42 try: 43 return self[attr] 44 except KeyError: 45 raise AttributeError(attr)
46
47 -class dictattr(dict):
48 - def __init__(self, proxy):
49 self.__proxy = proxy
50
51 - def __getitem__(self, attr):
52 try: 53 return getattr(self.__proxy, attr) 54 except AttributeError: 55 raise KeyError(attr)
56
57 -class nullobject(object):
58 - def __repr__(self):
59 return '<nullobject>'
60 - def __nonzero__(self):
61 return False
62
63 -class tempattr(object):
64 - def __init__(self, obj, attr, value):
65 self.obj = obj 66 self.attr = attr 67 self.value = value
68
69 - def __enter__(self):
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 # flatten ----- 80 # XXX move in a specific module and use yield instead 81 # do not mix flatten and translate 82 # 83 # def iterable(obj): 84 # try: iter(obj) 85 # except: return False 86 # return True 87 # 88 # def is_string_like(obj): 89 # try: obj +'' 90 # except (TypeError, ValueError): return False 91 # return True 92 # 93 #def is_scalar(obj): 94 # return is_string_like(obj) or not iterable(obj) 95 # 96 #def flatten(seq): 97 # for item in seq: 98 # if is_scalar(item): 99 # yield item 100 # else: 101 # for subitem in flatten(item): 102 # yield subitem 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 # XXX is function below still used ? 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 # private stuff ################################################################ 160
161 -def _handle_blacklist(blacklist, dirnames, filenames):
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