This commit is contained in:
Jonathan Corwin 2010-04-01 08:42:42 +01:00
parent 8d6d2b8272
commit 33f4bc05f2
1 changed files with 77 additions and 132 deletions

View File

@ -81,7 +81,8 @@ class sofimport:
def open_sof_file(self, filepath): def open_sof_file(self, filepath):
if os.name == u'nt': if os.name == u'nt':
url = u'file:///' + filepath.replace(u'\\', u'/').replace(u':', u'|').replace(u' ', u'%20') url = u'file:///' + filepath.replace(u'\\', u'/')
url = url.replace(u':', u'|').replace(u' ', u'%20')
else: else:
url = uno.systemPathToFileUrl(filepath) url = uno.systemPathToFileUrl(filepath)
properties = [] properties = []
@ -104,84 +105,85 @@ class sofimport:
self.song = None self.song = None
def process_paragraph(self, paragraph): def process_paragraph(self, paragraph):
text = '' text = u''
textportions = paragraph.createEnumeration() textportions = paragraph.createEnumeration()
while textportions.hasMoreElements(): while textportions.hasMoreElements():
textportion = textportions.nextElement() textportion = textportions.nextElement()
if textportion.BreakType in (PAGE_BEFORE, PAGE_BOTH): if textportion.BreakType in (PAGE_BEFORE, PAGE_BOTH):
self.process_paragraph_text(text) self.process_paragraph_text(text)
self.new_song() self.new_song()
text = '' text = u''
text += self.process_textportion(textportion) text += self.process_textportion(textportion)
if textportion.BreakType in (PAGE_AFTER, PAGE_BOTH): if textportion.BreakType in (PAGE_AFTER, PAGE_BOTH):
self.process_paragraph_text(text) self.process_paragraph_text(text)
self.new_song() self.new_song()
text = '' text = u''
self.process_paragraph_text(text) self.process_paragraph_text(text)
def process_paragraph_text(self, text): def process_paragraph_text(self, text):
for line in text.split('\n'): for line in text.split(u'\n'):
self.process_paragraph_line(line) self.process_paragraph_line(line)
if self.blanklines > 2: if self.blanklines > 2:
self.new_song() self.new_song()
def process_paragraph_line(self, text): def process_paragraph_line(self, text):
text = text.strip() text = text.strip()
if text == '': if text == u'':
self.blanklines += 1 self.blanklines += 1
if self.blanklines > 1: if self.blanklines > 1:
return return
if self.song.title != '': if self.song.title != u'':
self.song.finish_verse() self.song.finish_verse()
return return
#print ">" + text + "<" #print ">" + text + "<"
self.blanklines = 0 self.blanklines = 0
if self.skip_to_close_bracket: if self.skip_to_close_bracket:
if text.endswith(')'): if text.endswith(u')'):
self.skip_to_close_bracket = False self.skip_to_close_bracket = False
return return
if text.startswith('CCL Licence'): if text.startswith(u'CCL Licence'):
self.in_chorus = False self.in_chorus = False
return return
if text == 'A Songs of Fellowship Worship Resource': if text == u'A Songs of Fellowship Worship Resource':
return return
if text.startswith('(NB.') or text.startswith('(Regrettably') or text.startswith('(From'): if text.startswith(u'(NB.') or text.startswith(u'(Regrettably') \
or text.startswith(u'(From'):
self.skip_to_close_bracket = True self.skip_to_close_bracket = True
return return
if text.startswith('Copyright'): if text.startswith(u'Copyright'):
self.song.copyright = text self.song.copyright = text
return return
if text == '(Repeat)': if text == u'(Repeat)':
self.song.repeat_verse() self.song.repeat_verse()
return return
if self.song.title == '': if self.song.title == u'':
if self.song.copyright == '': if self.song.copyright == u'':
self.song.add_author(text) self.song.add_author(text)
else: else:
self.song.copyright += ' ' + text self.song.copyright += u' ' + text
return return
self.song.add_verse_line(text, self.in_chorus) self.song.add_verse_line(text, self.in_chorus)
def process_textportion(self, textportion): def process_textportion(self, textportion):
text = textportion.getString() text = textportion.getString()
text = self.tidy_text(text) text = self.tidy_text(text)
if text.strip() == '': if text.strip() == u'':
return text return text
if textportion.CharWeight == BOLD: if textportion.CharWeight == BOLD:
boldtext = text.strip() boldtext = text.strip()
if boldtext.isdigit() and self.song.songnumber == 0: if boldtext.isdigit() and self.song.songnumber == 0:
self.song.songnumber = int(boldtext) self.song.songnumber = int(boldtext)
return '' return u''
if self.song.title == '': if self.song.title == u'':
text = self.uncap_text(text) text = self.uncap_text(text)
title = text.strip() title = text.strip()
if title.startswith('\''): if title.startswith(u'\''):
title = title[1:] title = title[1:]
if title.endswith(','): if title.endswith(u','):
title = title[:-1] title = title[:-1]
self.song.title = title self.song.title = title
return text return text
if text.strip().startswith('('): if text.strip().startswith(u'('):
return text return text
self.in_chorus = (textportion.CharPosture == ITALIC) self.in_chorus = (textportion.CharPosture == ITALIC)
return text return text
@ -195,48 +197,51 @@ class sofimport:
self.in_chorus = False self.in_chorus = False
def tidy_text(self, text): def tidy_text(self, text):
text = text.replace('\t', ' ') text = text.replace(u'\t', u' ')
text = text.replace('\r', '\n') text = text.replace(u'\r', u'\n')
text = text.replace(u'\u2018', '\'') text = text.replace(u'\u2018', u'\'')
text = text.replace(u'\u2019', '\'') text = text.replace(u'\u2019', u'\'')
text = text.replace(u'\u201c', '"') text = text.replace(u'\u201c', u'"')
text = text.replace(u'\u201d', '"') text = text.replace(u'\u201d', u'"')
text = text.replace(u'\u2026', '...') text = text.replace(u'\u2026', u'...')
text = text.replace(u'\u2013', '-') text = text.replace(u'\u2013', u'-')
text = text.replace(u'\u2014', '-') text = text.replace(u'\u2014', u'-')
return text return text
def uncap_text(self, text): def uncap_text(self, text):
textarr = re.split('(\W+)', text) textarr = re.split(u'(\W+)', text)
textarr[0] = textarr[0].capitalize() textarr[0] = textarr[0].capitalize()
for i in range(1, len(textarr)): for i in range(1, len(textarr)):
if textarr[i] in ('JESUS', 'CHRIST', 'KING', 'ALMIGHTY', 'REDEEMER', 'SHEPHERD', # Do not translate these. Fixed strings in SOF song file
'SON', 'GOD', 'LORD', 'FATHER', 'HOLY', 'SPIRIT', 'LAMB', 'YOU', if textarr[i] in (u'JESUS', u'CHRIST', u'KING', u'ALMIGHTY',
'YOUR', 'I', 'I\'VE', 'I\'M', 'I\'LL', 'SAVIOUR', 'O', 'YOU\'RE', 'HE', 'HIS', 'HIM', 'ZION', u'REDEEMER', u'SHEPHERD', u'SON', u'GOD', u'LORD', u'FATHER',
'EMMANUEL', 'MAJESTY', 'JESUS\'', 'JIREH', 'JUDAH', 'LION', 'LORD\'S', 'ABRAHAM', 'GOD\'S', u'HOLY', u'SPIRIT', u'LAMB', u'YOU', u'YOUR', u'I', u'I\'VE',
'FATHER\'S', 'ELIJAH'): u'I\'M', u'I\'LL', u'SAVIOUR', u'O', u'YOU\'RE', u'HE', u'HIS',
u'HIM', u'ZION', u'EMMANUEL', u'MAJESTY', u'JESUS\'', u'JIREH',
u'JUDAH', u'LION', u'LORD\'S', u'ABRAHAM', u'GOD\'S',
u'FATHER\'S', u'ELIJAH'):
textarr[i] = textarr[i].capitalize() textarr[i] = textarr[i].capitalize()
else: else:
textarr[i] = textarr[i].lower() textarr[i] = textarr[i].lower()
text = ''.join(textarr) text = u''.join(textarr)
return text return text
class sofsong: class sofsong:
def __init__(self): def __init__(self):
self.songnumber = 0 self.songnumber = 0
self.title = "" self.title = u''
self.ischorus = False self.ischorus = False
self.versecount = 0 self.versecount = 0
self.choruscount = 0 self.choruscount = 0
self.verses = [] self.verses = []
self.order = [] self.order = []
self.authors = [] self.authors = []
self.copyright = "" self.copyright = u''
self.book = '' self.book = u''
self.currentverse = '' self.currentverse = u''
def finish_verse(self): def finish_verse(self):
if self.currentverse.strip() == '': if self.currentverse.strip() == u'':
return return
if self.ischorus: if self.ischorus:
splitat = None splitat = None
@ -244,36 +249,36 @@ class sofsong:
splitat = self.verse_splits() splitat = self.verse_splits()
if splitat: if splitat:
ln = 0 ln = 0
verse = '' verse = u''
for line in self.currentverse.split('\n'): for line in self.currentverse.split(u'\n'):
ln += 1 ln += 1
if line == '' or ln > splitat: if line == u'' or ln > splitat:
self.append_verse(verse) self.append_verse(verse)
ln = 0 ln = 0
if line: if line:
verse = line + '\n' verse = line + u'\n'
else: else:
verse = '' verse = u''
else: else:
verse += line + '\n' verse += line + u'\n'
if verse: if verse:
self.append_verse(verse) self.append_verse(verse)
else: else:
self.append_verse(self.currentverse) self.append_verse(self.currentverse)
self.currentverse = '' self.currentverse = u''
self.ischorus = False self.ischorus = False
def append_verse(self, verse): def append_verse(self, verse):
if self.ischorus: if self.ischorus:
self.choruscount += 1 self.choruscount += 1
versetag = 'C' + str(self.choruscount) versetag = u'C' + unicode(self.choruscount)
else: else:
self.versecount += 1 self.versecount += 1
versetag = 'V' + str(self.versecount) versetag = u'V' + unicode(self.versecount)
self.verses.append([versetag, verse]) self.verses.append([versetag, verse])
self.order.append(versetag) self.order.append(versetag)
if self.choruscount > 0 and not self.ischorus: if self.choruscount > 0 and not self.ischorus:
self.order.append('C' + str(self.choruscount)) self.order.append(u'C' + unicode(self.choruscount))
def repeat_verse(self): def repeat_verse(self):
self.finish_verse() self.finish_verse()
@ -281,46 +286,46 @@ class sofsong:
def add_verse_line(self, text, inchorus): def add_verse_line(self, text, inchorus):
if inchorus != self.ischorus and ((len(self.verses) > 0) or if inchorus != self.ischorus and ((len(self.verses) > 0) or
(self.currentverse.count('\n') > 1)): (self.currentverse.count(u'\n') > 1)):
self.finish_verse() self.finish_verse()
if inchorus: if inchorus:
self.ischorus = True self.ischorus = True
self.currentverse += text + '\n' self.currentverse += text + u'\n'
def add_author(self, text): def add_author(self, text):
text = text.replace(' and ', ' & ') text = text.replace(u' and ', u' & ')
for author in text.split(','): for author in text.split(u','):
authors = author.split('&') authors = author.split(u'&')
for i in range(len(authors)): for i in range(len(authors)):
author2 = authors[i].strip() author2 = authors[i].strip()
if author2.find(' ') == -1 and i < len(authors) - 1: if author2.find(u' ') == -1 and i < len(authors) - 1:
author2 = author2 + ' ' + authors[i+1].split(' ')[-1] author2 = author2 + u' ' + authors[i + 1].split(u' ')[-1]
self.authors.append(author2) self.authors.append(author2)
def finish(self): def finish(self):
self.finish_verse() self.finish_verse()
if self.songnumber == 0 \ if self.songnumber == 0 \
or self.title == '' \ or self.title == u'' \
or len(self.verses) == 0: or len(self.verses) == 0:
return False return False
if len(self.authors) == 0: if len(self.authors) == 0:
self.authors.append('Author Unknown') self.authors.append(u'Author Unknown')
self.title = self.title + ' - ' + str(self.songnumber)
if self.songnumber <= 640: if self.songnumber <= 640:
self.book = 'Songs of Fellowship 1' self.book = u'Songs of Fellowship 1'
elif self.songnumber <= 1150: elif self.songnumber <= 1150:
self.book = 'Songs of Fellowship 2' self.book = u'Songs of Fellowship 2'
elif self.songnumber <= 1690: elif self.songnumber <= 1690:
self.book = 'Songs of Fellowship 3' self.book = u'Songs of Fellowship 3'
else: else:
self.book = 'Songs of Fellowship 4' self.book = u'Songs of Fellowship 4'
self.print_song() self.print_song()
return True return True
def print_song(self): def print_song(self):
print '===== TITLE: ' + self.title + ' =====' print u'===== TITLE: ' + self.title + u' ====='
print u'ALTTITLE: ' + unicode(self.songnumber)
for (verselabel, verse) in self.verses: for (verselabel, verse) in self.verses:
print "VERSE " + verselabel + ": " + verse print u'VERSE ' + verselabel + u': ' + verse
print u'ORDER: ' + unicode(self.order) print u'ORDER: ' + unicode(self.order)
for author in self.authors: for author in self.authors:
print u'AUTHORS: ' + author print u'AUTHORS: ' + author
@ -436,69 +441,9 @@ class sofsong:
sof = sofimport() sof = sofimport()
sof.start_ooo() sof.start_ooo()
sof.open_sof_file("/home/jonathan/sof.rtf") sof.open_sof_file(u'/home/jonathan/sof.rtf')
#sof.open_sof_file("c:\users\jonathan\Desktop\sof3words.rtf") #sof.open_sof_file(u'c:\users\jonathan\Desktop\sof3words.rtf')
#sof.open_sof_file("c:\users\jonathan\Desktop\sof4words.rtf") #sof.open_sof_file(u'c:\users\jonathan\Desktop\sof4words.rtf')
sof.process_doc() sof.process_doc()
sof.close_ooo() sof.close_ooo()
"""
Sub FinishSong()
sql = "SELECT songid FROM songs where songtitle = '" & Title & "'"
set rs = db.Execute(sql)
If Not rs.EOF Then
rs.MoveFirst
sid = rs("songid")
sql = "UPDATE songs SET songtitle='" & title & "', lyrics='" & lyrics & "', copyrightinfo='" & _
Copyright & "', settingsid=0 WHERE songid=" & sid
db.Execute(sql)
Else
sql = "INSERT INTO songs ('songtitle', 'lyrics', 'copyrightinfo', 'settingsid') VALUES ('" & _
Title & "', '" & Lyrics & "', '" & Copyright & "',0)"
db.Execute(sql)
sql = "SELECT songid FROM songs where songtitle = '" & Title & "'"
set rs = db.Execute(sql)
db.Execute(sql)
rs.MoveFirst
sid = rs("songid")
End If
For aut = LBound(Authors) To UBound(Authors)
Authors(Aut) = Trim(Authors(aut))
If InStr(1, Authors(aut), " ") = 0 And aut < UBound(Authors) Then
temp = Split(Authors(aut+1), " ")
Authors(aut) = Authors(aut) + " " + temp(UBound(temp))
End If
If Right(Authors(aut), 1) = "." Then Authors(aut) = Left(Authors(aut), Len(Authors(aut)) - 1)
Authors(aut) = Replace(Authors(aut), "'", "''")
sql = "SELECT authorid FROM authors where authorname = '" & Authors(aut) & "'"
set rs = db.Execute(sql)
If Not rs.EOF Then
rs.MoveFirst
aid = rs("authorid")
Else
sql = "INSERT INTO authors ('authorname') VALUES ('" & Authors(aut) & "')"
db.Execute(sql)
sql = "SELECT authorid FROM authors where authorname = '" & Authors(aut) & "'"
set rs = db.Execute(sql)
rs.MoveFirst
aid = rs("authorid")
End If
' REPLACE seemed to insert a second duplicate row, so need to do select/insert instead.
sql = "SELECT count(*) FROM songauthors where songid = " & sid & " and authorid = " & aid
set rs = db.Execute(sql)
rs.MoveFirst
If rs(0)=0 Then
sql = "INSERT INTO songauthors VALUES (" & aid & "," & sid &")"
db.Execute(sql)
End If
Next
Exit Sub
End Sub
"""