Skip to content

Commit

Permalink
Remove unneeded arg validation code
Browse files Browse the repository at this point in the history
  • Loading branch information
thorbjoernl committed Jan 15, 2025
1 parent 401553e commit cc95143
Show file tree
Hide file tree
Showing 6 changed files with 2 additions and 94 deletions.
15 changes: 0 additions & 15 deletions src/aerovaldb/jsondb/jsonfiledb.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
parse_formatted_string,
parse_uri,
str_to_bool,
validate_filename_component,
)
from ..utils.filter import (
filter_contour,
Expand Down Expand Up @@ -220,19 +219,12 @@ async def _get(
route_args,
**kwargs,
):
validate_args = kwargs.pop("validate_args", True)
use_caching = kwargs.pop("cache", False)
default = kwargs.pop("default", None)
_raise_file_not_found_error = kwargs.pop("_raise_file_not_found_error", True)
access_type = self._normalize_access_type(kwargs.pop("access_type", None))

substitutions = route_args | kwargs
if validate_args:
[
validate_filename_component(x)
for x in substitutions.values()
if x is not None
]

logger.debug(f"Fetching data for {route}.")

Expand Down Expand Up @@ -299,11 +291,6 @@ async def _put(self, obj, route, route_args, **kwargs):
Otherwise it is assumed to be a serializable python object.
"""
substitutions = route_args | kwargs
[
validate_filename_component(x)
for x in substitutions.values()
if x is not None
]

path_template = await self._get_template(route, substitutions)
relative_path = path_template.format(**substitutions)
Expand Down Expand Up @@ -728,7 +715,6 @@ async def get_report_image(
"path": path,
},
access_type=access_type,
validate_args=False,
)
file_path = await self._get(
route=ROUTE_REPORT_IMAGE,
Expand All @@ -738,7 +724,6 @@ async def get_report_image(
"path": path,
},
access_type=AccessType.FILE_PATH,
validate_args=False,
)
logger.debug(f"Fetching image with path '{file_path}'")

Expand Down
3 changes: 0 additions & 3 deletions src/aerovaldb/sqlitedb/sqlitedb.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
extract_substitutions,
json_dumps_wrapper,
parse_uri,
validate_filename_component,
)

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -395,8 +394,6 @@ async def _get(self, route, route_args, **kwargs):
if k in AerovalSqliteDB.TABLE_COLUMN_NAMES[table_name]
}

[validate_filename_component(x) for x in args.values() if x is not None]

columnlist, substitutionlist = self._get_column_list_and_substitution_list(args)
cur.execute(
f"""
Expand Down
2 changes: 1 addition & 1 deletion src/aerovaldb/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .asyncio import async_and_sync, has_async_loop, run_until_finished
from .json import json_dumps_wrapper
from .string_utils import str_to_bool, validate_filename_component
from .string_utils import str_to_bool
from .uri import (
build_uri,
decode_arg,
Expand Down
28 changes: 0 additions & 28 deletions src/aerovaldb/utils/string_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,31 +41,3 @@ def str_to_bool(value: str, /, default: bool | None = None) -> bool:
return default

raise ValueError("Could not convert string to bool: '{value}'")


def validate_filename_component(value: str) -> None:
"""
Checks if a file name component contains characters which should
not be included in the file path.
:param value : The component to be validated.
:raises ValueError :
if value is not string or not a valid filename component
>>> from aerovaldb.utils.string_utils import validate_filename_component
>>> validate_filename_component("Hello world")
>>> validate_filename_component(5)
Traceback (most recent call last):
...
ValueError: Expected str, got <class 'int'>
>>> validate_filename_component("/")
Traceback (most recent call last):
...
ValueError: '/' is not a valid file name component.
"""
if not isinstance(value, str):
raise ValueError(f"Expected str, got {type(value)}")

if not PATH_COMPONENT_PATTERN.match(value):
raise ValueError(f"'{value}' is not a valid file name component.")
8 changes: 0 additions & 8 deletions tests/jsondb/test_jsonfiledb.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,6 @@ def test_jsonfiledb__get_uri_for_file(tmp_path):
)


def test_jsonfiledb_invalid_parameter_values():
with aerovaldb.open("json_files:./tests/test-db/json") as db:
with pytest.raises(ValueError) as e:
db.get_config("/%&/())()", "test")

assert "is not a valid file name component" in str(e.value)


def test_with_symlink():
with aerovaldb.open("json_files:./tests/test-db/json") as db:
data = db.get_config("linked-json-project", "experiment")
Expand Down
40 changes: 1 addition & 39 deletions tests/utils/test_string_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from aerovaldb.utils import str_to_bool, validate_filename_component
from aerovaldb.utils import str_to_bool


@pytest.mark.parametrize(
Expand Down Expand Up @@ -35,41 +35,3 @@ def test_str_to_bool_exception_2():
def test_str_to_bool_default():
assert str_to_bool("blah", default=True)
assert not str_to_bool("blah", default=False)


@pytest.mark.parametrize(
"value",
(
pytest.param(
"test1234_",
),
pytest.param(
"test-1234",
),
pytest.param(
"test 1234",
),
pytest.param("æøåÆØÅ"),
),
)
def test_validate_filename_component_valid(value: str):
validate_filename_component(value)


@pytest.mark.parametrize(
"value",
(
pytest.param(
"/",
),
pytest.param(
"abcd/alkfh",
),
pytest.param(
None,
),
),
)
def test_validate_filename_component_invalid(value):
with pytest.raises(ValueError):
validate_filename_component(value)

0 comments on commit cc95143

Please sign in to comment.