Permalink
Browse files

Making it run ...

  • Loading branch information...
ik5 committed Feb 5, 2013
1 parent 40c1b16 commit b8a294967964b942af26a02bc4b2da82b6a21475
Showing with 50 additions and 9 deletions.
  1. +50 −9 convert_quotes.py
View
@@ -61,18 +61,18 @@ def finalize(db_connection, logger=LOGGER) :
logger.error('The database connection is already closed')
-def iter_quotes(quotes_file = QUOTES_FILE) :
+def iter_quotes(quotes_file = QUOTES_FILE, logger = LOGGER) :
"Walks over the quotes file, yields (quote, author) tuples for each quote"
with open(quotes_file) as f:
quote = []
for line in f:
- if line != SEPARATOR :
- if line.strip :
+ if line != SEPARATOR : # not end of quote
+ if line.strip : # not and empty line
quote.append(line)
- else :
+ else : # end of quote
if not quote : # quote is empty ?
continue
@@ -81,40 +81,81 @@ def iter_quotes(quotes_file = QUOTES_FILE) :
author = quote.pop().strip()
else : # mark it as none it it was not found
author = None
-
- yield ''.join(quote).rstrip(), author
+
+ str_quote = ''.join(quote).rstrip()
+ if author :
+ logger.debug('About to work on quote "%s" by "%s"',
+ (str_quote, author)
+ )
+ else :
+ logger.debug('About to work on quote "%s" with no author',
+ str_quote
+ )
+
+ # extract it for external handler
+ yield str_quote, author
quote = []
def insert_to_db(con, quote, author, logger=LOGGER) :
cursor = con.cursor()
author_id = None
if author :
try :
+ logger.debug('About to insert author (%s) into the db', author)
cursor.execute('insert into quote_authors(AUTHOR) values(?)',
author)
except fdb.DatabaseError as e :
# usually means that the author already exists ...
logger.info('Could not insert author (%s): %s', (author, e))
try :
+ logger.debug('About to get the author (%s) id', author)
cursor.execute('select id from quote_authors where author=?',
author)
author_id = cursor.fetchone['id']
+ logger.debug('Author (%s) id : %d', (author, author_id))
except fdb.DatabaseError as e : # could not get the author id
logger.info('Could not find author (%s): %s', (author, e))
+ else:
+ logger.debug('Author is not set')
try :
+ logger.debug('Going to insert quote ("%s") to db', quote)
cursor.execute(('insert into quotes(body, author_ref) '
- 'values(?, ?)'), quote, author)
+ 'values(?, ?)'), (quote, author))
+ logger.debug('Going to commit everything')
con.commit()
except fdb.DatabaseError as e : # could not execute query or commit
logger.error('Could not insert quote: %s', e)
+ con.rollback()
return False
return True
+def run(con, logger = LOGGER) :
+ LOGGER.debug('Starting to parse file:')
+ for quote, author in iter_quotes():
+ insert_to_db(con, quote, author)
+
+ LOGGER.debug('Done parsing file')
+
+
+if __name__ == '__main__' :
+ if not os.path.exists(QUOTES_FILE) :
+ LOGGER.critical('The file %s was not found', QUOTES_FILE)
+ sys.exit('Error: The file {0} was not found'.format(QUOTES_FILE))
+ else :
+ LOGGER.debug('Found the quote file')
+
+ LOGGER.debug('Starting the initialization ...')
+ con = init_db()
+ atexit.register(finalize, con)
+
+ try :
+ run()
+ except Exception as e :
+ LOGGER.critical('Unexpected exception was raised: %s', e)
+ finalize(con)
-if __name__ == '__main__':
- pass

0 comments on commit b8a2949

Please sign in to comment.