Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
* Stop accessing element attributes directly (unless absolutely necessary)
* Got rid of all the bare excepts.
* More PEP8 ish style
* And much much more
  • Loading branch information
regebro committed Oct 25, 2017
1 parent 5ff4e04 commit c569bb9
Show file tree
Hide file tree
Showing 20 changed files with 433 additions and 566 deletions.
1 change: 1 addition & 0 deletions src/shoobx/rml2odt/directive.py
Expand Up @@ -15,6 +15,7 @@
"""
from z3c.rml import directive


class NotImplementedDirective(directive.RMLDirective):

def process(self):
Expand Down
27 changes: 10 additions & 17 deletions src/shoobx/rml2odt/document.py
Expand Up @@ -33,33 +33,27 @@


RMLSTYLE_HANDLERS = {
styles.ParagraphStyle: stylesheet.RegisterParagraphStyle
styles.ParagraphStyle: stylesheet.registerParagraphStyle
}


class ColorDefinition(directive.RMLDirective):
signature = rml_document.IColorDefinition

def process(self):
kwargs = dict(self.getAttributeValues())
id = kwargs.pop('id')
colorVal = self.element.attrib.get('value')
if colorVal.startswith('#'):
setattr(reportlab.lib.colors, id, reportlab.lib.colors.HexColor(colorVal))


class RegisterTTFont(directive.RMLDirective):
signature = rml_document.IRegisterTTFont

def process(self):
fontName, location = self.getAttributeValues(valuesOnly=True)

font = FontFace(name=fontName,fontfamily=fontName,
font = FontFace(name=fontName, fontfamily=fontName,
fontfamilygeneric='modern', fontpitch='variable')

source = FontFaceSrc()
urn = FontFaceUri(href = location, actuate = 'onRequest', type='simple')
urn = FontFaceUri(href=location, actuate='onRequest', type='simple')
source.appendChild(urn)
defSource = DefinitionSrc(href = location, actuate = 'onRequest')
defSource = DefinitionSrc(href=location, actuate='onRequest')
font.appendChild(source)
# font.appendChild(defSource)
self.parent.parent.document.fontfacedecls.addElement(font)
Expand All @@ -81,12 +75,11 @@ class DocInit(directive.RMLDirective):
# 'startIndex': StartIndex,
}

viewerOptions = dict(
(option[0].lower()+option[1:], option)
for option in ['HideToolbar', 'HideMenubar', 'HideWindowUI', 'FitWindow',
'CenterWindow', 'DisplayDocTitle',
'NonFullScreenPageMode', 'Direction', 'ViewArea',
'ViewClip', 'PrintArea', 'PrintClip', 'PrintScaling'])
viewerOptions = {option[0].lower()+option[1:]: option for option in
['HideToolbar', 'HideMenubar', 'HideWindowUI',
'FitWindow', 'CenterWindow', 'DisplayDocTitle',
'NonFullScreenPageMode', 'Direction', 'ViewArea',
'ViewClip', 'PrintArea', 'PrintClip', 'PrintScaling']}

def process(self):
kwargs = dict(self.getAttributeValues())
Expand Down
30 changes: 18 additions & 12 deletions src/shoobx/rml2odt/easyliststyle.py
Expand Up @@ -19,10 +19,12 @@
# Contributor(s):
#

import re, sys, os.path
sys.path.append(os.path.dirname(__file__))
import re
import sys
import os.path

from odf.style import Style, TextProperties, ListLevelProperties
from odf.text import ListStyle,ListLevelStyleNumber,ListLevelStyleBullet
from odf.text import ListStyle, ListLevelStyleNumber, ListLevelStyleBullet

"""
Create a <text:list-style> element from a string or array.
Expand All @@ -42,11 +44,13 @@
SHOW_ALL_LEVELS = True
SHOW_ONE_LEVEL = False


def styleFromString(name, specifiers, delim, spacing, showAllLevels):
specArray = specifiers.split(delim)
return styleFromList( name, specArray, spacing, showAllLevels )
return styleFromList(name, specArray, spacing, showAllLevels)


def styleFromList( styleName, specArray, spacing, showAllLevels):
def styleFromList(styleName, specArray, spacing, showAllLevels):
bullet = ""
fontname = ""
fontsize = ""
Expand All @@ -60,16 +64,16 @@ def styleFromList( styleName, specArray, spacing, showAllLevels):
listStyle = ListStyle(name=styleName)
numFormatPattern = re.compile("([1IiAa])")
cssLengthPattern = re.compile("([^a-z]+)\\s*([a-z]+)?")
m = cssLengthPattern.search( spacing )
if (m != None):
m = cssLengthPattern.search(spacing)
if (m is not None):
cssLengthNum = float(m.group(1))
if (m.lastindex == 2):
cssLengthUnits = m.group(2)
i = 0
while i < len(specArray):
specification = specArray[i]
m = numFormatPattern.search(specification)
if (m != None):
if (m is not None):
numberFormat = m.group(1)
numPrefix = specification[0:m.start(1)]
numSuffix = specification[m.end(1):]
Expand Down Expand Up @@ -98,11 +102,13 @@ def styleFromList( styleName, specArray, spacing, showAllLevels):
lls.setAttribute('numsuffix', numSuffix)
lls.setAttribute('displaylevels', displayLevels)
else:
lls = ListLevelStyleBullet(level=(i+1),bulletchar=bullet[0])
lls = ListLevelStyleBullet(level=(i+1), bulletchar=bullet[0])
llp = ListLevelProperties()
llp.setAttribute('spacebefore', str(cssLengthNum * (i+1)) + cssLengthUnits)
llp.setAttribute('minlabelwidth', str(cssLengthNum) + cssLengthUnits)
lls.addElement( llp )
llp.setAttribute('spacebefore',
str(cssLengthNum * (i+1)) + cssLengthUnits)
llp.setAttribute('minlabelwidth',
str(cssLengthNum) + cssLengthUnits)
lls.addElement(llp)
listStyle.addElement(lls)
i += 1
return listStyle

0 comments on commit c569bb9

Please sign in to comment.