from formatter import Formatter from html_parser import Parser import sys from distutils import dir_util import os def main(argv): for arg in argv: main_one_arg(arg) def main_one_arg(directory): if not os.path.exists(directory): raise OSError("Directory " + str(directory) + " does not exist.") destination = directory + "-protoxml" if not os.path.exists(destination): os.makedirs(destination) for filename in os.listdir(directory): parse_and_format(directory, filename, destination) def parse_and_format(dir_path, filename, dest): assert(os.path.exists(os.path.join(dir_path, filename))) path_to_file = os.path.join(dir_path, filename) path_to_dst = os.path.join(dest, filename) if os.path.isdir(path_to_file): if filename == "images": dir_util.copy_tree(path_to_file, path_to_dst) else: if not os.path.exists(path_to_dst): os.makedirs(path_to_dst) for sub_file in os.listdir(path_to_file): parse_and_format(path_to_file, sub_file, path_to_dst) elif os.path.isfile(path_to_file): parse_format_and_write_file(path_to_file, path_to_dst) else: print "Unhandled file type (probably symlink) with file " + str(path_to_file) def parse_format_and_write_file(filename, dest): print "parse file" has_been_parsed = False parser = None formatter = None try: parser = Parser(filename) parser.parse() has_been_parsed = True except Exception as e: print "Error with file while parsing: " + str(filename) + " (this file might not be HTML valid enough)" print "Error message: " + str(e) if has_been_parsed: try: formatter = Formatter(parser.get_data()) formatter.format() formatter.write_to_file(dest) except Exception as e: print "Error with file while fomatting: " + str(filename) if __name__=="__main__": main(sys.argv[1:]) # don't get the first arg (it's the command name)