diff --git a/ietf/utils/.gitignore b/ietf/utils/.gitignore new file mode 100644 index 000000000..ba8bdb3af --- /dev/null +++ b/ietf/utils/.gitignore @@ -0,0 +1,2 @@ +/*.pyc +/*.swp diff --git a/ietf/utils.py b/ietf/utils/__init__.py similarity index 94% rename from ietf/utils.py rename to ietf/utils/__init__.py index a292718c9..d497ef81a 100644 --- a/ietf/utils.py +++ b/ietf/utils/__init__.py @@ -1,5 +1,6 @@ -import operator -import syslog +from listop import orl, flattenl +from log import log + from django.utils.html import escape # look at snippets 59, 148, 99 for newforms helpers @@ -51,17 +52,6 @@ class FKAsOneToOne(object): setattr(instance, self.field, value) -def orl(list): - """ Return the "or" of every element in a list. - Used to generate "or" queries with a list of Q objects. """ - return reduce(operator.__or__, list) - -def flattenl(list): - """ Flatten a list one level, e.g., turn - [ ['a'], ['b'], ['c', 'd'] ] into - [ 'a', 'b', 'c', 'd' ] - """ - return reduce(operator.__concat__, list) def split_form(html, blocks): diff --git a/ietf/utils/listop.py b/ietf/utils/listop.py new file mode 100644 index 000000000..7a446969e --- /dev/null +++ b/ietf/utils/listop.py @@ -0,0 +1,13 @@ +import operator + +def orl(list): + """ Return the "or" of every element in a list. + Used to generate "or" queries with a list of Q objects. """ + return reduce(operator.__or__, list) + +def flattenl(list): + """ Flatten a list one level, e.g., turn + [ ['a'], ['b'], ['c', 'd'] ] into + [ 'a', 'b', 'c', 'd' ] + """ + return reduce(operator.__concat__, list) diff --git a/ietf/utils/log.py b/ietf/utils/log.py new file mode 100644 index 000000000..e74b27908 --- /dev/null +++ b/ietf/utils/log.py @@ -0,0 +1,33 @@ +import syslog +import inspect +import os.path +import ietf +from settings import BASE_DIR + +syslog.openlog("django", syslog.LOG_PID, syslog.LOG_USER) + +def getclass(frame): + cls = None + argnames, varargs, varkw, defaults = inspect.getargvalues(frame) + if len(argnames) > 0: + selfname = argnames[0] + cls = defaults[selfname].__class__ + return cls + +def getcaller(): + parent, pfile, pline, pfunction, lines, index = inspect.stack()[2] + pmodule = inspect.getmoduleinfo(pfile)[0] + pclass = getclass(parent) + return (pmodule, pclass, pfunction, pfile, pline) + +def log(msg): + mod, cls, func, file, line = getcaller() + file = os.path.abspath(file) + file = file.replace(BASE_DIR, "") + if func == "": + where = "" + else: + where = " in " + func + "()" + syslog.syslog("ietf%s(%d)%s: %s" % (file, line, where, msg)) + +log("IETFdb v%s started" % ietf.__version__) \ No newline at end of file