diff options
author | Sylvain <syt@logilab.fr> | 2006-11-08 17:00:02 +0100 |
---|---|---|
committer | Sylvain <syt@logilab.fr> | 2006-11-08 17:00:02 +0100 |
commit | 5152958ecda94ba39ed36fbc625bc5cd39ebab4d (patch) | |
tree | ccba85222bad5ff9a63c2ed95b216a2ef27c7724 /__init__.py | |
parent | 907582a18893d133346ab235412609824b25b7bd (diff) | |
download | logilab-common-5152958ecda94ba39ed36fbc625bc5cd39ebab4d.tar.gz |
reorganize, deprecation warning for deprecated stuff, remove old deprecated function/methods
Diffstat (limited to '__init__.py')
-rw-r--r-- | __init__.py | 205 |
1 files changed, 25 insertions, 180 deletions
diff --git a/__init__.py b/__init__.py index 71a1f1c..f8f2e17 100644 --- a/__init__.py +++ b/__init__.py @@ -1,3 +1,6 @@ +# Copyright (c) 2004-2006 LOGILAB S.A. (Paris, FRANCE). +# http://www.logilab.fr/ -- mailto:contact@logilab.fr +# # This program is free software; you can redistribute it and/or modify it under # the terms of the GNU General Public License as published by the Free Software # Foundation; either version 2 of the License, or (at your option) any later @@ -10,21 +13,26 @@ # You should have received a copy of the GNU General Public License along with # this program; if not, write to the Free Software Foundation, Inc., # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -""" Copyright (c) 2002-2006 LOGILAB S.A. (Paris, FRANCE). - http://www.logilab.fr/ -- mailto:contact@logilab.fr - -Logilab common libraries -""" +"""Logilab common libraries: -from __future__ import nested_scopes +a set of common functionnalities shared among logilab projects +""" -# bw compat -from logilab.common.graph import get_cycles +from logilab.common.deprecation import moved -# FIXME: move all those functions in a separated module +get_cycles = moved('logilab.common.graph', 'get_cycles') +cached = moved('logilab.common.decorators', 'cached') +ProgressBar = moved('logilab.common.shellutils', 'ProgressBar') +Execute = moved('logilab.common.shellutils', 'Execute') +acquire_lock = moved('logilab.common.shellutils', 'acquire_lock') +release_lock = moved('logilab.common.shellutils', 'release_lock') +deprecated_function = moved('logilab.common.deprecation', 'deprecated_function') +class_renamed = moved('logilab.common.deprecation', 'class_renamed') def intersection(list1, list2): """return the intersection of list1 and list2""" + warn('this function is deprecated, use a set instead', DeprecationWarning, + stacklevel=2) intersect_dict, result = {}, [] for item in list1: intersect_dict[item] = 1 @@ -35,6 +43,8 @@ def intersection(list1, list2): def difference(list1, list2): """return elements of list1 not in list2""" + warn('this function is deprecated, use a set instead', DeprecationWarning, + stacklevel=2) tmp, result = {}, [] for i in list2: tmp[i] = 1 @@ -45,6 +55,8 @@ def difference(list1, list2): def union(list1, list2): """return list1 union list2""" + warn('this function is deprecated, use a set instead', DeprecationWarning, + stacklevel=2) tmp = {} for i in list1: tmp[i] = 1 @@ -52,6 +64,9 @@ def union(list1, list2): tmp[i] = 1 return tmp.keys() + +# XXX are functions below still used ? + def make_domains(lists): """ given a list of lists, return a list of domain for each list to produce all @@ -88,7 +103,7 @@ def flatten(iterable, tr_func=None, results=None): if results is None: results = [] for val in iterable: - if type(val) in (type(()), type([])): + if isinstance(val, (list, tuple)): flatten(val, tr_func, results) elif tr_func is None: results.append(val) @@ -97,174 +112,4 @@ def flatten(iterable, tr_func=None, results=None): return results -def cached(callableobj, keyarg=None): - """simple decorator to cache result of method call""" - #print callableobj, keyarg, callableobj.func_code.co_argcount - if callableobj.func_code.co_argcount == 1 or keyarg == 0: - - def cache_wrapper1(self, *args): - cache = '_%s_cache_' % callableobj.__name__ - #print 'cache1?', cache - try: - return getattr(self, cache) - except AttributeError: - #print 'miss' - value = callableobj(self, *args) - setattr(self, cache, value) - return value - return cache_wrapper1 - - elif keyarg: - - def cache_wrapper2(self, *args, **kwargs): - cache = '_%s_cache_' % callableobj.__name__ - key = args[keyarg-1] - #print 'cache2?', cache, self, key - try: - _cache = getattr(self, cache) - except AttributeError: - #print 'init' - _cache = {} - setattr(self, cache, _cache) - try: - return _cache[key] - except KeyError: - #print 'miss', self, cache, key - _cache[key] = callableobj(self, *args, **kwargs) - return _cache[key] - return cache_wrapper2 - def cache_wrapper3(self, *args): - cache = '_%s_cache_' % callableobj.__name__ - #print 'cache3?', cache, self, args - try: - _cache = getattr(self, cache) - except AttributeError: - #print 'init' - _cache = {} - setattr(self, cache, _cache) - try: - return _cache[args] - except KeyError: - #print 'miss' - _cache[args] = callableobj(self, *args) - return _cache[args] - return cache_wrapper3 - -import sys - -class ProgressBar(object): - """a simple text progression bar""" - - def __init__(self, nbops, size=20., stream=sys.stdout): - self._dotevery = max(nbops / size, 1) - self._fstr = '\r[%-20s]' - self._dotcount, self._dots = 1, [] - self._stream = stream - - def update(self): - """update the progression bar""" - self._dotcount += 1 - if self._dotcount >= self._dotevery: - self._dotcount = 1 - self._dots.append('.') - self._stream.write(self._fstr % ''.join(self._dots)) - self._stream.flush() - - -import tempfile -import os -import time -from os.path import exists - -class Execute: - """This is a deadlock save version of popen2 (no stdin), that returns - an object with errorlevel, out and err - """ - - def __init__(self, command): - outfile = tempfile.mktemp() - errfile = tempfile.mktemp() - self.status = os.system("( %s ) >%s 2>%s" % - (command, outfile, errfile)) >> 8 - self.out = open(outfile,"r").read() - self.err = open(errfile,"r").read() - os.remove(outfile) - os.remove(errfile) -def acquire_lock(lock_file, max_try=10, delay=10): - """acquire a lock represented by a file on the file system""" - count = 0 - while max_try <= 0 or count < max_try: - if not exists(lock_file): - break - count += 1 - time.sleep(delay) - else: - raise Exception('Unable to acquire %s' % lock_file) - stream = open(lock_file, 'w') - stream.write(str(os.getpid())) - stream.close() - -def release_lock(lock_file): - """release a lock represented by a file on the file system""" - os.remove(lock_file) - - -## Deprecation utilities ######################### - -from warnings import warn - -class deprecated(type): - """metaclass to print a warning on instantiation of a deprecated class""" - - def __call__(cls, *args, **kwargs): - msg = getattr(cls, "__deprecation_warning__", - "%s is deprecated" % cls.__name__) - warn(msg, DeprecationWarning, stacklevel=2) - return type.__call__(cls, *args, **kwargs) - - -def class_renamed(old_name, new_class, message=None): - """automatically creates a class which fires a DeprecationWarning - when instantiated. - - >>> Set = class_renamed('Set', set, 'Set is now replaced by set') - >>> s = Set() - sample.py:57: DeprecationWarning: Set is now replaced by set - s = Set() - >>> - """ - clsdict = {} - if message is not None: - clsdict['__deprecation_warning__'] = message - try: - # new-style class - return deprecated(old_name, (new_class,), clsdict) - except (NameError, TypeError): - # old-style class - class DeprecatedClass(new_class): - """FIXME: There might be a better way to handle old/new-style class - """ - def __init__(self, *args, **kwargs): - warn(message, DeprecationWarning, stacklevel=2) - new_class.__init__(self, *args, **kwargs) - return DeprecatedClass - - -def deprecated_function(new_func, message=None): - """creates a function which fires a DeprecationWarning when used - - For example, if <bar> is deprecated in favour of <foo> : - >>> bar = deprecated_function(foo, 'bar is deprecated') - >>> bar() - sample.py:57: DeprecationWarning: bar is deprecated - bar() - >>> - """ - if message is None: - message = "this function is deprecated, use %s instead" % ( - new_func.func_name) - def deprecated(*args, **kwargs): - warn(message, DeprecationWarning, stacklevel=2) - return new_func(*args, **kwargs) - return deprecated |