diff options
author | Rafael Ortiz <rafael@activitycentral.com> | 2012-04-24 16:00:36 (GMT) |
---|---|---|
committer | Rafael Ortiz <rafael@activitycentral.com> | 2012-04-24 16:00:36 (GMT) |
commit | 55b7166264ea7950e30e87ae5ffc796ba32ffe62 (patch) | |
tree | 818c199de01909385c2f37e092067db904efc0c5 | |
parent | 3bf48373789fdcd44bef3bbffc44b1d561463a00 (diff) |
style fixes
-rw-r--r-- | store.py | 66 |
1 files changed, 35 insertions, 31 deletions
@@ -15,6 +15,7 @@ import shelve # for future releases #from xml.dom import minidom + class Db: def __init__(self, db_filename=None): init_db = """ @@ -22,20 +23,20 @@ CREATE TABLE IF NOT EXISTS notifications (id INTEGER PRIMARY KEY,title VARCHAR(3 if db_filename: self._db_filename = db_filename else: - self._db_filename = os.path.join(env.get_data_root(),"messages.db") + self._db_filename = os.path.join(env.get_data_root(), "messages.db") conn = self._connect() c = conn.cursor() c.executescript(init_db) self._close_connection(conn) - def dict_factory(self,cursor, row): + def dict_factory(self, cursor, row): d = {} for idx, col in enumerate(cursor.description): d[col[0]] = row[idx] return d def _connect(self): - conn = sqlite3.connect(self._db_filename,isolation_level=None) + conn = sqlite3.connect(self._db_filename, isolation_level=None) conn.text_factory = str return conn @@ -48,9 +49,9 @@ CREATE TABLE IF NOT EXISTS notifications (id INTEGER PRIMARY KEY,title VARCHAR(3 c = con.cursor() c.execute('BEGIN TRANSACTION') try: - c.execute('insert into notifications(%s,%s,%s,%s,%s,%s,%s,%s) values (?,?,?,?,?,?,?,?)' %tuple(keys), values) - except sqlite3.IntegrityError,e: - print "Error al insertar datos: %s" %str(e) + c.execute('insert into notifications(%s,%s,%s,%s,%s,%s,%s,%s) values (?,?,?,?,?,?,?,?)' % tuple(keys), values) + except sqlite3.IntegrityError, e: + print "Error al insertar datos: %s" % str(e) c.execute('END TRANSACTION') self._close_connection(con) @@ -60,9 +61,9 @@ CREATE TABLE IF NOT EXISTS notifications (id INTEGER PRIMARY KEY,title VARCHAR(3 c = con.cursor() c.execute('BEGIN TRANSACTION') try: - c.execute('delete from notifications where id=%i' %id) - except sqlite3.IntegrityError,e: - print "Error al eliminar datos: %s" %str(e) + c.execute('delete from notifications where id=%i' % id) + except sqlite3.IntegrityError, e: + print "Error al eliminar datos: %s" % str(e) c.execute('END TRANSACTION') self._close_connection(con) @@ -73,15 +74,15 @@ CREATE TABLE IF NOT EXISTS notifications (id INTEGER PRIMARY KEY,title VARCHAR(3 c = con.cursor() c.execute('BEGIN TRANSACTION') try: - c.execute('update notifications set fav=%i where id=%i' %(int(fav),id)) - except sqlite3.IntegrityError,e: - print "Error al eliminar datos: %s" %str(e) + c.execute('update notifications set fav=%i where id=%i' % (int(fav), id)) + except sqlite3.IntegrityError, e: + print "Error al eliminar datos: %s" % str(e) c.execute('END TRANSACTION') self._close_connection(con) def is_fav(self, id_msg): - fav = self.run_query("select fav from notifications where id=%i" %id_msg) - return fav[0]['fav'] + fav = self.run_query("select fav from notifications where id=%i" % id_msg) + return fav[0]['fav'] def run_query(self, query): """ @@ -105,12 +106,12 @@ CREATE TABLE IF NOT EXISTS notifications (id INTEGER PRIMARY KEY,title VARCHAR(3 if args: filters = 'where ' while args: - clave,valor = args.popitem() - filters+=clave+' '+str(valor) - filters+=' and ' if args else '' + clave, valor = args.popitem() + filters += clave + ' ' + str(valor) + filters += ' and ' if args else '' c = con.cursor() c.execute('BEGIN TRANSACTION') - c.execute('SELECT id, priority, title, text, launched, expires, type, fav FROM notifications %s order by priority desc;' %filters) + c.execute('SELECT id, priority, title, text, launched, expires, type, fav FROM notifications %s order by priority desc;' % filters) output = c.fetchall() c.execute('END TRANSACTION') self._close_connection(con) @@ -129,15 +130,17 @@ CREATE TABLE IF NOT EXISTS notifications (id INTEGER PRIMARY KEY,title VARCHAR(3 return output """ + class Store: + def __init__(self, xmlpath=None, db_filename=None): self.db = Db(db_filename) # Borro todas las notificaciones que ya expiraron # Para el futuro puede interesar tener un hisórico, habría que cambiar esto - today = datetime.datetime.strftime(datetime.date.today(), "%Y-%m-%d") - query = "delete from notifications where expires < %s;" %today + today = datetime.datetime.strftime(datetime.date.today(), "%Y-%m-%d") + query = "delete from notifications where expires < %s;" % today self.db.run_query(query) - # Implementado para futuras versiones + # Implementado para futuras versiones """ if not xmlpath: xmlpath = os.path.join(env.get_data_root(),"mensajes.xml") @@ -146,27 +149,28 @@ class Store: fsock.close() self.msg_list = self.xmldoc.getElementsByTagName("message") """ - # por el momento se implementa un parseo de los archivos shelve - # de texto que se encuentren en /etc/notifier/data/tmp/ + # por el momento se implementa un parseo de los archivos shelve + # de texto que se encuentren en /etc/notifier/data/tmp/ # obtengo todos los archivos que comiencen con 'notify_' - xmlpath = '/tmp' # os.path.join(env.get_data_root(),'tmp') - files = filter(lambda x: x.startswith("notify_"),os.listdir(xmlpath)) + xmlpath = '/tmp' # os.path.join(env.get_data_root(),'tmp') + files = filter(lambda x: x.startswith("notify_"), os.listdir(xmlpath)) self.msg_list = {} #msg_items = ['id', 'title', 'text', 'launched', 'expired', 'priority',] for file in files: - abspath = os.path.join(xmlpath,file) + abspath = os.path.join(xmlpath, file) try: f = shelve.open(abspath) self._save_message(f) f.close() os.remove(abspath) - except: pass + except: + pass #for msg in self.msg_list: # self._save_message(msg) def _save_message(self, msg): """Procesa los mensajes que vienen en el diccionario msg""" - print "procesando nodo %s" %msg["id"] + print "procesando nodo %s" % msg["id"] keys = msg.keys() values = [] for item in keys: @@ -176,11 +180,11 @@ class Store: def _save_XML_message(self, msg): # For future releases #TODO: Poder leer links html en el campo text - print "procesando nodo %s" %msg.getAttribute("id") + print "procesando nodo %s" % msg.getAttribute("id") refNode = msg.childNodes - keys = ["id","type","priority"] + keys = ["id", "type", "priority"] values = [] - map(lambda x: values.append(msg.getAttribute(x)),keys) + map(lambda x: values.append(msg.getAttribute(x)), keys) for node in refNode: if node.nodeType == 1: #print "clave: %s, valor: %s" % (node.localName, node.firstChild.data) |