Corrected if statment and added enumeration class.

This commit is contained in:
phill-ridout 2013-06-28 22:16:44 +01:00
parent 6fabfef7d8
commit e9c56b6cc5
2 changed files with 33 additions and 30 deletions

View File

@ -54,6 +54,19 @@ class FieldDescEntry:
self.size = size
class FieldType(object):
"""
An enumeration class for different field types that can be expected in an EasyWorship song file.
"""
String = 1
Int16 = 3
Int32 = 4
Logical = 9
Memo = 0x0c
Blob = 0x0d
Timestamp = 0x15
class EasyWorshipSongImport(SongImport):
"""
The :class:`EasyWorshipSongImport` class provides OpenLP with the
@ -65,7 +78,7 @@ class EasyWorshipSongImport(SongImport):
def doImport(self):
# Open the DB and MB files if they exist
import_source_mb = self.import_source.replace('.DB', '.MB')
if not (os.path.isfile(self.import_source) or os.path.isfile(import_source_mb)):
if not (os.path.isfile(self.import_source) or not os.path.isfile(import_source_mb)):
return
db_size = os.path.getsize(self.import_source)
if db_size < 0x800:
@ -231,26 +244,19 @@ class EasyWorshipSongImport(SongImport):
# Begin with empty field struct list
fsl = ['>']
for field_desc in field_descs:
if field_desc.field_type == 1:
# string
if field_desc.field_type == FieldType.String:
fsl.append('%ds' % field_desc.size)
elif field_desc.field_type == 3:
# 16-bit int
elif field_desc.field_type == FieldType.Int16:
fsl.append('H')
elif field_desc.field_type == 4:
# 32-bit int
elif field_desc.field_type == FieldType.Int32:
fsl.append('I')
elif field_desc.field_type == 9:
# Logical
elif field_desc.field_type == FieldType.Logical:
fsl.append('B')
elif field_desc.field_type == 0x0c:
# Memo
elif field_desc.field_type == FieldType.Memo:
fsl.append('%ds' % field_desc.size)
elif field_desc.field_type == 0x0d:
# Blob
elif field_desc.field_type == FieldType.Blob:
fsl.append('%ds' % field_desc.size)
elif field_desc.field_type == 0x15:
# Timestamp
elif field_desc.field_type == FieldType.Timestamp:
fsl.append('Q')
else:
fsl.append('%ds' % field_desc.size)
@ -267,20 +273,15 @@ class EasyWorshipSongImport(SongImport):
elif field == 0:
return None
# Format the field depending on the field type
if field_desc.field_type == 1:
# string
if field_desc.field_type == FieldType.String:
return field.rstrip('\0').decode(self.encoding)
elif field_desc.field_type == 3:
# 16-bit int
elif field_desc.field_type == FieldType.Int16:
return field ^ 0x8000
elif field_desc.field_type == 4:
# 32-bit int
elif field_desc.field_type == FieldType.Int32:
return field ^ 0x80000000
elif field_desc.field_type == 9:
# Logical
elif field_desc.field_type == FieldType.Logical:
return (field ^ 0x80 == 1)
elif field_desc.field_type == 0x0c or field_desc.field_type == 0x0d:
# Memo or Blob
elif field_desc.field_type == FieldType.Memo or field_desc.field_type == FieldType.Blob:
block_start, blob_size = struct.unpack_from('<II', field, len(field)-10)
sub_block = block_start & 0xff
block_start &= ~0xff

View File

@ -9,7 +9,7 @@ import os
from unittest import TestCase
from mock import patch, MagicMock
from openlp.plugins.songs.lib.ewimport import EasyWorshipSongImport, FieldDescEntry
from openlp.plugins.songs.lib.ewimport import EasyWorshipSongImport, FieldDescEntry, FieldType
TEST_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), u'../../../resources/easyworshipsongs'))
SONG_TEST_DATA = [
@ -68,9 +68,11 @@ class TestFieldDesc:
TEST_DATA_ENCODING = u'cp1252'
CODE_PAGE_MAPPINGS = [(852, u'cp1250'), (737, u'cp1253'), (775, u'cp1257'), (855, u'cp1251'), (857, u'cp1254'),
(866, u'cp1251'), (869, u'cp1253'), (862, u'cp1255'), (874, u'cp874')]
TEST_FIELD_DESCS = [TestFieldDesc(u'Title', 1, 50), TestFieldDesc(u'Text Percentage Bottom', 3, 2),
TestFieldDesc(u'RecID', 4, 4), TestFieldDesc(u'Default Background', 9, 1), TestFieldDesc(u'Words', 12, 250),
TestFieldDesc(u'Words', 12, 250), TestFieldDesc(u'BK Bitmap', 13, 10), TestFieldDesc(u'Last Modified', 21, 10)]
TEST_FIELD_DESCS = [TestFieldDesc(u'Title', FieldType.String, 50),
TestFieldDesc(u'Text Percentage Bottom', FieldType.Int16, 2), TestFieldDesc(u'RecID', FieldType.Int32, 4),
TestFieldDesc(u'Default Background', FieldType.Logical, 1), TestFieldDesc(u'Words', FieldType.Memo, 250),
TestFieldDesc(u'Words', FieldType.Memo, 250), TestFieldDesc(u'BK Bitmap', FieldType.Blob, 10),
TestFieldDesc(u'Last Modified', FieldType.Timestamp, 10)]
TEST_FIELDS = ['A Heart Like Thine\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0', 32868, 2147483750,
129, '{\\rtf1\\ansi\\deff0\\deftab254{\\fonttbl{\\f0\\fnil\\fcharset0 Arial;}{\\f1\\fnil\\fcharset0 Verdana;}}'
'{\\colortbl\\red0\\green0\\blue0;\\red255\\green0\\blue0;\\red0\\green128\\blue0;\\red0\\green0\\blue255;'
@ -94,7 +96,7 @@ class TestEasyWorshipSongImport(TestCase):
"""
# GIVEN: Set arguments
name = u'Title'
field_type = 1
field_type = FieldType.String
size = 50
# WHEN: A FieldDescEntry object is created.