Variable cleanup

This commit is contained in:
Jon Tibble 2010-05-27 15:41:47 +01:00
parent 3678ea5ea2
commit dc96d471e6
14 changed files with 98 additions and 87 deletions

View File

@ -28,10 +28,8 @@ import os
from PyQt4 import QtCore, QtGui
from openlp.core.lib.toolbar import *
from openlp.core.lib import contextMenuAction, contextMenuSeparator, \
SettingsManager
from serviceitem import ServiceItem
SettingsManager, OpenLPToolbar, ServiceItem
log = logging.getLogger(__name__)

View File

@ -38,30 +38,32 @@ class PluginManager(object):
"""
log.info(u'Plugin manager loaded')
def __init__(self, dir):
def __init__(self, plugin_dir):
"""
The constructor for the plugin manager. Passes the controllers on to
the plugins for them to interact with via their ServiceItems.
``dir``
``plugin_dir``
The directory to search for plugins.
"""
log.info(u'Plugin manager initing')
if not dir in sys.path:
log.debug(u'Inserting %s into sys.path', dir)
sys.path.insert(0, dir)
self.basepath = os.path.abspath(dir)
if not plugin_dir in sys.path:
log.debug(u'Inserting %s into sys.path', plugin_dir)
sys.path.insert(0, plugin_dir)
self.basepath = os.path.abspath(plugin_dir)
log.debug(u'Base path %s ', self.basepath)
self.plugin_helpers = []
self.plugins = []
# this has to happen after the UI is sorted self.find_plugins(dir)
# this has to happen after the UI is sorted
# self.find_plugins(plugin_dir)
log.info(u'Plugin manager Initialised')
def find_plugins(self, dir, plugin_helpers):
def find_plugins(self, plugin_dir, plugin_helpers):
"""
Scan the directory ``dir`` for objects inheriting from the ``Plugin``
class.
Scan the directory ``plugin_dir`` for objects inheriting from the
``Plugin`` class.
``dir``
``plugin_dir``
The directory to scan.
``plugin_helpers``
@ -69,10 +71,11 @@ class PluginManager(object):
"""
self.plugin_helpers = plugin_helpers
startdepth = len(os.path.abspath(dir).split(os.sep))
log.debug(u'find plugins %s at depth %d', unicode(dir), startdepth)
startdepth = len(os.path.abspath(plugin_dir).split(os.sep))
log.debug(u'finding plugins in %s at depth %d',
unicode(plugin_dir), startdepth)
for root, dirs, files in os.walk(dir):
for root, dirs, files in os.walk(plugin_dir):
for name in files:
if name.endswith(u'.py') and not name.startswith(u'__'):
path = os.path.abspath(os.path.join(root, name))
@ -80,7 +83,7 @@ class PluginManager(object):
if thisdepth - startdepth > 2:
# skip anything lower down
continue
modulename, pyext = os.path.splitext(path)
modulename = os.path.splitext(path)[0]
prefix = os.path.commonprefix([self.basepath, path])
# hack off the plugin base path
modulename = modulename[len(prefix) + 1:]
@ -91,8 +94,8 @@ class PluginManager(object):
try:
__import__(modulename, globals(), locals(), [])
except ImportError, e:
log.exception(u'Failed to import module %s on path %s for reason %s',
modulename, path, e.args[0])
log.exception(u'Failed to import module %s on path %s '
'for reason %s', modulename, path, e.args[0])
plugin_classes = Plugin.__subclasses__()
plugin_objects = []
for p in plugin_classes:
@ -214,3 +217,4 @@ class PluginManager(object):
if plugin.is_active():
plugin.finalise()
log.info(u'Finalisation Complete for %s ' % plugin.name)

View File

@ -172,11 +172,12 @@ class SettingsManager(object):
path = os.path.join(path, section)
try:
files = os.listdir(path)
except:
except OSError:
return []
if extension:
return [file for file in files
if extension == os.path.splitext(file)[1]]
return [filename for filename in files
if extension == os.path.splitext(filename)[1]]
else:
# no filtering required
return files

View File

@ -136,7 +136,7 @@ class SongXMLParser(object):
try:
self.song_xml = ElementTree(
element=XML(unicode(xml).encode('unicode-escape')))
except:
except ExpatError:
log.exception(u'Invalid xml %s', xml)
def get_verses(self):
@ -144,9 +144,9 @@ class SongXMLParser(object):
Iterates through the verses in the XML and returns a list of verses
and their attributes.
"""
iter = self.song_xml.getiterator()
xml_iter = self.song_xml.getiterator()
verse_list = []
for element in iter:
for element in xml_iter:
if element.tag == u'verse':
if element.text is None:
element.text = u''

View File

@ -97,7 +97,8 @@ class ThemeXML(object):
"""
if self.background_filename and path:
self.theme_name = self.theme_name.rstrip().lstrip()
self.background_filename = self.background_filename.rstrip().lstrip()
self.background_filename = \
self.background_filename.rstrip().lstrip()
self.background_filename = os.path.join(path, self.theme_name,
self.background_filename)
@ -244,7 +245,8 @@ class ThemeXML(object):
background.appendChild(element)
def add_display(self, shadow, shadow_color, outline, outline_color,
horizontal, vertical, wrap, transition, shadow_pixel=5, outline_pixel=2):
horizontal, vertical, wrap, transition, shadow_pixel=5,
outline_pixel=2):
"""
Add a Display options.
@ -349,7 +351,6 @@ class ThemeXML(object):
"""
self.base_parse_xml()
self.parse_xml(xml)
self.theme_filename_extended = False
def base_parse_xml(self):
"""
@ -409,3 +410,4 @@ class ThemeXML(object):
if key[0:1] != u'_':
theme_strings.append(u'%30s: %s' % (key, getattr(self, key)))
return u'\n'.join(theme_strings)

View File

@ -115,26 +115,27 @@ class Theme(object):
for element in iter:
delphiColorChange = False
if element.tag != u'Theme':
t = element.text
element_text = element.text
val = 0
# easy!
if type(t) == type(None):
val = t
if element_text is None:
val = element_text
# strings need special handling to sort the colours out
if type(t) is types.StringType or type(t) is types.UnicodeType:
if t[0] == u'$': # might be a hex number
if type(element_text) is types.StringType or \
type(element_text) is types.UnicodeType:
if element_text[0] == u'$': # might be a hex number
try:
val = int(t[1:], 16)
val = int(element_text[1:], 16)
except ValueError: # nope
pass
elif DelphiColors.has_key(t):
val = DelphiColors[t]
elif DelphiColors.has_key(element_text):
val = DelphiColors[element_text]
delphiColorChange = True
else:
try:
val = int(t)
val = int(element_text)
except ValueError:
val = t
val = element_text
if (element.tag.find(u'Color') > 0 or
(element.tag.find(u'BackgroundParameter') == 0 and
type(val) == type(0))):

View File

@ -154,8 +154,8 @@ class AmendThemeForm(QtGui.QDialog, Ui_AmendThemeDialog):
unicode(self.theme.background_endColor),
self.theme.background_direction)
else:
(path, filename) = \
os.path.split(unicode(self.theme.background_filename))
filename = \
os.path.split(unicode(self.theme.background_filename))[0]
new_theme.add_background_image(filename)
save_to = os.path.join(self.path, theme_name, filename)
save_from = self.theme.background_filename

View File

@ -147,9 +147,11 @@ class ServiceManager(QtGui.QWidget):
self.ServiceManagerList.setAlternatingRowColors(True)
self.ServiceManagerList.setHeaderHidden(True)
self.ServiceManagerList.setExpandsOnDoubleClick(False)
self.ServiceManagerList.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.ServiceManagerList.setContextMenuPolicy(
QtCore.Qt.CustomContextMenu)
QtCore.QObject.connect(self.ServiceManagerList,
QtCore.SIGNAL('customContextMenuRequested(QPoint)'), self.contextMenu)
QtCore.SIGNAL('customContextMenuRequested(QPoint)'),
self.contextMenu)
self.ServiceManagerList.setObjectName(u'ServiceManagerList')
# enable drop
self.ServiceManagerList.__class__.dragEnterEvent = self.dragEnterEvent
@ -172,7 +174,8 @@ class ServiceManager(QtGui.QWidget):
self.trUtf8('Move to end'), self.onServiceEnd)
self.OrderToolbar.addSeparator()
self.OrderToolbar.addToolbarButton(
self.trUtf8('&Delete From Service'), u':/general/general_delete.png',
self.trUtf8('&Delete From Service'),
u':/general/general_delete.png',
self.trUtf8('Delete From Service'), self.onDeleteFromService)
self.Layout.addWidget(self.OrderToolbar)
# Connect up our signals and slots
@ -205,7 +208,8 @@ class ServiceManager(QtGui.QWidget):
self.dndMenu = QtGui.QMenu()
self.newAction = self.dndMenu.addAction(self.trUtf8('&Add New Item'))
self.newAction.setIcon(build_icon(u':/general/general_edit.png'))
self.addToAction = self.dndMenu.addAction(self.trUtf8('&Add to Selected Item'))
self.addToAction = self.dndMenu.addAction(
self.trUtf8('&Add to Selected Item'))
self.addToAction.setIcon(build_icon(u':/general/general_edit.png'))
#build the context menu
self.menu = QtGui.QMenu()
@ -269,7 +273,7 @@ class ServiceManager(QtGui.QWidget):
self.makeLive()
def onServiceItemNoteForm(self):
item, count = self.findServiceItem()
item = self.findServiceItem()[0]
self.serviceNoteForm.textEdit.setPlainText(
self.serviceItems[item][u'service_item'].notes)
if self.serviceNoteForm.exec_():
@ -278,7 +282,7 @@ class ServiceManager(QtGui.QWidget):
self.repaintServiceList(item, 0)
def onServiceItemEditForm(self):
item, count = self.findServiceItem()
item = self.findServiceItem()[0]
self.serviceItemEditForm.setServiceItem(
self.serviceItems[item][u'service_item'])
if self.serviceItemEditForm.exec_():
@ -477,7 +481,7 @@ class ServiceManager(QtGui.QWidget):
"""
Remove the current ServiceItem from the list
"""
item, count = self.findServiceItem()
item = self.findServiceItem()[0]
if item is not -1:
self.serviceItems.remove(self.serviceItems[item])
self.repaintServiceList(0, 0)
@ -514,7 +518,8 @@ class ServiceManager(QtGui.QWidget):
else:
treewidgetitem.setIcon(0, serviceitem.iconic_representation)
else:
treewidgetitem.setIcon(0, build_icon(u':/general/general_delete.png'))
treewidgetitem.setIcon(
0, build_icon(u':/general/general_delete.png'))
treewidgetitem.setText(0, serviceitem.title)
treewidgetitem.setToolTip(0, serviceitem.notes)
treewidgetitem.setData(0, QtCore.Qt.UserRole,
@ -576,7 +581,7 @@ class ServiceManager(QtGui.QWidget):
cPickle.dump(service, file)
file.close()
zip.write(servicefile)
except:
except IOError:
log.exception(u'Failed to save service to disk')
finally:
if file:
@ -585,7 +590,7 @@ class ServiceManager(QtGui.QWidget):
zip.close()
try:
os.remove(servicefile)
except:
except IOError:
pass #if not present do not worry
name = filename.split(os.path.sep)
self.serviceName = name[-1]
@ -636,23 +641,23 @@ class ServiceManager(QtGui.QWidget):
self.parent.serviceSettingsSection,
os.path.split(filename)[0])
zip = None
f = None
file_to = None
try:
zip = zipfile.ZipFile(unicode(filename))
for file in zip.namelist():
osfile = unicode(QtCore.QDir.toNativeSeparators(file))
names = osfile.split(os.path.sep)
file_to = os.path.join(self.servicePath,
file_path = os.path.join(self.servicePath,
names[len(names) - 1])
f = open(file_to, u'wb')
f.write(zip.read(file))
f.flush()
f.close()
if file_to.endswith(u'osd'):
p_file = file_to
f = open(p_file, u'r')
items = cPickle.load(f)
f.close()
file_to = open(file_path, u'wb')
file_to.write(zip.read(file))
file_to.flush()
file_to.close()
if file_path.endswith(u'osd'):
p_file = file_path
file_to = open(p_file, u'r')
items = cPickle.load(file_to)
file_to.close()
self.onNewService()
for item in items:
serviceitem = ServiceItem()
@ -663,13 +668,13 @@ class ServiceManager(QtGui.QWidget):
try:
if os.path.isfile(p_file):
os.remove(p_file)
except:
except IOError:
log.exception(u'Failed to remove osd file')
except:
except IOError:
log.exception(u'Problem loading a service file')
finally:
if f:
f.close()
if file_to:
file_to.close()
if zip:
zip.close()
self.isNew = False
@ -737,7 +742,7 @@ class ServiceManager(QtGui.QWidget):
Service Item to be added
"""
sitem, count = self.findServiceItem()
sitem = self.findServiceItem()[0]
item.render()
if replace:
item.merge(self.serviceItems[sitem][u'service_item'])
@ -789,7 +794,7 @@ class ServiceManager(QtGui.QWidget):
"""
Send the current item to the Preview slide controller
"""
item, count = self.findServiceItem()
item = self.findServiceItem()[0]
if item == -1:
return False
else:
@ -825,7 +830,7 @@ class ServiceManager(QtGui.QWidget):
"""
Posts a remote edit message to a plugin to allow item to be edited.
"""
item, count = self.findServiceItem()
item = self.findServiceItem()[0]
if self.serviceItems[item][u'service_item']\
.is_capable(ItemCapabilities.AllowsEdit):
Receiver.send_message(u'%s_edit' %
@ -942,7 +947,7 @@ class ServiceManager(QtGui.QWidget):
def onThemeChangeAction(self):
theme = unicode(self.sender().text())
item, count = self.findServiceItem()
item = self.findServiceItem()[0]
self.serviceItems[item][u'service_item'].theme = theme
self.regenerateServiceItems()
@ -955,7 +960,7 @@ class ServiceManager(QtGui.QWidget):
def listRequest(self, message=None):
data = []
curindex, count = self.findServiceItem()
curindex = self.findServiceItem()[0]
if curindex >= 0 and curindex < len(self.serviceItems):
curitem = self.serviceItems[curindex]
else:
@ -969,3 +974,4 @@ class ServiceManager(QtGui.QWidget):
data_item[u'selected'] = (item == curitem)
data.append(data_item)
Receiver.send_message(u'servicemanager_list_response', data)

View File

@ -233,7 +233,7 @@ class ThemeManager(QtGui.QWidget):
try:
zip = zipfile.ZipFile(themePath, u'w')
source = os.path.join(self.path, theme)
for root, dirs, files in os.walk(source):
for files in os.walk(source)[2]:
for name in files:
zip.write(
os.path.join(source, name),
@ -272,11 +272,9 @@ class ThemeManager(QtGui.QWidget):
#check to see file is in theme root directory
theme = os.path.join(self.path, name)
if os.path.exists(theme):
(path, filename) = os.path.split(unicode(file))
textName = os.path.splitext(name)[0]
if textName == self.global_theme:
name = u'%s (%s)' % (textName,
self.trUtf8('default'))
name = u'%s (%s)' % (textName, self.trUtf8('default'))
else:
name = textName
thumb = os.path.join(self.thumbPath, u'%s.png' % textName)
@ -567,3 +565,4 @@ class ThemeManager(QtGui.QWidget):
#theme.theme_mode
theme.theme_name = theme.theme_name.strip()
#theme.theme_version

View File

@ -126,7 +126,7 @@ class ImageMediaItem(MediaManagerItem):
def loadList(self, list):
for file in list:
(path, filename) = os.path.split(unicode(file))
filename = os.path.split(unicode(file))[1]
thumb = os.path.join(self.servicePath, filename)
if os.path.exists(thumb):
if self.validate(file, thumb):

View File

@ -143,7 +143,7 @@ class MediaMediaItem(MediaManagerItem):
def loadList(self, list):
for file in list:
(path, filename) = os.path.split(unicode(file))
filename = os.path.split(unicode(file))[1]
item_name = QtGui.QListWidgetItem(filename)
img = QtGui.QPixmap(u':/media/media_video.png').toImage()
item_name.setIcon(build_icon(img))

View File

@ -131,7 +131,7 @@ class PresentationMediaItem(MediaManagerItem):
for file in list:
if currlist.count(file) > 0:
continue
(path, filename) = os.path.split(unicode(file))
filename = os.path.split(unicode(file))[1]
if titles.count(filename) > 0:
QtGui.QMessageBox.critical(
self, self.trUtf8('File exists'), self.trUtf8(

View File

@ -256,35 +256,35 @@ class MessageListener(object):
self.previewHandler.slide(slide, isLive)
def first(self, message):
isLive, item = self.decode_message(message)
isLive = self.decode_message(message)[0]
if isLive:
self.liveHandler.first()
else:
self.previewHandler.first()
def last(self, message):
isLive, item = self.decode_message(message)
isLive = self.decode_message(message)[0]
if isLive:
self.liveHandler.last()
else:
self.previewHandler.last()
def next(self, message):
isLive, item = self.decode_message(message)
isLive = self.decode_message(message)[0]
if isLive:
self.liveHandler.next()
else:
self.previewHandler.next()
def previous(self, message):
isLive, item = self.decode_message(message)
isLive = self.decode_message(message)[0]
if isLive:
self.liveHandler.previous()
else:
self.previewHandler.previous()
def shutdown(self, message):
isLive, item = self.decode_message(message)
isLive = self.decode_message(message)[0]
if isLive:
Receiver.send_message(u'maindisplay_show')
self.liveHandler.shutdown()
@ -292,17 +292,17 @@ class MessageListener(object):
self.previewHandler.shutdown()
def hide(self, message):
isLive, item = self.decode_message(message)
isLive = self.decode_message(message)[0]
if isLive:
self.liveHandler.stop()
def blank(self, message):
isLive, item = self.decode_message(message)
isLive = self.decode_message(message)[0]
if isLive:
self.liveHandler.blank()
def unblank(self, message):
isLive, item = self.decode_message(message)
isLive = self.decode_message(message)[0]
if isLive:
self.liveHandler.unblank()

View File

@ -191,7 +191,7 @@ class HttpConnection(object):
path = os.path.normpath(os.path.join(self.parent.html_dir, filename))
if not path.startswith(self.parent.html_dir):
return None
(fileroot, ext) = os.path.splitext(filename)
ext = os.path.splitext(filename)[1]
if ext == u'.html':
mimetype = u'text/html'
elif ext == u'.css':