Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def testInlineCSS(self):
inlined = style.inline(self.svg, self.css)
self.assertNotEqual(inlined, self.svg)
assert 'fill:purple' not in inlined
doc = minidom.parseString(inlined)
polygon = doc.getElementsByTagName('polygon').item(0).getAttribute('style')
self.assertIn('stroke:green', polygon)
self.assertIn('fill:orange', polygon)
cat = doc.getElementsByTagName('polyline').item(1).getAttribute('style')
self.assertIn('fill:red', cat)
polyline = doc.getElementsByTagName('polyline').item(0).getAttribute('style')
self.assertIn('stroke:blue', polyline)
def testCDATA(self):
xml = """"""
content = 'this is some text & > <'
style._register()
et = ElementTree.fromstring(xml)
e = ElementTree.Element("data")
cd = dom.cdata(content)
e.append(cd)
et.append(e)
string = ElementTree.tostring(et, encoding='utf-8')
self.assertIn(content.encode('utf8'), string)
def test_rescale(self):
new = style.rescale(self.file, 100)
g = minidom.parseString(new).getElementsByTagName('g')[0]
assert 'scale(100)' in g.attributes.get('transform').value
class_fields (Sequence): A comma-separated string or list of class names to
use the SVG drawing.
id_field (string): Field to use to determine id of each element in the drawing.
inline (bool): If False, do not move CSS declarations into each element.
precision (int): Precision for rounding output coordinates.
simplify (int): Integer between 1 and 99 describing simplification level.
99: not very much. 1: a lot.
Returns:
String (unicode in Python 2) containing an entire SVG document.
'''
scale = (1. / scale) if scale else 1.
bounds = bounding.check(bounds)
# Try to read style file(s)
styles = u''.join(_style.pick(s) for s in kwargs.pop('style', []))
class_fields = set(a for c in kwargs.pop('class_fields', []) for a in c.split(','))
data_fields = set(a for c in kwargs.pop('data_fields', []) for a in c.split(','))
drawing = SVGIS(
layers,
bounds=bounds,
scalar=scale,
crs=kwargs.pop('crs', None),
style=styles,
clip=kwargs.pop('clip', True),
id_field=kwargs.pop('id_field', None),
class_fields=class_fields,
data_fields=data_fields,
simplify=kwargs.pop('simplify', None)
).compose(**kwargs)
for t in transforms:
geom = t(geom) if t is not None else geom
except ValueError as e:
self.log.warning('error transforming feature %s of %s: %s',
kwargs.get('id', feature.get('id', '?')), name, e)
return ''
# Set up the element's properties.
drawargs = _style.construct_datas(datas, feature['properties'])
classes = _style.construct_classes(classes, feature['properties'])
# Add the layer name to the class list.
if name != '?':
classes.insert(0, _style.sanitize(name))
drawargs['class'] = ' '.join(classes)
if 'id_field' in kwargs and kwargs['id_field'] in feature['properties']:
drawargs['id'] = _style.sanitize(feature['properties'].get(kwargs['id_field']))
try:
# Draw the geometry.
return draw.geometry(geom, precision=precision, **drawargs)
except errors.SvgisError as e:
self.log.warning('unable to draw feature %s of %s: %s',
kwargs.get('id', feature.get('id', '?')), name, e)
return u''
def style(layer, output, **kwargs):
"""Add or inline the CSS styles of an SVG"""
result = _style.add_style(layer, kwargs['style'], kwargs['replace'])
if kwargs['inline']:
result = _style.inline(result)
click.echo(result.encode('utf-8'), file=output)
try:
# Check if geometry exists (a bit unpythonic, but cleaner errs this way).
if geom is None:
raise ValueError('NULL geometry')
# Apply transformations to the geometry.
for t in transforms:
geom = t(geom) if t is not None else geom
except ValueError as e:
self.log.warning('error transforming feature %s of %s: %s',
kwargs.get('id', feature.get('id', '?')), name, e)
return ''
# Set up the element's properties.
drawargs = _style.construct_datas(datas, feature['properties'])
classes = _style.construct_classes(classes, feature['properties'])
# Add the layer name to the class list.
if name != '?':
classes.insert(0, _style.sanitize(name))
drawargs['class'] = ' '.join(classes)
if 'id_field' in kwargs and kwargs['id_field'] in feature['properties']:
drawargs['id'] = _style.sanitize(feature['properties'].get(kwargs['id_field']))
try:
# Draw the geometry.
return draw.geometry(geom, precision=precision, **drawargs)
if kwargs.pop('viewbox', True):
viewbox = [dims[0], -dims[3]] + size
self.log.debug('drawing with viewbox')
else:
viewbox = None
transform_attrib += ' translate({},{})'.format(-dims[0], -dims[3])
self.log.debug('translating contents to fit')
# Create container and then SVG
container = svg.group(members, fill_rule='evenodd', transform=transform_attrib)
drawing = svg.drawing(size, [container], style=style, precision=precision, viewbox=viewbox)
if kwargs.pop('inline', False):
self.log.info('inlining styles')
drawing = _style.inline(drawing, style)
return drawing