From 51a3c373c56b088922a5d6bcec10e56b378a9e2f Mon Sep 17 00:00:00 2001 From: Henrik Levkowetz Date: Mon, 11 Jun 2012 15:56:51 +0000 Subject: [PATCH] Added a debug utility module on the same level as south,html5lib etc. - Legacy-Id: 4446 --- debug.py | 85 +++++++++++++++++++++++++++++++++++++++++++ ietf/person/models.py | 2 + 2 files changed, 87 insertions(+) create mode 100644 debug.py diff --git a/debug.py b/debug.py new file mode 100644 index 000000000..0aa674421 --- /dev/null +++ b/debug.py @@ -0,0 +1,85 @@ + +import sys +import inspect +import syslog +from pprint import pformat + +# A debug decorator, written by Paul Butler, taken from +# http://paulbutler.org/archives/python-debugging-with-decorators/ +# Additional functions and decorator functionality added by +# Henrik Levkowetz + +# Number of times to indent output +# A list is used to force access by reference +__report_indent = [4] +increment = 2 +debug = True +syslog.openlog("debug", syslog.LOG_PID, syslog.LOG_USER) + +def set_indent(i): + __report_indent[0] = i + +def trace(fn): # renamed from 'report' by henrik 16 Jun 2011 + """Decorator to print information about a function + call for use while debugging. + Prints function name, arguments, and call number + when the function is called. Prints this information + again along with the return value when the function + returns. + """ + def fix(s,n=32): + if len(s) > n+3: + s = s[:n]+"..." + s = s.replace('\n',' ') + return s + def wrap(*params,**kwargs): + call = wrap.callcount = wrap.callcount + 1 + + indent = ' ' * __report_indent[0] + fc = "%s(%s)" % (fn.__name__, ', '.join( + [fix(repr(a)) for a in params] + + ["%s = %s" % (a, fix(repr(b))) for a,b in kwargs.items()] + )) + + print "%s* %s [#%s]" % (indent, fc, call) + __report_indent[0] += increment + ret = fn(*params,**kwargs) + __report_indent[0] -= increment + sys.stderr.write( "%s %s [#%s] ==> %s\n" % (indent, fc, call, repr(ret))) + + return ret + wrap.callcount = 0 + if debug: + return wrap + else: + return fn + +def show(name): + if debug: + frame = inspect.stack()[1][0] + value = eval(name, frame.f_globals, frame.f_locals) + indent = ' ' * (__report_indent[0]) + sys.stderr.write("%s%s: %s\n" % (indent, name, value)) + +def log(name): + if debug: + frame = inspect.stack()[1][0] + value = eval(name, frame.f_globals, frame.f_locals) + indent = ' ' * (__report_indent[0]) + syslog.syslog("%s%s: %s" % (indent, name, value)) + +def pprint(name): + if debug: + frame = inspect.stack()[1][0] + value = eval(name, frame.f_globals, frame.f_locals) + indent = ' ' * (__report_indent[0]) + sys.stdout.write("%s%s:\n" % (indent, name)) + lines = pformat(value).split('\n') + for line in lines: + sys.stdout.write("%s %s\n"%(indent, line)) + +def say(s): + if debug: + indent = ' ' * (__report_indent[0]) + sys.stderr.write("%s%s\n" % (indent, s)) + diff --git a/ietf/person/models.py b/ietf/person/models.py index ab8d1d205..27acdc5a8 100644 --- a/ietf/person/models.py +++ b/ietf/person/models.py @@ -29,6 +29,8 @@ class PersonInfo(models.Model): prefix, first, middle, last, suffix = self.ascii_parts() return (first and first[0]+"." or "")+(middle or "")+" "+last+(suffix and " "+suffix or "") def plain_name(self): + if self.ascii_short: + return self.ascii_short prefix, first, middle, last, suffix = name_parts(self.name) return u" ".join([first, last]) def last_name(self):