|
| 1 | +''' |
| 2 | +The main console script for the Python solutions to the Codejam problems |
| 3 | +''' |
| 4 | +import sys |
| 5 | +import inspect |
| 6 | +import pkgutil |
| 7 | +import importlib |
| 8 | +import argparse |
| 9 | +import codejam |
| 10 | +from codejam.codejambase import CodeJamBase |
| 11 | + |
| 12 | + |
| 13 | +def run_func(args): |
| 14 | + ''' |
| 15 | + Runs the specified Codejam problem if it exists |
| 16 | + ''' |
| 17 | + try: |
| 18 | + proj_mod = importlib.import_module('codejam.%s.runner' % args.problem) |
| 19 | + |
| 20 | + def valid_class(obj): |
| 21 | + '''Simple predicate function used to inspect the module''' |
| 22 | + return inspect.isclass(obj) \ |
| 23 | + and proj_mod.__name__ == obj.__module__ \ |
| 24 | + and issubclass(obj, CodeJamBase) |
| 25 | + |
| 26 | + class_list = inspect.getmembers(proj_mod, valid_class) |
| 27 | + if len(class_list) != 1: |
| 28 | + print 'The chosen solution %s is invalid:' % args.problem, \ |
| 29 | + 'It contains %d CodeJamBase classes' % len(class_list) |
| 30 | + return 2 |
| 31 | + |
| 32 | + print 'Running the \'%s\' Codejam solution:' % args.problem |
| 33 | + if args.output_file != sys.stdout: |
| 34 | + print 'Solution output written to \'%s\'' % args.output_file.name |
| 35 | + # the class we want in the first entry in the list and 2nd in the tuple |
| 36 | + solver = class_list[0][1](args.input_file, args.output_file) |
| 37 | + return solver.solve() |
| 38 | + except ImportError: |
| 39 | + print 'No Codejam problem named \'%s\'' % args.problem |
| 40 | + return 2 |
| 41 | + |
| 42 | + |
| 43 | +def list_func(_): |
| 44 | + ''' |
| 45 | + Lists all available Codejam solutions |
| 46 | + ''' |
| 47 | + |
| 48 | + print 'Listing all available Codejam solutions:' |
| 49 | + |
| 50 | + for _, modname, _ in pkgutil.iter_modules(codejam.__path__): |
| 51 | + try: |
| 52 | + importlib.import_module('codejam.%s.runner' % modname) |
| 53 | + print modname |
| 54 | + except ImportError: |
| 55 | + pass |
| 56 | + #print "Found submodule %s (is a package: %s)" % (modname, ispkg) |
| 57 | + |
| 58 | + |
| 59 | +def parse_cmdline(): |
| 60 | + ''' |
| 61 | + Parses the commandline args for the codejam problem |
| 62 | + ''' |
| 63 | + parser = argparse.ArgumentParser( |
| 64 | + description='Command line runner for Google Codejam solutions' |
| 65 | + ) |
| 66 | + |
| 67 | + parser.add_argument( |
| 68 | + '--version', |
| 69 | + action='version', |
| 70 | + version='%(prog)s ' + codejam.__version__ |
| 71 | + ) |
| 72 | + |
| 73 | + subparsers = parser.add_subparsers(title='subcommands') |
| 74 | + parser_run = subparsers.add_parser( |
| 75 | + 'run', |
| 76 | + description='Comand for running a specific Codejam solutions', |
| 77 | + help='runs a specific Codejam solution', |
| 78 | + ) |
| 79 | + |
| 80 | + parser_run.add_argument( |
| 81 | + 'problem', |
| 82 | + metavar='PROBLEM_NAME', |
| 83 | + help='the name of the Codejam problem to run' |
| 84 | + ) |
| 85 | + |
| 86 | + parser_run.add_argument( |
| 87 | + 'input_file', |
| 88 | + metavar='INPUT_FILE', |
| 89 | + type=argparse.FileType('r'), |
| 90 | + help='input file for the Codejam problem' |
| 91 | + ) |
| 92 | + |
| 93 | + parser_run.add_argument( |
| 94 | + 'output_file', |
| 95 | + metavar='OUTPUT_FILE', |
| 96 | + nargs='?', |
| 97 | + type=argparse.FileType('w'), |
| 98 | + default=sys.stdout, |
| 99 | + help='optional output file for the Codejam problem' |
| 100 | + ) |
| 101 | + |
| 102 | + parser_run.set_defaults(func=run_func) |
| 103 | + |
| 104 | + parser_list = subparsers.add_parser( |
| 105 | + 'list', |
| 106 | + description='Comand for listing available Codejam solutions', |
| 107 | + help='lists available Codejam solution' |
| 108 | + ) |
| 109 | + |
| 110 | + parser_list.set_defaults(func=list_func) |
| 111 | + |
| 112 | + return parser.parse_args() |
| 113 | + |
| 114 | + |
| 115 | +def main(): |
| 116 | + ''' |
| 117 | + Main function: parses the args and calls the proper codejam problem |
| 118 | + ''' |
| 119 | + try: |
| 120 | + args = parse_cmdline() |
| 121 | + return args.func(args) |
| 122 | + #for line in args.input_file: |
| 123 | + # args.output_file.write(line) |
| 124 | + except KeyboardInterrupt: |
| 125 | + return 1 |
0 commit comments