|
| 1 | +import argparse |
| 2 | +import importlib |
| 3 | +import os |
| 4 | +import sys |
| 5 | + |
| 6 | +from autoarchaeologist import Excavation |
| 7 | + |
| 8 | +EXCAVATORS = [ |
| 9 | + 'examples.showcase' |
| 10 | +] |
| 11 | + |
| 12 | + |
| 13 | +def action_for_args(args): |
| 14 | + if args.excavator is not None: |
| 15 | + return ("excavator", load_excavator_by_name(args.excavator)) |
| 16 | + else: |
| 17 | + raise argparse.ArgumentError(None, "no valid excavation was requsted") |
| 18 | + |
| 19 | + |
| 20 | +def load_excavator_by_name(excavator): |
| 21 | + package_name, excavation_prefix = excavator.split('.') |
| 22 | + excavation_name = f"{excavation_prefix}_excavation" |
| 23 | + |
| 24 | + exacations_package = importlib.import_module(package_name) |
| 25 | + return getattr(exacations_package, excavation_name) |
| 26 | + |
| 27 | + |
| 28 | +def parse_arguments(argv=None): |
| 29 | + parser = argparse.ArgumentParser() |
| 30 | + parser.add_argument("-d", "--dir", default="/tmp/_autoarchaologist") |
| 31 | + parser.add_argument('--excavator', choices=EXCAVATORS) |
| 32 | + parser.add_argument('filename') |
| 33 | + |
| 34 | + return parser.parse_args(args=argv) |
| 35 | + |
| 36 | + |
| 37 | +def process_arguments(args): |
| 38 | + if args.dir == ".": |
| 39 | + args.dir = os.path.join(os.getcwd(), "output", "_autoarchaologist") |
| 40 | + if args.filename is not None: |
| 41 | + args.filename = os.path.abspath(args.filename) |
| 42 | + else: |
| 43 | + raise ValueError() |
| 44 | + |
| 45 | + return args |
| 46 | + |
| 47 | + |
| 48 | +def perform_excavation(args): |
| 49 | + match action_for_args(args): |
| 50 | + case "excavator", AnExcavation: |
| 51 | + assert issubclass(AnExcavation, Excavation) |
| 52 | + ctx = AnExcavation(html_dir=args.dir) |
| 53 | + case action, _: |
| 54 | + raise NotImplementedError(f"action: {action}") |
| 55 | + |
| 56 | + ff = ctx.add_file_artifact(args.filename) |
| 57 | + |
| 58 | + ctx.start_examination() |
| 59 | + |
| 60 | + return ctx |
| 61 | + |
| 62 | + |
| 63 | +if __name__ == "__main__": |
| 64 | + args = process_arguments(parse_arguments()) |
| 65 | + |
| 66 | + try: |
| 67 | + os.mkdir(args.dir) |
| 68 | + except FileExistsError: |
| 69 | + pass |
| 70 | + |
| 71 | + ctx = perform_excavation(args) |
| 72 | + ctx.produce_html() |
| 73 | + print("Now point your browser at", ctx.filename_for(ctx).link) |
0 commit comments