-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule_sql_migration.py
executable file
·98 lines (86 loc) · 3.15 KB
/
module_sql_migration.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
#!./pdf_python/bin/python
# -*- coding: utf-8 -*-
import os
import re
from model import Book, Chapter, Verse, session
class ReadTheBible:
"""
class ReadTheBible is to convert BibleQuote module into SQLite3 database
"""
path = ''
def __init__(self, path):
"""
constructor
"""
self.path = path
def read_bible_ini(self):
"""
this method is reading the info about current BQ-module from the
bibleqt.ini file after it finds the reference to the next book file,
it calls the read_the_book method
"""
try:
bqtini = open(os.path.join(self.path, 'Bibleqt.ini'), 'r')
except:
print "oops, it seems file doesn't exists"
return
for line in bqtini:
uline = line.decode('utf-8', 'replace')
if "PathName" in uline:
path_name = uline.split(' = ')[1].rstrip()
if "FullName" in uline:
full_name = uline.split(' = ')[1].rstrip()
if "ShortName" in uline:
short_names = uline.split(' = ')[1].rstrip()
if "ChapterQty" in uline:
chapter_qty = uline.split(' = ')[1].rstrip()
self.read_the_book(path_name, full_name, short_names)
bqtini.close()
def read_the_book(self, path, f_name, sh_names):
"""
this method is reading each book, chapter by chapter and verse after
verse and put it into the DataBase
path - the path to the book
f_name - full title of the current book
sh_names - list of the shorten names
"""
try:
module = open(os.path.join(self.path, path), 'r')
except:
print "oops, the module %s doesn't exists" % \
(os.path.join(self.path, path))
return
print path, f_name, sh_names
book = Book()
book.name = f_name
book.shortname = sh_names
reg = re.compile(ur'(\d+)')
tag_remove = re.compile(ur'<.*?>')
re_strong = re.compile(ur' [0-9]+')
chapt_number = 1
for line in module:
uline = line.decode('utf-8', 'replace')
if u'<h4>' in uline.lower() or u'<h1>' in uline.lower():
uline = tag_remove.sub('', uline)
chapter = Chapter()
chapter.name = uline
chapter.number = chapt_number
chapt_number += 1
book.chapters.append(chapter)
session.add(chapter)
if u'<p>' in uline.lower() or u'<sup>' in uline.lower():
uline = tag_remove.sub('', uline)
uline = re_strong.sub('', uline)
verse = Verse()
verse.number = int(reg.findall(uline)[0])
verse.text = uline
chapter.verses.append(verse)
session.add(verse)
session.add(chapter)
session.add(book)
session.commit()
module.close()
if __name__ == "__main__":
module_path = os.path.join(os.getcwd(), 'jub')
module_read = ReadTheBible(module_path)
module_read.read_bible_ini()