Started fixing up docstrings and doing some more of a code cleanup.

This commit is contained in:
Raoul Snyman 2009-07-08 08:55:08 +02:00
parent c8d5b7acb2
commit e66fa9b87d
9 changed files with 345 additions and 155 deletions

View File

@ -21,27 +21,35 @@ Place, Suite 330, Boston, MA 02111-1307 USA
import codecs
import sys
def convert_file(self, inname, outname):
"""
Convert a file from another encoding into UTF-8.
class Convert():
def __init__(self):
pass
``inname``
The name of the file to be opened and converted.
def process(self, inname, outname):
infile = codecs.open(inname, 'r', encoding='iso-8859-1')
writefile = codecs.open(outname, 'w', encoding='utf-8')
for line in infile:
#replace the quotes with quotes
line = line.replace(u'\'\'', u'\'')
writefile.write(line)
infile.close()
writefile.close()
``outname``
The output file name.
"""
infile = codecs.open(inname, 'r', encoding='iso-8859-1')
writefile = codecs.open(outname, 'w', encoding='utf-8')
for line in infile:
#replace the quotes with quotes
line = line.replace(u'\'\'', u'\'')
writefile.write(line)
infile.close()
writefile.close()
if __name__ == '__main__':
"""
Run the conversion script.
"""
if len(sys.argv) < 2:
print 'No action specified.'
sys.exit()
print u'Uncode conversion '
print u'Input file = ', sys.argv[1:]
print u'Output file = ', sys.argv[2:]
mig = Convert()
mig.process(sys.argv[1:],sys.argv[2:])
print 'Uncode conversion:'
print 'Input file = ', sys.argv[1]
print 'Output file = ', sys.argv[2]
print 'Converting...'
convert_file(sys.argv[1], sys.argv[2])
print 'Done.'

73
demo.py
View File

@ -16,12 +16,14 @@ this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA
"""
from openlp.core import Renderer
from openlp.theme import Theme
import sys
import time
from PyQt4 import QtGui, QtCore
from openlp.core import Renderer
from openlp.theme import Theme
words="""How sweet the name of Jesus sounds
In a believer's ear!
It soothes his sorrows, heals his wounds,
@ -29,53 +31,74 @@ And drives away his fear.
"""
class TstFrame(QtGui.QMainWindow):
""" We simply derive a new class of QMainWindow"""
# {{{ init
"""
We simply derive a new class of QMainWindow
"""
def __init__(self, *args, **kwargs):
"""Create the DemoPanel."""
"""
Create the DemoPanel.
"""
QtGui.QMainWindow.__init__(self)
self.resize(1024,768)
self.size=(1024,768)
self.v=0
self._font=QtGui.QFont(u'Decorative', 32)
self.framecount=0
self.resize(1024, 768)
self.size = (1024, 768)
self.v = 0
self._font = QtGui.QFont(u'Decorative', 32)
self.framecount = 0
self.totaltime = 0
self.dir=1
self.y=1
# self.startTimer(10)
self.frame=QtGui.QFrame()
self.dir = 1
self.y = 1
self.frame = QtGui.QFrame()
self.setCentralWidget(self.frame)
self.r=Renderer()
self.r = Renderer()
self.r.set_theme(Theme(u'demo_theme.xml'))
self.r.set_text_rectangle(self.frame.frameRect())
self.r.set_paint_dest(self)
self.r.set_words_openlp(words)
def timerEvent(self, event):
"""
Update the form on a timer event.
``event``
The event which triggered this update.
"""
self.update()
def paintEvent(self, event):
"""
Repaint the canvas.
``event``
The event which triggered this repaint.
"""
self.r.set_text_rectangle(self.frame.frameRect())
self.r.scale_bg_image()
t1=time.clock()
t1 = time.clock()
self.r.render_screen(0)
t2 = time.clock()
deltat=t2-t1
deltat = t2 - t1
self.totaltime += deltat
self.framecount+=1
self.framecount += 1
print "Timing result: %5.3ffps" %(self.framecount/float(self.totaltime))
# }}}
class Demo:
class Demo(object):
"""
The demo application itself.
"""
def __init__(self):
"""
Construct the application.
"""
app = QtGui.QApplication(sys.argv)
main=TstFrame()
main = TstFrame()
main.show()
sys.exit(app.exec_())
if __name__=="__main__":
t=Demo()
"""
Run the demo.
"""
t = Demo()

View File

@ -23,22 +23,29 @@ import sys
import logging
from PyQt4 import QtCore, QtGui
from openlp.core.lib import Receiver
from openlp.core.resources import *
from openlp.core.ui import MainWindow, SplashScreen
logging.basicConfig(level=logging.DEBUG,
format=u'%(asctime)s:%(msecs)3d %(name)-15s %(levelname)-8s %(message)s',
datefmt=u'%m-%d %H:%M:%S', filename=u'openlp.log', filemode=u'w')
from openlp.core.resources import *
from openlp.core.ui import MainWindow, SplashScreen
class OpenLP(QtGui.QApplication):
"""
The core application class. This class inherits from Qt's QApplication
class in order to provide the core of the application.
"""
global log
log = logging.getLogger(u'OpenLP Application')
log.info(u'Application Loaded')
def run(self):
#set the default string encoding
"""
Run the OpenLP application.
"""
#set the default string encoding
try:
sys.setappdefaultencoding(u'utf-8')
except:
@ -68,5 +75,8 @@ class OpenLP(QtGui.QApplication):
sys.exit(app.exec_())
if __name__ == u'__main__':
"""
Instantiate and run the application.
"""
app = OpenLP(sys.argv)
app.run()

View File

@ -17,6 +17,7 @@ You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA
"""
from settingsmanager import SettingsManager
from openlp.core.lib.pluginmanager import PluginManager

View File

@ -19,11 +19,13 @@ Place, Suite 330, Boston, MA 02111-1307 USA
For XML Schema see wiki.openlp.org
"""
import os, os.path
from openlp.core.lib import str_to_bool
from xml.dom.minidom import Document
import os
from xml.dom.minidom import Document
from xml.etree.ElementTree import ElementTree, XML, dump
from openlp.core.lib import str_to_bool
blankthemexml=\
'''<?xml version="1.0" encoding="iso-8859-1"?>
<theme version="1.0">
@ -62,26 +64,38 @@ blankthemexml=\
</theme>
'''
class ThemeXML():
class ThemeXML(object):
"""
A class to encapsulate the Theme XML.
"""
def __init__(self):
"""
Initialise the theme object.
"""
# Create the minidom document
self.theme_xml = Document()
def extend_image_filename(self, path):
"""
Add the path name to the image name so the background can be rendered.
``path``
The path name to be added.
"""
if self.background_filename is not None:
self.background_filename = os.path.join(path, self.theme_name, self.background_filename)
if self.background_filename is not None and path is not None:
self.background_filename = os.path.join(path, self.theme_name,
self.background_filename)
def new_document(self, name):
"""
Create a new theme XML document.
"""
self.theme = self.theme_xml.createElement(u'theme')
self.theme_xml.appendChild(self.theme)
self.theme.setAttribute(u'version', u'1.0')
self.name = self.theme_xml.createElement(u'name')
ctn = self.theme_xml.createTextNode(name)
self.name.appendChild(ctn)
text_node = self.theme_xml.createTextNode(name)
self.name.appendChild(text_node)
self.theme.appendChild(self.name)
def add_background_transparent(self):
@ -95,23 +109,33 @@ class ThemeXML():
def add_background_solid(self, bkcolor):
"""
Add a Solid background.
``bkcolor``
The color of the background.
"""
background = self.theme_xml.createElement(u'background')
background.setAttribute(u'mode', u'opaque')
background.setAttribute(u'type', u'solid')
self.theme.appendChild(background)
self.child_element(background, u'color', bkcolor)
def add_background_gradient(self, startcolor, endcolor, direction):
"""
Add a gradient background.
``startcolor``
The gradient's starting colour.
``endcolor``
The gradient's ending colour.
``direction``
The direction of the gradient.
"""
background = self.theme_xml.createElement(u'background')
background.setAttribute(u'mode', u'opaque')
background.setAttribute(u'type', u'gradient')
self.theme.appendChild(background)
# Create startColor element
self.child_element(background, u'startColor', startcolor)
# Create endColor element
@ -122,39 +146,63 @@ class ThemeXML():
def add_background_image(self, filename):
"""
Add a image background.
``filename``
The file name of the image.
"""
background = self.theme_xml.createElement(u'background')
background.setAttribute(u'mode', u'opaque')
background.setAttribute(u'type', u'image')
self.theme.appendChild(background)
#Create Filename element
self.child_element(background, u'filename', filename)
def add_font(self, name, color, proportion, override, fonttype=u'main', xpos=0, ypos=0 ,width=0, height=0):
def add_font(self, name, color, proportion, override, fonttype=u'main',
xpos=0, ypos=0, width=0, height=0):
"""
Add a Font.
``name``
The name of the font.
``color``
The colour of the font.
``proportion``
The size of the font.
``override``
Whether or not to override the default positioning of the theme.
``fonttype``
The type of font, ``main`` or ``footer``. Defaults to ``main``.
``xpos``
The X position of the text block.
``ypos``
The Y position of the text block.
``width``
The width of the text block.
``height``
The height of the text block.
"""
background = self.theme_xml.createElement(u'font')
background.setAttribute(u'type',fonttype)
self.theme.appendChild(background)
#Create Font name element
self.child_element(background, u'name', name)
#Create Font color element
self.child_element(background, u'color', color)
#Create Proportion name element
self.child_element(background, u'proportion', proportion)
#Create Proportion name element
self.child_element(background, u'proportion', proportion)
#Create Location element
element = self.theme_xml.createElement(u'location')
element.setAttribute(u'override',override)
if override == u'True':
element.setAttribute(u'x', xpos)
element.setAttribute(u'y', ypos)
@ -162,79 +210,120 @@ class ThemeXML():
element.setAttribute(u'height', height)
background.appendChild(element)
def add_display(self, shadow, shadowColor, outline, outlineColor, horizontal, vertical, wrap):
def add_display(self, shadow, shadow_color, outline, outline_color,
horizontal, vertical, wrap):
"""
Add a Display options.
``shadow``
Whether or not to show a shadow.
``shadow_color``
The colour of the shadow.
``outline``
Whether or not to show an outline.
``outline_color``
The colour of the outline.
``horizontal``
The horizontal alignment of the text.
``vertical``
The vertical alignment of the text.
``wrap``
Wrap style.
"""
background = self.theme_xml.createElement(u'display')
self.theme.appendChild(background)
tagElement = self.theme_xml.createElement(u'shadow')
tagElement.setAttribute(u'color',shadowColor)
tagValue = self.theme_xml.createTextNode(shadow)
tagElement.appendChild(tagValue)
background.appendChild(tagElement)
tagElement = self.theme_xml.createElement(u'outline')
tagElement.setAttribute(u'color',outlineColor)
tagValue = self.theme_xml.createTextNode(outline)
tagElement.appendChild(tagValue)
background.appendChild(tagElement)
tagElement = self.theme_xml.createElement(u'horizontalAlign')
tagValue = self.theme_xml.createTextNode(horizontal)
tagElement.appendChild(tagValue)
background.appendChild(tagElement)
tagElement = self.theme_xml.createElement(u'verticalAlign')
tagValue = self.theme_xml.createTextNode(vertical)
tagElement.appendChild(tagValue)
background.appendChild(tagElement)
tagElement = self.theme_xml.createElement(u'wrapStyle')
tagValue = self.theme_xml.createTextNode(wrap)
tagElement.appendChild(tagValue)
background.appendChild(tagElement)
# Shadow
element = self.theme_xml.createElement(u'shadow')
element.setAttribute(u'color', shadow_color)
value = self.theme_xml.createTextNode(shadow)
element.appendChild(value)
background.appendChild(element)
# Outline
element = self.theme_xml.createElement(u'outline')
element.setAttribute(u'color', outline_color)
value = self.theme_xml.createTextNode(outline)
element.appendChild(value)
background.appendChild(element)
# Horizontal alignment
element = self.theme_xml.createElement(u'horizontalAlign')
value = self.theme_xml.createTextNode(horizontal)
element.appendChild(value)
background.appendChild(element)
# Vertical alignment
element = self.theme_xml.createElement(u'verticalAlign')
value = self.theme_xml.createTextNode(vertical)
element.appendChild(value)
background.appendChild(element)
# Wrap style
element = self.theme_xml.createElement(u'wrapStyle')
value = self.theme_xml.createTextNode(wrap)
element.appendChild(value)
background.appendChild(element)
def child_element(self, element, tag, value):
"""
Generic child element creator.
"""
child = self.theme_xml.createElement(tag)
child.appendChild(self.theme_xml.createTextNode(value))
element.appendChild(child)
return child
def dump_xml(self):
"""
Dump the XML to file.
"""
# Debugging aid to see what we have
print self.theme_xml.toprettyxml(indent=u' ')
def extract_xml(self):
"""
Pull out the XML string.
"""
# Print our newly created XML
return self.theme_xml.toxml()
def parse(self, xml):
self.baseParseXml()
"""
Read in an XML string and parse it.
``xml``
The XML string to parse.
"""
self.base_parse_xml()
self.parse_xml(xml)
self.theme_filename_extended = False
def baseParseXml(self):
def base_parse_xml(self):
"""
Pull in the blank theme XML as a starting point.
"""
self.parse_xml(blankthemexml)
def parse_xml(self, xml):
"""
Parse an XML string.
``xml``
The XML string to parse.
"""
theme_xml = ElementTree(element=XML(xml))
iter = theme_xml.getiterator()
master = u''
for element in iter:
#print element.tag, element.text
if len(element.getchildren()) > 0:
master = element.tag + u'_'
if len(element.attrib) > 0:
#print "D", element.tag , element.attrib
for e in element.attrib.iteritems():
#print "A", master, e[0], e[1]
if master == u'font_' and e[0] == u'type':
master += e[1] + u'_'
elif master == u'display_' and (element.tag == u'shadow' or element.tag == u'outline'):
#print "b", master, element.tag, element.text, e[0], e[1]
et = str_to_bool(element.text)
setattr(self, master + element.tag , et)
setattr(self, master + element.tag + u'_'+ e[0], e[1])
@ -245,12 +334,14 @@ class ThemeXML():
e1 = str_to_bool(e[1])
setattr(self, field, e1)
else:
#print "c", element.tag, element.text
if element.tag is not None:
field = master + element.tag
setattr(self, field, element.text)
def __str__(self):
"""
Return a string representation of this object.
"""
s = u''
for k in dir(self):
if k[0:1] != u'_':

View File

@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4
"""
@ -19,15 +18,19 @@ this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA
"""
import types
import logging
from PyQt4 import QtCore, QtGui
import logging
class OpenLPToolbar(QtGui.QToolBar):
"""
Lots of toolbars around the place, so it makes sense to have a common way to manage them
Lots of toolbars around the place, so it makes sense to have a common way
to manage them. This is the base toolbar class.
"""
def __init__(self, parent):
"""
Initialise the toolbar.
"""
QtGui.QToolBar.__init__(self, None)
# useful to be able to reuse button icons...
self.icons = {}
@ -37,6 +40,23 @@ class OpenLPToolbar(QtGui.QToolBar):
def addToolbarButton(self, title, icon, tooltip=None, slot=None, objectname=None):
"""
A method to help developers easily add a button to the toolbar.
``title``
The title of the button.
``icon``
The icon of the button. This can be an instance of QIcon, or a
string cotaining either the absolute path to the image, or an
internal resource path starting with ':/'.
``tooltip``
A hint or tooltip for this button.
``slot``
The method to run when this button is clicked.
``objectname``
The name of the object, as used in `<button>.setObjectName()`.
"""
ButtonIcon = None
if type(icon) is QtGui.QIcon:
@ -58,6 +78,13 @@ class OpenLPToolbar(QtGui.QToolBar):
self.icons[title] = ButtonIcon
def getIconFromTitle(self, title):
"""
Search through the list of icons for an icon with a particular title,
and return that icon.
``title``
The title of the icon to search for.
"""
if self.icons.has_key(title):
return self.icons[title]
else:

View File

@ -22,81 +22,80 @@ import platform
import sys
import os
from types import StringType, NoneType, UnicodeType
sys.path.append(os.path.abspath(u'./../..'))
from xml.etree.ElementTree import ElementTree, XML
sys.path.append(os.path.abspath(os.path.join('.', '..', '..')))
class XmlRootClass(object):
"""Root class for themes, songs etc
provides interface for parsing xml files into object attributes
if you overload this class and provide a function called
post_tag_hook, it will be called thusly for each tag,value pair:
(element.tag, val) = self.post_tag_hook(element.tag, val)
"""
def _setFromXml(self, xml, rootTag):
"""Set song properties from given xml content
Root class for themes, songs etc
xml (string) -- formatted as xml tags and values
rootTag -- main tag of the xml
This class provides interface for parsing xml files into object attributes.
If you overload this class and provide a function called `post_tag_hook`,
it will be called thusly for each `tag, value` pair::
(element.tag, val) = self.post_tag_hook(element.tag, val)
"""
def _setFromXml(self, xml, root_tag):
"""
Set song properties from given xml content.
``xml``
Formatted xml tags and values.
``root_tag``
The root tag of the xml.
"""
root = ElementTree(element=XML(xml))
iter = root.getiterator()
for element in iter:
if element.tag != rootTag:
t = element.text
#print element.tag, t, type(t)
if type(t) == NoneType:
# easy!
val=t
elif type(t) == UnicodeType :
val=t
elif type(t) == StringType:
# strings need special handling to sort the colours out
#print "str",
if t[0] == '$':
# might be a hex number
#print "hex",
if element.tag != root_tag:
text = element.text
if type(text) is NoneType:
val = text
elif type(text) is UnicodeType :
val = text
elif type(text) is StringType:
# Strings need special handling to sort the colours out
if text[0] == '$':
# This might be a hex number, let's try to convert it.
try:
val = int(t[1:], 16)
val = int(text[1:], 16)
except ValueError:
# nope
#print "nope",
pass
else:
#print "last chance",
# Let's just see if it's a integer.
try:
val=int(t)
#print "int",
val = int(text)
except ValueError:
#print "give up",
val=t
# Ok, it seems to be a string.
val = text
if hasattr(self, u'post_tag_hook'):
(element.tag, val) = self.post_tag_hook(element.tag, val)
setattr(self, element.tag, val)
pass
def __str__(self):
"""Return string with all public attributes
"""
Return string with all public attributes
The string is formatted with one attribute per line
If the string is split on newline then the length of the
list is equal to the number of attributes
"""
l = []
for k in dir(self):
if not k.startswith(u'_'):
l.append(u'%30s : %s' %(k,getattr(self,k)))
return u'\n'.join(l)
attributes = []
for attrib in dir(self):
if not attrib.startswith(u'_'):
attributes.append(u'%30s : %s' % (attrib, getattr(self, attrib)))
return u'\n'.join(attributes)
def _get_as_string(self):
"""Return one string with all public attributes"""
s=""
for k in dir(self):
if not k.startswith(u'_'):
s+= u'_%s_' %(getattr(self,k))
return s
"""
Return one string with all public attributes
"""
result = u''
for attrib in dir(self):
if not attrib.startswith(u'_'):
result += u'_%s_' % getattr(self, attrib)
return result

View File

@ -19,5 +19,7 @@ Place, Suite 330, Boston, MA 02111-1307 USA
"""
class SettingsManager(object):
def __init__(self):
pass
"""
A base settings manager class.
"""
pass

View File

@ -1,4 +1,23 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4
"""
OpenLP - Open Source Lyrics Projection
Copyright (c) 2008 Raoul Snyman
Portions copyright (c) 2008-2009 Martin Thompson, Tim Bentley,
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA
"""
import os
import sys
@ -17,20 +36,30 @@ logging.basicConfig(level=logging.DEBUG,
filename='openlp-migration.log',
filemode='w')
class Migration():
class Migration(object):
"""
A class to take care of the migration process.
"""
def __init__(self):
"""
Initialise the process.
"""
self.display = Display()
self.stime = time.strftime(u'%Y-%m-%d-%H%M%S', time.localtime())
self.display.output(u'OpenLp v1.9.0 Migration Utility Started')
def process(self):
"""
Perform the conversion.
"""
#MigrateFiles(self.display).process()
MigrateSongs(self.display).process()
#MigrateBibles(self.display).process()
def move_log_file(self):
"""
Move the log file to a new location.
"""
fname = 'openlp-migration.log'
c = os.path.splitext(fname)
b = (c[0]+'-'+ unicode(self.stime) + c[1])