def main(): import argcomplete basic_desc = "crappy configuration tool" lic = "(c) [email protected]" shared_parser = argparse.ArgumentParser(add_help=False) shared_parser.add_argument("--no_color", help="don't color output", action="store_true") main_parser = argparse.ArgumentParser(description="%s %s" % (basic_desc, lic)) sub_parsers = main_parser.add_subparsers(dest="subcommand") sub_parsers.required = True show_parser = sub_parsers.add_parser( "show", description="show configuration - %s" % lic, parents=[shared_parser]) show_parser.add_argument( "config", help="optional config file to display (default: package settings)", nargs='?') show_parser.add_argument("--brief", help="show only the .json data", action="store_true") set_parser = sub_parsers.add_parser( "set", description=SET_HELP, parents=[shared_parser], formatter_class=argparse.RawTextHelpFormatter) set_parser.add_argument("params", choices=list(DEFAULT_SETTINGS_DICT.keys()), nargs=argparse.REMAINDER, help="parameters to set") set_parser.add_argument( "-c", "--config", help="optional config file (default: package settings)", default=None) set_parser.add_argument("-m", "--merge", help="other config file to merge in (priority)", default=None) set_parser.add_argument("--soft", help="do a soft-merge (no overwriting)", action="store_true") gen_parser = sub_parsers.add_parser( "generate", description=GENERATE_HELP, parents=[shared_parser], formatter_class=argparse.RawTextHelpFormatter) gen_parser.add_argument("-o", "--out", help="path for config file to generate") reset_parser = sub_parsers.add_parser( "reset", description="reset package settings - %s" % lic, parents=[shared_parser]) reset_parser.add_argument("-y", help="acknowledge automatically", action="store_true") argcomplete.autocomplete(main_parser) if len(sys.argv) > 1 and sys.argv[1] == "set": args, other_args = main_parser.parse_known_args() other_args = [arg for arg in sys.argv[2:] if not arg.startswith('-')] else: args, other_args = main_parser.parse_known_args() log.configure_logging() colorama.init() config = settings.DEFAULT_PATH if hasattr(args, "config"): if args.config: config = args.config if args.subcommand == "show": if not args.brief and not args.config: style = Style.BRIGHT if not args.no_color else Style.NORMAL doc_str = "\n".join( "{0}{1}{2}:\n{3}\n".format(style, k, Style.RESET_ALL, v[1]) for k, v in sorted(DEFAULT_SETTINGS_DICT_DOC.items())) logger.info(doc_str) logger.info("{0}\n{1}\n{0}".format(SEP, config)) show(config, colored=not args.no_color) if config == settings.DEFAULT_PATH and not args.brief: logger.info(SEP + "\nSee text above for parameter descriptions.") elif args.subcommand == "set": if not os.access(config, os.W_OK): logger.error("No permission to modify " + config) sys.exit() if other_args or args.merge: logger.info("{0}\nOld configuration:\n{0}".format(SEP)) show(config, colored=not args.no_color) try: set_cfg(config, other_args) except ConfigError as e: logger.error(e) sys.exit(1) if args.merge: merge_json_union(config, args.merge, args.soft) logger.info(SEP + "\nNew configuration:\n" + SEP) show(config, colored=not args.no_color) else: logger.error("No configuration parameters given (see --help).") elif args.subcommand == "generate": if other_args: logger.info( "{0}\nParsed by argparse:\n{1}\n" "{0}\nWARNING:\nMake sure you use the 'long-style' -- options " "(e.g. --plot) if possible\nand no combined short '-' flags, " "(e.g. -vp)\n{0}".format(SEP, other_args)) data = generate(other_args) log_info_dict_json(data, colored=not args.no_color) if args.out and user.check_and_confirm_overwrite(args.out): with open(args.out, 'w') as out: out.write(json.dumps(data, indent=4, sort_keys=True)) elif not args.out: logger.warning("\n(-o | --out) not specified - saving nothing") else: logger.error("No command line arguments given (see --help)") elif args.subcommand == "reset": if not os.access(config, os.W_OK): logger.error("No permission to modify" + config) sys.exit() if args.y or user.confirm( "Reset the package settings to the default settings? (y/n)"): settings.reset() logger.info("{0}\nPackage settings after reset:\n{0}".format(SEP)) show(settings.DEFAULT_PATH, colored=not args.no_color)
def main(): import argcomplete basic_desc = "crappy configuration tool" lic = "(c) [email protected]" shared_parser = argparse.ArgumentParser(add_help=False) shared_parser.add_argument("--no_color", help="don't color output", action="store_true") main_parser = argparse.ArgumentParser(description="%s %s" % (basic_desc, lic)) sub_parsers = main_parser.add_subparsers(dest="subcommand") sub_parsers.required = True show_parser = sub_parsers.add_parser("show", description="show configuration - %s" % lic, parents=[shared_parser]) show_parser.add_argument("config", help="optional config file to display (default: package settings)", nargs='?') show_parser.add_argument("--brief", help="show only the .json data", action="store_true") set_parser = sub_parsers.add_parser("set", description=SET_HELP, parents=[shared_parser], formatter_class=argparse.RawTextHelpFormatter) set_parser.add_argument("params", choices=list(DEFAULT_SETTINGS_DICT.keys()), nargs=argparse.REMAINDER, help="parameters to set") set_parser.add_argument("-c", "--config", help="optional config file (default: package settings)", default=None) set_parser.add_argument("-m", "--merge", help="other config file to merge in (priority)", default=None) set_parser.add_argument("--soft", help="do a soft-merge (no overwriting)", action="store_true") gen_parser = sub_parsers.add_parser("generate", description=GENERATE_HELP, parents=[shared_parser], formatter_class=argparse.RawTextHelpFormatter) gen_parser.add_argument("-o", "--out", help="path for config file to generate") reset_parser = sub_parsers.add_parser("reset", description="reset package settings - %s" % lic, parents=[shared_parser]) reset_parser.add_argument("-y", help="acknowledge automatically", action="store_true") argcomplete.autocomplete(main_parser) if len(sys.argv) > 1 and sys.argv[1] == "set": args, other_args = main_parser.parse_known_args() other_args = [arg for arg in sys.argv[2:] if not arg.startswith('-')] else: args, other_args = main_parser.parse_known_args() log.configure_logging() colorama.init() config = settings.DEFAULT_PATH if hasattr(args, "config"): if args.config: config = args.config if args.subcommand == "show": if not args.brief and not args.config: style = Style.BRIGHT if not args.no_color else Style.NORMAL doc_str = "\n".join("{0}{1}{2}:\n{3}\n".format(style, k, Style.RESET_ALL, v[1]) for k, v in sorted(DEFAULT_SETTINGS_DICT_DOC.items())) logger.info(doc_str) logger.info("{0}\n{1}\n{0}".format(SEP, config)) show(config, colored=not args.no_color) if config == settings.DEFAULT_PATH and not args.brief: logger.info(SEP + "\nSee text above for parameter descriptions.") elif args.subcommand == "set": if not os.access(config, os.W_OK): logger.error("No permission to modify " + config) sys.exit() if other_args or args.merge: logger.info("{0}\nOld configuration:\n{0}".format(SEP)) show(config, colored=not args.no_color) try: set_cfg(config, other_args) except ConfigError as e: logger.error(e) sys.exit(1) if args.merge: merge_json_union(config, args.merge, args.soft) logger.info(SEP + "\nNew configuration:\n" + SEP) show(config, colored=not args.no_color) else: logger.error("No configuration parameters given (see --help).") elif args.subcommand == "generate": if other_args: logger.info("{0}\nParsed by argparse:\n{1}\n" "{0}\nWARNING:\n" "Make sure you use the 'long-style' -- options (e.g. --plot) if possible\n" "and no combined short '-' flags, (e.g. -vp)\n{0}".format(SEP, other_args)) data = generate(other_args) log_info_dict_json(data, colored=not args.no_color) if args.out and user.check_and_confirm_overwrite(args.out): with open(args.out, 'w') as out: out.write(json.dumps(data, indent=4, sort_keys=True)) elif not args.out: logger.warning("\n(-o | --out) not specified - saving nothing") else: logger.error("No command line arguments given (see --help)") elif args.subcommand == "reset": if not os.access(config, os.W_OK): logger.error("No permission to modify" + config) sys.exit() if args.y or user.confirm("Reset the package settings to the default settings? (y/n)"): settings.reset() logger.info("{0}\nPackage settings after reset:\n{0}".format(SEP)) show(settings.DEFAULT_PATH, colored=not args.no_color)