From ab5eceb4f02c2a19d247923f23070a9d02a265f9 Mon Sep 17 00:00:00 2001 From: Jayaram Kancherla Date: Tue, 3 Dec 2024 11:21:41 -0800 Subject: [PATCH 1/3] List all resources --- src/pybiocfilecache/BiocFileCache.py | 9 +++++++++ tests/test_cache.py | 3 +++ 2 files changed, 12 insertions(+) diff --git a/src/pybiocfilecache/BiocFileCache.py b/src/pybiocfilecache/BiocFileCache.py index 8ce375c..128ae44 100644 --- a/src/pybiocfilecache/BiocFileCache.py +++ b/src/pybiocfilecache/BiocFileCache.py @@ -274,3 +274,12 @@ def update( # but lets just add it to the cache. res = self.add(rname=rname, fpath=fpath, action=action) return res + + def list_all(self) -> List[Resource]: + """List all resources currently in the cache. + + Returns: + List of all Resource objects in the cache. + """ + with self.sessionLocal() as session: + return session.query(Resource).all() \ No newline at end of file diff --git a/tests/test_cache.py b/tests/test_cache.py index ed732af..941a8ed 100644 --- a/tests/test_cache.py +++ b/tests/test_cache.py @@ -41,6 +41,9 @@ def test_add_get_operations(): frec3 = open(rec3.rpath, "r").read().strip() assert frec3 == "test2" + rtrip = bfc.list_all() + assert len(rtrip) == 3 + bfc.purge() From f2b699139199729bce75759745260448e334171a Mon Sep 17 00:00:00 2001 From: Jayaram Kancherla Date: Tue, 3 Dec 2024 11:22:47 -0800 Subject: [PATCH 2/3] update precommit config --- .pre-commit-config.yaml | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 3c9601c..f153fa6 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -17,19 +17,19 @@ repos: - id: mixed-line-ending args: ['--fix=auto'] # replace 'auto' with 'lf' to enforce Linux/Mac line endings or 'crlf' for Windows -- repo: https://github.com/PyCQA/docformatter - rev: v1.7.5 - hooks: - - id: docformatter - additional_dependencies: [tomli] - args: [--in-place, --wrap-descriptions=120, --wrap-summaries=120] - # --config, ./pyproject.toml +# - repo: https://github.com/PyCQA/docformatter +# rev: master +# hooks: +# - id: docformatter +# additional_dependencies: [tomli] +# args: [--in-place, --wrap-descriptions=120, --wrap-summaries=120] +# # --config, ./pyproject.toml -- repo: https://github.com/psf/black - rev: 24.8.0 - hooks: - - id: black - language_version: python3 +# - repo: https://github.com/psf/black +# rev: 24.8.0 +# hooks: +# - id: black +# language_version: python3 - repo: https://github.com/astral-sh/ruff-pre-commit # Ruff version. @@ -37,6 +37,7 @@ repos: hooks: - id: ruff args: [--fix, --exit-non-zero-on-fix] + - id: ruff-format ## If like to embrace black styles even in the docs: # - repo: https://github.com/asottile/blacken-docs @@ -49,4 +50,4 @@ repos: # - repo: https://github.com/codespell-project/codespell # rev: v2.2.5 # hooks: -# - id: codespell +# - id: codespell \ No newline at end of file From 77777c2bd1242e135893cb88952c3303ff1a412e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 3 Dec 2024 19:22:56 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .pre-commit-config.yaml | 2 +- src/pybiocfilecache/BiocFileCache.py | 21 +++++---------------- src/pybiocfilecache/db/db_config.py | 4 +--- src/pybiocfilecache/utils.py | 8 ++------ 4 files changed, 9 insertions(+), 26 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index f153fa6..e60a5f4 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -50,4 +50,4 @@ repos: # - repo: https://github.com/codespell-project/codespell # rev: v2.2.5 # hooks: -# - id: codespell \ No newline at end of file +# - id: codespell diff --git a/src/pybiocfilecache/BiocFileCache.py b/src/pybiocfilecache/BiocFileCache.py index 128ae44..e8dfaa2 100644 --- a/src/pybiocfilecache/BiocFileCache.py +++ b/src/pybiocfilecache/BiocFileCache.py @@ -95,11 +95,7 @@ def add( raise NoFpathError(f"Resource at '{fpath}' does not exist.") rid = generate_id() - rpath = ( - f"{self.cache}/{rid}" + (f".{fpath.suffix}" if ext else "") - if action != "asis" - else str(fpath) - ) + rpath = f"{self.cache}/{rid}" + (f".{fpath.suffix}" if ext else "") if action != "asis" else str(fpath) # create new record in the database res = Resource( @@ -151,11 +147,7 @@ def query(self, query: str, field: str = "rname") -> List[Resource]: List of matching resources from cache. """ with self.sessionLocal() as session: - return ( - session.query(Resource) - .filter(Resource[field].ilike("%{}%".format(query))) - .all() - ) + return session.query(Resource).filter(Resource[field].ilike("%{}%".format(query))).all() def _get(self, session: Session, rname: str) -> Optional[Resource]: """Get a resource with `rname` from given `Session`. @@ -170,9 +162,7 @@ def _get(self, session: Session, rname: str) -> Optional[Resource]: Returns: The `Resource` for the `rname` if available. """ - resource: Optional[Resource] = ( - session.query(Resource).filter(Resource.rname == rname).first() - ) + resource: Optional[Resource] = session.query(Resource).filter(Resource.rname == rname).first() if resource is not None: # `Resource` may exist but `rpath` could still be being @@ -182,8 +172,7 @@ def _get(self, session: Session, rname: str) -> Optional[Resource]: while not Path(str(resource.rpath)).exists(): if time() - start >= timeout: raise RpathTimeoutError( - f"For resource: '{rname}' the rpath does not exist " - f"after {timeout} seconds." + f"For resource: '{rname}' the rpath does not exist " f"after {timeout} seconds." ) sleep(0.1) @@ -282,4 +271,4 @@ def list_all(self) -> List[Resource]: List of all Resource objects in the cache. """ with self.sessionLocal() as session: - return session.query(Resource).all() \ No newline at end of file + return session.query(Resource).all() diff --git a/src/pybiocfilecache/db/db_config.py b/src/pybiocfilecache/db/db_config.py index e99c96d..638433a 100644 --- a/src/pybiocfilecache/db/db_config.py +++ b/src/pybiocfilecache/db/db_config.py @@ -72,9 +72,7 @@ def create_schema(cache_dir: str) -> Tuple[Engine, sessionmaker]: Returns: A tuple of sqlalchemy engine and session maker. """ - engine = create_engine( - f"sqlite:///{cache_dir}", connect_args={"check_same_thread": False} - ) + engine = create_engine(f"sqlite:///{cache_dir}", connect_args={"check_same_thread": False}) Base.metadata.create_all(bind=engine, checkfirst=True) sessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) diff --git a/src/pybiocfilecache/utils.py b/src/pybiocfilecache/utils.py index 7e5d75f..442d762 100644 --- a/src/pybiocfilecache/utils.py +++ b/src/pybiocfilecache/utils.py @@ -60,9 +60,7 @@ def copy_or_move( """ if action not in ["copy", "move", "asis"]: - raise ValueError( - f"Action must be either 'move', 'copy' or 'asis', provided {action}." - ) + raise ValueError(f"Action must be either 'move', 'copy' or 'asis', provided {action}.") try: if action == "copy": @@ -84,6 +82,4 @@ def setup_logging(loglevel): loglevel (int): minimum loglevel for emitting messages """ logformat = "[%(asctime)s] %(levelname)s:%(name)s:%(message)s" - logging.basicConfig( - level=loglevel, stream=sys.stdout, format=logformat, datefmt="%Y-%m-%d %H:%M:%S" - ) + logging.basicConfig(level=loglevel, stream=sys.stdout, format=logformat, datefmt="%Y-%m-%d %H:%M:%S")