Skip to content

Commit

Permalink
Remove commented code
Browse files Browse the repository at this point in the history
  • Loading branch information
thorbjoernl committed Aug 2, 2024
1 parent e1f5d6a commit 59a76df
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 63 deletions.
16 changes: 7 additions & 9 deletions src/aerovaldb/aerovaldb.py
Original file line number Diff line number Diff line change
Expand Up @@ -1140,9 +1140,9 @@ async def get_by_uri(
Note:
-----
URI is implementation specific. While AerovalJsonFileDB returns
a file path, this behaviour should not be relied upon as other
implementations may not.
URI is intended to be consistent between implementations. Using get_by_uri()
to fetch an identifier which can then be written to another connector using
its respective put_by_uri() method.
"""
raise NotImplementedError

Expand All @@ -1156,9 +1156,9 @@ async def put_by_uri(self, obj, uri: str):
Note:
-----
URI is implementation specific. While AerovalJsonFileDB returns
a file path as the uri, this behaviour should not be relied upon
as other implementations will not.
URI is intended to be consistent between implementations. Using get_by_uri()
to fetch an identifier which can then be written to another connector using
its respective put_by_uri() method.
"""
raise NotImplementedError

Expand Down Expand Up @@ -1195,6 +1195,4 @@ def _normalize_access_type(
if access_type is None:
return default

raise ValueError(
f"Access_type, {access_type}, could not be normalized. This is probably due to input that is not a str or AccessType instance."
)
assert False
86 changes: 37 additions & 49 deletions src/aerovaldb/jsondb/jsonfiledb.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@

from aerovaldb.aerovaldb import AerovalDB
from aerovaldb.exceptions import UnusedArguments, TemplateNotFound
from aerovaldb.serialization.default_serialization import default_serialization
from aerovaldb.types import AccessType

from ..utils import async_and_sync, json_dumps_wrapper, parse_uri
from .uri import get_uri
from ..utils import (
async_and_sync,
json_dumps_wrapper,
parse_uri,
parse_formatted_string,
build_uri,
)
from .templatemapper import (
TemplateMapper,
DataVersionToTemplateMapper,
Expand All @@ -29,6 +33,9 @@
from ..lock.lock import FakeLock, FileLock
from hashlib import md5
import simplejson # type: ignore
from ..sqlitedb.utils import (
extract_substitutions,
) # TODO: Move this to a more approriate location before merging PR.

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -477,6 +484,32 @@ async def get_heatmap(
cache=True,
)

@async_and_sync
async def _get_uri_for_file(self, file_path: str) -> str:
"""
For the provided data file path, returns the corresponding
URI.
:param file_path : The file_path.
"""
file_path = os.path.relpath(file_path, start=self._basedir)

for route in self.PATH_LOOKUP:
template = await self._get_template(route, {})
route_arg_names = extract_substitutions(template)

try:
all_args = parse_formatted_string(f"./{template}", file_path)

route_args = {k: v for k, v in all_args.items() if k in route_arg_names}
kwargs = {
k: v for k, v in all_args.items() if not (k in route_arg_names)
}
except:
continue
else:
return build_uri(route, route_args, kwargs)

def list_glob_stats(
self, project: str, experiment: str
) -> Generator[str, None, None]:
Expand Down Expand Up @@ -579,6 +612,7 @@ async def get_by_uri(
cache: bool = False,
default=None,
):
access_type = self._normalize_access_type(access_type)
if access_type in [AccessType.URI]:
return uri

Expand All @@ -592,58 +626,12 @@ async def get_by_uri(
access_type=access_type,
**kwargs,
)
# if not isinstance(uri, str):
# uri = str(uri)
# if uri.startswith("."):
# uri = get_uri(os.path.join(self._basedir, uri))

#
# if not uri.startswith(self._basedir):
# raise PermissionError(
# f"URI {uri} is out of bounds of the current aerovaldb connection."
# )
#
# access_type = self._normalize_access_type(access_type)
#
# if not os.path.exists(uri):
# if default is None or access_type == AccessType.FILE_PATH:
# raise FileNotFoundError(f"Object with URI {uri} does not exist.")
#
# return default
# if access_type == AccessType.FILE_PATH:
# return uri
#
# if access_type == AccessType.JSON_STR:
# raw = await self._cache.get_json(uri, no_cache=not cache)
# return json_dumps_wrapper(raw)

# raw = await self._cache.get_json(uri, no_cache=not cache)
#
# return simplejson.loads(raw, allow_nan=True)

@async_and_sync
async def put_by_uri(self, obj, uri: str):
route, route_args, kwargs = parse_uri(uri)

await self._put(obj, route, route_args, **kwargs)
# if not isinstance(uri, str):
# uri = str(uri)
# if uri.startswith("."):
# uri = get_uri(os.path.join(self._basedir, uri))

#
# if not uri.startswith(self._basedir):
# raise PermissionError(
# f"URI {uri} is out of bounds of the current aerovaldb connection."
# )
# if not os.path.exists(os.path.dirname(uri)):
# os.makedirs(os.path.dirname(uri))
# if isinstance(obj, str):
# json = obj
# else:
# json = json_dumps_wrapper(obj)
# with open(uri, "w") as f:
# f.write(json)

def _get_lock_file(self) -> str:
os.makedirs(os.path.expanduser("~/.aerovaldb/.lock/"), exist_ok=True)
Expand Down
15 changes: 10 additions & 5 deletions tests/jsondb/test_jsonfiledb.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import asyncio
import math
import os
import random

import pytest
import simplejson # type: ignore
import aerovaldb
from aerovaldb.jsondb.jsonfiledb import AerovalJsonFileDB


@pytest.mark.asyncio
Expand Down Expand Up @@ -102,3 +98,12 @@ def test_getter_with_default_error():
db.get_by_uri(
"/v0/report/project/experiment/invalid-json", default={"data": "data"}
)


def test_jsonfiledb__get_uri_for_file(tmp_path):
with aerovaldb.open(f"json_files:{str(tmp_path)}") as db:
db: AerovalJsonFileDB
assert (
db._get_uri_for_file(str(tmp_path / "project/experiments.json"))
== "/v0/experiments/project"
)

0 comments on commit 59a76df

Please sign in to comment.