Skip to content

Commit

Permalink
fix typo (#11)
Browse files Browse the repository at this point in the history
* fix typo
  • Loading branch information
floriankrb authored Sep 30, 2024
1 parent 14531b9 commit 4f59e35
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 52 deletions.
22 changes: 11 additions & 11 deletions src/anemoi/utils/sanetise.py → src/anemoi/utils/sanitise.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,41 +20,41 @@
RE2 = re.compile(r"\(([^}]*)\)")


def sanetise(obj):
"""sanetise an object:
def sanitise(obj):
"""sanitise an object:
- by replacing all full paths with shortened versions.
- by replacing URL passwords with '***'.
"""

if isinstance(obj, dict):
return {sanetise(k): sanetise(v) for k, v in obj.items()}
return {sanitise(k): sanitise(v) for k, v in obj.items()}

if isinstance(obj, list):
return [sanetise(v) for v in obj]
return [sanitise(v) for v in obj]

if isinstance(obj, tuple):
return tuple(sanetise(v) for v in obj)
return tuple(sanitise(v) for v in obj)

if isinstance(obj, str):
return _sanetise_string(obj)
return _sanitise_string(obj)

return obj


def _sanetise_string(obj):
def _sanitise_string(obj):

parsed = urlparse(obj, allow_fragments=True)

if parsed.scheme:
return _sanetise_url(parsed)
return _sanitise_url(parsed)

if obj.startswith("/") or obj.startswith("~"):
return _sanetise_path(obj)
return _sanitise_path(obj)

return obj


def _sanetise_url(parsed):
def _sanitise_url(parsed):

LIST = [
"pass",
Expand Down Expand Up @@ -98,7 +98,7 @@ def _sanetise_url(parsed):
return urlunparse([scheme, netloc, path, params, query, fragment])


def _sanetise_path(path):
def _sanitise_path(path):
bits = list(reversed(Path(path).parts))
result = [bits.pop(0)]
for bit in bits:
Expand Down
82 changes: 41 additions & 41 deletions tests/test_sanetise.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,60 +6,60 @@
# nor does it submit to any jurisdiction.


from anemoi.utils.sanetise import sanetise
from anemoi.utils.sanitise import sanitise


def test_sanetise_urls():
assert sanetise("http://johndoe:password@host:port/path") == "http://user:***@host:port/path"
def test_sanitise_urls():
assert sanitise("http://johndoe:password@host:port/path") == "http://user:***@host:port/path"

assert sanetise("http://www.example.com/path?pass=secret") == "http://www.example.com/path?pass=hidden"
assert sanetise("http://www.example.com/path?password=secret") == "http://www.example.com/path?password=hidden"
assert sanetise("http://www.example.com/path?token=secret") == "http://www.example.com/path?token=hidden"
assert sanetise("http://www.example.com/path?user=secret") == "http://www.example.com/path?user=hidden"
assert sanetise("http://www.example.com/path?key=secret") == "http://www.example.com/path?key=hidden"
assert sanetise("http://www.example.com/path?pwd=secret") == "http://www.example.com/path?pwd=hidden"
assert sanetise("http://www.example.com/path?_key=secret") == "http://www.example.com/path?_key=hidden"
assert sanetise("http://www.example.com/path?_token=secret") == "http://www.example.com/path?_token=hidden"
assert sanetise("http://www.example.com/path?apikey=secret") == "http://www.example.com/path?apikey=hidden"
assert sanetise("http://www.example.com/path?api_key=secret") == "http://www.example.com/path?api_key=hidden"
assert sanetise("http://www.example.com/path?api_token=secret") == "http://www.example.com/path?api_token=hidden"
assert sanetise("http://www.example.com/path?_api_token=secret") == "http://www.example.com/path?_api_token=hidden"
assert sanetise("http://www.example.com/path?_api_key=secret") == "http://www.example.com/path?_api_key=hidden"
assert sanetise("http://www.example.com/path?username=secret") == "http://www.example.com/path?username=hidden"
assert sanetise("http://www.example.com/path?login=secret") == "http://www.example.com/path?login=hidden"
assert sanitise("http://www.example.com/path?pass=secret") == "http://www.example.com/path?pass=hidden"
assert sanitise("http://www.example.com/path?password=secret") == "http://www.example.com/path?password=hidden"
assert sanitise("http://www.example.com/path?token=secret") == "http://www.example.com/path?token=hidden"
assert sanitise("http://www.example.com/path?user=secret") == "http://www.example.com/path?user=hidden"
assert sanitise("http://www.example.com/path?key=secret") == "http://www.example.com/path?key=hidden"
assert sanitise("http://www.example.com/path?pwd=secret") == "http://www.example.com/path?pwd=hidden"
assert sanitise("http://www.example.com/path?_key=secret") == "http://www.example.com/path?_key=hidden"
assert sanitise("http://www.example.com/path?_token=secret") == "http://www.example.com/path?_token=hidden"
assert sanitise("http://www.example.com/path?apikey=secret") == "http://www.example.com/path?apikey=hidden"
assert sanitise("http://www.example.com/path?api_key=secret") == "http://www.example.com/path?api_key=hidden"
assert sanitise("http://www.example.com/path?api_token=secret") == "http://www.example.com/path?api_token=hidden"
assert sanitise("http://www.example.com/path?_api_token=secret") == "http://www.example.com/path?_api_token=hidden"
assert sanitise("http://www.example.com/path?_api_key=secret") == "http://www.example.com/path?_api_key=hidden"
assert sanitise("http://www.example.com/path?username=secret") == "http://www.example.com/path?username=hidden"
assert sanitise("http://www.example.com/path?login=secret") == "http://www.example.com/path?login=hidden"

assert sanetise("http://www.example.com/path;pass=secret") == "http://www.example.com/path;pass=hidden"
assert sanetise("http://www.example.com/path;password=secret") == "http://www.example.com/path;password=hidden"
assert sanetise("http://www.example.com/path;token=secret") == "http://www.example.com/path;token=hidden"
assert sanetise("http://www.example.com/path;user=secret") == "http://www.example.com/path;user=hidden"
assert sanetise("http://www.example.com/path;key=secret") == "http://www.example.com/path;key=hidden"
assert sanetise("http://www.example.com/path;pwd=secret") == "http://www.example.com/path;pwd=hidden"
assert sanetise("http://www.example.com/path;_key=secret") == "http://www.example.com/path;_key=hidden"
assert sanetise("http://www.example.com/path;_token=secret") == "http://www.example.com/path;_token=hidden"
assert sanetise("http://www.example.com/path;apikey=secret") == "http://www.example.com/path;apikey=hidden"
assert sanetise("http://www.example.com/path;api_key=secret") == "http://www.example.com/path;api_key=hidden"
assert sanetise("http://www.example.com/path;api_token=secret") == "http://www.example.com/path;api_token=hidden"
assert sanetise("http://www.example.com/path;_api_token=secret") == "http://www.example.com/path;_api_token=hidden"
assert sanetise("http://www.example.com/path;_api_key=secret") == "http://www.example.com/path;_api_key=hidden"
assert sanetise("http://www.example.com/path;username=secret") == "http://www.example.com/path;username=hidden"
assert sanetise("http://www.example.com/path;login=secret") == "http://www.example.com/path;login=hidden"
assert sanitise("http://www.example.com/path;pass=secret") == "http://www.example.com/path;pass=hidden"
assert sanitise("http://www.example.com/path;password=secret") == "http://www.example.com/path;password=hidden"
assert sanitise("http://www.example.com/path;token=secret") == "http://www.example.com/path;token=hidden"
assert sanitise("http://www.example.com/path;user=secret") == "http://www.example.com/path;user=hidden"
assert sanitise("http://www.example.com/path;key=secret") == "http://www.example.com/path;key=hidden"
assert sanitise("http://www.example.com/path;pwd=secret") == "http://www.example.com/path;pwd=hidden"
assert sanitise("http://www.example.com/path;_key=secret") == "http://www.example.com/path;_key=hidden"
assert sanitise("http://www.example.com/path;_token=secret") == "http://www.example.com/path;_token=hidden"
assert sanitise("http://www.example.com/path;apikey=secret") == "http://www.example.com/path;apikey=hidden"
assert sanitise("http://www.example.com/path;api_key=secret") == "http://www.example.com/path;api_key=hidden"
assert sanitise("http://www.example.com/path;api_token=secret") == "http://www.example.com/path;api_token=hidden"
assert sanitise("http://www.example.com/path;_api_token=secret") == "http://www.example.com/path;_api_token=hidden"
assert sanitise("http://www.example.com/path;_api_key=secret") == "http://www.example.com/path;_api_key=hidden"
assert sanitise("http://www.example.com/path;username=secret") == "http://www.example.com/path;username=hidden"
assert sanitise("http://www.example.com/path;login=secret") == "http://www.example.com/path;login=hidden"


def test_sanetise_paths():
def test_sanitise_paths():
# We want to keep earthkit-data's url and path pattern

assert sanetise("/home/johndoe/.ssh/id_rsa") == "/.../id_rsa"
assert sanitise("/home/johndoe/.ssh/id_rsa") == "/.../id_rsa"

assert (
sanetise("/data/model/{date:strftime(%Y)}/{date:strftime(%m)}/{date:strftime(%d)}/analysis.grib")
sanitise("/data/model/{date:strftime(%Y)}/{date:strftime(%m)}/{date:strftime(%d)}/analysis.grib")
== "/.../{date:strftime(%Y)}/{date:strftime(%m)}/{date:strftime(%d)}/analysis.grib"
)

assert sanetise("test.grib") == "test.grib"
assert sanetise("../test.grib") == "../test.grib"
assert sanetise("./test.grib") == "./test.grib"
assert sanetise("sub/folder/test.grib") == "sub/folder/test.grib"
assert sanetise("./folder/test.grib") == "./folder/test.grib"
assert sanitise("test.grib") == "test.grib"
assert sanitise("../test.grib") == "../test.grib"
assert sanitise("./test.grib") == "./test.grib"
assert sanitise("sub/folder/test.grib") == "sub/folder/test.grib"
assert sanitise("./folder/test.grib") == "./folder/test.grib"


if __name__ == "__main__":
Expand Down

0 comments on commit 4f59e35

Please sign in to comment.