From: Zooko O'Whielacronx Date: Mon, 4 Dec 2006 09:03:25 +0000 (-0700) Subject: mv amdlib/util/* to allmydata/util/ X-Git-Tag: tahoe_v0.1.0-0-UNSTABLE~468 X-Git-Url: https://git.rkrishnan.org/running.html?a=commitdiff_plain;h=9a300595862e7143f2c93935b8d736741026ce1e;p=tahoe-lafs%2Ftahoe-lafs.git mv amdlib/util/* to allmydata/util/ --- diff --git a/allmydata/bucketstore.py b/allmydata/bucketstore.py index d3963c46..773391bd 100644 --- a/allmydata/bucketstore.py +++ b/allmydata/bucketstore.py @@ -7,7 +7,7 @@ from allmydata.util import idlib from zope.interface import implements from allmydata.interfaces import RIBucketWriter, RIBucketReader -from amdlib.util.assertutil import precondition, _assert +from allmydata.util.assertutil import precondition, _assert class NoSuchBucketError(Failure): pass diff --git a/allmydata/util/assertutil.py b/allmydata/util/assertutil.py new file mode 100644 index 00000000..2adae8d6 --- /dev/null +++ b/allmydata/util/assertutil.py @@ -0,0 +1,82 @@ +# Copyright (c) 2003-2006 Bryce "Zooko" Wilcox-O'Hearn +# mailto:zooko@zooko.com +# http://zooko.com/ +# Permission is hereby granted, free of charge, to any person obtaining a +# copy of this work to deal in this work without restriction (including the +# rights to use, modify, distribute, sublicense, and/or sell copies) + +""" +Tests useful in assertion checking, prints out nicely formated messages too. +""" + +from humanreadable import hr + +from twisted.python import log + +def _assert(___cond=False, *___args, **___kwargs): + if ___cond: + return True + msgbuf=[] + if ___args: + msgbuf.append("%s %s" % tuple(map(hr, (___args[0], type(___args[0]),)))) + msgbuf.extend([", %s %s" % tuple(map(hr, (arg, type(arg),))) for arg in ___args[1:]]) + if ___kwargs: + msgbuf.append(", %s: %s %s" % ((___kwargs.items()[0][0],) + tuple(map(hr, (___kwargs.items()[0][1], type(___kwargs.items()[0][1]),))))) + else: + if ___kwargs: + msgbuf.append("%s: %s %s" % ((___kwargs.items()[0][0],) + tuple(map(hr, (___kwargs.items()[0][1], type(___kwargs.items()[0][1]),))))) + msgbuf.extend([", %s: %s %s" % tuple(map(hr, (k, v, type(v),))) for k, v in ___kwargs.items()[1:]]) + + raise AssertionError, "".join(msgbuf) + + return False + +def precondition(___cond=False, *___args, **___kwargs): + try: + if ___cond: + return True + msgbuf=["precondition", ] + if ___args or ___kwargs: + msgbuf.append(": ") + if ___args: + msgbuf.append("%s %s" % tuple(map(hr, (___args[0], type(___args[0]),)))) + msgbuf.extend([", %s %s" % tuple(map(hr, (arg, type(arg),))) for arg in ___args[1:]]) + if ___kwargs: + msgbuf.append(", %s: %s %s" % ((___kwargs.items()[0][0],) + tuple(map(hr, (___kwargs.items()[0][1], type(___kwargs.items()[0][1]),))))) + else: + if ___kwargs: + msgbuf.append("%s: %s %s" % ((___kwargs.items()[0][0],) + tuple(map(hr, (___kwargs.items()[0][1], type(___kwargs.items()[0][1]),))))) + msgbuf.extend([", %s: %s %s" % tuple(map(hr, (k, v, type(v),))) for k, v in ___kwargs.items()[1:]]) + except Exception, le: + log.msg("assertutil.precondition(): INTERNAL ERROR IN pyutil.assertutil. %s %s %s" % (type(le), repr(le), le.args,)) + log.err() + raise le + except: + log.msg("assertutil.precondition(): INTERNAL ERROR IN pyutil.assertutil.") + log.err() + raise + + raise AssertionError, "".join(msgbuf) + + return False + +def postcondition(___cond=False, *___args, **___kwargs): + if ___cond: + return True + msgbuf=["postcondition", ] + if ___args or ___kwargs: + msgbuf.append(": ") + if ___args: + msgbuf.append("%s %s" % tuple(map(hr, (___args[0], type(___args[0]),)))) + msgbuf.extend([", %s %s" % tuple(map(hr, (arg, type(arg),))) for arg in ___args[1:]]) + if ___kwargs: + msgbuf.append(", %s: %s %s" % ((___kwargs.items()[0][0],) + tuple(map(hr, (___kwargs.items()[0][1], type(___kwargs.items()[0][1]),))))) + else: + if ___kwargs: + msgbuf.append("%s: %s %s" % ((___kwargs.items()[0][0],) + tuple(map(hr, (___kwargs.items()[0][1], type(___kwargs.items()[0][1]),))))) + msgbuf.extend([", %s: %s %s" % tuple(map(hr, (k, v, type(v),))) for k, v in ___kwargs.items()[1:]]) + + raise AssertionError, "".join(msgbuf) + + return False + diff --git a/allmydata/util/humanreadable.py b/allmydata/util/humanreadable.py new file mode 100644 index 00000000..bae826c5 --- /dev/null +++ b/allmydata/util/humanreadable.py @@ -0,0 +1,128 @@ +# Copyright (c) 2001 Autonomous Zone Industries +# Copyright (c) 2002-2005 Bryce "Zooko" Wilcox-O'Hearn +# This file is licensed under the +# GNU Lesser General Public License v2.1. +# See the file COPYING or visit http://www.gnu.org/ for details. + +import exceptions, os +from repr import Repr + +class BetterRepr(Repr): + def __init__(self): + Repr.__init__(self) + + # Note: These levels can get adjusted dynamically! My goal is to get + # more info when printing important debug stuff like exceptions and + # stack traces and less info when logging normal events. + # --Zooko 2000-10-14 + if 0: # DEBUG MODE + self.maxlevel = 24 + self.maxdict = 24 + self.maxlist = 24 + self.maxtuple = 24 + self.maxstring = 800 + self.maxother = 800 + else: + self.maxlevel = 6 + self.maxdict = 6 + self.maxlist = 6 + self.maxtuple = 6 + self.maxstring = 300 + self.maxother = 300 + + def repr_function(self, obj, level): + if hasattr(obj, 'func_code'): + return '<' + obj.func_name + '() at ' + os.path.basename(obj.func_code.co_filename) + ':' + str(obj.func_code.co_firstlineno) + '>' + else: + return '<' + obj.func_name + '() at (builtin)' + + def repr_instance_method(self, obj, level): + if hasattr(obj, 'func_code'): + return '<' + obj.im_class.__name__ + '.' + obj.im_func.__name__ + '() at ' + os.path.basename(obj.im_func.func_code.co_filename) + ':' + str(obj.im_func.func_code.co_firstlineno) + '>' + else: + return '<' + obj.im_class.__name__ + '.' + obj.im_func.__name__ + '() at (builtin)' + + def repr_long(self, obj, level): + s = `obj` # XXX Hope this isn't too slow... + if len(s) > self.maxlong: + i = max(0, (self.maxlong-3)/2) + j = max(0, self.maxlong-3-i) + s = s[:i] + '...' + s[len(s)-j:] + if s[-1] == 'L': + return s[:-1] + return s + + def repr_instance(self, obj, level): + """ + If it is an instance of Exception, format it nicely (trying to emulate + the format that you see when an exception is actually raised, plus + bracketing '<''s). If it is an instance of dict call self.repr_dict() + on it. If it is an instance of list call self.repr_list() on it. Else + call Repr.repr_instance(). + """ + if isinstance(obj, exceptions.Exception): + # Don't cut down exception strings so much. + tms = self.maxstring + self.maxstring = max(512, tms * 4) + tml = self.maxlist + self.maxlist = max(12, tml * 4) + try: + if hasattr(obj, 'args'): + if len(obj.args) == 1: + return '<' + obj.__class__.__name__ + ': ' + self.repr1(obj.args[0], level-1) + '>' + else: + return '<' + obj.__class__.__name__ + ': ' + self.repr1(obj.args, level-1) + '>' + else: + return '<' + obj.__class__.__name__ + '>' + finally: + self.maxstring = tms + self.maxlist = tml + + if isinstance(obj, dict): + return self.repr_dict(obj, level) + + if isinstance(obj, list): + return self.repr_list(obj, level) + + return Repr.repr_instance(self, obj, level) + + def repr_list(self, obj, level): + """ + copied from standard repr.py and fixed to work on multithreadedly mutating lists. + """ + if level <= 0: return '[...]' + n = len(obj) + myl = obj[:min(n, self.maxlist)] + s = '' + for item in myl: + entry = self.repr1(item, level-1) + if s: s = s + ', ' + s = s + entry + if n > self.maxlist: s = s + ', ...' + return '[' + s + ']' + + def repr_dict(self, obj, level): + """ + copied from standard repr.py and fixed to work on multithreadedly mutating dicts. + """ + if level <= 0: return '{...}' + s = '' + n = len(obj) + items = obj.items()[:min(n, self.maxdict)] + items.sort() + for key, val in items: + entry = self.repr1(key, level-1) + ':' + self.repr1(val, level-1) + if s: s = s + ', ' + s = s + entry + if n > self.maxdict: s = s + ', ...' + return '{' + s + '}' + +# This object can be changed by other code updating this module's "brepr" +# variables. This is so that (a) code can use humanreadable with +# "from humanreadable import hr; hr(mything)", and (b) code can override +# humanreadable to provide application-specific human readable output +# (e.g. libbase32's base32id.AbbrevRepr). +brepr = BetterRepr() + +def hr(x): + return brepr.repr(x) diff --git a/amdlib/__init__.py b/amdlib/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/amdlib/util/__init__.py b/amdlib/util/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/amdlib/util/assertutil.py b/amdlib/util/assertutil.py deleted file mode 100644 index 2adae8d6..00000000 --- a/amdlib/util/assertutil.py +++ /dev/null @@ -1,82 +0,0 @@ -# Copyright (c) 2003-2006 Bryce "Zooko" Wilcox-O'Hearn -# mailto:zooko@zooko.com -# http://zooko.com/ -# Permission is hereby granted, free of charge, to any person obtaining a -# copy of this work to deal in this work without restriction (including the -# rights to use, modify, distribute, sublicense, and/or sell copies) - -""" -Tests useful in assertion checking, prints out nicely formated messages too. -""" - -from humanreadable import hr - -from twisted.python import log - -def _assert(___cond=False, *___args, **___kwargs): - if ___cond: - return True - msgbuf=[] - if ___args: - msgbuf.append("%s %s" % tuple(map(hr, (___args[0], type(___args[0]),)))) - msgbuf.extend([", %s %s" % tuple(map(hr, (arg, type(arg),))) for arg in ___args[1:]]) - if ___kwargs: - msgbuf.append(", %s: %s %s" % ((___kwargs.items()[0][0],) + tuple(map(hr, (___kwargs.items()[0][1], type(___kwargs.items()[0][1]),))))) - else: - if ___kwargs: - msgbuf.append("%s: %s %s" % ((___kwargs.items()[0][0],) + tuple(map(hr, (___kwargs.items()[0][1], type(___kwargs.items()[0][1]),))))) - msgbuf.extend([", %s: %s %s" % tuple(map(hr, (k, v, type(v),))) for k, v in ___kwargs.items()[1:]]) - - raise AssertionError, "".join(msgbuf) - - return False - -def precondition(___cond=False, *___args, **___kwargs): - try: - if ___cond: - return True - msgbuf=["precondition", ] - if ___args or ___kwargs: - msgbuf.append(": ") - if ___args: - msgbuf.append("%s %s" % tuple(map(hr, (___args[0], type(___args[0]),)))) - msgbuf.extend([", %s %s" % tuple(map(hr, (arg, type(arg),))) for arg in ___args[1:]]) - if ___kwargs: - msgbuf.append(", %s: %s %s" % ((___kwargs.items()[0][0],) + tuple(map(hr, (___kwargs.items()[0][1], type(___kwargs.items()[0][1]),))))) - else: - if ___kwargs: - msgbuf.append("%s: %s %s" % ((___kwargs.items()[0][0],) + tuple(map(hr, (___kwargs.items()[0][1], type(___kwargs.items()[0][1]),))))) - msgbuf.extend([", %s: %s %s" % tuple(map(hr, (k, v, type(v),))) for k, v in ___kwargs.items()[1:]]) - except Exception, le: - log.msg("assertutil.precondition(): INTERNAL ERROR IN pyutil.assertutil. %s %s %s" % (type(le), repr(le), le.args,)) - log.err() - raise le - except: - log.msg("assertutil.precondition(): INTERNAL ERROR IN pyutil.assertutil.") - log.err() - raise - - raise AssertionError, "".join(msgbuf) - - return False - -def postcondition(___cond=False, *___args, **___kwargs): - if ___cond: - return True - msgbuf=["postcondition", ] - if ___args or ___kwargs: - msgbuf.append(": ") - if ___args: - msgbuf.append("%s %s" % tuple(map(hr, (___args[0], type(___args[0]),)))) - msgbuf.extend([", %s %s" % tuple(map(hr, (arg, type(arg),))) for arg in ___args[1:]]) - if ___kwargs: - msgbuf.append(", %s: %s %s" % ((___kwargs.items()[0][0],) + tuple(map(hr, (___kwargs.items()[0][1], type(___kwargs.items()[0][1]),))))) - else: - if ___kwargs: - msgbuf.append("%s: %s %s" % ((___kwargs.items()[0][0],) + tuple(map(hr, (___kwargs.items()[0][1], type(___kwargs.items()[0][1]),))))) - msgbuf.extend([", %s: %s %s" % tuple(map(hr, (k, v, type(v),))) for k, v in ___kwargs.items()[1:]]) - - raise AssertionError, "".join(msgbuf) - - return False - diff --git a/amdlib/util/humanreadable.py b/amdlib/util/humanreadable.py deleted file mode 100644 index bae826c5..00000000 --- a/amdlib/util/humanreadable.py +++ /dev/null @@ -1,128 +0,0 @@ -# Copyright (c) 2001 Autonomous Zone Industries -# Copyright (c) 2002-2005 Bryce "Zooko" Wilcox-O'Hearn -# This file is licensed under the -# GNU Lesser General Public License v2.1. -# See the file COPYING or visit http://www.gnu.org/ for details. - -import exceptions, os -from repr import Repr - -class BetterRepr(Repr): - def __init__(self): - Repr.__init__(self) - - # Note: These levels can get adjusted dynamically! My goal is to get - # more info when printing important debug stuff like exceptions and - # stack traces and less info when logging normal events. - # --Zooko 2000-10-14 - if 0: # DEBUG MODE - self.maxlevel = 24 - self.maxdict = 24 - self.maxlist = 24 - self.maxtuple = 24 - self.maxstring = 800 - self.maxother = 800 - else: - self.maxlevel = 6 - self.maxdict = 6 - self.maxlist = 6 - self.maxtuple = 6 - self.maxstring = 300 - self.maxother = 300 - - def repr_function(self, obj, level): - if hasattr(obj, 'func_code'): - return '<' + obj.func_name + '() at ' + os.path.basename(obj.func_code.co_filename) + ':' + str(obj.func_code.co_firstlineno) + '>' - else: - return '<' + obj.func_name + '() at (builtin)' - - def repr_instance_method(self, obj, level): - if hasattr(obj, 'func_code'): - return '<' + obj.im_class.__name__ + '.' + obj.im_func.__name__ + '() at ' + os.path.basename(obj.im_func.func_code.co_filename) + ':' + str(obj.im_func.func_code.co_firstlineno) + '>' - else: - return '<' + obj.im_class.__name__ + '.' + obj.im_func.__name__ + '() at (builtin)' - - def repr_long(self, obj, level): - s = `obj` # XXX Hope this isn't too slow... - if len(s) > self.maxlong: - i = max(0, (self.maxlong-3)/2) - j = max(0, self.maxlong-3-i) - s = s[:i] + '...' + s[len(s)-j:] - if s[-1] == 'L': - return s[:-1] - return s - - def repr_instance(self, obj, level): - """ - If it is an instance of Exception, format it nicely (trying to emulate - the format that you see when an exception is actually raised, plus - bracketing '<''s). If it is an instance of dict call self.repr_dict() - on it. If it is an instance of list call self.repr_list() on it. Else - call Repr.repr_instance(). - """ - if isinstance(obj, exceptions.Exception): - # Don't cut down exception strings so much. - tms = self.maxstring - self.maxstring = max(512, tms * 4) - tml = self.maxlist - self.maxlist = max(12, tml * 4) - try: - if hasattr(obj, 'args'): - if len(obj.args) == 1: - return '<' + obj.__class__.__name__ + ': ' + self.repr1(obj.args[0], level-1) + '>' - else: - return '<' + obj.__class__.__name__ + ': ' + self.repr1(obj.args, level-1) + '>' - else: - return '<' + obj.__class__.__name__ + '>' - finally: - self.maxstring = tms - self.maxlist = tml - - if isinstance(obj, dict): - return self.repr_dict(obj, level) - - if isinstance(obj, list): - return self.repr_list(obj, level) - - return Repr.repr_instance(self, obj, level) - - def repr_list(self, obj, level): - """ - copied from standard repr.py and fixed to work on multithreadedly mutating lists. - """ - if level <= 0: return '[...]' - n = len(obj) - myl = obj[:min(n, self.maxlist)] - s = '' - for item in myl: - entry = self.repr1(item, level-1) - if s: s = s + ', ' - s = s + entry - if n > self.maxlist: s = s + ', ...' - return '[' + s + ']' - - def repr_dict(self, obj, level): - """ - copied from standard repr.py and fixed to work on multithreadedly mutating dicts. - """ - if level <= 0: return '{...}' - s = '' - n = len(obj) - items = obj.items()[:min(n, self.maxdict)] - items.sort() - for key, val in items: - entry = self.repr1(key, level-1) + ':' + self.repr1(val, level-1) - if s: s = s + ', ' - s = s + entry - if n > self.maxdict: s = s + ', ...' - return '{' + s + '}' - -# This object can be changed by other code updating this module's "brepr" -# variables. This is so that (a) code can use humanreadable with -# "from humanreadable import hr; hr(mything)", and (b) code can override -# humanreadable to provide application-specific human readable output -# (e.g. libbase32's base32id.AbbrevRepr). -brepr = BetterRepr() - -def hr(x): - return brepr.repr(x)