-
-
Notifications
You must be signed in to change notification settings - Fork 143
Expand file tree
/
Copy pathwrappers.py
More file actions
248 lines (176 loc) · 7.26 KB
/
Copy pathwrappers.py
File metadata and controls
248 lines (176 loc) · 7.26 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
from collections import OrderedDict
class BorderSpacing:
"""
Border spacing wrapper.
Examples:
BorderSpacing(1px)
BorderSpacing(1px, 2px)
"""
def __init__(self, horizontal, vertical=None):
self._horizontal = horizontal
self._vertical = vertical
def __repr__(self):
if self._vertical is None:
string = 'BorderSpacing({horizontal})'.format(horizontal=repr(self._horizontal))
else:
string = 'BorderSpacing({horizontal}, {vertical})'.format(horizontal=repr(self._horizontal),
vertical=repr(self._vertical))
return string
def __str__(self):
if self._vertical is not None:
string = '{horizontal} {vertical}'.format(horizontal=self._horizontal,
vertical=self._vertical)
else:
string = '{horizontal}'.format(horizontal=self._horizontal)
return string
@property
def horizontal(self):
"""Return the horizontal border spacing."""
return self._horizontal
@property
def vertical(self):
"""Return the vertical border spacing."""
return self._horizontal if self._vertical is None else self._vertical
class Quotes:
"""
Content opening and closing quotes wrapper.
Examples:
Quotes([('<', '>')])
Quotes([('<', '>'), ('{', '}')])
Quotes([('<', '>'), ('{', '}'), ('[', ']')])
"""
def __init__(self, values):
self._quotes = values
def __repr__(self):
return 'Quotes({values})'.format(values=self._quotes)
def __str__(self):
quotes = []
for start, end in self._quotes:
quotes.append(repr(start))
quotes.append(repr(end))
return ' '.join(val for val in quotes)
def __len__(self):
return len(self._quotes)
def __eq__(self, other):
return self.__class__ == other.__class__ and self._quotes == other._quotes
def opening(self, level):
"""Return the opening quote for the given level."""
try:
return self._quotes[level][0]
except IndexError:
raise IndexError('Quotes level out of range')
def closing(self, level):
"""Return the opening quote for the given level."""
try:
return self._quotes[level][-1]
except IndexError:
raise IndexError('Quotes level out of range')
class Shorthand:
VALID_KEYS = []
def __init__(self, **kwargs):
if self.VALID_KEYS:
for key in kwargs:
if key not in self.VALID_KEYS:
raise ValueError('Invalid key "{key}". Valid keys are {keys}'.format(key=key,
keys=self.VALID_KEYS))
setattr(self, key, kwargs[key])
else:
raise ValueError('Shorthand must define `VALID_KEYS`')
def __eq__(self, other):
return other.__class__ == self.__class__ and self.to_dict() == other.to_dict()
def __repr__(self):
items = []
properties = self.to_dict()
for key, value in properties.items():
items.append("{key}={value}".format(key=key, value=repr(value)))
class_name = self.__class__.__name__
string = "{class_name}({items})".format(class_name=class_name, items=', '.join(items))
return string.format(**properties)
def __str__(self):
parts = []
for __, value in self.to_dict().items():
parts.append(str(value))
return ' '.join(parts)
def to_dict(self):
"""Return dictionary of the defined properties."""
properties = OrderedDict()
for key in self.VALID_KEYS:
if key in self.__dict__:
properties[key] = self.__dict__[key]
return properties
class Outline(Shorthand):
VALID_KEYS = ['outline_color', 'outline_style', 'outline_width']
class BorderTop(Shorthand):
VALID_KEYS = ['border_top_width', 'border_top_style', 'border_top_color']
class BorderRight(Shorthand):
VALID_KEYS = ['border_right_width', 'border_right_style', 'border_right_color']
class BorderBottom(Shorthand):
VALID_KEYS = ['border_bottom_width', 'border_bottom_style', 'border_bottom_color']
class BorderLeft(Shorthand):
VALID_KEYS = ['border_left_width', 'border_left_style', 'border_left_color']
class Border(Shorthand):
VALID_KEYS = ['border_width', 'border_style', 'border_color']
class Uri:
"""Wrapper for a url."""
def __init__(self, url):
self._url = url
def __repr__(self):
return 'url("%s")' % self._url
def __str__(self):
return repr(self)
@property
def url(self):
return self._url
class ImmutableList(list):
"""Immutable list to store list properties."""
def __init__(self, iterable=()):
super().__init__(iterable)
def _get_error_message(self, err):
return str(err).replace('list', self.__class__.__name__, 1)
# def __eq__(self, other):
# return other.__class__ == self.__class__ and self == other
def __getitem__(self, index):
try:
return super().__getitem__(index)
except Exception as err:
error_msg = self._get_error_message(err)
raise err.__class__(error_msg)
def __setitem__(self, index, value):
raise TypeError("{} values cannot be changed!".format(self.__class__.__name__))
def __hash__(self):
return hash((self.__class__.__name__, tuple(self)))
def __repr__(self):
class_name = self.__class__.__name__
if len(self) != 0:
text = '{class_name}([{data}])'.format(data=repr(list(self))[1:-1], class_name=class_name)
else:
text = '{class_name}()'.format(class_name=class_name)
return text
def __str__(self):
return ', '.join(str(v) for v in self)
def copy(self):
return self.__class__(self)
# Disable mutating methods
def append(self, object):
raise TypeError("{} values cannot be changed!".format(self.__class__.__name__))
def extend(self, iterable):
raise TypeError("{} values cannot be changed!".format(self.__class__.__name__))
def insert(self, index, object):
raise TypeError("{} values cannot be changed!".format(self.__class__.__name__))
def pop(self, index=None):
raise TypeError("{} values cannot be changed!".format(self.__class__.__name__))
def remove(self, value):
raise TypeError("{} values cannot be changed!".format(self.__class__.__name__))
def reverse(self):
raise TypeError("{} values cannot be changed!".format(self.__class__.__name__))
def sort(self, cmp=None, key=None, reverse=False):
raise TypeError("{} values cannot be changed!".format(self.__class__.__name__))
class Cursor(ImmutableList):
"""Immutable list to store cursor property."""
##############################################################################
# Flex
##############################################################################
class FlexFlow(Shorthand):
VALID_KEYS = ['flex_direction', 'flex_wrap']
class Flex(Shorthand):
VALID_KEYS = ['flex_grow', 'flex_shrink', 'flex_basis']