forked from python-openxml/python-docx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshared.py
More file actions
165 lines (132 loc) · 4.11 KB
/
shared.py
File metadata and controls
165 lines (132 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# encoding: utf-8
"""
Objects shared by docx modules.
"""
from __future__ import absolute_import, print_function, unicode_literals
class Length(int):
"""
Base class for length constructor classes Inches, Cm, Mm, Px, and Emu.
Behaves as an int count of English Metric Units, 914400 to the inch,
36000 to the cm. Provides convenience unit conversion methods in the form
of read-only properties. Immutable.
"""
_EMUS_PER_INCH = 914400
_EMUS_PER_CM = 360000
_EMUS_PER_MM = 36000
_EMUS_PER_PX = 12700
def __new__(cls, emu):
return int.__new__(cls, emu)
@property
def inches(self):
"""
The equivalent length expressed in inches (float).
"""
return self / float(self._EMUS_PER_INCH)
@property
def cm(self):
"""
The equivalent length expressed in centimeters (float).
"""
return self / float(self._EMUS_PER_CM)
@property
def mm(self):
"""
The equivalent length expressed in millimeters (float).
"""
return self / float(self._EMUS_PER_MM)
@property
def px(self):
# round can somtimes return values like x.999999 which are truncated
# to x by int(); adding the 0.1 prevents this
return int(round(self / float(self._EMUS_PER_PX)) + 0.1)
@property
def emu(self):
"""
The equivalent length expressed in English Metric Units (int).
"""
return self
class Inches(Length):
"""
Convenience constructor for length in inches, e.g.
``width = Inches(0.5)``.
"""
def __new__(cls, inches):
emu = int(inches * Length._EMUS_PER_INCH)
return Length.__new__(cls, emu)
class Cm(Length):
"""
Convenience constructor for length in centimeters, e.g.
``height = Cm(12)``.
"""
def __new__(cls, cm):
emu = int(cm * Length._EMUS_PER_CM)
return Length.__new__(cls, emu)
class Emu(Length):
"""
Convenience constructor for length in English Metric Units, e.g.
``width = Emu(457200)``.
"""
def __new__(cls, emu):
return Length.__new__(cls, int(emu))
class Mm(Length):
"""
Convenience constructor for length in millimeters, e.g.
``width = Mm(240.5)``.
"""
def __new__(cls, mm):
emu = int(mm * Length._EMUS_PER_MM)
return Length.__new__(cls, emu)
class Pt(int):
"""
Convenience class for setting font sizes in points
"""
_UNITS_PER_POINT = 100
def __new__(cls, pts):
units = int(pts * Pt._UNITS_PER_POINT)
return int.__new__(cls, units)
class Px(Length):
"""
Convenience constructor for length in pixels.
"""
def __new__(cls, px):
emu = int(px * Length._EMUS_PER_PX)
return Length.__new__(cls, emu)
def lazyproperty(f):
"""
@lazyprop decorator. Decorated method will be called only on first access
to calculate a cached property value. After that, the cached value is
returned.
"""
cache_attr_name = '_%s' % f.__name__ # like '_foobar' for prop 'foobar'
docstring = f.__doc__
def get_prop_value(obj):
try:
return getattr(obj, cache_attr_name)
except AttributeError:
value = f(obj)
setattr(obj, cache_attr_name, value)
return value
return property(get_prop_value, doc=docstring)
def write_only_property(f):
"""
@write_only_property decorator. Creates a property (descriptor attribute)
that accepts assignment, but not getattr (use in an expression).
"""
docstring = f.__doc__
return property(fset=f, doc=docstring)
class Parented(object):
"""
Provides common services for document elements that occur below a part
but may occasionally require an ancestor object to provide a service,
such as add or drop a relationship. Provides ``self._parent`` attribute
to subclasses.
"""
def __init__(self, parent):
super(Parented, self).__init__()
self._parent = parent
@property
def part(self):
"""
The package part containing this object
"""
return self._parent.part