Skip to content

Commit

Permalink
Suppress unwanted character encoding on local files
Browse files Browse the repository at this point in the history
  • Loading branch information
mraspaud committed Dec 6, 2024
1 parent 9b7d8f6 commit 78844b2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
13 changes: 11 additions & 2 deletions src/pytroll_watchers/publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import logging
from contextlib import closing, contextmanager, suppress
from copy import deepcopy
from urllib.parse import unquote

import fsspec
from posttroll.message import Message
Expand Down Expand Up @@ -85,7 +86,7 @@ def unpack_archive(path, unpack):
protocol=unpack,
target_protocol=path.protocol,
target_options=path.storage_options,
fo=path.as_uri())
fo=as_uri(path))


def unpack_dir(path):
Expand All @@ -99,7 +100,7 @@ def unpack_dir(path):

def _build_file_location(file_item, include_dir=None):
file_location = dict()
file_location["uri"] = file_item.as_uri()
file_location["uri"] = as_uri(file_item)
if include_dir:
uid = include_dir + file_item.path.rsplit(include_dir, 1)[-1]
else:
Expand All @@ -113,6 +114,14 @@ def _build_file_location(file_item, include_dir=None):

return file_location

def as_uri(file_item):
"""Represent file item’s path as an unquoted uri."""
with suppress(AttributeError):
protocol = file_item.protocol
if protocol.startswith("http"):
return file_item.as_uri()
return unquote(file_item.as_uri())


@contextmanager
def dummy_connect(file_item):
Expand Down
5 changes: 3 additions & 2 deletions tests/test_local_watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ def test_watchdog_generator_with_something_else(tmp_path):

def test_publish_paths(tmp_path, patched_local_events, caplog): # noqa
"""Test publishing paths."""
filename = os.fspath(tmp_path / "foo.txt")
basename = "foo+bar,baz_.txt"
filename = os.fspath(tmp_path / basename)

local_settings = dict(directory=tmp_path)
publisher_settings = dict(nameservers=False, port=1979)
Expand All @@ -95,7 +96,7 @@ def test_publish_paths(tmp_path, patched_local_events, caplog): # noqa
assert "uri" not in message_settings["data"]
assert len(messages) == 1
message = Message(rawstr=messages[0])
assert message.data["uri"] == f"file://{str(tmp_path)}/foo.txt"
assert message.data["uri"] == f"file://{str(tmp_path)}/{basename}"
assert message.data["sensor"] == "viirs"
assert "filesystem" not in message.data
assert f"Starting watch on '{local_settings['directory']}'" in caplog.text
Expand Down

0 comments on commit 78844b2

Please sign in to comment.