-
Notifications
You must be signed in to change notification settings - Fork 7
/
transform.py
187 lines (157 loc) · 5.32 KB
/
transform.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
"""
OTVision script to call the transform main with arguments parsed from command line
"""
# Copyright (C) 2022 OpenTrafficCam Contributors
# <https://github.com/OpenTrafficCam
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import argparse
import logging
from pathlib import Path
import OTVision
import OTVision.config as config
from OTVision.helpers.files import check_if_all_paths_exist
from OTVision.helpers.log import DEFAULT_LOG_FILE, LOGGER_NAME, VALID_LOG_LEVELS, log
def parse() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Transform tracks to UTM coordinates")
parser.add_argument(
"-p",
"--paths",
nargs="+",
type=str,
help="Path or list of paths to tracks files",
required=False,
)
parser.add_argument(
"-c",
"--config",
type=str,
help="Path to custom user configuration yaml file.",
required=False,
)
parser.add_argument(
"-r",
"--refpts_file",
type=str,
help="Path to refpts file. If not given, each tracks file should have one",
)
parser.add_argument(
"-o",
"--overwrite",
action=argparse.BooleanOptionalAction,
help="Overwrite existing output files",
)
parser.add_argument(
"--log_level_console",
type=str,
choices=VALID_LOG_LEVELS,
help="Log level for logging to the console",
required=False,
)
parser.add_argument(
"--log_level_file",
type=str,
choices=VALID_LOG_LEVELS,
help="Log level for logging to a log file",
required=False,
)
parser.add_argument(
"--logfile",
default=str(DEFAULT_LOG_FILE),
type=str,
help="Specify log file directory.",
required=False,
)
parser.add_argument(
"--logfile_overwrite",
action="store_true",
help="Overwrite log file if it already exists.",
required=False,
)
return parser.parse_args()
def _process_config(args: argparse.Namespace) -> None:
if args.config:
config.parse_user_config(args.config)
else:
user_config_cwd = Path(__file__).parent / "user_config.otvision.yaml"
if user_config_cwd.exists():
config.parse_user_config(str(user_config_cwd))
def _process_parameters(
args: argparse.Namespace, log: logging.Logger
) -> tuple[list[Path], Path | None, bool]: # sourcery skip: assign-if-exp
try:
paths = _extract_paths(args)
except IOError:
log.exception("Unable to extract paths from command line or config.yaml")
raise
except Exception:
log.exception("")
raise
# TODO: Check if path to refpts_file is given from CLI (like e.g. for overwrite)
if args.refpts_file:
refpts_file = Path(args.refpts_file)
else:
refpts_file = None
if args.overwrite is None:
overwrite = config.CONFIG[config.TRANSFORM][config.OVERWRITE]
else:
overwrite = args.overwrite
return paths, refpts_file, overwrite
def _extract_paths(args: argparse.Namespace) -> list[Path]:
if args.paths is None:
str_paths = config.CONFIG[config.TRANSFORM][config.PATHS]
else:
str_paths = args.paths
if len(str_paths) == 0:
raise IOError(
"No paths have been passed as command line args."
"No paths have been defined in the user config."
)
paths = [Path(str_path).expanduser() for str_path in str_paths]
check_if_all_paths_exist(paths)
return paths
def _configure_logger(args: argparse.Namespace) -> logging.Logger:
if args.log_level_console is None:
log_level_console = config.CONFIG[config.LOG][config.LOG_LEVEL_CONSOLE]
else:
log_level_console = args.log_level_console
if args.log_level_file is None:
log_level_file = config.CONFIG[config.LOG][config.LOG_LEVEL_FILE]
else:
log_level_file = args.log_level_file
log.add_console_handler(level=log_level_console)
log.add_file_handler(
Path(args.logfile).expanduser(),
log_level_file,
args.logfile_overwrite,
)
return logging.getLogger(LOGGER_NAME)
def main() -> None:
args = parse()
_process_config(args)
log = _configure_logger(args)
paths, refpts_file, overwrite = _process_parameters(args, log)
log.info("Call transform from command line")
log.info(f"Arguments: {vars(args)}")
try:
OTVision.transform(paths=paths, refpts_file=refpts_file, overwrite=overwrite)
except FileNotFoundError:
log.exception(f"One of the following files cannot be found: {paths}")
raise
except Exception:
log.exception("")
raise
if __name__ == "__main__":
main()