-
Notifications
You must be signed in to change notification settings - Fork 8
/
convert_quotes.py
executable file
·179 lines (141 loc) · 5.31 KB
/
convert_quotes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env python2
#
#
#
import os
import sys
import fdb
import logging
import atexit
def init_logger() :
"""initialize the logger"""
#
# readability over everything else !
# http://www.python.org/dev/peps/pep-0008/#pet-peeves <- Really ?
# I'm an Object Pascal developer, and that's how we do things,
# even our editors/ide support this :P
#
logger = logging.getLogger('convert_quotes')
# place output to file
handler = logging.FileHandler('log/convert.log')
formatter = logging.Formatter(('[%(asctime)s - %(levelname)s] '
'%(message)s'))
handler.setLevel(logging.DEBUG)
handler.setFormatter(formatter)
logger.addHandler(handler)
# place output to screen
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
ch_format = logging.Formatter('%(levelname)s %(message)s')
ch.setFormatter(ch_format)
logger.addHandler(ch)
return logger
LOGGER = init_logger()
LOGGER.info('Entering convert_quotes.py')
QUOTES_FILE = 'quotes.txt'
SEPARATOR = '----\n'
AUTHOR_MARK = ' '
def init_db(logger = LOGGER) :
"""initialize the database connection
raised an fdb.DatabaseError if database error"""
try :
con = fdb.connect(
dsn = '127.0.0.1:quotes',
user = 'sysdba',
password = 'masterkey'
)
logger.info('Connected to the database')
except fdb.DatabaseError as e :
logger.critical('Database connection error: %s', e)
raise e
return con
def finalize(db_connection, logger=LOGGER) :
"""close the database connection"""
if not db_connection.closed :
logger.info('Closing database connection')
db_connection.close
else :
logger.error('The database connection is already closed')
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 : # not end of quote
if line.strip : # not and empty line
quote.append(line)
else : # end of quote
if not quote : # quote is empty ?
continue
# try to extract the author if exists
if quote[-1].startswith(AUTHOR_MARK) :
author = quote.pop().strip()
else : # mark it as none it it was not found
author = None
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,))
row = cursor.fetchone()
author_id = row[0]
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_id))
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)
run(con)
#try :
# run(con)
#except :
# LOGGER.critical('Unexpected exception was raised: %s',
# sys.exc_info()[0])
# finalize(con)
# sys.exit()