Lots of small refactors and cleanups

bzr-revno: 1224
This commit is contained in:
Jon Tibble 2011-01-18 16:19:01 +00:00
commit e2e75af066
10 changed files with 153 additions and 177 deletions

View File

@ -28,9 +28,9 @@ import re
try:
import enchant
from enchant import DictNotFoundError
enchant_available = True
ENCHANT_AVAILABLE = True
except ImportError:
enchant_available = False
ENCHANT_AVAILABLE = False
# based on code from
# http://john.nachtimwald.com/2009/08/22/qplaintextedit-with-in-line-spell-check
@ -45,7 +45,7 @@ class SpellTextEdit(QtGui.QPlainTextEdit):
def __init__(self, *args):
QtGui.QPlainTextEdit.__init__(self, *args)
# Default dictionary based on the current locale.
if enchant_available:
if ENCHANT_AVAILABLE:
try:
self.dict = enchant.Dict()
except DictNotFoundError:
@ -72,7 +72,7 @@ class SpellTextEdit(QtGui.QPlainTextEdit):
self.setTextCursor(cursor)
# Check if the selected word is misspelled and offer spelling
# suggestions if it is.
if enchant_available and self.textCursor().hasSelection():
if ENCHANT_AVAILABLE and self.textCursor().hasSelection():
text = unicode(self.textCursor().selectedText())
if not self.dict.check(text):
spell_menu = QtGui.QMenu(translate('OpenLP.SpellTextEdit',

View File

@ -163,8 +163,7 @@ class AdvancedTab(SettingsTab):
settings.value(u'enable exit confirmation',
QtCore.QVariant(True)).toBool())
self.hideMouseCheckBox.setChecked(
settings.value(u'hide mouse',
QtCore.QVariant(False)).toBool())
settings.value(u'hide mouse', QtCore.QVariant(False)).toBool())
settings.endGroup()
def save(self):

View File

@ -26,7 +26,6 @@
"""
The :mod:`utils` module provides the utility libraries for OpenLP
"""
import logging
import os
import re
@ -36,12 +35,18 @@ import urllib2
from datetime import datetime
from PyQt4 import QtGui, QtCore
if sys.platform != u'win32' and sys.platform != u'darwin':
try:
from xdg import BaseDirectory
XDG_BASE_AVAILABLE = True
except ImportError:
XDG_BASE_AVAILABLE = False
import openlp
from openlp.core.lib import Receiver, translate
log = logging.getLogger(__name__)
images_filter = None
IMAGES_FILTER = None
class VersionThread(QtCore.QThread):
"""
@ -113,77 +118,46 @@ class AppLocation(object):
The directory type you want, for instance the data directory.
"""
if dir_type == AppLocation.AppDir:
if hasattr(sys, u'frozen') and sys.frozen == 1:
app_path = os.path.abspath(os.path.split(sys.argv[0])[0])
else:
app_path = os.path.split(openlp.__file__)[0]
return app_path
return _get_frozen_path(
os.path.abspath(os.path.split(sys.argv[0])[0]),
os.path.split(openlp.__file__)[0])
elif dir_type == AppLocation.ConfigDir:
if sys.platform == u'win32':
path = os.path.join(os.getenv(u'APPDATA'), u'openlp')
elif sys.platform == u'darwin':
path = os.path.join(os.getenv(u'HOME'), u'Library',
u'Application Support', u'openlp')
else:
try:
from xdg import BaseDirectory
path = os.path.join(
BaseDirectory.xdg_config_home, u'openlp')
except ImportError:
path = os.path.join(os.getenv(u'HOME'), u'.openlp')
return path
return _get_os_dir_path(u'openlp',
os.path.join(os.getenv(u'HOME'), u'Library',
u'Application Support', u'openlp'),
os.path.join(BaseDirectory.xdg_config_home, u'openlp'),
os.path.join(os.getenv(u'HOME'), u'.openlp'))
elif dir_type == AppLocation.DataDir:
if sys.platform == u'win32':
path = os.path.join(os.getenv(u'APPDATA'), u'openlp', u'data')
elif sys.platform == u'darwin':
path = os.path.join(os.getenv(u'HOME'), u'Library',
u'Application Support', u'openlp', u'Data')
else:
try:
from xdg import BaseDirectory
path = os.path.join(BaseDirectory.xdg_data_home, u'openlp')
except ImportError:
path = os.path.join(os.getenv(u'HOME'), u'.openlp', u'data')
return path
return _get_os_dir_path(os.path.join(u'openlp', u'data'),
os.path.join(os.getenv(u'HOME'), u'Library',
u'Application Support', u'openlp', u'Data'),
os.path.join(BaseDirectory.xdg_data_home, u'openlp'),
os.path.join(os.getenv(u'HOME'), u'.openlp', u'data'))
elif dir_type == AppLocation.PluginsDir:
plugin_path = None
app_path = os.path.abspath(os.path.split(sys.argv[0])[0])
if hasattr(sys, u'frozen') and sys.frozen == 1:
plugin_path = os.path.join(app_path, u'plugins')
else:
plugin_path = os.path.join(
os.path.split(openlp.__file__)[0], u'plugins')
return plugin_path
return _get_frozen_path(os.path.join(app_path, u'plugins'),
os.path.join(os.path.split(openlp.__file__)[0], u'plugins'))
elif dir_type == AppLocation.VersionDir:
if hasattr(sys, u'frozen') and sys.frozen == 1:
version_path = os.path.abspath(os.path.split(sys.argv[0])[0])
else:
version_path = os.path.split(openlp.__file__)[0]
return version_path
return _get_frozen_path(
os.path.abspath(os.path.split(sys.argv[0])[0]),
os.path.split(openlp.__file__)[0])
elif dir_type == AppLocation.CacheDir:
if sys.platform == u'win32':
path = os.path.join(os.getenv(u'APPDATA'), u'openlp')
elif sys.platform == u'darwin':
path = os.path.join(os.getenv(u'HOME'), u'Library',
u'Application Support', u'openlp')
else:
try:
from xdg import BaseDirectory
path = os.path.join(
BaseDirectory.xdg_cache_home, u'openlp')
except ImportError:
path = os.path.join(os.getenv(u'HOME'), u'.openlp')
return path
return _get_os_dir_path(u'openlp',
os.path.join(os.getenv(u'HOME'), u'Library',
u'Application Support', u'openlp'),
os.path.join(BaseDirectory.xdg_cache_home, u'openlp'),
os.path.join(os.getenv(u'HOME'), u'.openlp'))
if dir_type == AppLocation.LanguageDir:
if hasattr(sys, u'frozen') and sys.frozen == 1:
app_path = os.path.abspath(os.path.split(sys.argv[0])[0])
else:
app_path = os.path.split(openlp.__file__)[0]
app_path = _get_frozen_path(
os.path.abspath(os.path.split(sys.argv[0])[0]),
os.path.split(openlp.__file__)[0])
return os.path.join(app_path, u'i18n')
@staticmethod
def get_data_path():
"""
Return the path OpenLP stores all its data under.
"""
path = AppLocation.get_directory(AppLocation.DataDir)
if not os.path.exists(path):
os.makedirs(path)
@ -191,12 +165,38 @@ class AppLocation(object):
@staticmethod
def get_section_data_path(section):
"""
Return the path a particular module stores its data under.
"""
data_path = AppLocation.get_data_path()
path = os.path.join(data_path, section)
if not os.path.exists(path):
os.makedirs(path)
return path
def _get_os_dir_path(win_option, darwin_option, base_dir_option,
non_base_dir_option):
"""
Return a path based on which OS and environment we are running in.
"""
if sys.platform == u'win32':
return os.path.join(os.getenv(u'APPDATA'), win_option)
elif sys.platform == u'darwin':
return darwin_option
else:
if XDG_BASE_AVAILABLE:
return base_dir_option
else:
return non_base_dir_option
def _get_frozen_path(frozen_option, non_frozen_option):
"""
Return a path based on the system status.
"""
if hasattr(sys, u'frozen') and sys.frozen == 1:
return frozen_option
else:
return non_frozen_option
def check_latest_version(current_version):
"""
@ -225,9 +225,8 @@ def check_latest_version(current_version):
remote_version = None
try:
remote_version = unicode(urllib2.urlopen(req, None).read()).strip()
except IOError, e:
if hasattr(e, u'reason'):
log.exception(u'Reason for failure: %s', e.reason)
except IOError:
log.exception(u'Failed to download the latest OpenLP version file')
if remote_version:
version_string = remote_version
return version_string
@ -264,18 +263,21 @@ def get_images_filter():
Returns a filter string for a file dialog containing all the supported
image formats.
"""
global images_filter
if not images_filter:
global IMAGES_FILTER
if not IMAGES_FILTER:
log.debug(u'Generating images filter.')
formats = [unicode(fmt)
for fmt in QtGui.QImageReader.supportedImageFormats()]
visible_formats = u'(*.%s)' % u'; *.'.join(formats)
actual_formats = u'(*.%s)' % u' *.'.join(formats)
images_filter = u'%s %s %s' % (translate('OpenLP', 'Image Files'),
IMAGES_FILTER = u'%s %s %s' % (translate('OpenLP', 'Image Files'),
visible_formats, actual_formats)
return images_filter
return IMAGES_FILTER
def split_filename(path):
"""
Return a list of the parts in a given path.
"""
path = os.path.abspath(path)
if not os.path.isfile(path):
return path, u''

View File

@ -48,49 +48,27 @@ class VerseType(object):
``verse_type``
The type to return a string for
"""
if verse_type == VerseType.Verse:
return translate('SongsPlugin.VerseType', 'Verse')
elif verse_type == VerseType.Chorus:
return translate('SongsPlugin.VerseType', 'Chorus')
elif verse_type == VerseType.Bridge:
return translate('SongsPlugin.VerseType', 'Bridge')
elif verse_type == VerseType.PreChorus:
return translate('SongsPlugin.VerseType', 'Pre-Chorus')
elif verse_type == VerseType.Intro:
return translate('SongsPlugin.VerseType', 'Intro')
elif verse_type == VerseType.Ending:
return translate('SongsPlugin.VerseType', 'Ending')
elif verse_type == VerseType.Other:
return translate('SongsPlugin.VerseType', 'Other')
@staticmethod
def expand_string(verse_type):
"""
Return the VerseType for a given string
``verse_type``
The string to return a VerseType for
"""
verse_type = verse_type.lower()
if verse_type == \
if not isinstance(verse_type, int):
verse_type = verse_type.lower()
if verse_type == VerseType.Verse or verse_type == \
unicode(VerseType.to_string(VerseType.Verse)).lower()[0]:
return translate('SongsPlugin.VerseType', 'Verse')
elif verse_type == \
elif verse_type == VerseType.Chorus or verse_type == \
unicode(VerseType.to_string(VerseType.Chorus)).lower()[0]:
return translate('SongsPlugin.VerseType', 'Chorus')
elif verse_type == \
elif verse_type == VerseType.Bridge or verse_type == \
unicode(VerseType.to_string(VerseType.Bridge)).lower()[0]:
return translate('SongsPlugin.VerseType', 'Bridge')
elif verse_type == \
elif verse_type == VerseType.PreChorus or verse_type == \
unicode(VerseType.to_string(VerseType.PreChorus)).lower()[0]:
return translate('SongsPlugin.VerseType', 'PreChorus')
elif verse_type == \
return translate('SongsPlugin.VerseType', 'Pre-Chorus')
elif verse_type == VerseType.Intro or verse_type == \
unicode(VerseType.to_string(VerseType.Intro)).lower()[0]:
return translate('SongsPlugin.VerseType', 'Intro')
elif verse_type == \
elif verse_type == VerseType.Ending or verse_type == \
unicode(VerseType.to_string(VerseType.Ending)).lower()[0]:
return translate('SongsPlugin.VerseType', 'Ending')
elif verse_type == \
elif verse_type == VerseType.Other or verse_type == \
unicode(VerseType.to_string(VerseType.Other)).lower()[0]:
return translate('SongsPlugin.VerseType', 'Other')
@ -163,7 +141,7 @@ def retrieve_windows_encoding(recommendation=None):
translate('SongsPlugin', 'Character Encoding'),
translate('SongsPlugin', 'The codepage setting is responsible\n'
'for the correct character representation.\n'
'Usually you are fine with the preselected choise.'),
'Usually you are fine with the preselected choice.'),
[pair[1] for pair in encodings], recommended_index, False)
else:
choice = QtGui.QInputDialog.getItem(None,

View File

@ -23,7 +23,9 @@
# with this program; if not, write to the Free Software Foundation, Inc., 59 #
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
###############################################################################
"""
The :mod:`importer` modules provides the general song import functionality.
"""
from opensongimport import OpenSongImport
from olpimport import OpenLPSongImport
from openlyricsimport import OpenLyricsImport
@ -34,19 +36,19 @@ from songbeamerimport import SongBeamerImport
# Imports that might fail
try:
from olp1import import OpenLP1SongImport
has_openlp1 = True
HAS_OPENLP1 = True
except ImportError:
has_openlp1 = False
HAS_OPENLP1 = False
try:
from sofimport import SofImport
has_sof = True
HAS_SOF = True
except ImportError:
has_sof = False
HAS_SOF = False
try:
from oooimport import OooImport
has_ooo = True
HAS_OOO = True
except ImportError:
has_ooo = False
HAS_OOO = False
class SongFormat(object):
"""
@ -118,14 +120,20 @@ class SongFormat(object):
@staticmethod
def set_availability(format, available):
"""
Set the availability for a given song format.
"""
SongFormat._format_availability[format] = available
@staticmethod
def get_availability(format):
"""
Return the availability of a given song format.
"""
return SongFormat._format_availability.get(format, True)
SongFormat.set_availability(SongFormat.OpenLP1, has_openlp1)
SongFormat.set_availability(SongFormat.SongsOfFellowship, has_sof)
SongFormat.set_availability(SongFormat.Generic, has_ooo)
SongFormat.set_availability(SongFormat.OpenLP1, HAS_OPENLP1)
SongFormat.set_availability(SongFormat.SongsOfFellowship, HAS_SOF)
SongFormat.set_availability(SongFormat.Generic, HAS_OOO)
__all__ = [u'SongFormat']
__all__ = [u'SongFormat']

View File

@ -194,8 +194,7 @@ class SongMediaItem(MediaManagerItem):
elif search_type == 5:
log.debug(u'Theme Search')
search_results = self.parent.manager.get_all_objects(Song,
Song.theme_name == search_keywords,
Song.search_lyrics.asc())
Song.theme_name == search_keywords, Song.search_lyrics.asc())
self.displayResultsSong(search_results)
def onSongListLoad(self):

View File

@ -83,7 +83,7 @@ class SongBeamerImport(SongImport):
def do_import(self):
"""
Recieve a single file, or a list of files to import.
Receive a single file or a list of files to import.
"""
if isinstance(self.import_source, list):
self.import_wizard.progressBar.setMaximum(
@ -94,21 +94,21 @@ class SongBeamerImport(SongImport):
self.current_verse = u''
self.current_verse_type = u'V'
read_verses = False
self.file_name = os.path.split(file)[1]
file_name = os.path.split(file)[1]
self.import_wizard.incrementProgressBar(
u'Importing %s' % (self.file_name), 0)
u'Importing %s' % (file_name), 0)
if os.path.isfile(file):
detect_file = open(file, u'r')
details = chardet.detect(detect_file.read(2048))
detect_file.close()
infile = codecs.open(file, u'r', details['encoding'])
self.songData = infile.readlines()
songData = infile.readlines()
infile.close()
else:
return False
self.title = self.file_name.split('.sng')[0]
self.title = file_name.split('.sng')[0]
read_verses = False
for line in self.songData:
for line in songData:
# Just make sure that the line is of the type 'Unicode'.
line = unicode(line).strip()
if line.startswith(u'#') and not read_verses:
@ -136,7 +136,7 @@ class SongBeamerImport(SongImport):
self.finish()
self.import_wizard.incrementProgressBar(unicode(translate(
'SongsPlugin.SongBeamerImport', 'Importing %s...')) %
self.file_name)
file_name)
return True
def replace_html_tags(self):

View File

@ -62,7 +62,6 @@ class SongImport(QtCore.QObject):
Create defaults for properties - call this before each song
if importing many songs at once to ensure a clean beginning
"""
self.authors = []
self.title = u''
self.song_number = u''
self.alternate_title = u''
@ -251,17 +250,11 @@ class SongImport(QtCore.QObject):
def finish(self):
"""
All fields have been set to this song. Write it away
All fields have been set to this song. Write the song to disk.
"""
if not self.authors:
self.authors.append(unicode(translate('SongsPlugin.SongImport',
'Author unknown')))
self.commit_song()
def commit_song(self):
"""
Write the song and its fields to disk
"""
log.info(u'commiting song %s to database', self.title)
song = Song()
song.title = self.title

View File

@ -107,63 +107,60 @@ class WowImport(SongImport):
def do_import(self):
"""
Recieve a single file, or a list of files to import.
Receive a single file or a list of files to import.
"""
if isinstance(self.import_source, list):
self.import_wizard.progressBar.setMaximum(len(self.import_source))
for file in self.import_source:
self.author = u''
self.copyright = u''
self.file_name = os.path.split(file)[1]
author = u''
copyright = u''
file_name = os.path.split(file)[1]
self.import_wizard.incrementProgressBar(
u'Importing %s' % (self.file_name), 0)
u'Importing %s' % (file_name), 0)
# Get the song title
self.title = self.file_name.rpartition(u'.')[0]
self.songData = open(file, 'rb')
if self.songData.read(19) != u'WoW File\nSong Words':
self.title = file_name.rpartition(u'.')[0]
songData = open(file, 'rb')
if songData.read(19) != u'WoW File\nSong Words':
continue
# Seek to byte which stores number of blocks in the song
self.songData.seek(56)
self.no_of_blocks = ord(self.songData.read(1))
songData.seek(56)
no_of_blocks = ord(songData.read(1))
# Seek to the beging of the first block
self.songData.seek(82)
for block in range(self.no_of_blocks):
self.lines_to_read = ord(self.songData.read(1))
songData.seek(82)
for block in range(no_of_blocks):
self.lines_to_read = ord(songData.read(1))
# Skip 3 nulls to the beginnig of the 1st line
self.songData.seek(3, os.SEEK_CUR)
self.block_text = u''
songData.seek(3, os.SEEK_CUR)
block_text = u''
while self.lines_to_read:
self.length_of_line = ord(self.songData.read(1))
self.line_text = unicode(
self.songData.read(self.length_of_line), u'cp1252')
self.songData.seek(1, os.SEEK_CUR)
if self.block_text != u'':
self.block_text += u'\n'
self.block_text += self.line_text
songData.read(ord(songData.read(1))), u'cp1252')
songData.seek(1, os.SEEK_CUR)
if block_text != u'':
block_text += u'\n'
block_text += self.line_text
self.lines_to_read -= 1
self.block_type = BLOCK_TYPES[ord(self.songData.read(1))]
block_type = BLOCK_TYPES[ord(songData.read(1))]
# Skip 3 nulls at the end of the block
self.songData.seek(3, os.SEEK_CUR)
songData.seek(3, os.SEEK_CUR)
# Blocks are seperated by 2 bytes, skip them, but not if
# this is the last block!
if (block + 1) < self.no_of_blocks:
self.songData.seek(2, os.SEEK_CUR)
self.add_verse(self.block_text, self.block_type)
# Now to extact the author
self.author_length = ord(self.songData.read(1))
if self.author_length != 0:
self.author = unicode(
self.songData.read(self.author_length), u'cp1252')
if (block + 1) < no_of_blocks:
songData.seek(2, os.SEEK_CUR)
self.add_verse(block_text, block_type)
# Now to extract the author
author_length = ord(songData.read(1))
if author_length != 0:
author = unicode(songData.read(author_length), u'cp1252')
# Finally the copyright
self.copyright_length = ord(self.songData.read(1))
if self.copyright_length != 0:
self.copyright = unicode(
self.songData.read(self.copyright_length), u'cp1252')
self.parse_author(self.author)
self.add_copyright(self.copyright)
self.songData.close()
copyright_length = ord(songData.read(1))
if copyright_length != 0:
copyright = unicode(
songData.read(copyright_length), u'cp1252')
self.parse_author(author)
self.add_copyright(copyright)
songData.close()
self.finish()
self.import_wizard.incrementProgressBar(
u'Importing %s' % (self.file_name))
u'Importing %s' % (file_name))
return True

View File

@ -435,7 +435,7 @@ class OpenLyrics(object):
text += u'\n'
text += u'\n'.join([unicode(line) for line in lines.line])
verse_name = self._get(verse, u'name')
verse_type = unicode(VerseType.expand_string(verse_name[0]))[0]
verse_type = unicode(VerseType.to_string(verse_name[0]))[0]
verse_number = re.compile(u'[a-zA-Z]*').sub(u'', verse_name)
verse_part = re.compile(u'[0-9]*').sub(u'', verse_name[1:])
# OpenLyrics allows e. g. "c", but we need "c1".