Skip to content

Commit 9d4e4ac

Browse files
committed
Define representations of all SIS objects
Example of repr(obj): <SimplePathObject path (376.415, 523.219) #path2>
1 parent 0e7e28b commit 9d4e4ac

File tree

1 file changed

+58
-15
lines changed

1 file changed

+58
-15
lines changed

simpinkscr/simple_inkscape_scripting.py

+58-15
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,18 @@ def _python_to_svg_str(val):
113113
else:
114114
# Common case
115115
return ' '.join([_python_to_svg_str(v) for v in val])
116+
try:
117+
# Simple Inkscape Scripting objects are converted to a string of
118+
# the form "url(#id)".
119+
return val.get_inkex_object().get_id(as_url=2)
120+
except AttributeError:
121+
pass
122+
try:
123+
# inkex objects also are converted to a string of the form
124+
# "url(#id)".
125+
return val.get_id(as_url=2)
126+
except AttributeError:
127+
pass
116128
return str(val) # Everything else is converted to a string as usual.
117129

118130

@@ -369,6 +381,18 @@ def svg(self, xmlns=False, pretty_print=False):
369381
return obj.tostring().decode('utf-8')
370382

371383

384+
class GenericReprMixin():
385+
'''Provide a generic __repr__ method for Simple Inkscape Scripting
386+
classes that don't define a more customized __repr__.'''
387+
388+
def __repr__(self):
389+
iobj = self.get_inkex_object()
390+
return '<%s %s %s>' % \
391+
(self.__class__.__name__,
392+
iobj.TAG,
393+
iobj.get_id(1))
394+
395+
372396
class SimpleObject(SVGOutputMixin):
373397
'Encapsulate an Inkscape object and additional metadata.'
374398

@@ -428,11 +452,15 @@ def __init__(self, obj, transform, conn_avoid, clip_path_obj, mask_obj,
428452
_simple_top.svg_attach.append(obj)
429453
self.parent = None
430454

431-
def __str__(self):
432-
'''Return the object as a string of the form "url(#id)". This
433-
enables the object to be used as a value in style key=value
434-
arguments such as shape_inside.'''
435-
return self._inkscape_obj.get_id(as_url=2)
455+
def __repr__(self):
456+
'Return a unique description of the object as a string.'
457+
iobj = self.get_inkex_object()
458+
bbox = iobj.bounding_box()
459+
return '<%s %s (%s) %s>' % \
460+
(self.__class__.__name__,
461+
iobj.TAG,
462+
bbox.center,
463+
iobj.get_id(1))
436464

437465
def __eq__(self, other):
438466
'''Two SimpleObjects are equal if they encapsulate the same
@@ -1512,7 +1540,7 @@ def __init__(self, obj, transform, conn_avoid, clip_path_obj, mask_obj,
15121540
self._self_type = 'hyperlink'
15131541

15141542

1515-
class SimpleFilter(SVGOutputMixin):
1543+
class SimpleFilter(SVGOutputMixin, GenericReprMixin):
15161544
'Represent an SVG filter effect.'
15171545

15181546
def __init__(self, name=None, pt1=None, pt2=None, filter_units=None,
@@ -1612,7 +1640,7 @@ def add(self, ftype, **kw_args):
16121640
return self.SimpleFilterPrimitive(self, 'fe' + ftype, **kw_args)
16131641

16141642

1615-
class SimpleGradient(SVGOutputMixin):
1643+
class SimpleGradient(SVGOutputMixin, GenericReprMixin):
16161644
'Virtual base class for an SVG linear or radial gradient pattern.'
16171645

16181646
# Map Inkscape repetition names to SVG names.
@@ -1708,7 +1736,7 @@ def __init__(self, center=None, radius=None, focus=None, fr=None,
17081736
self.grad = grad
17091737

17101738

1711-
class SimplePathEffect(SVGOutputMixin):
1739+
class SimplePathEffect(SVGOutputMixin, GenericReprMixin):
17121740
'Represent an Inkscape live path effect.'
17131741

17141742
def __init__(self, effect, **kwargs):
@@ -1751,6 +1779,15 @@ def get_inkex_object(self):
17511779
"Return the guide's underlying inkex object."
17521780
return self._inkscape_obj
17531781

1782+
def __repr__(self):
1783+
'Return a unique description of the guide as a string.'
1784+
iobj = self.get_inkex_object()
1785+
return '<%s %s %s %s>' % \
1786+
(self.__class__.__name__,
1787+
iobj.TAG,
1788+
self._pos,
1789+
iobj.get_id(1))
1790+
17541791
def _move_to_wrapper(self, pos, angle):
17551792
"Wrap inkex's move_to with a coordinate transformation."
17561793
global _simple_top
@@ -1840,7 +1877,7 @@ def _from_inkex_object(self, iobj):
18401877
return SimpleGuide(pos, angle, _inkex_object=iobj)
18411878

18421879

1843-
class SimpleCanvas:
1880+
class SimpleCanvas():
18441881
'Get and set the canvas size and viewbox.'
18451882

18461883
def __init__(self, svg_root):
@@ -2031,7 +2068,7 @@ def resize_to_content(self, objs=None):
20312068
self.true_height = '%.10g%s' % (tht, uht)
20322069

20332070

2034-
class SimplePage(SVGOutputMixin):
2071+
class SimplePage(SVGOutputMixin, GenericReprMixin):
20352072
'Represent an Inkscape page.'
20362073

20372074
def __init__(self, number, name=None, pos=None, size=None, iobj=None):
@@ -2112,7 +2149,7 @@ class RectangularForeignObject(inkex.ForeignObject,
21122149
pass
21132150

21142151

2115-
class SimpleMetadata:
2152+
class SimpleMetadata(GenericReprMixin):
21162153
'Provide a simple interface to document metadata.'
21172154

21182155
def __init__(self):
@@ -2292,12 +2329,19 @@ def __init__(self):
22922329
}
22932330
self._licenses['Open Font License'] = self._licenses['OFL']
22942331

2332+
def get_inkex_object(self):
2333+
'''Return the SimpleMetadata's underlying inkex object.
2334+
Regrettably, this exhibits the side effect of creating a
2335+
<metadata> tag if one does not already exist (tested with
2336+
Inkscape 1.3.2.'''
2337+
global _simple_top
2338+
return _simple_top.svg_root.metadata
2339+
22952340
def _search_hierarchy(self, *args):
22962341
'''Search an XML hierarchy given tuples of (namespace, tag).
22972342
<rdf:RDF> is the implicit first element of the hierarchy. This
22982343
method returns the final node.'''
2299-
global _simple_top
2300-
elt = _simple_top.svg_root.metadata
2344+
elt = self.get_inkex_object()
23012345
for ns_tag in [(self.rdf, 'RDF')] + list(args):
23022346
child = elt.find('./{%s}%s' % ns_tag)
23032347
if child is None:
@@ -2309,8 +2353,7 @@ def _create_hierarchy(self, *args):
23092353
'''Create an XML hierarchy given tuples of (namespace, tag).
23102354
<rdf:RDF> is the implicit first element of the hierarchy. This
23112355
method returns the final node.'''
2312-
global _simple_top
2313-
elt = _simple_top.svg_root.metadata
2356+
elt = self.get_inkex_object()
23142357
for ns_tag in [(self.rdf, 'RDF')] + list(args):
23152358
child = elt.find('./{%s}%s' % ns_tag)
23162359
if child is None:

0 commit comments

Comments
 (0)