rewrite to python: export to bluemap
This commit is contained in:
@@ -1,15 +1,80 @@
|
||||
import json
|
||||
from textwrap import indent
|
||||
from typing import Any
|
||||
from pathlib import Path
|
||||
|
||||
# from minecraft_kartverket.marker_sets import WORLDS
|
||||
# from minecraft_kartverket.lib_marker import Point
|
||||
from minecraft_kartverket.marker_sets import WORLDS
|
||||
from minecraft_kartverket.lib_marker import Point
|
||||
|
||||
|
||||
def generate_bluemap_output(output_dir: Path):
|
||||
raise NotImplementedError("Bluemap export is not implemented yet")
|
||||
...
|
||||
output_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
data = convert_data_to_bluemap_structure()
|
||||
|
||||
for world_name, marker_sets in data.items():
|
||||
with open(output_dir / f"{slug(world_name)}.hocon", "w") as f:
|
||||
f.write(to_hocon(marker_sets))
|
||||
|
||||
|
||||
def slug(s: str) -> str:
|
||||
return (
|
||||
s.lower()
|
||||
.replace(" ", "-")
|
||||
.replace("ø", "o")
|
||||
.replace("æ", "ae")
|
||||
.replace("å", "a")
|
||||
)
|
||||
|
||||
|
||||
def convert_data_to_bluemap_structure() -> dict:
|
||||
...
|
||||
result = {}
|
||||
|
||||
def to_hocon():
|
||||
...
|
||||
for world_name, marker_sets in WORLDS.items():
|
||||
result[world_name] = {}
|
||||
|
||||
for marker_set in marker_sets:
|
||||
result[world_name][slug(marker_set.name)] = {
|
||||
"label": marker_set.name,
|
||||
"toggleable": True,
|
||||
"default-hidden": not marker_set.show_by_default,
|
||||
"markers": {},
|
||||
}
|
||||
|
||||
for marker in marker_set.markers:
|
||||
if isinstance(marker, Point):
|
||||
result[world_name][slug(marker_set.name)][slug(marker.name)] = {
|
||||
"type": "poi",
|
||||
"label": marker.name,
|
||||
"position": {
|
||||
"x": marker.x,
|
||||
"y": marker.y,
|
||||
"z": marker.z,
|
||||
},
|
||||
}
|
||||
else:
|
||||
raise NotImplementedError(f"Unknown marker type: {marker}")
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def to_hocon(x: Any) -> str:
|
||||
if isinstance(x, str):
|
||||
return json.dumps(x, ensure_ascii=False)
|
||||
elif isinstance(x, int):
|
||||
return str(x)
|
||||
elif isinstance(x, float):
|
||||
return str(x)
|
||||
elif isinstance(x, bool):
|
||||
return str(x).lower()
|
||||
elif isinstance(x, list):
|
||||
items = [indent(to_hocon(y), " ") for y in x]
|
||||
return f"[\n{'\n'.join(items)}\n]"
|
||||
elif isinstance(x, dict):
|
||||
items = [
|
||||
f' {k}: {indent(to_hocon(v), ' ').removeprefix(' ')}'
|
||||
for k, v in x.items()
|
||||
]
|
||||
return f"{{\n{'\n'.join(items)}\n}}"
|
||||
else:
|
||||
raise ValueError(f"Unknown type: {x}")
|
||||
|
||||
Reference in New Issue
Block a user