forked from stac47/osm-garmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.py
executable file
·164 lines (116 loc) · 3.89 KB
/
map.py
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
159
160
161
162
163
164
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
CLI client application.
TODO:
- progress bar (http://www.linuxtrack.com/t1171-barre-de-progression.htm)
- parsing arguments
Created on 2013-06-22
@author : Laurent Stacul
"""
import argparse
import re
from abc import ABCMeta, abstractmethod
from datetime import datetime
import scripts.logconfig
scripts.logconfig.configLoggers()
import scripts.mapcreator
import scripts.disttree
_parser = argparse.ArgumentParser()
_parser.add_argument("-i", "--inputmap", type=str,
help="The input map descriptor file.",
default="map.xml")
_subparsers = _parser.add_subparsers(help="Sub-commands help")
class Command(object, metaclass=ABCMeta):
""" Base class representing a user command entry."""
_commands = []
_command_regex = re.compile(r"^(\w+)Command$")
@staticmethod
@abstractmethod
def register_parser(parser):
pass
@classmethod
def register(cls):
m = re.match(Command._command_regex, cls.__name__)
if m:
cls.name = m.group(1).lower()
else:
msg = "Command class name must follow the <name>Command pattern"
raise Exception(msg)
Command._commands.append(cls)
sp = _subparsers.add_parser(cls.name)
sp.set_defaults(command=cls.name)
cls.register_parser(sp)
@staticmethod
def execute(s, args):
for c in Command._commands:
if c.name == s:
c()(args)
def __init__(self):
super().__init__()
def __call__(self, args):
start_time = datetime.utcnow()
print("{} - Running [{}]".format(start_time.isoformat(),
self.name))
self._do_run(args)
end_time = datetime.utcnow()
print("{} - Finished [{}]".format(end_time.isoformat(),
self.name))
elapsed_time = end_time - start_time
print("Elapsed time: {}".format(elapsed_time))
@abstractmethod
def _do_run(self):
pass
class AutoRegisterCommand(ABCMeta):
def __new__(cls, *args, **kwargs):
newclass = super().__new__(cls, *args, **kwargs)
newclass.register()
return newclass
class InitCommand(Command, metaclass=AutoRegisterCommand):
@staticmethod
def register_parser(parser):
parser.help = "Init the working directory"
def __init__(self):
super().__init__()
def _do_run(self, args):
scripts.disttree.init()
class CleanCommand(Command, metaclass=AutoRegisterCommand):
@staticmethod
def register_parser(parser):
parser.help = "Clean the working directory"
def __init__(self):
super().__init__()
def _do_run(self, args):
scripts.disttree.clean()
class DownloadCommand(Command, metaclass=AutoRegisterCommand):
@staticmethod
def register_parser(parser):
parser.help = "Download OSM maps."
def __init__(self):
super().__init__()
def _do_run(self, args):
scripts.disttree.create()
if scripts.mapcreator.download(args.inputmap) == 0:
print("Nothing to download")
class SplitCommand(Command, metaclass=AutoRegisterCommand):
@staticmethod
def register_parser(parser):
parser.help = "Split the maps into smaller tiles."
def __init__(self):
super().__init__()
def _do_run(self, args):
scripts.disttree.create()
scripts.mapcreator.split_maps()
class BuildCommand(Command, metaclass=AutoRegisterCommand):
@staticmethod
def register_parser(parser):
parser.help = "Build the final gmapsupp file."
def __init__(self):
super().__init__()
def _do_run(self, args):
scripts.mapcreator.create_map_from_tiles()
def main():
args = _parser.parse_args()
Command.execute(args.command, args)
if __name__ == "__main__":
main()