Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,27 @@ 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.
rev: v0.6.8
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
Expand Down
28 changes: 13 additions & 15 deletions src/pybiocfilecache/BiocFileCache.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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`.
Expand All @@ -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
Expand All @@ -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)

Expand Down Expand Up @@ -274,3 +263,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()
4 changes: 1 addition & 3 deletions src/pybiocfilecache/db/db_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 2 additions & 6 deletions src/pybiocfilecache/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand All @@ -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")
3 changes: 3 additions & 0 deletions tests/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down
Loading