Skip to content

Commit ef0e972

Browse files
karthik1024Steve Canny
authored and
Steve Canny
committed
compat: add pptx.compat sub-package
1 parent 9dec12e commit ef0e972

File tree

8 files changed

+118
-25
lines changed

8 files changed

+118
-25
lines changed

pptx/compat/__init__.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# encoding: utf-8
2+
3+
"""
4+
Provides Python 2/3 compatibility objects
5+
"""
6+
7+
from __future__ import (
8+
absolute_import, division, print_function, unicode_literals
9+
)
10+
11+
import sys
12+
13+
if sys.version_info >= (3, 0):
14+
from .python3 import ( # noqa
15+
BytesIO, is_integer, is_string, is_unicode, to_unicode, Unicode
16+
)
17+
else:
18+
from .python2 import ( # noqa
19+
BytesIO, is_integer, is_string, is_unicode, to_unicode, Unicode
20+
)

pptx/compat/python2.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# encoding: utf-8
2+
3+
"""
4+
Provides Python 2 compatibility objects
5+
"""
6+
7+
from StringIO import StringIO as BytesIO # noqa
8+
9+
10+
def is_integer(obj):
11+
"""
12+
Return True if *obj* is an integer (int, long), False otherwise.
13+
"""
14+
return isinstance(obj, (int, long))
15+
16+
17+
def is_string(obj):
18+
"""
19+
Return True if *obj* is a string, False otherwise.
20+
"""
21+
return isinstance(obj, basestring)
22+
23+
24+
def is_unicode(obj):
25+
"""
26+
Return True if *obj* is a unicode string, False otherwise.
27+
"""
28+
return isinstance(obj, unicode)
29+
30+
31+
def to_unicode(text):
32+
"""
33+
Return *text* as a unicode string. *text* can be a 7-bit ASCII string,
34+
a UTF-8 encoded 8-bit string, or unicode. String values are converted to
35+
unicode assuming UTF-8 encoding. Unicode values are returned unchanged.
36+
"""
37+
# both str and unicode inherit from basestring
38+
if not isinstance(text, basestring):
39+
tmpl = 'expected UTF-8 encoded string or unicode, got %s value %s'
40+
raise TypeError(tmpl % (type(text), text))
41+
# return unicode strings unchanged
42+
if isinstance(text, unicode):
43+
return text
44+
# otherwise assume UTF-8 encoding, which also works for ASCII
45+
return unicode(text, 'utf-8')
46+
47+
48+
Unicode = unicode

pptx/compat/python3.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# encoding: utf-8
2+
3+
"""
4+
Provides Python 3 compatibility objects
5+
"""
6+
7+
from io import BytesIO # noqa
8+
9+
10+
def is_integer(obj):
11+
"""
12+
Return True if *obj* is an int, False otherwise.
13+
"""
14+
return isinstance(obj, int)
15+
16+
17+
def is_string(obj):
18+
"""
19+
Return True if *obj* is a string, False otherwise.
20+
"""
21+
return isinstance(obj, str)
22+
23+
24+
def is_unicode(obj):
25+
"""
26+
Return True if *obj* is a unicode string, False otherwise.
27+
"""
28+
return isinstance(obj, str)
29+
30+
31+
def to_unicode(text):
32+
"""
33+
Return *text* as a unicode string. All text in Python 3 is unicode, so
34+
this just returns *text* unchanged.
35+
"""
36+
if not isinstance(text, str):
37+
tmpl = 'expected unicode string, got %s value %s'
38+
raise TypeError(tmpl % (type(text), text))
39+
return text
40+
41+
42+
Unicode = str

pptx/oxml/text.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from __future__ import absolute_import
88

99
from . import parse_xml
10+
from ..compat import to_unicode
1011
from ..enum.text import (
1112
MSO_AUTO_SIZE, MSO_TEXT_UNDERLINE_TYPE, MSO_VERTICAL_ANCHOR,
1213
PP_PARAGRAPH_ALIGNMENT
@@ -18,7 +19,7 @@
1819
ST_TextSpacingPoint, ST_TextTypeface, ST_TextWrappingType, XsdBoolean,
1920
XsdString
2021
)
21-
from ..util import Emu, Length, to_unicode
22+
from ..util import Emu, Length
2223
from .xmlchemy import (
2324
BaseOxmlElement, Choice, OneAndOnlyOne, OneOrMore, OptionalAttribute,
2425
RequiredAttribute, ZeroOrMore, ZeroOrOne, ZeroOrOneChoice

pptx/shapes/table.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
from warnings import warn
1010

1111
from . import Subshape
12+
from ..compat import to_unicode
1213
from ..dml.fill import FillFormat
1314
from ..text.text import TextFrame
14-
from ..util import lazyproperty, to_unicode
15+
from ..util import lazyproperty
1516

1617

1718
class Table(object):

pptx/text/text.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from __future__ import absolute_import, print_function
88

9+
from ..compat import to_unicode
910
from ..dml.fill import FillFormat
1011
from ..enum.dml import MSO_FILL
1112
from ..enum.text import MSO_AUTO_SIZE, MSO_UNDERLINE
@@ -14,7 +15,7 @@
1415
from ..opc.constants import RELATIONSHIP_TYPE as RT
1516
from ..oxml.simpletypes import ST_TextWrappingType
1617
from ..shapes import Subshape
17-
from ..util import Centipoints, Emu, lazyproperty, Pt, to_unicode
18+
from ..util import Centipoints, Emu, lazyproperty, Pt
1819

1920

2021
class TextFrame(Subshape):

pptx/util.py

-19
Original file line numberDiff line numberDiff line change
@@ -177,22 +177,3 @@ def get_prop_value(obj):
177177
return value
178178

179179
return property(get_prop_value, doc=docstring)
180-
181-
182-
def to_unicode(text):
183-
"""
184-
Return *text* as a unicode string.
185-
186-
*text* can be a 7-bit ASCII string, a UTF-8 encoded 8-bit string, or
187-
unicode. String values are converted to unicode assuming UTF-8 encoding.
188-
Unicode values are returned unchanged.
189-
"""
190-
# both str and unicode inherit from basestring
191-
if not isinstance(text, basestring):
192-
tmpl = 'expected UTF-8 encoded string or unicode, got %s value %s'
193-
raise TypeError(tmpl % (type(text), text))
194-
# return unicode strings unchanged
195-
if isinstance(text, unicode):
196-
return text
197-
# otherwise assume UTF-8 encoding, which also works for ASCII
198-
return unicode(text, 'utf-8')

tests/test_util.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
import platform
1010
import pytest
1111

12-
from pptx.util import (
13-
Length, Centipoints, Cm, Emu, Inches, Mm, Pt, Px, to_unicode
14-
)
12+
from pptx.compat import to_unicode
13+
from pptx.util import Length, Centipoints, Cm, Emu, Inches, Mm, Pt, Px
1514

1615

1716
def test_to_unicode_raises_on_non_string():

0 commit comments

Comments
 (0)