2011-10-08 20:10:10 +02:00
|
|
|
import os
|
|
|
|
import tempfile
|
|
|
|
|
2011-10-11 17:16:07 +02:00
|
|
|
db_encoding = 'utf8'
|
|
|
|
|
|
|
|
def value_to_db(value):
|
|
|
|
if type(value) == unicode:
|
|
|
|
return value.encode(db_encoding)
|
|
|
|
return value
|
|
|
|
|
|
|
|
def value_from_db(value):
|
|
|
|
if type(value) == str:
|
|
|
|
return unicode(value, db_encoding)
|
|
|
|
return value
|
|
|
|
|
2011-10-08 21:20:21 +02:00
|
|
|
def execute_query(cursor, query, bindings):
|
|
|
|
for (key, val) in bindings.items():
|
|
|
|
if val == None:
|
|
|
|
query = query.replace('%(' + key + ')d', 'NULL')
|
2011-10-11 17:16:07 +02:00
|
|
|
bindings = map_dict(value_to_db, bindings)
|
2011-10-08 21:20:21 +02:00
|
|
|
cursor.execute(query, bindings)
|
|
|
|
|
2011-10-11 17:16:07 +02:00
|
|
|
def fetchone(cursor):
|
|
|
|
return map(value_from_db, cursor.fetchone())
|
|
|
|
|
|
|
|
def fetchall(cursor):
|
|
|
|
return map(lambda row: map(value_from_db, row),
|
|
|
|
cursor.fetchall())
|
|
|
|
|
2011-10-08 11:06:15 +02:00
|
|
|
def make_result_dict(cursor, row):
|
|
|
|
d = {}
|
|
|
|
for i in xrange(len(row)):
|
|
|
|
d[cursor.description[i][0]] = row[i]
|
|
|
|
return d
|
|
|
|
|
|
|
|
def fetchone_dict(cursor):
|
2011-10-11 17:16:07 +02:00
|
|
|
row = fetchone(cursor)
|
2011-10-08 11:06:15 +02:00
|
|
|
if row != None:
|
|
|
|
return make_result_dict(cursor, row)
|
|
|
|
return None
|
|
|
|
|
|
|
|
def fetchall_dict(cursor):
|
|
|
|
return map(lambda r: make_result_dict(cursor, r),
|
2011-10-11 17:16:07 +02:00
|
|
|
fetchall(cursor))
|
2011-10-08 11:06:15 +02:00
|
|
|
|
|
|
|
def first(lst):
|
|
|
|
return lst[0]
|
|
|
|
|
|
|
|
def second(lst):
|
|
|
|
return lst[1]
|
|
|
|
|
|
|
|
def count(predicate, lst):
|
|
|
|
c = 0
|
|
|
|
for elem in lst:
|
|
|
|
if predicate(elem):
|
|
|
|
c = c+1
|
|
|
|
return c
|
|
|
|
|
|
|
|
def find(predicate, lst):
|
|
|
|
for elem in lst:
|
|
|
|
if predicate(elem):
|
|
|
|
return elem
|
|
|
|
return None
|
|
|
|
|
|
|
|
def unique(lst):
|
|
|
|
newlst = []
|
|
|
|
for elem in lst:
|
|
|
|
if elem not in newlst:
|
|
|
|
newlst.append(elem)
|
|
|
|
return newlst
|
|
|
|
|
|
|
|
def mapcond(fun, predicate, lst):
|
|
|
|
def mapfun(x):
|
|
|
|
if predicate(x):
|
|
|
|
return fun(x)
|
|
|
|
return x
|
|
|
|
return map(mapfun, lst)
|
|
|
|
|
2011-10-11 17:16:07 +02:00
|
|
|
def map_dict(fun, d):
|
|
|
|
res = {}
|
|
|
|
for key in d:
|
|
|
|
res[key] = fun(d[key])
|
|
|
|
return res
|
|
|
|
|
2011-10-08 11:06:15 +02:00
|
|
|
def maptup(fun, lst):
|
|
|
|
return tuple(map(fun, lst))
|
|
|
|
|
|
|
|
def translate(value, translations):
|
|
|
|
return translations.get(value, value)
|
|
|
|
|
2011-10-08 13:37:37 +02:00
|
|
|
def str_or_empty(value):
|
|
|
|
if value:
|
|
|
|
return str(value)
|
|
|
|
return ''
|
|
|
|
|
2011-10-08 11:06:15 +02:00
|
|
|
def p(s):
|
|
|
|
encoded = s
|
|
|
|
if isinstance(s, unicode):
|
|
|
|
encoded = s.encode('utf8')
|
|
|
|
print encoded
|
|
|
|
|
|
|
|
def cut_str(string, length, ellipsis='...'):
|
|
|
|
if len(string) < length:
|
|
|
|
return string
|
|
|
|
return string[0:length-len(ellipsis)]+ellipsis
|
2011-10-08 12:13:32 +02:00
|
|
|
|
|
|
|
def combine_dicts(*dicts):
|
|
|
|
res = {}
|
|
|
|
for d in dicts:
|
|
|
|
res.update(d)
|
|
|
|
return res
|
|
|
|
|
|
|
|
def run_editor(filename):
|
|
|
|
if os.path.exists(filename):
|
|
|
|
os.system("%s %s || /usr/bin/env vi %s" %
|
|
|
|
(os.getenv("EDITOR"), filename, filename))
|
|
|
|
else:
|
|
|
|
exit("Error: %s: File does not exist!" % filename)
|
|
|
|
|
2011-10-08 20:10:10 +02:00
|
|
|
def write_tmpfile(pfix, content, encoding='utf8'):
|
2011-10-08 12:13:32 +02:00
|
|
|
file = tempfile.NamedTemporaryFile(prefix=pfix+'-', dir='/tmp', delete=False)
|
2011-10-08 20:10:10 +02:00
|
|
|
file.write(content.encode(encoding))
|
2011-10-08 12:13:32 +02:00
|
|
|
name = file.name
|
|
|
|
file.close()
|
|
|
|
return name
|