# HG changeset patch # User Björn Ricks # Date 1393843057 -3600 # Node ID 328ec66517b06945369537c12ae55bdbfcc82dae # Parent 3a17869476731251c0179f07fcf8ec3cf5f4e7e6 Fix coding style for pep8 diff -r 3a1786947673 -r 328ec66517b0 getan/backend.py --- a/getan/backend.py Mon Mar 03 11:34:31 2014 +0100 +++ b/getan/backend.py Mon Mar 03 11:37:37 2014 +0100 @@ -22,7 +22,7 @@ DEFAULT_DATABASE = "time.db" CREATE_TABLES = [ -""" + """ CREATE TABLE projects ( id INTEGER PRIMARY KEY AUTOINCREMENT, key VARCHAR(16) NOT NULL CONSTRAINT unique_key UNIQUE, @@ -30,7 +30,7 @@ active BOOLEAN DEFAULT 1 ) """, -""" + """ CREATE TABLE entries ( id INTEGER PRIMARY KEY AUTOINCREMENT, project_id INTEGER REFERENCES projects(id), @@ -89,27 +89,29 @@ logger = logging.getLogger() + class InvalidProjectKeyError(Exception): pass + class Backend(object): - def __init__(self, database = DEFAULT_DATABASE): + def __init__(self, database=DEFAULT_DATABASE): self.database = database self.ensure_exists() - self.con = db.connect(database, - detect_types=db.PARSE_DECLTYPES | - db.PARSE_COLNAMES) + self.con = db.connect(database, + detect_types=db.PARSE_DECLTYPES | + db.PARSE_COLNAMES) self.con.text_factory = lambda x: unicode(x, "utf-8", "ignore") - def ensure_exists(self): """ Creates the database file if it does not exist. """ - if os.path.isfile(self.database): return + if os.path.isfile(self.database): + return con, cur = None, None try: - con = db.connect(self.database); + con = db.connect(self.database) cur = con.cursor() try: for sql in CREATE_TABLES: @@ -119,15 +121,16 @@ con.rollback() raise finally: - if cur: cur.close() - if con: con.close() - + if cur: + cur.close() + if con: + con.close() def load_projects(self): """ Loads active projects from database and returns them as array """ logger.debug("load active projects from database.") cur = None - try : + try: cur = self.con.cursor() cur.execute(LOAD_ACTIVE_PROJECTS) @@ -139,8 +142,8 @@ break # check key if not row[1]: - raise InvalidProjectKeyError("Project with id %s needs " \ - "a key" % row[0]) + raise InvalidProjectKeyError("Project with id %s needs " + "a key" % row[0]) proj = Project(*row) proj.entries = self.load_entries(proj.id) projects.append(proj) @@ -151,7 +154,6 @@ finally: close(cur) - def load_entries(self, project_id): """ Loads all entries that belong to a specific project """ logger.debug("load entries that belong to project %s" % project_id) @@ -174,7 +176,6 @@ finally: close(cur) - def insert_project_entry(self, project, stop_time, desc): if project is None: return @@ -217,7 +218,6 @@ finally: close(cur) - def move_entries(self, entries, new_project_id): if entries is None or new_project_id is None: return @@ -253,13 +253,15 @@ cur = None try: cur = self.con.cursor() - cur.execute(UPDATE_PROJECT, (project.key, project.desc, project.id)) + cur.execute( + UPDATE_PROJECT, (project.key, project.desc, project.id)) self.con.commit() logger.debug("Updated project: (%d) %s %s" % (project.id, - project.key, project.desc)) + project.key, project.desc)) finally: close(cur) + def close(cur): """ This function closes a database cursor if it is existing """ if cur: