Skip to content

Commit 59e19b6

Browse files
authored
Minor changes to match resource ids (#28)
1 parent 0c2d3a2 commit 59e19b6

7 files changed

Lines changed: 54 additions & 38 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## Version 0.6.1
4+
5+
- Generate rid's that match with R's cache.
6+
- remove rname pattern checks.
7+
- Rename GitHub actions for consistency with the rest of the packages.
8+
39
## Version 0.6.0
410

511
- Reverting schema changes that break compatibility with the R/BiocFileCache implementation.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[![Project generated with PyScaffold](https://img.shields.io/badge/-PyScaffold-005CA0?logo=pyscaffold)](https://pyscaffold.org/)
22
[![PyPI-Server](https://img.shields.io/pypi/v/pyBiocFileCache.svg)](https://pypi.org/project/pyBiocFileCache/)
3-
![Unit tests](https://github.com/BiocPy/pyBiocFileCache/actions/workflows/pypi-test.yml/badge.svg)
3+
![Unit tests](https://github.com/BiocPy/pyBiocFileCache/actions/workflows/run-tests.yml/badge.svg)
44

55
# pyBiocFileCache
66

src/pybiocfilecache/cache.py

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import json
21
import logging
32
from contextlib import contextmanager
43
from datetime import datetime
@@ -108,6 +107,7 @@ def _setup_database(self) -> None:
108107
"""),
109108
{"version": SCHEMA_VERSION},
110109
)
110+
conn.commit()
111111

112112
return SCHEMA_VERSION
113113

@@ -263,7 +263,7 @@ def add(
263263
Returns:
264264
The `Resource` object added to the cache.
265265
"""
266-
self._validate_rname(rname)
266+
# self._validate_rname(rname)
267267
fpath = Path(fpath)
268268

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

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

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

462-
def export_metadata(self, path: Path) -> None:
463-
"""Export cache metadata to JSON file."""
464-
data = {
465-
"resources": [
466-
{
467-
"rname": r.rname,
468-
"rtype": r.rtype,
469-
"expires": r.expires.isoformat() if r.expires else None,
470-
"etag": r.etag,
471-
}
472-
for r in self.list_resources()
473-
],
474-
"export_time": datetime.now().isoformat(),
475-
}
476-
477-
with open(path, "w") as f:
478-
json.dump(data, f, indent=2)
479-
480-
def import_metadata(self, path: Path) -> None:
481-
"""Import cache metadata from JSON file."""
482-
with open(path) as f:
483-
data = json.load(f)
484-
485-
with self.get_session() as session:
486-
for resource_data in data["resources"]:
487-
resource = self._get(session, resource_data["rname"])
488-
if resource:
489-
resource.expires = (
490-
datetime.fromisoformat(resource_data["expires"]) if resource_data["expires"] else None
491-
)
492-
session.merge(resource)
493-
session.commit()
462+
# def export_metadata(self, path: Path) -> None:
463+
# """Export cache metadata to JSON file."""
464+
# data = {
465+
# "resources": [
466+
# {
467+
# "rname": r.rname,
468+
# "rtype": r.rtype,
469+
# "expires": r.expires.isoformat() if r.expires else None,
470+
# "etag": r.etag,
471+
# }
472+
# for r in self.list_resources()
473+
# ],
474+
# "export_time": datetime.now().isoformat(),
475+
# }
476+
477+
# with open(path, "w") as f:
478+
# json.dump(data, f, indent=2)
479+
480+
# def import_metadata(self, path: Path) -> None:
481+
# """Import cache metadata from JSON file."""
482+
# with open(path) as f:
483+
# data = json.load(f)
484+
485+
# with self.get_session() as session:
486+
# for resource_data in data["resources"]:
487+
# resource = self._get(session, resource_data["rname"])
488+
# if resource:
489+
# resource.expires = (
490+
# datetime.fromisoformat(resource_data["expires"]) if resource_data["expires"] else None
491+
# )
492+
# session.merge(resource)
493+
# session.commit()
494494

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

614614
return False
615+
616+
def __len__(self):
617+
with self.get_session() as session:
618+
return session.query(Resource).count()

src/pybiocfilecache/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,4 @@ class Resource(Base):
7373
expires = Column(DateTime, default=None)
7474

7575
def __repr__(self) -> str:
76-
return f"<Resource(id='{self.id}', rname='{self.rname}')>"
76+
return f"<Resource(rid='{self.rid}', rname='{self.rname}')>"

src/pybiocfilecache/utils.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,17 @@ def create_tmp_dir() -> Path:
2222
return Path(tempfile.mkdtemp())
2323

2424

25-
def generate_id() -> str:
25+
def generate_uuid() -> str:
2626
"""Generate unique identifier."""
2727
return uuid.uuid4().hex
2828

2929

30+
def generate_id(size) -> str:
31+
"""Generate unique identifier."""
32+
size += 1
33+
return "BFC" + str(size)
34+
35+
3036
def validate_rname(rname: str, pattern: str) -> bool:
3137
"""Validate resource name format."""
3238
return bool(re.match(pattern, rname))

0 commit comments

Comments
 (0)