forked from edward/owl-map
Update
This commit is contained in:
parent
2e8ff40d3d
commit
fd35658e51
|
@ -1,3 +1,5 @@
|
||||||
|
"""Utility functions."""
|
||||||
|
|
||||||
import json
|
import json
|
||||||
import math
|
import math
|
||||||
import os.path
|
import os.path
|
||||||
|
@ -112,20 +114,26 @@ def any_upper(value: str) -> bool:
|
||||||
return any(c.isupper() for c in value)
|
return any(c.isupper() for c in value)
|
||||||
|
|
||||||
|
|
||||||
def find_log_file(place):
|
|
||||||
start = f"{place.place_id}_"
|
|
||||||
for f in os.scandir(good_location()):
|
|
||||||
if f.name.startswith(start):
|
|
||||||
return f.path
|
|
||||||
|
|
||||||
|
|
||||||
def get_free_space(config: flask.config.Config) -> int:
|
def get_free_space(config: flask.config.Config) -> int:
|
||||||
"""Return the amount of available free space."""
|
"""Return the amount of available free space."""
|
||||||
s = os.statvfs(config["FREE_SPACE_PATH"])
|
s = os.statvfs(config["FREE_SPACE_PATH"])
|
||||||
return s.f_bsize * s.f_bavail
|
return s.f_bsize * s.f_bavail
|
||||||
|
|
||||||
|
|
||||||
def display_distance(units, dist):
|
def metric_display_distance(units: str, dist: float) -> str | None:
|
||||||
|
"""Convert distance from metres to the specified metric units."""
|
||||||
|
if units == "km_and_metres":
|
||||||
|
units = "km" if dist > 500 else "metres"
|
||||||
|
if units == "metres":
|
||||||
|
return f"{dist:,.0f} m"
|
||||||
|
if units == "km":
|
||||||
|
return f"{dist / 1000:,.2f} km"
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def display_distance(units: str, dist: float) -> str | None:
|
||||||
|
"""Convert distance from metres to the specified units."""
|
||||||
if units in ("miles_and_feet", "miles_and_yards"):
|
if units in ("miles_and_feet", "miles_and_yards"):
|
||||||
total_feet = dist * feet_per_metre
|
total_feet = dist * feet_per_metre
|
||||||
miles = total_feet / feet_per_mile
|
miles = total_feet / feet_per_mile
|
||||||
|
@ -142,12 +150,7 @@ def display_distance(units, dist):
|
||||||
miles = dist / metres_per_mile
|
miles = dist / metres_per_mile
|
||||||
return f"{miles:,.2f} miles" if miles > 0.5 else f"{dist:,.0f} metres"
|
return f"{miles:,.2f} miles" if miles > 0.5 else f"{dist:,.0f} metres"
|
||||||
|
|
||||||
if units == "km_and_metres":
|
return metric_display_distance(units, dist)
|
||||||
units = "km" if dist > 500 else "metres"
|
|
||||||
if units == "metres":
|
|
||||||
return f"{dist:,.0f} m"
|
|
||||||
if units == "km":
|
|
||||||
return f"{dist / 1000:,.2f} km"
|
|
||||||
|
|
||||||
|
|
||||||
def is_in_range(address_range: str, address: str) -> bool:
|
def is_in_range(address_range: str, address: str) -> bool:
|
||||||
|
@ -177,20 +180,27 @@ def is_in_range(address_range: str, address: str) -> bool:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def format_wikibase_time(v):
|
class WikibaseTime(typing.TypedDict):
|
||||||
p = v["precision"]
|
"""Wikibase Time dict."""
|
||||||
|
|
||||||
|
precision: int
|
||||||
|
time: str
|
||||||
|
|
||||||
|
|
||||||
|
def format_wikibase_time(v: WikibaseTime) -> str | None:
|
||||||
|
"""Format wikibase time value into human readable string."""
|
||||||
t = v["time"]
|
t = v["time"]
|
||||||
|
|
||||||
# TODO: handle dates with century precision (7)
|
match v["precision"]:
|
||||||
# example: https://www.wikidata.org/wiki/Q108266998
|
case 11: # year, month and day
|
||||||
|
|
||||||
if p == 11:
|
|
||||||
return date.fromisoformat(t[1:11]).strftime("%-d %B %Y")
|
return date.fromisoformat(t[1:11]).strftime("%-d %B %Y")
|
||||||
if p == 10:
|
case 10: # year and month
|
||||||
return date.fromisoformat(t[1:8] + "-01").strftime("%B %Y")
|
return date.fromisoformat(t[1:8] + "-01").strftime("%B %Y")
|
||||||
if p == 9:
|
case 9: # year
|
||||||
return t[1:5]
|
return t[1:5]
|
||||||
if p == 7:
|
case 7: # century
|
||||||
century = ((int(t[:5]) - 1) // 100) + 1
|
century = ((int(t[:5]) - 1) // 100) + 1
|
||||||
end = " BC" if century < 0 else ""
|
ordinal_num: str = num2words(abs(century), to="ordinal_num")
|
||||||
return num2words(abs(century), to="ordinal_num") + " century" + end
|
return f"{ordinal_num} {century}{' BC' if century < 0 else ''}"
|
||||||
|
case _: # not handled
|
||||||
|
return None
|
||||||
|
|
Loading…
Reference in a new issue