This repository has been archived by the owner on Jun 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathgenerate_json_style.py
50 lines (39 loc) · 1.7 KB
/
generate_json_style.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
"""
Helper file that takes static/style/layers.js and makes it into a JSON.
This will make it easier to load style to Maputnik editor.
"""
import json
import logging
import sys
from pathlib import Path
from typing import Union
logging.basicConfig(format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")
logger = logging.getLogger(__file__)
logger.setLevel(logging.INFO)
def save_json(file_path: Union[str, Path], data: dict) -> None:
logger.info(f"Saving .json file. [path={file_path}]")
with open(file=file_path, mode="w", encoding="utf-8") as f:
json.dump(data, f, allow_nan=False, separators=(",", ":"))
logger.info("Done saving json file.")
def main(wd: Path) -> None:
js_file_path = wd.joinpath('layers.js')
if not js_file_path.is_file():
logger.error(f'Given path: "{js_file_path}" is not a file.')
sys.exit(1) # exit with non-zero error code
# remove string "export default " the rest is valid json
style = open(js_file_path, 'r', encoding='utf-8').read()[15:]
try:
style_json = json.loads(style)
except json.JSONDecodeError as e:
logger.error(f'There was a problem parsing json style: {e}', exc_info=True)
raise
json_file_path = wd.joinpath('layers.json')
save_json(file_path=json_file_path, data=style_json)
if __name__ == "__main__":
this_files_dir = Path(__file__).parent.resolve().joinpath('static/style/').resolve()
work_dir = Path(sys.argv[1]) if len(sys.argv) > 1 else this_files_dir
if not work_dir.is_dir():
logger.error(f'Given path: "{work_dir}" is not a directory.')
sys.exit(1) # exit with non-zero error code
logger.info(f'Work dir is: {work_dir}')
main(wd=work_dir)