def run_document(self): """Run the actions on the svg xml tree""" if not self.has_action: return self.svg_document # First save the document svgfile = tempfile.mktemp(".svg") self.svg_document.write(svgfile) # Run the action on the document self.run_file(svgfile) # Open the resulting file stream = open(svgfile, 'r') new_svg_doc = etree.parse(stream) stream.close() # Clean up. try: os.remove(svgfile) except Exception: pass # Set the current SVG document self.svg_document = new_svg_doc # Return the new document return new_svg_doc
def effect(self): tree = None if self.options.input_filename: tree = etree.parse(open(self.options.input_filename)) else: tree = self.document paths = findElements(tree, "path", self.selected) polygons = findElements(tree, "polygon", self.selected) colors = None if not self.options.colors else [Color.FromRGB(c) for c in re.findall("[a-z0-9]{6}", self.options.colors, re.IGNORECASE)] if self.options.change_fill: fill(tree, paths + polygons, self.options.color_space, self.options.grayscale, self.options.randomness, colors, self.options.tones) if self.options.change_lines: stroke(tree, paths + polygons, self.options.color_space, self.options.lines_width, Color.FromRGB(self.options.lines_color)) if self.options.change_opacity: opacity(tree, paths + polygons, self.options.remove_opacity, float(self.options.opacity_start), float(self.options.opacity_end), self.options.opacity_formula) if self.options.input_filename: etree.register_namespace('namespace',"http://www.w3.org/2000/svg") xml = etree.tostring(tree.getroot()) xml = minidom.parseString(xml).toprettyxml(indent=" ") target_file = output_file(self.options.input_filename, self.options.color_space) if self.options.overwrite: target_file = self.options.input_filename with open(target_file, 'w') as file: file.write(re.sub("(\s*\n)+", "\n", xml, re.MULTILINE))