Skip to content

Commit

Permalink
Minor changes to match resource ids (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkanche authored Jan 8, 2025
1 parent 0c2d3a2 commit 59e19b6
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 38 deletions.
File renamed without changes.
File renamed without changes.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Version 0.6.1

- Generate rid's that match with R's cache.
- remove rname pattern checks.
- Rename GitHub actions for consistency with the rest of the packages.

## Version 0.6.0

- Reverting schema changes that break compatibility with the R/BiocFileCache implementation.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[![Project generated with PyScaffold](https://img.shields.io/badge/-PyScaffold-005CA0?logo=pyscaffold)](https://pyscaffold.org/)
[![PyPI-Server](https://img.shields.io/pypi/v/pyBiocFileCache.svg)](https://pypi.org/project/pyBiocFileCache/)
![Unit tests](https://github.com/BiocPy/pyBiocFileCache/actions/workflows/pypi-test.yml/badge.svg)
![Unit tests](https://github.com/BiocPy/pyBiocFileCache/actions/workflows/run-tests.yml/badge.svg)

# pyBiocFileCache

Expand Down
74 changes: 39 additions & 35 deletions src/pybiocfilecache/cache.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import json
import logging
from contextlib import contextmanager
from datetime import datetime
Expand Down Expand Up @@ -108,6 +107,7 @@ def _setup_database(self) -> None:
"""),
{"version": SCHEMA_VERSION},
)
conn.commit()

return SCHEMA_VERSION

Expand Down Expand Up @@ -263,7 +263,7 @@ def add(
Returns:
The `Resource` object added to the cache.
"""
self._validate_rname(rname)
# self._validate_rname(rname)
fpath = Path(fpath)

if not fpath.exists():
Expand All @@ -273,7 +273,7 @@ def add(
raise RnameExistsError(f"Resource '{rname}' already exists")

# Generate paths and check size
rid = generate_id()
rid = generate_id(size=len(self))
rpath = self.config.cache_dir / f"{rid}{fpath.suffix if ext else ''}" if action != "asis" else fpath

# Create resource record
Expand Down Expand Up @@ -459,38 +459,38 @@ def validate_resource(self, resource: Resource) -> bool:
logger.error(f"Failed to validate resource: {resource.rname}", exc_info=e)
return False

def export_metadata(self, path: Path) -> None:
"""Export cache metadata to JSON file."""
data = {
"resources": [
{
"rname": r.rname,
"rtype": r.rtype,
"expires": r.expires.isoformat() if r.expires else None,
"etag": r.etag,
}
for r in self.list_resources()
],
"export_time": datetime.now().isoformat(),
}

with open(path, "w") as f:
json.dump(data, f, indent=2)

def import_metadata(self, path: Path) -> None:
"""Import cache metadata from JSON file."""
with open(path) as f:
data = json.load(f)

with self.get_session() as session:
for resource_data in data["resources"]:
resource = self._get(session, resource_data["rname"])
if resource:
resource.expires = (
datetime.fromisoformat(resource_data["expires"]) if resource_data["expires"] else None
)
session.merge(resource)
session.commit()
# def export_metadata(self, path: Path) -> None:
# """Export cache metadata to JSON file."""
# data = {
# "resources": [
# {
# "rname": r.rname,
# "rtype": r.rtype,
# "expires": r.expires.isoformat() if r.expires else None,
# "etag": r.etag,
# }
# for r in self.list_resources()
# ],
# "export_time": datetime.now().isoformat(),
# }

# with open(path, "w") as f:
# json.dump(data, f, indent=2)

# def import_metadata(self, path: Path) -> None:
# """Import cache metadata from JSON file."""
# with open(path) as f:
# data = json.load(f)

# with self.get_session() as session:
# for resource_data in data["resources"]:
# resource = self._get(session, resource_data["rname"])
# if resource:
# resource.expires = (
# datetime.fromisoformat(resource_data["expires"]) if resource_data["expires"] else None
# )
# session.merge(resource)
# session.commit()

def verify_cache(self) -> Tuple[int, int]:
"""Verify integrity of all cached resources.
Expand Down Expand Up @@ -612,3 +612,7 @@ def purge(self, force: bool = False) -> bool:
logger.warning(f"Failed to remove {file}: {file_e}")

return False

def __len__(self):
with self.get_session() as session:
return session.query(Resource).count()
2 changes: 1 addition & 1 deletion src/pybiocfilecache/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,4 @@ class Resource(Base):
expires = Column(DateTime, default=None)

def __repr__(self) -> str:
return f"<Resource(id='{self.id}', rname='{self.rname}')>"
return f"<Resource(rid='{self.rid}', rname='{self.rname}')>"
8 changes: 7 additions & 1 deletion src/pybiocfilecache/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,17 @@ def create_tmp_dir() -> Path:
return Path(tempfile.mkdtemp())


def generate_id() -> str:
def generate_uuid() -> str:
"""Generate unique identifier."""
return uuid.uuid4().hex


def generate_id(size) -> str:
"""Generate unique identifier."""
size += 1
return "BFC" + str(size)


def validate_rname(rname: str, pattern: str) -> bool:
"""Validate resource name format."""
return bool(re.match(pattern, rname))
Expand Down

0 comments on commit 59e19b6

Please sign in to comment.