__init__.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 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. :type STD_BLACKLIST: tuple
  20. :var STD_BLACKLIST: directories ignored by default by the functions in
  21. this package which have to recurse into directories
  22. :type IGNORED_EXTENSIONS: tuple
  23. :var IGNORED_EXTENSIONS: file extensions that may usually be ignored
  24. """
  25. __docformat__ = "restructuredtext en"
  26. from logilab.common.__pkginfo__ import version as __version__
  27. STD_BLACKLIST = ('CVS', '.svn', '.hg', 'debian', 'dist', 'build')
  28. IGNORED_EXTENSIONS = ('.pyc', '.pyo', '.elc', '~', '.swp', '.orig')
  29. # set this to False if you've mx DateTime installed but you don't want your db
  30. # adapter to use it (should be set before you got a connection)
  31. USE_MX_DATETIME = True
  32. class attrdict(dict):
  33. """A dictionary for which keys are also accessible as attributes."""
  34. def __getattr__(self, attr):
  35. try:
  36. return self[attr]
  37. except KeyError:
  38. raise AttributeError(attr)
  39. class dictattr(dict):
  40. def __init__(self, proxy):
  41. self.__proxy = proxy
  42. def __getitem__(self, attr):
  43. try:
  44. return getattr(self.__proxy, attr)
  45. except AttributeError:
  46. raise KeyError(attr)
  47. class nullobject(object):
  48. def __repr__(self):
  49. return '<nullobject>'
  50. def __nonzero__(self):
  51. return False
  52. class tempattr(object):
  53. def __init__(self, obj, attr, value):
  54. self.obj = obj
  55. self.attr = attr
  56. self.value = value
  57. def __enter__(self):
  58. self.oldvalue = getattr(self.obj, self.attr)
  59. setattr(self.obj, self.attr, self.value)
  60. return self.obj
  61. def __exit__(self, exctype, value, traceback):
  62. setattr(self.obj, self.attr, self.oldvalue)
  63. # flatten -----
  64. # XXX move in a specific module and use yield instead
  65. # do not mix flatten and translate
  66. #
  67. # def iterable(obj):
  68. # try: iter(obj)
  69. # except: return False
  70. # return True
  71. #
  72. # def is_string_like(obj):
  73. # try: obj +''
  74. # except (TypeError, ValueError): return False
  75. # return True
  76. #
  77. #def is_scalar(obj):
  78. # return is_string_like(obj) or not iterable(obj)
  79. #
  80. #def flatten(seq):
  81. # for item in seq:
  82. # if is_scalar(item):
  83. # yield item
  84. # else:
  85. # for subitem in flatten(item):
  86. # yield subitem
  87. def flatten(iterable, tr_func=None, results=None):
  88. """Flatten a list of list with any level.
  89. If tr_func is not None, it should be a one argument function that'll be called
  90. on each final element.
  91. :rtype: list
  92. >>> flatten([1, [2, 3]])
  93. [1, 2, 3]
  94. """
  95. if results is None:
  96. results = []
  97. for val in iterable:
  98. if isinstance(val, (list, tuple)):
  99. flatten(val, tr_func, results)
  100. elif tr_func is None:
  101. results.append(val)
  102. else:
  103. results.append(tr_func(val))
  104. return results
  105. # XXX is function below still used ?
  106. def make_domains(lists):
  107. """
  108. Given a list of lists, return a list of domain for each list to produce all
  109. combinations of possibles values.
  110. :rtype: list
  111. Example:
  112. >>> make_domains(['a', 'b'], ['c','d', 'e'])
  113. [['a', 'b', 'a', 'b', 'a', 'b'], ['c', 'c', 'd', 'd', 'e', 'e']]
  114. """
  115. domains = []
  116. for iterable in lists:
  117. new_domain = iterable[:]
  118. for i in range(len(domains)):
  119. domains[i] = domains[i]*len(iterable)
  120. if domains:
  121. missing = (len(domains[0]) - len(iterable)) / len(iterable)
  122. i = 0
  123. for j in range(len(iterable)):
  124. value = iterable[j]
  125. for dummy in range(missing):
  126. new_domain.insert(i, value)
  127. i += 1
  128. i += 1
  129. domains.append(new_domain)
  130. return domains
  131. # private stuff ################################################################
  132. def _handle_blacklist(blacklist, dirnames, filenames):
  133. """remove files/directories in the black list
  134. dirnames/filenames are usually from os.walk
  135. """
  136. for norecurs in blacklist:
  137. if norecurs in dirnames:
  138. dirnames.remove(norecurs)
  139. elif norecurs in filenames:
  140. filenames.remove(norecurs)