From df246bd418fbed4497f5cf09345059d9bd63e0a8 Mon Sep 17 00:00:00 2001 From: yuezheng Date: Mon, 28 Aug 2017 20:27:20 +0800 Subject: [PATCH] Add options: format/style/rebuild into generate.py 1. Add format option; 2. Add style option; 3. Add rebuild option to skip exist output file. --- generate.py | 46 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/generate.py b/generate.py index d5e239e..eec2099 100755 --- a/generate.py +++ b/generate.py @@ -6,6 +6,7 @@ import re import os.path +from optparse import OptionParser from os import walk @@ -49,11 +50,20 @@ def getSources(root="./src"): return sources -def generate(src="src", target="output", format="png", style="modern-blue"): +def generate(src="src", target="output", format="png", style="modern-blue", + rebuild=False): for f in getSources(src): src_path = os.path.dirname(f) src_file = os.path.basename(f) target_path = target + src_path[src_path.index('/'):] + + work_path = os.getcwd() + "/" + output_file_name = src_file[:src_file.rindex('.') + 1] + format + target_full_path = work_path + target_path + "/" + output_file_name + if os.path.exists(target_full_path) and not rebuild: + print("Output file: %s already exists, skip it" % output_file_name) + continue + output_file = src_file[:src_file.rindex('.') + 1] + format if not os.path.isdir(target_path): @@ -67,9 +77,37 @@ def generate(src="src", target="output", format="png", style="modern-blue"): def main(): - generate("src", "output", "png", - "modern-blue") # png only, svg & pdf must register and pay 15$. - + styles = [ + "default", + "rose", + "qsd", + "napkin", + "vs2010", + "patent", + "omegapple", + "modern-blue", + "earth", + "roundgreen", + "magazine" + ] + style_s = ", ".join(styles) + + parser = OptionParser() + parser.add_option("-r", "--rebuild", dest="rebuild", default=False, + help="Rebuild exist output image") + parser.add_option("-f", "--format", dest="out_format", default="png", + choices=["png", "svg", "pdf"], + help="Specify the output format, only png is free") + parser.add_option("-s", "--style", dest="out_style", default="modern-blue", + choices=styles, + help="Specify the output style, " + "you can choose from: %s" % style_s) + + (options, args) = parser.parse_args() + if not options.rebuild: + print "The current generation will not overwrite old file!" + generate("src", "output", options.out_format, + options.out_style, rebuild=options.rebuild) if __name__ == "__main__": main()