* Moved utility functions into utils/ directory, and started breaking out

utilities into separate files.
 * Added a log() function in ietf/utils.  It uses syslog, but adds some
   information about where it was called from.
 - Legacy-Id: 130
This commit is contained in:
Henrik Levkowetz 2007-05-11 15:48:45 +00:00
parent 5315578c5d
commit cf20093762
4 changed files with 51 additions and 13 deletions

2
ietf/utils/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/*.pyc
/*.swp

View file

@ -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):

13
ietf/utils/listop.py Normal file
View file

@ -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)

33
ietf/utils/log.py Normal file
View file

@ -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 == "<module>":
where = ""
else:
where = " in " + func + "()"
syslog.syslog("ietf%s(%d)%s: %s" % (file, line, where, msg))
log("IETFdb v%s started" % ietf.__version__)