aboutsummaryrefslogtreecommitdiff
path: root/tools/python/post_generation/__main__.py
blob: 81090b52d050402ddbd41403b454457641ddd554 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import argparse
import json
import os
import sys

from post_generation.hierarchy_to_countries import (
    hierarchy_to_countries as hierarchy_to_countries_,
)
from post_generation.inject_promo_ids import inject_promo_ids
from post_generation.localads_mwm_to_csv import create_csv


class PostGeneration:
    def __init__(self):
        parser = argparse.ArgumentParser(
            description="Post-generation instruments",
            usage="""post_generation <command> [<args>]
The post_generation commands are:
    localads_mwm_to_csv    Prepares CSV files for uploading to localads database from mwm files.
    hierarchy_to_countries Produces countries.json from hierarchy.txt.
    inject_promo_ids       Injects promo osm ids into countries.json
    """,
        )
        parser.add_argument("command", help="Subcommand to run")
        args = parser.parse_args(sys.argv[1:2])
        if not hasattr(self, args.command):
            print(f"Unrecognized command {args.command}")
            parser.print_help()
            exit(1)
        getattr(self, args.command)()

    @staticmethod
    def localads_mwm_to_csv():
        parser = argparse.ArgumentParser(
            description="Prepares CSV files for uploading to localads database "
            "from mwm files."
        )
        parser.add_argument("mwm", help="path to mwm files")
        parser.add_argument(
            "--osm2ft", help="path to osm2ft files (default is the same as mwm)"
        )
        parser.add_argument(
            "--output", default=".", help="path to generated files ('.' by default)"
        )
        types_default = os.path.join(
            os.path.dirname(__file__), "..", "..", "..", "data", "types.txt"
        )
        parser.add_argument(
            "--types", default=types_default, help="path to omim/data/types.txt"
        )
        parser.add_argument(
            "--threads", type=int, default=1, help="number of threads to process files"
        )
        parser.add_argument(
            "--mwm_version", type=int, required=True, help="Mwm version"
        )
        args = parser.parse_args(sys.argv[2:])
        if not args.osm2ft:
            args.osm2ft = args.mwm

        create_csv(
            args.output,
            args.mwm,
            args.osm2ft,
            args.mwm_version,
            args.threads,
        )

    @staticmethod
    def hierarchy_to_countries():
        parser = argparse.ArgumentParser(
            description="Produces countries.json from hierarchy.txt."
        )
        parser.add_argument("--target", required=True, help="Path to mwm files")
        parser.add_argument(
            "--hierarchy", required=True, default="hierarchy.txt", help="Hierarchy file"
        )
        parser.add_argument("--old", required=True, help="old_vs_new.csv file")
        parser.add_argument("--osm", required=True, help="borders_vs_osm.csv file")
        parser.add_argument(
            "--countries_synonyms", required=True, help="countries_synonyms.csv file"
        )
        parser.add_argument(
            "--mwm_version", type=int, required=True, help="Mwm version"
        )
        parser.add_argument(
            "-o",
            "--output",
            required=True,
            help="Output countries.json file (default is stdout)",
        )
        args = parser.parse_args(sys.argv[2:])
        countries = hierarchy_to_countries_(
            args.old,
            args.osm,
            args.countries_synonyms,
            args.hierarchy,
            args.target,
            args.mwm_version,
        )
        if args.output:
            with open(args.output, "w") as f:
                json.dump(countries, f, ensure_ascii=False, indent=1)
        else:
            print(json.dumps(countries, ensure_ascii=False, indent=1))

    @staticmethod
    def inject_promo_ids():
        parser = argparse.ArgumentParser(
            description="Injects promo cities osm ids into countries.json"
        )
        parser.add_argument("--mwm", required=True, help="path to mwm files")
        parser.add_argument(
            "--types", required=True, help="path to omim/data/types.txt"
        )
        parser.add_argument(
            "--promo_cities", required=True, help="Path to promo cities file"
        )
        parser.add_argument(
            "--promo_countries", required=True, help="Path to promo countries file"
        )
        parser.add_argument(
            "--osm2ft", help="path to osm2ft files (default is the same as mwm)"
        )
        parser.add_argument(
            "--countries",
            help="path to countries.json file (default is countries.json file into mwm directory)",
        )
        parser.add_argument(
            "--output",
            help="Output countries.json file (default is countries.json file into mwm directory)",
        )
        args = parser.parse_args(sys.argv[2:])

        if not args.osm2ft:
            args.osm2ft = args.mwm
        if not args.countries:
            args.countries = os.path.join(args.mwm, "countries.json")
        if not args.output:
            args.output = os.path.join(args.mwm, "countries.json")

        with open(args.countries) as f:
            countries = json.load(f)

        inject_promo_ids(
            countries,
            args.promo_cities,
            args.promo_countries,
            args.mwm,
            args.types,
            args.osm2ft,
        )

        with open(args.output, "w") as f:
            json.dump(countries, f, ensure_ascii=False, indent=1)


PostGeneration()