Merge lp:~raoul-snyman/openlp/biblefixes into lp:openlp
- biblefixes
- Merge into trunk
Proposed by
Raoul Snyman
Status: | Merged |
---|---|
Merged at revision: | not available |
Proposed branch: | lp:~raoul-snyman/openlp/biblefixes |
Merge into: | lp:openlp |
Diff against target: | None lines |
To merge this branch: | bzr merge lp:~raoul-snyman/openlp/biblefixes |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Martin Thompson (community) | Approve | ||
Tim Bentley | Approve | ||
Review via email:
|
Commit message
Description of the change
To post a comment you must log in.
Revision history for this message
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
Raoul Snyman (raoul-snyman) wrote : | # |
- 493. By Tim Bentley
-
Various bug fixes and code improvements.
Revision history for this message
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
Tim Bentley (trb143) wrote : | # |
Looks good.
Approved.
review:
Approve
- 494. By Raoul Snyman
-
Merged in the changes from the biblefixes branch.
Revision history for this message
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
Martin Thompson (mjthompson) wrote : | # |
I can't comment on much of the code, not familiar with sqlalchemy, but while I'm here...
Niggles:
420 + osis = codecs.
Is there a reason 'encoding' shouldn't be unicode?
1088 +mapper(
1089 + properties=
1090 +mapper(Book, book_table,
1091 + properties=
More unicode strings?
review:
Approve
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'openlp/core/ui/slidecontroller.py' |
2 | --- openlp/core/ui/slidecontroller.py 2009-07-11 05:18:34 +0000 |
3 | +++ openlp/core/ui/slidecontroller.py 2009-07-12 20:38:34 +0000 |
4 | @@ -212,11 +212,12 @@ |
5 | self.ControllerLayout.addWidget(self.Toolbar) |
6 | self.BaseToolbar.addServiceManagerItem(item, slideno) |
7 | |
8 | + |
9 | class MasterPreview(QtCore.QObject): |
10 | """ |
11 | - Class from which all Previews should extend allowing plugins to have their own |
12 | - previews |
13 | -s """ |
14 | + Class from which all Previews should extend allowing plugins to |
15 | + have their own previews |
16 | + """ |
17 | def __init__(self, parent): |
18 | self.parent = parent |
19 | QtCore.QObject.__init__(self) |
20 | @@ -369,28 +370,30 @@ |
21 | Display the slide number passed |
22 | """ |
23 | log.debug(u'add Service Manager Item') |
24 | + self.serviceitem = serviceitem |
25 | + slide_pixmap = QtGui.QPixmap.fromImage(self.serviceitem.frames[0][u'image']) |
26 | + slide_width = 300 |
27 | + slide_height = slide_width * slide_pixmap.height() / slide_pixmap.width() |
28 | self.PreviewListWidget.clear() |
29 | self.PreviewListWidget.setRowCount(0) |
30 | - self.serviceitem = serviceitem |
31 | - framenumber = 0 |
32 | - for frame in self.serviceitem.frames: |
33 | + self.PreviewListWidget.setColumnWidth(0, slide_width) |
34 | + for framenumber, frame in enumerate(self.serviceitem.frames): |
35 | self.PreviewListWidget.setRowCount(self.PreviewListWidget.rowCount() + 1) |
36 | pixmap = QtGui.QPixmap.fromImage(frame[u'image']) |
37 | item = QtGui.QTableWidgetItem() |
38 | label = QtGui.QLabel() |
39 | - label.setMargin(15) |
40 | + label.setMargin(8) |
41 | label.setScaledContents(True) |
42 | - width = 300 |
43 | - height = width * pixmap.height() / pixmap.width() |
44 | label.setPixmap(pixmap) |
45 | - self.PreviewListWidget.setCellWidget(framenumber, 0,label) |
46 | - self.PreviewListWidget.setItem( framenumber, 0, item) |
47 | - self.PreviewListWidget.setRowHeight(framenumber, height) |
48 | - self.PreviewListWidget.setColumnWidth(0, width) |
49 | - framenumber += 1 |
50 | + self.PreviewListWidget.setCellWidget(framenumber, 0, label) |
51 | + self.PreviewListWidget.setItem(framenumber, 0, item) |
52 | + self.PreviewListWidget.setRowHeight(framenumber, slide_height) |
53 | + slide_width = self.PreviewListWidget.viewport().size().width() |
54 | + self.PreviewListWidget.setColumnWidth(0, slide_width) |
55 | if slideno > self.PreviewListWidget.rowCount(): |
56 | self.PreviewListWidget.selectRow(self.PreviewListWidget.rowCount()) |
57 | else: |
58 | self.PreviewListWidget.selectRow(slideno) |
59 | self.onSlideSelected() |
60 | self.serviceLoaded() |
61 | + self.PreviewListWidget.setFocus() |
62 | |
63 | === modified file 'openlp/plugins/bibles/lib/bibleDBimpl.py' |
64 | --- openlp/plugins/bibles/lib/bibleDBimpl.py 2009-06-16 18:21:24 +0000 |
65 | +++ openlp/plugins/bibles/lib/bibleDBimpl.py 2009-07-14 19:44:15 +0000 |
66 | @@ -1,6 +1,10 @@ |
67 | +# -*- coding: utf-8 -*- |
68 | +# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4 |
69 | """ |
70 | OpenLP - Open Source Lyrics Projection |
71 | + |
72 | Copyright (c) 2008 Raoul Snyman |
73 | + |
74 | Portions copyright (c) 2008 - 2009 Martin Thompson, Tim Bentley |
75 | |
76 | This program is free software; you can redistribute it and/or modify it under |
77 | @@ -16,43 +20,34 @@ |
78 | Place, Suite 330, Boston, MA 02111-1307 USA |
79 | """ |
80 | import os |
81 | -import os.path |
82 | import logging |
83 | |
84 | -from sqlalchemy import * |
85 | -from sqlalchemy.sql import select |
86 | -from sqlalchemy.orm import sessionmaker, mapper, scoped_session |
87 | - |
88 | from common import BibleCommon |
89 | from openlp.core.utils import ConfigHelper |
90 | -from openlp.plugins.bibles.lib.tables import * |
91 | -from openlp.plugins.bibles.lib.classes import * |
92 | +from openlp.plugins.bibles.lib.models import * |
93 | |
94 | class BibleDBImpl(BibleCommon): |
95 | global log |
96 | log=logging.getLogger(u'BibleDBImpl') |
97 | log.info(u'BibleDBimpl loaded') |
98 | |
99 | - def __init__(self, biblepath , biblename, config): |
100 | + def __init__(self, biblepath, biblename, config): |
101 | # Connect to database |
102 | self.config = config |
103 | - self.biblefile = os.path.join(biblepath, biblename+u'.sqlite') |
104 | + self.biblefile = os.path.join(biblepath, biblename + u'.sqlite') |
105 | log.debug(u'Load bible %s on path %s', biblename, self.biblefile) |
106 | db_type = self.config.get_config(u'db type', u'sqlite') |
107 | + db_url = u'' |
108 | if db_type == u'sqlite': |
109 | - self.db = create_engine(u'sqlite:///' + self.biblefile) |
110 | + db_url = u'sqlite:///' + self.biblefile |
111 | else: |
112 | - self.db_url = u'%s://%s:%s@%s/%s' % \ |
113 | + db_url = u'%s://%s:%s@%s/%s' % \ |
114 | (db_type, self.config.get_config(u'db username'), |
115 | self.config.get_config(u'db password'), |
116 | self.config.get_config(u'db hostname'), |
117 | self.config.get_config(u'db database')) |
118 | - self.db.echo = False |
119 | - metadata.bind = self.db |
120 | - metadata.bind.echo = False |
121 | - self.session = scoped_session(sessionmaker(autoflush=True, autocommit=False)) |
122 | - self.session.configure(bind=self.db) |
123 | - metadata.create_all(self.db) |
124 | + self.metadata, self.session = init_models(db_url) |
125 | + self.metadata.create_all(checkfirst=True) |
126 | |
127 | def create_tables(self): |
128 | log.debug( u'createTables') |
129 | @@ -63,122 +58,127 @@ |
130 | |
131 | def add_verse(self, bookid, chap, vse, text): |
132 | #log.debug(u'add_verse %s,%s,%s", bookid, chap, vse) |
133 | - metadata.bind.echo = False |
134 | - session = self.session() |
135 | + #metadata.bind.echo = False |
136 | verse = Verse() |
137 | verse.book_id = bookid |
138 | verse.chapter = chap |
139 | verse.verse = vse |
140 | verse.text = text |
141 | - session.add(verse) |
142 | - session.commit() |
143 | + self.session.add(verse) |
144 | + self.session.commit() |
145 | |
146 | def create_chapter(self, bookid, chap, textlist): |
147 | log.debug(u'create_chapter %s,%s', bookid, chap) |
148 | #log.debug(u'Text %s ", textlist) |
149 | - metadata.bind.echo = False |
150 | - session = self.session() |
151 | + #metadata.bind.echo = False |
152 | #text list has book and chapter as first to elements of the array |
153 | - for v , t in textlist.iteritems(): |
154 | + for verse_number, verse_text in textlist.iteritems(): |
155 | verse = Verse() |
156 | verse.book_id = bookid |
157 | verse.chapter = chap |
158 | - verse.verse = v |
159 | - verse.text = t |
160 | - session.add(verse) |
161 | - session.commit() |
162 | + verse.verse = verse_number |
163 | + verse.text = verse_text |
164 | + self.session.add(verse) |
165 | + self.session.commit() |
166 | |
167 | - def create_book(self, bookname, bookabbrev, testament = 1): |
168 | + def create_book(self, bookname, bookabbrev, testament=1): |
169 | log.debug(u'create_book %s,%s', bookname, bookabbrev) |
170 | - metadata.bind.echo = False |
171 | - session = self.session() |
172 | + #metadata.bind.echo = False |
173 | book = Book() |
174 | book.testament_id = testament |
175 | book.name = bookname |
176 | book.abbreviation = bookabbrev |
177 | - session.add(book) |
178 | - session.commit() |
179 | + self.session.add(book) |
180 | + self.session.commit() |
181 | return book |
182 | |
183 | def save_meta(self, key, value): |
184 | log.debug(u'save_meta %s/%s', key, value) |
185 | - metadata.bind.echo = False |
186 | - session = self.session() |
187 | - bmeta= BibleMeta() |
188 | + #metadata.bind.echo = False |
189 | + bmeta = BibleMeta() |
190 | bmeta.key = key |
191 | bmeta.value = value |
192 | - session.add(bmeta) |
193 | - session.commit() |
194 | + self.session.add(bmeta) |
195 | + self.session.commit() |
196 | |
197 | def get_meta(self, metakey): |
198 | log.debug(u'get meta %s', metakey) |
199 | - return self.session.query(BibleMeta).filter_by(key = metakey).first() |
200 | + return self.session.query(BibleMeta).filter_by(key=metakey).first() |
201 | |
202 | def delete_meta(self, metakey): |
203 | biblemeta = self.get_meta(metakey) |
204 | try: |
205 | - session.delete(biblemeta) |
206 | - session.commit() |
207 | + self.session.delete(biblemeta) |
208 | + self.session.commit() |
209 | return True |
210 | except: |
211 | return False |
212 | |
213 | def _load_testament(self, testament): |
214 | log.debug(u'load_testaments %s', testament) |
215 | - metadata.bind.echo = False |
216 | - session = self.session() |
217 | + #metadata.bind.echo = False |
218 | test = ONTestament() |
219 | test.name = testament |
220 | - session.add(test) |
221 | - session.commit() |
222 | + self.session.add(test) |
223 | + self.session.commit() |
224 | |
225 | def get_bible_books(self): |
226 | - log.debug(u'get_bible_books ') |
227 | + log.debug(u'get_bible_books') |
228 | return self.session.query(Book).order_by(Book.id).all() |
229 | |
230 | def get_max_bible_book_verses(self, bookname, chapter): |
231 | - log.debug(u'get_max_bible_book_verses %s,%s', bookname , chapter) |
232 | - metadata.bind.echo = False |
233 | - s = text (u'select max(verse.verse) from verse,book where chapter = :c and book_id = book.id and book.name = :b ') |
234 | - return self.db.execute(s, c=chapter, b=bookname).fetchone() |
235 | + log.debug(u'get_max_bible_book_verses %s, %s', bookname, chapter) |
236 | + #metadata.bind.echo = False |
237 | + #s = text (u'select max(verse.verse) from verse,book where chapter = :c and book_id = book.id and book.name = :b ') |
238 | + #return self.db.execute(s, c=chapter, b=bookname).fetchone() |
239 | + verse = self.session.query(Verse).join(Book).filter(Book.name==bookname).filter(Verse.chapter==chapter).order_by(Verse.verse.desc()).first() |
240 | + return verse.verse |
241 | |
242 | def get_max_bible_book_chapter(self, bookname): |
243 | - log.debug(u'get_max_bible_book_chapter %s', bookname ) |
244 | - metadata.bind.echo = False |
245 | - s = text (u'select max(verse.chapter) from verse,book where book_id = book.id and book.name = :b') |
246 | - return self.db.execute(s, b=bookname).fetchone() |
247 | + log.debug(u'get_max_bible_book_chapter %s', bookname) |
248 | + #metadata.bind.echo = False |
249 | + #s = text (u'select max(verse.chapter) from verse,book where book_id = book.id and book.name = :b') |
250 | + #return self.db.execute(s, b=bookname).fetchone() |
251 | + verse = self.session.query(Verse).join(Book).filter(Book.name==bookname).order_by(Verse.chapter.desc()).first() |
252 | + return verse.chapter |
253 | |
254 | def get_bible_book(self, bookname): |
255 | log.debug(u'get_bible_book %s', bookname) |
256 | bk = self.session.query(Book).filter(Book.name.like(bookname + u'%')).first() |
257 | if bk == None: |
258 | - bk = self.session.query(Book).filter(Book.abbreviation.like(bookname+u'%')).first() |
259 | + bk = self.session.query(Book).filter(Book.abbreviation.like(bookname + u'%')).first() |
260 | return bk |
261 | |
262 | def get_bible_chapter(self, id, chapter): |
263 | - log.debug(u'get_bible_chapter %s,%s', id, chapter ) |
264 | - metadata.bind.echo = False |
265 | - return self.session.query(Verse).filter_by(chapter = chapter ).filter_by(book_id = id).first() |
266 | + log.debug(u'get_bible_chapter %s, %s', id, chapter) |
267 | + #metadata.bind.echo = False |
268 | + return self.session.query(Verse).filter_by(chapter=chapter).filter_by(book_id=id).first() |
269 | |
270 | def get_bible_text(self, bookname, chapter, sverse, everse): |
271 | - log.debug(u'get_bible_text %s,%s,%s,%s', bookname, chapter, sverse, everse) |
272 | - metadata.bind.echo = False |
273 | - bookname = bookname + u"%" |
274 | - s = text (u'select name,chapter,verse.verse, verse.text FROM verse , book where verse.book_id == book.id AND verse.chapter == :c AND (verse.verse between :v1 and :v2) and (book.name like :b)') |
275 | - return self.db.execute(s, c=chapter, v1=sverse , v2=everse, b=bookname).fetchall() |
276 | + log.debug(u'get_bible_text %s, %s, %s, %s', bookname, chapter, sverse, everse) |
277 | + #metadata.bind.echo = False |
278 | + #bookname = bookname + u"%" |
279 | + #s = text (u'select name,chapter,verse.verse, verse.text FROM verse , book where verse.book_id == book.id AND verse.chapter == :c AND (verse.verse between :v1 and :v2) and (book.name like :b)') |
280 | + #return self.db.execute(s, c=chapter, v1=sverse , v2=everse, b=bookname).fetchall() |
281 | + verses = self.session.query(Verse).join(Book).filter(Book.name==bookname).filter(Verse.chapter==chapter).filter(Verse.verse>=sverse).filter(Verse.verse<=everse).order_by(Verse.verse).all() |
282 | + return verses |
283 | |
284 | - def get_verses_from_text(self,versetext): |
285 | + def get_verses_from_text(self, versetext): |
286 | log.debug(u'get_verses_from_text %s',versetext) |
287 | - metadata.bind.echo = False |
288 | - versetext = "%"+versetext+"%" |
289 | - s = text (u'select book.name, verse.chapter, verse.verse, verse.text FROM verse , book where verse.book_id == book.id and verse.text like :t') |
290 | - return self.db.execute(s, t=versetext).fetchall() |
291 | + #metadata.bind.echo = False |
292 | + versetext = u'%%%s%%' % versetext |
293 | + #s = text (u'select book.name, verse.chapter, verse.verse, verse.text FROM verse , book where verse.book_id == book.id and verse.text like :t') |
294 | + #return self.db.execute(s, t=versetext).fetchall() |
295 | + verses = self.session.query(Verse).filter(Verse.text.like(versetext)).all() |
296 | + return verses |
297 | |
298 | def dump_bible(self): |
299 | log.debug( u'.........Dumping Bible Database') |
300 | log.debug( '...............................Books ') |
301 | - s = text (u'select * FROM book ') |
302 | - log.debug( self.db.execute(s).fetchall()) |
303 | + #s = text (u'select * FROM book ') |
304 | + books = self.session.query(Book).all() |
305 | + log.debug(books) |
306 | log.debug( u'...............................Verses ') |
307 | - s = text (u'select * FROM verse ') |
308 | - log.debug( self.db.execute(s).fetchall()) |
309 | + #s = text (u'select * FROM verse ') |
310 | + verses = self.session.query(Verse).all() |
311 | + log.debug(verses) |
312 | |
313 | === modified file 'openlp/plugins/bibles/lib/bibleHTTPimpl.py' |
314 | --- openlp/plugins/bibles/lib/bibleHTTPimpl.py 2009-07-12 15:11:57 +0000 |
315 | +++ openlp/plugins/bibles/lib/bibleHTTPimpl.py 2009-07-13 20:11:36 +0000 |
316 | @@ -97,7 +97,7 @@ |
317 | """ |
318 | log.debug(u'get_bible_chapter %s,%s,%s,%s', version, bookid, bookname, chapter) |
319 | bookname = bookname.replace(u' ', '') |
320 | - urlstring = u'http://bible.crosswalk.com/OnlineStudyBible/bible.cgi?word='+bookname+u'+'+unicode(chapter)+u'&version='+version |
321 | + urlstring = u'http://bible.crosswalk.com/OnlineStudyBible/bible.cgi?word=%s+%d&version=%s' % (bookname, chapter, version) |
322 | xml_string = self._get_web_text(urlstring, self.proxyurl) |
323 | #log.debug(u'Return data %s', xml_string) |
324 | ## Strip Book Title from Heading to return it to system |
325 | @@ -207,7 +207,7 @@ |
326 | ev = CWExtract(self.proxyurl) |
327 | else: |
328 | ev = BGExtract(self.proxyurl) |
329 | - |
330 | return ev.get_bible_chapter(self.bibleid, bookid, bookname, chapter) |
331 | - except: |
332 | - log.error(u'Error thrown = %s', sys.exc_info()[1]) |
333 | + except Exception, e: |
334 | + log.error(u'Error thrown = %s', e.args[0]) |
335 | + print e |
336 | |
337 | === modified file 'openlp/plugins/bibles/lib/bibleOSISimpl.py' |
338 | --- openlp/plugins/bibles/lib/bibleOSISimpl.py 2009-07-10 15:41:08 +0000 |
339 | +++ openlp/plugins/bibles/lib/bibleOSISimpl.py 2009-07-13 20:08:43 +0000 |
340 | @@ -1,6 +1,8 @@ |
341 | """ |
342 | OpenLP - Open Source Lyrics Projection |
343 | + |
344 | Copyright (c) 2008 Raoul Snyman |
345 | + |
346 | Portions copyright (c) 2008 - 2009 Martin Thompson, Tim Bentley |
347 | |
348 | This program is free software; you can redistribute it and/or modify it under |
349 | @@ -20,37 +22,69 @@ |
350 | import logging |
351 | import chardet |
352 | import codecs |
353 | + |
354 | +from PyQt4 import QtCore |
355 | + |
356 | from openlp.plugins.bibles.lib.bibleDBimpl import BibleDBImpl |
357 | from openlp.core.lib import Receiver |
358 | -from PyQt4 import QtCore |
359 | |
360 | class BibleOSISImpl(): |
361 | + """ |
362 | + OSIS Bible format importer class. |
363 | + """ |
364 | global log |
365 | log = logging.getLogger(u'BibleOSISImpl') |
366 | log.info(u'BibleOSISImpl loaded') |
367 | |
368 | def __init__(self, biblepath, bibledb): |
369 | + """ |
370 | + Constructor to create and set up an instance of the |
371 | + BibleOSISImpl class. |
372 | + |
373 | + ``biblepath`` |
374 | + This does not seem to be used. |
375 | + |
376 | + ``bibledb`` |
377 | + A reference to a Bible database object. |
378 | + """ |
379 | self.bibledb = bibledb |
380 | # books of the bible linked to bibleid {osis , name} |
381 | self.booksOfBible = {} |
382 | # books of the bible linked to bibleid {osis ,Abbrev } |
383 | self.abbrevOfBible = {} |
384 | - |
385 | filepath = os.path.split(os.path.abspath(__file__))[0] |
386 | - filepath = os.path.abspath(os.path.join(filepath, u'..', u'resources',u'osisbooks.csv')) |
387 | + filepath = os.path.abspath(os.path.join( |
388 | + filepath, u'..', u'resources',u'osisbooks.csv')) |
389 | fbibles=open(filepath, u'r') |
390 | for line in fbibles: |
391 | p = line.split(u',') |
392 | self.booksOfBible[p[0]] = p[1].replace(u'\n', u'') |
393 | self.abbrevOfBible[p[0]] = p[2].replace(u'\n', u'') |
394 | self.loadbible = True |
395 | - QtCore.QObject.connect(Receiver().get_receiver(),QtCore.SIGNAL(u'openlpstopimport'),self.stop_import) |
396 | + QtCore.QObject.connect(Receiver().get_receiver(), |
397 | + QtCore.SIGNAL(u'openlpstopimport'), self.stop_import) |
398 | |
399 | def stop_import(self): |
400 | + """ |
401 | + Stops the import of the Bible. |
402 | + """ |
403 | self.loadbible = False |
404 | |
405 | def load_data(self, osisfile_record, dialogobject=None): |
406 | - osis = codecs.open(osisfile_record, u'r') |
407 | + """ |
408 | + Loads a Bible from file. |
409 | + |
410 | + ``osisfile_record`` |
411 | + The file to import from. |
412 | + |
413 | + ``dialogobject`` |
414 | + The Import dialog, so that we can increase the counter on |
415 | + the progress bar. |
416 | + """ |
417 | + detect_file = open(osisfile_record, u'r') |
418 | + details = chardet.detect(detect_file.read(2048)) |
419 | + detect_file.close() |
420 | + osis = codecs.open(osisfile_record, u'r', details['encoding']) |
421 | book_ptr = None |
422 | id = 0 |
423 | count = 0 |
424 | @@ -110,8 +144,11 @@ |
425 | if p[0] == u'Matt': |
426 | testament += 1 |
427 | book_ptr = p[0] |
428 | - book = self.bibledb.create_book(self.booksOfBible[p[0]] , self.abbrevOfBible[p[0]], testament) |
429 | - dialogobject.incrementProgressBar(self.booksOfBible[p[0]] ) |
430 | + book = self.bibledb.create_book( |
431 | + self.booksOfBible[p[0]], |
432 | + self.abbrevOfBible[p[0]], testament) |
433 | + dialogobject.incrementProgressBar( |
434 | + self.booksOfBible[p[0]]) |
435 | Receiver().send_message(u'openlpprocessevents') |
436 | count = 0 |
437 | self.bibledb.add_verse(book.id, p[1], p[2], text) |
438 | @@ -121,10 +158,20 @@ |
439 | Receiver().send_message(u'openlpprocessevents') |
440 | count = 0 |
441 | |
442 | - def remove_block(self, start_tag, end_tag, text): |
443 | + def remove_block(self, start_tag, end_tag, text): |
444 | """ |
445 | - removes a block of text between two tags |
446 | - <tag attrib=xvf > Some not wanted text </tag> |
447 | + Removes a block of text between two tags:: |
448 | + |
449 | + <tag attrib="xvf">Some not wanted text</tag> |
450 | + |
451 | + ``start_tag`` |
452 | + The XML tag to look for. |
453 | + |
454 | + ``end_tag`` |
455 | + The ending XML tag. |
456 | + |
457 | + ``text`` |
458 | + The string of XML to search. |
459 | """ |
460 | pos = text.find(start_tag) |
461 | while pos > -1: |
462 | @@ -136,10 +183,17 @@ |
463 | pos = text.find(start_tag) |
464 | return text |
465 | |
466 | - def remove_tag(self, start_tag, text): |
467 | + def remove_tag(self, start_tag, text): |
468 | """ |
469 | - removes a single tag |
470 | - <tag attrib1=fajkdf attrib2=fajkdf attrib2=fajkdf /> |
471 | + Removes a single tag:: |
472 | + |
473 | + <tag attrib1="fajkdf" attrib2="fajkdf" attrib3="fajkdf" /> |
474 | + |
475 | + ``start_tag`` |
476 | + The XML tag to remove. |
477 | + |
478 | + ``text`` |
479 | + The string of XML to search. |
480 | """ |
481 | pos = text.find(start_tag) |
482 | while pos > -1: |
483 | |
484 | === removed file 'openlp/plugins/bibles/lib/classes.py' |
485 | --- openlp/plugins/bibles/lib/classes.py 2009-02-22 07:44:08 +0000 |
486 | +++ openlp/plugins/bibles/lib/classes.py 1970-01-01 00:00:00 +0000 |
487 | @@ -1,67 +0,0 @@ |
488 | -# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4 |
489 | -""" |
490 | -OpenLP - Open Source Lyrics Projection |
491 | -Copyright (c) 2008 Raoul Snyman |
492 | -Portions copyright (c) 2008 - 2009 Martin Thompson, Tim Bentley |
493 | - |
494 | -This program is free software; you can redistribute it and/or modify it under |
495 | -the terms of the GNU General Public License as published by the Free Software |
496 | -Foundation; version 2 of the License. |
497 | - |
498 | -This program is distributed in the hope that it will be useful, but WITHOUT ANY |
499 | -WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
500 | -PARTICULAR PURPOSE. See the GNU General Public License for more details. |
501 | - |
502 | -You should have received a copy of the GNU General Public License along with |
503 | -this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
504 | -Place, Suite 330, Boston, MA 02111-1307 USA |
505 | -""" |
506 | -from sqlalchemy.orm import mapper, relation |
507 | -from openlp.plugins.bibles.lib.tables import * |
508 | - |
509 | -class BaseModel(object): |
510 | - """ |
511 | - BaseModel provides a base object with a set of generic functions |
512 | - """ |
513 | - |
514 | - @classmethod |
515 | - def populate(cls, **kwargs): |
516 | - """ |
517 | - Creates an instance of a class and populates it, returning the instance |
518 | - """ |
519 | - me = cls() |
520 | - keys = kwargs.keys() |
521 | - for key in keys: |
522 | - me.__setattr__(key, kwargs[key]) |
523 | - return me |
524 | - |
525 | -class BibleMeta(BaseModel): |
526 | - """ |
527 | - Bible Meta Data |
528 | - """ |
529 | - pass |
530 | - |
531 | -class ONTestament(BaseModel): |
532 | - """ |
533 | - Bible Testaments |
534 | - """ |
535 | - pass |
536 | - |
537 | -class Book(BaseModel): |
538 | - """ |
539 | - Song model |
540 | - """ |
541 | - pass |
542 | - |
543 | -class Verse(BaseModel): |
544 | - """ |
545 | - Topic model |
546 | - """ |
547 | - pass |
548 | - |
549 | -mapper(BibleMeta, meta_table) |
550 | -mapper(ONTestament, testament_table, |
551 | - properties={'books': relation(Book, backref='testament')}) |
552 | -mapper(Book, book_table, |
553 | - properties={'verses': relation(Verse, backref='book')}) |
554 | -mapper(Verse, verse_table) |
555 | |
556 | === modified file 'openlp/plugins/bibles/lib/manager.py' |
557 | --- openlp/plugins/bibles/lib/manager.py 2009-07-09 05:15:26 +0000 |
558 | +++ openlp/plugins/bibles/lib/manager.py 2009-07-13 20:08:43 +0000 |
559 | @@ -2,7 +2,9 @@ |
560 | # vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4 |
561 | """ |
562 | OpenLP - Open Source Lyrics Projection |
563 | + |
564 | Copyright (c) 2008 Raoul Snyman |
565 | + |
566 | Portions copyright (c) 2008 - 2009 Martin Thompson, Tim Bentley |
567 | |
568 | This program is free software; you can redistribute it and/or modify it under |
569 | @@ -18,7 +20,7 @@ |
570 | Place, Suite 330, Boston, MA 02111-1307 USA |
571 | """ |
572 | import logging |
573 | -import os, os.path |
574 | +import os |
575 | import sys |
576 | |
577 | from common import SearchResults |
578 | @@ -26,72 +28,93 @@ |
579 | from bibleCSVimpl import BibleCSVImpl |
580 | from bibleDBimpl import BibleDBImpl |
581 | from bibleHTTPimpl import BibleHTTPImpl |
582 | + |
583 | from openlp.plugins.bibles.lib.tables import * |
584 | from openlp.plugins.bibles.lib.classes import * |
585 | |
586 | -class BibleManager(): |
587 | +class BibleMode(object): |
588 | + Full = 1 |
589 | + Partial = 2 |
590 | + |
591 | +class BibleManager(object): |
592 | + """ |
593 | + The Bible manager which holds and manages all the Bibles. |
594 | + """ |
595 | global log |
596 | log=logging.getLogger(u'BibleManager') |
597 | log.info(u'Bible manager loaded') |
598 | + |
599 | def __init__(self, config): |
600 | """ |
601 | - Finds all the bibles defined for the system |
602 | - Creates an Interface Object for each bible containing connection information |
603 | - Throws Exception if no Bibles are found. |
604 | + Finds all the bibles defined for the system and creates an |
605 | + interface object for each bible containing connection |
606 | + information. Throws Exception if no Bibles are found. |
607 | |
608 | Init confirms the bible exists and stores the database path. |
609 | + |
610 | + ``config`` |
611 | + The plugin's configuration object. |
612 | """ |
613 | self.config = config |
614 | log.debug(u'Bible Initialising') |
615 | - self.bible_db_cache = None # dict of bible database classes |
616 | - self.bible_http_cache = None # dict of bible http readers |
617 | + # dict of bible database objects |
618 | + self.bible_db_cache = None |
619 | + # dict of bible http readers |
620 | + self.bible_http_cache = None |
621 | self.biblePath = self.config.get_data_path() |
622 | - self.proxyname = self.config.get_config(u'proxy name') #get proxy name for screen |
623 | + #get proxy name for screen |
624 | + self.proxyname = self.config.get_config(u'proxy name') |
625 | self.bibleSuffix = u'sqlite' |
626 | self.dialogobject = None |
627 | self.reload_bibles() |
628 | |
629 | def reload_bibles(self): |
630 | log.debug(u'Reload bibles') |
631 | - |
632 | files = self.config.get_files(self.bibleSuffix) |
633 | log.debug(u'Bible Files %s', files ) |
634 | - |
635 | self.bible_db_cache = {} |
636 | self.bible_http_cache = {} |
637 | - |
638 | - self.book_testaments = {} # books of the bible with testaments |
639 | - self.book_abbreviations = {} # books of the bible with abbreviation |
640 | + # books of the bible with testaments |
641 | + self.book_testaments = {} |
642 | + # books of the bible with abbreviation |
643 | + self.book_abbreviations = {} |
644 | self.web_bibles_present = False |
645 | - |
646 | - |
647 | for f in files: |
648 | nme = f.split(u'.') |
649 | bname = nme[0] |
650 | - self.bible_db_cache[bname] = BibleDBImpl(self.biblePath, bname, self.config) |
651 | - biblesource = self.bible_db_cache[bname].get_meta(u'WEB') # look to see if lazy load bible exists and get create getter. |
652 | + self.bible_db_cache[bname] = BibleDBImpl(self.biblePath, |
653 | + bname, self.config) |
654 | + # look to see if lazy load bible exists and get create getter. |
655 | + biblesource = self.bible_db_cache[bname].get_meta(u'WEB') |
656 | if biblesource: |
657 | self.web_bibles_present = True |
658 | nhttp = BibleHTTPImpl() |
659 | - nhttp.set_bible_source(biblesource.value) # tell The Server where to get the verses from. |
660 | + # tell The Server where to get the verses from. |
661 | + nhttp.set_bible_source(biblesource.value) |
662 | self.bible_http_cache [bname] = nhttp |
663 | - meta = self.bible_db_cache[bname].get_meta(u'proxy') # look to see if lazy load bible exists and get create getter. |
664 | + # look to see if lazy load bible exists and get create getter. |
665 | + meta = self.bible_db_cache[bname].get_meta(u'proxy') |
666 | proxy = None |
667 | if meta != None: |
668 | proxy = meta.value |
669 | - nhttp.set_proxy(proxy) # tell The Server where to get the verses from. |
670 | - bibleid = self.bible_db_cache[bname].get_meta(u'bibleid').value # look to see if lazy load bible exists and get create getter. |
671 | - nhttp.set_bibleid(bibleid) # tell The Server where to get the verses from. |
672 | + # tell The Server where to get the verses from. |
673 | + nhttp.set_proxy(proxy) |
674 | + # look to see if lazy load bible exists and get create getter. |
675 | + bibleid = self.bible_db_cache[bname].get_meta(u'bibleid').value |
676 | + # tell The Server where to get the verses from. |
677 | + nhttp.set_bibleid(bibleid) |
678 | else: |
679 | - self.bible_http_cache [bname] = None # makes the Full / partial code easier. |
680 | - |
681 | + # makes the Full / partial code easier. |
682 | + self.bible_http_cache [bname] = None |
683 | if self.web_bibles_present: |
684 | - self.book_testaments = {} # books of the bible linked to bibleid {osis , name} |
685 | - self.book_abbreviations = {} # books of the bible linked to bibleid {osis ,Abbrev } |
686 | - |
687 | + # books of the bible linked to bibleid {osis, name} |
688 | + self.book_testaments = {} |
689 | + # books of the bible linked to bibleid {osis, abbrev} |
690 | + self.book_abbreviations = {} |
691 | filepath = os.path.split(os.path.abspath(__file__))[0] |
692 | - filepath = os.path.abspath(os.path.join(filepath, u'..', u'resources',u'httpbooks.csv')) |
693 | - fbibles=open(filepath, 'r') |
694 | + filepath = os.path.abspath(os.path.join( |
695 | + filepath, u'..', u'resources',u'httpbooks.csv')) |
696 | + fbibles = open(filepath, u'r') |
697 | for line in fbibles: |
698 | p = line.split(u',') |
699 | self.book_abbreviations[p[0]] = p[1].replace(u'\n', '') |
700 | @@ -99,31 +122,64 @@ |
701 | log.debug(u'Bible Initialised') |
702 | |
703 | def process_dialog(self, dialogobject): |
704 | + """ |
705 | + Sets the reference to the dialog with the progress bar on it. |
706 | + |
707 | + ``dialogobject`` |
708 | + The reference to the dialog. |
709 | + """ |
710 | self.dialogobject = dialogobject |
711 | |
712 | - def register_http_bible(self, biblename, biblesource, bibleid, proxyurl=None, proxyid=None, proxypass=None): |
713 | - """ |
714 | - Return a list of bibles from a given URL. |
715 | - The selected Bible can then be registered and LazyLoaded into a database |
716 | - """ |
717 | - log.debug(u'register_HTTP_bible %s,%s,%s,%s,%s,%s', biblename, biblesource, bibleid, proxyurl, proxyid, proxypass) |
718 | + def register_http_bible(self, biblename, biblesource, bibleid, |
719 | + proxyurl=None, proxyid=None, proxypass=None): |
720 | + """ |
721 | + Return a list of bibles from a given URL. The selected Bible |
722 | + can then be registered and LazyLoaded into a database. |
723 | + |
724 | + ``biblename`` |
725 | + The name of the bible to register. |
726 | + |
727 | + ``biblesource`` |
728 | + Where this Bible stores it's verses. |
729 | + |
730 | + ``bibleid`` |
731 | + The identifier for a Bible. |
732 | + |
733 | + ``proxyurl`` |
734 | + Defaults to *None*. An optional URL to a proxy server. |
735 | + |
736 | + ``proxyid`` |
737 | + Defaults to *None*. A username for logging into the proxy |
738 | + server. |
739 | + |
740 | + ``proxypass`` |
741 | + Defaults to *None*. The password to accompany the username. |
742 | + """ |
743 | + log.debug(u'register_HTTP_bible %s, %s, %s, %s, %s, %s', |
744 | + biblename, biblesource, bibleid, proxyurl, proxyid, proxypass) |
745 | if self._is_new_bible(biblename): |
746 | - nbible = BibleDBImpl(self.biblePath, biblename, self.config) # Create new Bible |
747 | - nbible.create_tables() # Create Database |
748 | + # Create new Bible |
749 | + nbible = BibleDBImpl(self.biblePath, biblename, self.config) |
750 | + # Create Database |
751 | + nbible.create_tables() |
752 | self.bible_db_cache[biblename] = nbible |
753 | - |
754 | nhttp = BibleHTTPImpl() |
755 | nhttp.set_bible_source(biblesource) |
756 | self.bible_http_cache [biblename] = nhttp |
757 | - nbible.save_meta(u'WEB', biblesource) # register a lazy loading interest |
758 | - nbible.save_meta(u'bibleid', bibleid) # store the we id of the bible |
759 | + # register a lazy loading interest |
760 | + nbible.save_meta(u'WEB', biblesource) |
761 | + # store the web id of the bible |
762 | + nbible.save_meta(u'bibleid', bibleid) |
763 | if proxyurl != None and proxyurl != "": |
764 | - nbible.save_meta(u'proxy', proxyurl) # store the proxy URL |
765 | + # store the proxy URL |
766 | + nbible.save_meta(u'proxy', proxyurl) |
767 | nhttp.set_proxy(proxyurl) |
768 | if proxyid != None and proxyid != "": |
769 | - nbible.save_meta(u'proxyid', proxyid) # store the proxy userid |
770 | + # store the proxy userid |
771 | + nbible.save_meta(u'proxyid', proxyid) |
772 | if proxypass != None and proxypass != "": |
773 | - nbible.save_meta(u'proxypass', proxypass) # store the proxy password |
774 | + # store the proxy password |
775 | + nbible.save_meta(u'proxypass', proxypass) |
776 | return True |
777 | else: |
778 | log.debug(u'register_http_file_bible %s not created already exists', biblename) |
779 | @@ -165,21 +221,26 @@ |
780 | log.debug(u'register_OSIS_file_bible %s , %s not created already exists', biblename, osisfile) |
781 | return False |
782 | |
783 | - def get_bibles(self, mode=u'full'): |
784 | + def get_bibles(self, mode=BibleMode.Full): |
785 | + """ |
786 | + Returns a list of Books of the bible. When ``mode`` is set to |
787 | + ``BibleMode.Full`` this method returns all the Bibles for the |
788 | + Advanced Search, and when the mode is ``BibleMode.Partial`` |
789 | + this method returns all the bibles for the Quick Search. |
790 | + |
791 | + ``mode`` |
792 | + Defaults to ``BibleMode.Full``. The Bible mode. |
793 | + """ |
794 | log.debug(u'get_bibles') |
795 | - """ |
796 | - Returns a list of Books of the bible |
797 | - Mode "Full" - Returns all the bibles for the Queck seearch |
798 | - Mode "Partial" - Returns CSV and OSIS bbles for the Advanced Search |
799 | - """ |
800 | - r=[] |
801 | - for b , o in self.bible_db_cache.iteritems(): |
802 | - if mode == u'full': |
803 | - r.append(b) |
804 | + bible_list = [] |
805 | + for bible_name, bible_object in self.bible_db_cache.iteritems(): |
806 | + if mode == BibleMode.Full: |
807 | + bible_list.append(bible_name) |
808 | else: |
809 | - if self.bible_http_cache [b] == None: # we do not have an http bible |
810 | - r.append(b) |
811 | - return r |
812 | + if self.bible_http_cache[bible_name] is None: |
813 | + # we do not have an http bible |
814 | + bible_list.append(bible_name) |
815 | + return bible_list |
816 | |
817 | def get_bible_books(self,bible): |
818 | """ |
819 | @@ -192,7 +253,7 @@ |
820 | """ |
821 | Returns the number of Chapters for a given book |
822 | """ |
823 | - log.debug(u'get_book_chapter_count %s,%s', bible, book) |
824 | + log.debug(u'get_book_chapter_count %s, %s', bible, book) |
825 | return self.bible_db_cache[bible].get_max_bible_book_chapter(book) |
826 | |
827 | def get_book_verse_count(self, bible, book, chapter): |
828 | @@ -227,7 +288,7 @@ |
829 | log.debug(u'get_meta %s,%s', bible, key) |
830 | return self.bible_db_cache[bible].get_meta(key) |
831 | |
832 | - def get_verse_text(self, bible, bookname, schapter, echapter, sverse, everse = 0 ): |
833 | + def get_verse_text(self, bible, bookname, schapter, echapter, sverse, everse=0): |
834 | """ |
835 | Returns a list of verses for a given Book, Chapter and ranges of verses. |
836 | If the end verse(everse) is less then the start verse(sverse) |
837 | @@ -237,7 +298,7 @@ |
838 | """ |
839 | text = [] |
840 | log.debug(u'get_verse_text %s,%s,%s,%s,%s,%s', bible, bookname, schapter, echapter, sverse, everse) |
841 | - if not self.bible_http_cache [bible] == None: |
842 | + if not self.bible_http_cache[bible] == None: |
843 | # check to see if book/chapter exists |
844 | book= self.bible_db_cache[bible].get_bible_book(bookname) |
845 | if book == None: |
846 | |
847 | === modified file 'openlp/plugins/bibles/lib/mediaitem.py' |
848 | --- openlp/plugins/bibles/lib/mediaitem.py 2009-07-13 17:02:38 +0000 |
849 | +++ openlp/plugins/bibles/lib/mediaitem.py 2009-07-13 20:11:36 +0000 |
850 | @@ -2,7 +2,9 @@ |
851 | # vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4 |
852 | """ |
853 | OpenLP - Open Source Lyrics Projection |
854 | + |
855 | Copyright (c) 2008 Raoul Snyman |
856 | + |
857 | Portions copyright (c) 2008-2009 Martin Thompson, Tim Bentley |
858 | |
859 | This program is free software; you can redistribute it and/or modify it under |
860 | @@ -21,8 +23,10 @@ |
861 | |
862 | from PyQt4 import QtCore, QtGui |
863 | |
864 | -from openlp.core.lib import ServiceItem, MediaManagerItem, Receiver, translate, contextMenuAction, contextMenuSeparator |
865 | +from openlp.core.lib import translate, ServiceItem, MediaManagerItem, \ |
866 | + Receiver, contextMenuAction, contextMenuSeparator |
867 | from openlp.plugins.bibles.forms import BibleImportForm |
868 | +from openlp.plugins.bibles.lib.manager import BibleMode |
869 | |
870 | class BibleList(QtGui.QListWidget): |
871 | |
872 | @@ -255,12 +259,12 @@ |
873 | log.debug(u'Loading Bibles') |
874 | self.QuickVersionComboBox.clear() |
875 | self.AdvancedVersionComboBox.clear() |
876 | - bibles = self.parent.biblemanager.get_bibles(u'full') |
877 | + bibles = self.parent.biblemanager.get_bibles(BibleMode.Full) |
878 | # load bibles into the combo boxes |
879 | for bible in bibles: |
880 | self.QuickVersionComboBox.addItem(bible) |
881 | - # Without HTT |
882 | - bibles = self.parent.biblemanager.get_bibles(u'partial') |
883 | + # Without HTTP |
884 | + bibles = self.parent.biblemanager.get_bibles(BibleMode.Partial) |
885 | first = True |
886 | # load bibles into the combo boxes |
887 | for bible in bibles: |
888 | @@ -287,8 +291,8 @@ |
889 | self.adjustComboBox(frm, self.verses, self.AdvancedToVerse) |
890 | |
891 | def onAdvancedToChapter(self): |
892 | - t1 = self.AdvancedFromChapter.currentText() |
893 | - t2 = self.AdvancedToChapter.currentText() |
894 | + t1 = self.AdvancedFromChapter.currentText() |
895 | + t2 = self.AdvancedToChapter.currentText() |
896 | if t1 != t2: |
897 | bible = unicode(self.AdvancedVersionComboBox.currentText()) |
898 | book = unicode(self.AdvancedBookComboBox.currentText()) |
899 | @@ -344,12 +348,12 @@ |
900 | bitem = self.ListView.item(item.row()) |
901 | text = unicode((bitem.data(QtCore.Qt.UserRole)).toString()) |
902 | verse = text[:text.find(u'(')] |
903 | - bible = text[text.find(u'(') + 1:text.find(u')')] |
904 | + bible = text[text.find(u'(') + 1:-1] |
905 | self.searchByReference(bible, verse) |
906 | - book = self.search_results[0][0] |
907 | - chapter = unicode(self.search_results[0][1]) |
908 | - verse = unicode(self.search_results[0][2]) |
909 | - text = self.search_results[0][3] |
910 | + book = self.search_results[0].book.name |
911 | + chapter = unicode(self.search_results[0].chapter) |
912 | + verse = unicode(self.search_results[0].verse) |
913 | + text = self.search_results[0].text |
914 | #Paragraph style force new line per verse |
915 | if self.parent.bibles_tab.paragraph_style: |
916 | text = text + u'\n\n' |
917 | @@ -406,8 +410,8 @@ |
918 | |
919 | def initialiseChapterVerse(self, bible, book): |
920 | log.debug(u'initialiseChapterVerse %s , %s', bible, book) |
921 | - self.chapters_from = self.parent.biblemanager.get_book_chapter_count(bible, book)[0] |
922 | - self.verses = self.parent.biblemanager.get_book_verse_count(bible, book, 1)[0] |
923 | + self.chapters_from = self.parent.biblemanager.get_book_chapter_count(bible, book) |
924 | + self.verses = self.parent.biblemanager.get_book_verse_count(bible, book, 1) |
925 | self.adjustComboBox(1, self.chapters_from, self.AdvancedFromChapter) |
926 | self.adjustComboBox(1, self.chapters_from, self.AdvancedToChapter) |
927 | self.adjustComboBox(1, self.verses, self.AdvancedFromVerse) |
928 | @@ -420,10 +424,16 @@ |
929 | combo.addItem(unicode(i)) |
930 | |
931 | def displayResults(self, bible): |
932 | - for book, chap, vse , txt in self.search_results: |
933 | - bible_text = unicode(u' %s %d:%d (%s)'%(book , chap,vse, bible)) |
934 | + for verse in self.search_results: |
935 | + #bible_text = unicode(u' %s %d:%d (%s)'%(book , chap,vse, bible)) |
936 | + #bible_verse = QtGui.QListWidgetItem(bible_text) |
937 | + #bible_verse.setData(QtCore.Qt.UserRole, QtCore.QVariant(bible_text)) |
938 | + #self.ListView.addItem(bible_verse) |
939 | + bible_text = u' %s %d:%d (%s)' % (verse.book.name, |
940 | + verse.chapter, verse.verse, bible) |
941 | bible_verse = QtGui.QListWidgetItem(bible_text) |
942 | - bible_verse.setData(QtCore.Qt.UserRole, QtCore.QVariant(bible_text)) |
943 | + bible_verse.setData(QtCore.Qt.UserRole, |
944 | + QtCore.QVariant(bible_text)) |
945 | self.ListView.addItem(bible_verse) |
946 | |
947 | def searchByReference(self, bible, search): |
948 | |
949 | === renamed file 'openlp/plugins/bibles/lib/tables.py' => 'openlp/plugins/bibles/lib/models.py' |
950 | --- openlp/plugins/bibles/lib/tables.py 2009-06-16 18:21:24 +0000 |
951 | +++ openlp/plugins/bibles/lib/models.py 2009-07-14 19:44:15 +0000 |
952 | @@ -1,7 +1,10 @@ |
953 | +# -*- coding: utf-8 -*- |
954 | # vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4 |
955 | """ |
956 | OpenLP - Open Source Lyrics Projection |
957 | + |
958 | Copyright (c) 2008 Raoul Snyman |
959 | + |
960 | Portions copyright (c) 2008 - 2009 Martin Thompson, Tim Bentley |
961 | |
962 | This program is free software; you can redistribute it and/or modify it under |
963 | @@ -14,39 +17,95 @@ |
964 | |
965 | You should have received a copy of the GNU General Public License along with |
966 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
967 | -Place, Suite 330, Boston, MA 02111-1307 USA |
968 | +Place, Suite 330, Boston, MA 02111-1307 USA |
969 | """ |
970 | -import string |
971 | -from sqlalchemy import * |
972 | -from sqlalchemy import Column, Table, MetaData, ForeignKey, schema |
973 | +import string |
974 | + |
975 | +from sqlalchemy import Column, Table, MetaData, ForeignKey, types, \ |
976 | + create_engine |
977 | +from sqlalchemy.orm import mapper, relation, sessionmaker, scoped_session |
978 | + |
979 | +class BaseModel(object): |
980 | + """ |
981 | + BaseModel provides a base object with a set of generic functions |
982 | + """ |
983 | + @classmethod |
984 | + def populate(cls, **kwargs): |
985 | + """ |
986 | + Creates an instance of a class and populates it, returning the instance |
987 | + """ |
988 | + me = cls() |
989 | + keys = kwargs.keys() |
990 | + for key in keys: |
991 | + me.__setattr__(key, kwargs[key]) |
992 | + return me |
993 | + |
994 | + |
995 | +class BibleMeta(BaseModel): |
996 | + """ |
997 | + Bible Meta Data |
998 | + """ |
999 | + pass |
1000 | + |
1001 | + |
1002 | +class ONTestament(BaseModel): |
1003 | + """ |
1004 | + Bible Testaments |
1005 | + """ |
1006 | + pass |
1007 | + |
1008 | + |
1009 | +class Book(BaseModel): |
1010 | + """ |
1011 | + Song model |
1012 | + """ |
1013 | + pass |
1014 | + |
1015 | + |
1016 | +class Verse(BaseModel): |
1017 | + """ |
1018 | + Topic model |
1019 | + """ |
1020 | + pass |
1021 | + |
1022 | + |
1023 | +def init_models(db_url): |
1024 | + engine = create_engine(db_url) |
1025 | + metadata.bind = engine |
1026 | + session = scoped_session(sessionmaker(autoflush=True, |
1027 | + autocommit=False, |
1028 | + bind=engine)) |
1029 | + # Don't think this is needed... |
1030 | + #metadata.bind.echo = False |
1031 | + #Define the tables and indexes |
1032 | + return metadata, session |
1033 | + |
1034 | |
1035 | metadata = MetaData() |
1036 | -#Define the tables and indexes |
1037 | -meta_table = Table(u'metadata', metadata, |
1038 | - Column(u'key', String(255), primary_key=True), |
1039 | - Column(u'value', String(255)), |
1040 | -) |
1041 | - |
1042 | -testament_table = Table(u'testament', metadata, |
1043 | - Column(u'id', Integer, primary_key=True), |
1044 | - Column(u'name', String(30)), |
1045 | -) |
1046 | - |
1047 | -book_table = Table(u'book', metadata, |
1048 | - Column(u'id', Integer, primary_key=True), |
1049 | - Column(u'testament_id', Integer, schema.ForeignKey(u'testament.id')), |
1050 | - Column(u'name', String(30)), |
1051 | - Column(u'abbreviation', String(5)), |
1052 | -) |
1053 | -Index(u'idx_name', book_table.c.name, book_table.c.id) |
1054 | -Index(u'idx_abbrev', book_table.c.abbreviation, book_table.c.id) |
1055 | - |
1056 | -verse_table = Table(u'verse', metadata, |
1057 | - Column(u'id', Integer, primary_key=True), |
1058 | - Column(u'book_id', Integer , schema.ForeignKey(u'book.id')), |
1059 | - Column(u'chapter', Integer), |
1060 | - Column(u'verse', Integer), |
1061 | - Column(u'text', Text), |
1062 | -) |
1063 | -Index(u'idx_chapter_verse_book', verse_table.c.chapter, verse_table.c.verse, verse_table.c.book_id, verse_table.c.id) |
1064 | -Index(u'idx_chapter_verse_text', verse_table.c.text, verse_table.c.verse, verse_table.c.book_id, verse_table.c.id) |
1065 | \ No newline at end of file |
1066 | +meta_table = Table(u'metadata', metadata, |
1067 | + Column(u'key', types.Unicode(255), primary_key=True, index=True), |
1068 | + Column(u'value', types.Unicode(255)), |
1069 | +) |
1070 | +testament_table = Table(u'testament', metadata, |
1071 | + Column(u'id', types.Integer, primary_key=True), |
1072 | + Column(u'name', types.Unicode(50)), |
1073 | +) |
1074 | +book_table = Table(u'book', metadata, |
1075 | + Column(u'id', types.Integer, primary_key=True), |
1076 | + Column(u'testament_id', types.Integer, ForeignKey(u'testament.id')), |
1077 | + Column(u'name', types.Unicode(50), index=True), |
1078 | + Column(u'abbreviation', types.Unicode(5), index=True), |
1079 | +) |
1080 | +verse_table = Table(u'verse', metadata, |
1081 | + Column(u'id', types.Integer, primary_key=True, index=True), |
1082 | + Column(u'book_id', types.Integer, ForeignKey(u'book.id'), index=True), |
1083 | + Column(u'chapter', types.Integer, index=True), |
1084 | + Column(u'verse', types.Integer, index=True), |
1085 | + Column(u'text', types.UnicodeText, index=True), |
1086 | +) |
1087 | +mapper(BibleMeta, meta_table) |
1088 | +mapper(ONTestament, testament_table, |
1089 | + properties={'books': relation(Book, backref='testament')}) |
1090 | +mapper(Book, book_table, |
1091 | + properties={'verses': relation(Verse, backref='book')}) |
1092 | +mapper(Verse, verse_table) |
Some more fixes!