diff --git a/gluon/contrib/pyrtf/Constants.py b/gluon/contrib/pyrtf/Constants.py index e8930798..fd011c13 100644 --- a/gluon/contrib/pyrtf/Constants.py +++ b/gluon/contrib/pyrtf/Constants.py @@ -1,3 +1,7 @@ +import sys + +PY2 = sys.version_info[0] == 2 + class ViewKind : """An integer (0-5) that represents the view mode of the document.""" diff --git a/gluon/contrib/pyrtf/Elements.py b/gluon/contrib/pyrtf/Elements.py index afdff5f8..308872dd 100644 --- a/gluon/contrib/pyrtf/Elements.py +++ b/gluon/contrib/pyrtf/Elements.py @@ -1,10 +1,16 @@ from __future__ import print_function -from types import IntType, FloatType, LongType, StringTypes -from copy import deepcopy -from binascii import hexlify +from copy import deepcopy +from binascii import hexlify -from Constants import * -from Styles import * +from .Constants import * +from .Styles import * + +if PY2: + NumberTypes = (int, float, long) + StringType = basestring +else: + NumberTypes = (int, float) + StringType = str class UnhandledParamError( Exception ) : def __init__( self, param ) : @@ -587,7 +593,7 @@ class Table : def AddRow( self, *cells ) : height = None - if isinstance( cells[ 0 ], (IntType, FloatType, LongType) ): + if isinstance( cells[ 0 ], NumberTypes ): height = int( cells[ 0 ] ) cells = cells[ 1 : ] diff --git a/gluon/contrib/pyrtf/PropertySets.py b/gluon/contrib/pyrtf/PropertySets.py index 3816e5ca..d5871f5a 100644 --- a/gluon/contrib/pyrtf/PropertySets.py +++ b/gluon/contrib/pyrtf/PropertySets.py @@ -9,9 +9,14 @@ The TextPropertySet can be used for text or in a Paragraph Style. """ -from types import StringType -from copy import deepcopy +from copy import deepcopy +from .Constants import PY2 + +if PY2: + StringType = basestring +else: + StringType = str # # We need some basic Type like fonts, colours and paper definitions diff --git a/gluon/contrib/pyrtf/Renderer.py b/gluon/contrib/pyrtf/Renderer.py index 33273a7c..7c8829f6 100644 --- a/gluon/contrib/pyrtf/Renderer.py +++ b/gluon/contrib/pyrtf/Renderer.py @@ -1,6 +1,11 @@ -from types import StringType, ListType, TupleType from copy import deepcopy -from Elements import * + +from .Elements import * +from .Constants import PY2 +if PY2: + StringType = basestring +else: + StringType = str DEFAULT_TAB_WIDTH = 720 @@ -438,7 +443,7 @@ class Renderer : elif clss == Table : self.WriteTableElement( element ) - elif clss == StringType : + elif ininstance(element, StringType) : self.WriteParagraphElement( Paragraph( element ) ) elif clss in [ RawCode, Image ] : diff --git a/gluon/contrib/pyrtf/Styles.py b/gluon/contrib/pyrtf/Styles.py index 9494cbd8..402fe991 100644 --- a/gluon/contrib/pyrtf/Styles.py +++ b/gluon/contrib/pyrtf/Styles.py @@ -6,7 +6,7 @@ At present there are only two, Text and Paragraph but ListStyles will be added s """ -from PropertySets import * +from .PropertySets import * class TextStyle : def __init__( self, text_props, name=None, shading_props=None ) : diff --git a/gluon/contrib/pyrtf/__init__.py b/gluon/contrib/pyrtf/__init__.py index 7cfce418..1b55c5aa 100644 --- a/gluon/contrib/pyrtf/__init__.py +++ b/gluon/contrib/pyrtf/__init__.py @@ -1,12 +1,17 @@ -from PropertySets import * -from Elements import * -from Styles import * -from Renderer import * +from .PropertySets import * +from .Elements import * +from .Styles import * +from .Renderer import * +from .Constants import PY2 + +if PY2: + from cStringIO import StringIO as BytesIO +else: + from io import BytesIO def dumps(doc): - import cStringIO - s=cStringIO.StringIO() - r=Renderer() - r.Write(doc,s) + s = BytesIO() + r = Renderer() + r.Write(doc, s) return s.getvalue()