Skip to content

Commit

Permalink
refactor(utils): replace URI_replace_settings.yml with clean_uri.toml
Browse files Browse the repository at this point in the history
... and move the `clean_uri.toml` to the `utils` folder

Closes: #572
  • Loading branch information
b1rger committed Jan 30, 2024
1 parent 8f1613b commit 12b1d80
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 19 deletions.
7 changes: 0 additions & 7 deletions apis_core/default_settings/URI_replace_settings.yml

This file was deleted.

7 changes: 7 additions & 0 deletions apis_core/utils/clean_uri.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[geonames_org]
regex = '^https?://(?:[^.]*[.])?geonames[.]org/([0-9]+)'
replace = 'https://sws.geonames.org/{}/'

[d-nb_info]
regex = '^https?://(?:[^.]*[.])?d-nb.info/gnd/([0-9A-Za-u\-]+)'
replace = 'https://d-nb.info/gnd/{}'
19 changes: 8 additions & 11 deletions apis_core/utils/normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,16 @@
# SPDX-License-Identifier: MIT

import re
import tomllib
from apis_core.utils.settings import clean_uri_mapping_file
from yaml import safe_load


def clean_uri(uri: str) -> str:
if uri:
settings = safe_load(clean_uri_mapping_file().read_text())
for mapping in settings.get("mappings", []):
domain = mapping["domain"]
regex = mapping["regex"]
replace = mapping["replace"]
if domain in uri:
m = re.match(regex, uri)
if m:
uri = replace.format(m.group(1))
settings = tomllib.loads(clean_uri_mapping_file().read_text())
if uri is not None:
for entry in settings.values():
regex = entry["regex"]
replace = entry["replace"]
if m := re.match(regex, uri):
uri = replace.format(m.group(1))
return uri
3 changes: 2 additions & 1 deletion apis_core/utils/settings.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# SPDX-FileCopyrightText: 2023 Birger Schacht
# SPDX-License-Identifier: MIT

import os
from pathlib import Path
from django.conf import settings

Expand All @@ -11,7 +12,7 @@ def default_settings() -> Path:


def clean_uri_mapping_file() -> Path:
default = default_settings() / "URI_replace_settings.yml"
default = Path(__file__).parent / "clean_uri.toml"
mapping_file = getattr(settings, "CLEANURI_MAPPINGS", default)
return Path(mapping_file)

Expand Down

0 comments on commit 12b1d80

Please sign in to comment.