import os
import sys
import re


def rounder(matchobj):
    return matchobj.group(1) + str(round(float(matchobj.group(2))))


def main():
    if (len(sys.argv)) == 1 and not sys.argv[1].endswith('.scml'):
        print "Need to provide the scml file path to align."
        return
    scml = sys.argv[1]
    if not os.path.exists(scml):
        print "Provided scml file path does not exist."
        return

    with open(scml, 'r+') as f:
        line = f.readline()
        lines = []
        while line:
            if ' x=\"' in line:
                line = re.sub(r'( x=\"-?)([0-9]+(?:\.[0-9]*)?)', rounder, line)
            if ' y=\"' in line:
                line = re.sub(r'( y=\"-?)([0-9]+(?:\.[0-9]*)?)', rounder, line)
            lines.append(line)
            line = f.readline()

        f.seek(0)
        f.writelines(lines)


if __name__ == "__main__":
    main()
