Created
May 2, 2014 19:14
-
-
Save tueda/a885808519362ae6dc0c to your computer and use it in GitHub Desktop.
Manipuate JaxoDraw2 xml files.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
from xml.etree.ElementTree import * | |
from optparse import OptionParser | |
def get_xypoints(node): | |
xpoints = [] | |
ypoints = [] | |
for e in node.findall(".//void[@class='java.awt.Point'][@method='getField']"): | |
e_name = e.find("string") | |
if e_name is None or e_name.text not in ("x", "y"): | |
continue | |
e_set = e.find("void[@method='set']") | |
if e_set is None: | |
continue | |
e_int = e_set.find("int") | |
if e_int is None: | |
continue | |
if e_name.text == "x": | |
xpoints.append(e_int) | |
else: | |
ypoints.append(e_int) | |
return (tuple(xpoints), tuple(ypoints)) | |
def process_file(filename, commands): | |
tree = parse(filename) | |
root = tree.getroot() | |
xmin = 999999 | |
xmax = -999999 | |
ymin = 999999 | |
ymax = -999999 | |
xypoints = get_xypoints(root) | |
for ex in xypoints[0]: | |
x = int(ex.text) | |
if xmin > x: | |
xmin = x | |
elif xmax < x: | |
xmax = x | |
for ey in xypoints[1]: | |
y = int(ey.text) | |
if ymin > y: | |
ymin = y | |
elif ymax < y: | |
ymax = y | |
if xmin <= xmax and ymin <= ymax: | |
for cmd in commands: | |
if cmd == "xflip": | |
for ex in xypoints[0]: | |
x = int(ex.text) | |
x = xmax - (x - xmin) | |
ex.text = str(x) | |
if cmd == "yflip": | |
for ey in xypoints[1]: | |
y = int(ey.text) | |
y = ymax - (y - ymin) | |
ey.text = str(y) | |
tree.write(filename, "UTF-8") | |
opt_parser = OptionParser(usage="Usage: %prog [options] files...") | |
opt_parser.add_option("--xflip", action="append_const", const="xflip", dest="commands", help="flip horizontally") | |
opt_parser.add_option("--yflip", action="append_const", const="yflip", dest="commands", help="flip vertically") | |
(opts, args) = opt_parser.parse_args() | |
for f in args: | |
process_file(f, opts.commands) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment