* 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:
parent
5315578c5d
commit
cf20093762
2
ietf/utils/.gitignore
vendored
Normal file
2
ietf/utils/.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
/*.pyc
|
||||||
|
/*.swp
|
|
@ -1,5 +1,6 @@
|
||||||
import operator
|
from listop import orl, flattenl
|
||||||
import syslog
|
from log import log
|
||||||
|
|
||||||
from django.utils.html import escape
|
from django.utils.html import escape
|
||||||
# look at snippets 59, 148, 99 for newforms helpers
|
# look at snippets 59, 148, 99 for newforms helpers
|
||||||
|
|
||||||
|
@ -51,17 +52,6 @@ class FKAsOneToOne(object):
|
||||||
setattr(instance, self.field, value)
|
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):
|
def split_form(html, blocks):
|
13
ietf/utils/listop.py
Normal file
13
ietf/utils/listop.py
Normal 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
33
ietf/utils/log.py
Normal 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__)
|
Loading…
Reference in a new issue