diff --git a/.github/workflows/pypi-publish.yml b/.github/workflows/publish-pypi.yml similarity index 100% rename from .github/workflows/pypi-publish.yml rename to .github/workflows/publish-pypi.yml diff --git a/.github/workflows/pypi-test.yml b/.github/workflows/run-tests.yml similarity index 100% rename from .github/workflows/pypi-test.yml rename to .github/workflows/run-tests.yml diff --git a/CHANGELOG.md b/CHANGELOG.md index e01ca64..41dc724 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/README.md b/README.md index bfe4887..81cb9ec 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/pybiocfilecache/cache.py b/src/pybiocfilecache/cache.py index 0f7a806..4258e4c 100644 --- a/src/pybiocfilecache/cache.py +++ b/src/pybiocfilecache/cache.py @@ -1,4 +1,3 @@ -import json import logging from contextlib import contextmanager from datetime import datetime @@ -108,6 +107,7 @@ def _setup_database(self) -> None: """), {"version": SCHEMA_VERSION}, ) + conn.commit() return SCHEMA_VERSION @@ -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(): @@ -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 @@ -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. @@ -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() diff --git a/src/pybiocfilecache/models.py b/src/pybiocfilecache/models.py index 8040b99..31e4176 100644 --- a/src/pybiocfilecache/models.py +++ b/src/pybiocfilecache/models.py @@ -73,4 +73,4 @@ class Resource(Base): expires = Column(DateTime, default=None) def __repr__(self) -> str: - return f"" + return f"" diff --git a/src/pybiocfilecache/utils.py b/src/pybiocfilecache/utils.py index 9dda562..97bac34 100644 --- a/src/pybiocfilecache/utils.py +++ b/src/pybiocfilecache/utils.py @@ -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))