Blame view

web/hp_cmd.py 2.26 KB
94a66476   hitier   Add dry_run optio...
1
2
import sys

89f61bb2   hitier   New get_default_s...
3
from run import generate_csv_file_if_needed, init_console_logger, config, get_default_sta_sto
a5e8081b   hitier   New command facility
4
5
6
7
from datetime import datetime
import argparse

if __name__ == "__main__":
7b3d3593   hitier   Move inputs confi...
8
    hp_parser = argparse.ArgumentParser(prog='Heliopropa Command tool')
a5e8081b   hitier   New command facility
9
10

    hp_parser.add_argument('-clog', '--console-log', action='store_true')
94a66476   hitier   Add dry_run optio...
11
    hp_parser.add_argument('-s', '--dry-run', action='store_true')
a5e8081b   hitier   New command facility
12
13
14

    subparsers = hp_parser.add_subparsers(dest='hpcmd_name')

7b3d3593   hitier   Move inputs confi...
15
16
    csvgen_parser = subparsers.add_parser('show_config',
                                          help='Show configuration targets and inputs')
a5e8081b   hitier   New command facility
17
    csvgen_parser = subparsers.add_parser('csv_gen',
7b3d3593   hitier   Move inputs confi...
18
                                          help='Generate CSV for given target/input (can be input/input)')
94a66476   hitier   Add dry_run optio...
19
    csvgen_parser.add_argument('-a', '--all', action='store_true')
a5e8081b   hitier   New command facility
20
21
22
23
24
25
26
27
28
    csvgen_parser.add_argument('-t', '--target')
    csvgen_parser.add_argument('-i', '--input')
    csvgen_parser.add_argument('-b', '--begin')
    csvgen_parser.add_argument('-e', '--end')

    args = hp_parser.parse_args()

    if args.console_log:
        _logger = init_console_logger()
a5e8081b   hitier   New command facility
29

7b3d3593   hitier   Move inputs confi...
30
31
32
33
34
35
36
37
    if args.hpcmd_name == 'show_config':
        for meteor in config['targets'] + config['inputs']:
            meteor_type = meteor['type']
            meteor_name = f"'{meteor['name']}'"
            meteor_slug = meteor['slug']
            meteor_models = ', '.join(list(meteor['models'].keys()))
            print(f"{meteor_type:6} {meteor_slug:6} {meteor_name:11}: models = {meteor_models}")
    elif args.hpcmd_name == 'csv_gen':
89f61bb2   hitier   New get_default_s...
38
        # build default dates in not -b or not -e
7b3d3593   hitier   Move inputs confi...
39
40
41
        sta, sto = get_default_sta_sto(args.begin, args.end)

        meteors_list = config['targets'] + config['inputs']
89f61bb2   hitier   New get_default_s...
42

94a66476   hitier   Add dry_run optio...
43
44
        if args.all:
            # build the {target_slug: [input list]} dictionnary from config.yaml
7b3d3593   hitier   Move inputs confi...
45
            meteors_dict = {t['slug']: list(t['models'].keys()) for t in meteors_list}
94a66476   hitier   Add dry_run optio...
46
47
        else:
            # todo: check -i an -t
7b3d3593   hitier   Move inputs confi...
48
            meteors_dict = {args.target: [args.input]}
94a66476   hitier   Add dry_run optio...
49

7b3d3593   hitier   Move inputs confi...
50
        for _t, _i_list in meteors_dict.items():
94a66476   hitier   Add dry_run optio...
51
52
53
54
55
            # print(_t, _i_list)
            for _i in _i_list:
                print(f"generating csv for {_t}/ {_i}")
                if args.dry_run:
                    continue
89f61bb2   hitier   New get_default_s...
56
                generate_csv_file_if_needed(_t, _i, sta, sto)