Welcome to mirror list, hosted at ThFree Co, Russian Federation.

features_db_updater.py « local_ads « python « tools - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 127e995a5438715a70949f8d31ca8d7772a2e403 (plain)
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
#!/usr/bin/env python2.7

from __future__ import print_function

import os
import sys

# TODO(mgsergio, zveric, yershov): Make mwm an installable module.
sys.path.append(
    os.path.join(
        os.path.dirname(__file__), '..', 'mwm'
    )
)

import argparse
import csv
# c_long is used to get signed int64. Postgres can't handle uint64.
import ctypes
import logging
import mwm

from itertools import islice
from zlib import adler32


def get_mapping(mapping_name):
    with open(mapping_name, 'rb') as f:
        osm2ft = mwm.read_osm2ft(f, tuples=False)

    for osmid, fid in osm2ft.iteritems():
        yield ctypes.c_long(osmid).value, fid


def print_mapping(mapping, count):
    for osmid, fid in islice(mapping, count):
        print('{}\t{}'.format(osmid, fid))


def generate_id_from_name_and_version(name, version):
    return ctypes.c_long((adler32(name) << 32) | version).value


def generate_csvs(mapping, mapping_name, version, output_path):
    mwm_id = generate_id_from_name_and_version(
        mapping_name,
        version
    )

    with open(os.path.join(output_path, 'mwm.csv'), 'ab') as f:
        w = csv.writer(f)
        # TODO(mgsergio): Either remove or make so this is will write only one header.
        # w.writerow(['id', 'name', 'version'])
        w.writerow([
            mwm_id,
            mapping_name,
            version,
        ])

    with open(os.path.join(output_path, 'mapping.csv'), 'ab') as f:
        w = csv.writer(f)
        # TODO(mgsergio): Either remove or make so this is will write only one header.
        # w.writerow(['osmid', 'fid', 'mwm_id', mwm_version])
        for row in mapping:
            w.writerow(row + (mwm_id, version))


def get_args():
    parser = argparse.ArgumentParser()
    src = parser.add_mutually_exclusive_group(required=True)
    dst = parser.add_mutually_exclusive_group(required=True)

    src.add_argument(
        '--mapping_names',
        nargs='+',
        help='osm2ft files to handle.'
    )
    src.add_argument(
        '--mapping_path',
        nargs=1,
        action=AppendOsm2FidAction,
        dest='mapping_names',
        help='Path to folder with .osm2ft. Each file whould be handled.'
    )

    dst.add_argument(
        '--output_path',
        help='A path to an output folder.'
    )
    dst.add_argument(
        '--head',
        type=int,
        help='Write that much lines of osmid <-> fid to stdout.'
    )

    parser.add_argument(
        '--version',
        required=True,
        type=int,
        help='The version of mwm for which a mapping is generated.'
    )

    return parser.parse_args();


def main():
    args = get_args()
    for mapping_name in args.mapping_names:
        mapping = get_mapping(mapping_name)
        if args.head:
            print('{}:'.format(mapping_name))
            print_mapping(mapping, args.head)
            exit(0)
        mwm_name = (
            os.path.basename(mapping_name)
            .split('.', 1)
        )[0]
        logging.info('Writing mapping for {}'.format(mapping_name))
        generate_csvs(
            mapping,
            mwm_name,
            args.version,
            args.output_path
        )


class AppendOsm2FidAction(argparse.Action):
    def __init__(self, option_strings, dest, nargs=None, **kwargs):
        assert nargs == 1, 'nargs should equals to 1.'
        super(AppendOsm2FidAction, self).__init__(
            option_strings,
            dest,
            nargs=1,
            **kwargs
        )

    def __call__(self, parser, namespace, values, option_string=None):
        values = [
            os.path.join(values[0], mapping_name)
            for mapping_name in os.listdir(values[0])
            if mapping_name.endswith('.osm2ft')
        ]
        setattr(namespace, self.dest, values)


if __name__ == '__main__':
    logging.basicConfig(level=logging.INFO)
    main()