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

View File

@ -9,7 +9,7 @@ import os
from unittest import TestCase from unittest import TestCase
from mock import patch, MagicMock 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')) TEST_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), u'../../../resources/easyworshipsongs'))
SONG_TEST_DATA = [ SONG_TEST_DATA = [
@ -68,9 +68,11 @@ class TestFieldDesc:
TEST_DATA_ENCODING = u'cp1252' TEST_DATA_ENCODING = u'cp1252'
CODE_PAGE_MAPPINGS = [(852, u'cp1250'), (737, u'cp1253'), (775, u'cp1257'), (855, u'cp1251'), (857, u'cp1254'), 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')] (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), TEST_FIELD_DESCS = [TestFieldDesc(u'Title', FieldType.String, 50),
TestFieldDesc(u'RecID', 4, 4), TestFieldDesc(u'Default Background', 9, 1), TestFieldDesc(u'Words', 12, 250), TestFieldDesc(u'Text Percentage Bottom', FieldType.Int16, 2), TestFieldDesc(u'RecID', FieldType.Int32, 4),
TestFieldDesc(u'Words', 12, 250), TestFieldDesc(u'BK Bitmap', 13, 10), TestFieldDesc(u'Last Modified', 21, 10)] 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, 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;}}' 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;' '{\\colortbl\\red0\\green0\\blue0;\\red255\\green0\\blue0;\\red0\\green128\\blue0;\\red0\\green0\\blue255;'
@ -94,7 +96,7 @@ class TestEasyWorshipSongImport(TestCase):
""" """
# GIVEN: Set arguments # GIVEN: Set arguments
name = u'Title' name = u'Title'
field_type = 1 field_type = FieldType.String
size = 50 size = 50
# WHEN: A FieldDescEntry object is created. # WHEN: A FieldDescEntry object is created.