Skip to content

Commit 24b7702

Browse files
authored
List all cached resources (#19)
1 parent 85e2b2b commit 24b7702

5 files changed

Lines changed: 32 additions & 36 deletions

File tree

.pre-commit-config.yaml

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,27 @@ repos:
1717
- id: mixed-line-ending
1818
args: ['--fix=auto'] # replace 'auto' with 'lf' to enforce Linux/Mac line endings or 'crlf' for Windows
1919

20-
- repo: https://github.com/PyCQA/docformatter
21-
rev: v1.7.5
22-
hooks:
23-
- id: docformatter
24-
additional_dependencies: [tomli]
25-
args: [--in-place, --wrap-descriptions=120, --wrap-summaries=120]
26-
# --config, ./pyproject.toml
20+
# - repo: https://github.com/PyCQA/docformatter
21+
# rev: master
22+
# hooks:
23+
# - id: docformatter
24+
# additional_dependencies: [tomli]
25+
# args: [--in-place, --wrap-descriptions=120, --wrap-summaries=120]
26+
# # --config, ./pyproject.toml
2727

28-
- repo: https://github.com/psf/black
29-
rev: 24.8.0
30-
hooks:
31-
- id: black
32-
language_version: python3
28+
# - repo: https://github.com/psf/black
29+
# rev: 24.8.0
30+
# hooks:
31+
# - id: black
32+
# language_version: python3
3333

3434
- repo: https://github.com/astral-sh/ruff-pre-commit
3535
# Ruff version.
3636
rev: v0.6.8
3737
hooks:
3838
- id: ruff
3939
args: [--fix, --exit-non-zero-on-fix]
40+
- id: ruff-format
4041

4142
## If like to embrace black styles even in the docs:
4243
# - repo: https://github.com/asottile/blacken-docs

src/pybiocfilecache/BiocFileCache.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,7 @@ def add(
9595
raise NoFpathError(f"Resource at '{fpath}' does not exist.")
9696

9797
rid = generate_id()
98-
rpath = (
99-
f"{self.cache}/{rid}" + (f".{fpath.suffix}" if ext else "")
100-
if action != "asis"
101-
else str(fpath)
102-
)
98+
rpath = f"{self.cache}/{rid}" + (f".{fpath.suffix}" if ext else "") if action != "asis" else str(fpath)
10399

104100
# create new record in the database
105101
res = Resource(
@@ -151,11 +147,7 @@ def query(self, query: str, field: str = "rname") -> List[Resource]:
151147
List of matching resources from cache.
152148
"""
153149
with self.sessionLocal() as session:
154-
return (
155-
session.query(Resource)
156-
.filter(Resource[field].ilike("%{}%".format(query)))
157-
.all()
158-
)
150+
return session.query(Resource).filter(Resource[field].ilike("%{}%".format(query))).all()
159151

160152
def _get(self, session: Session, rname: str) -> Optional[Resource]:
161153
"""Get a resource with `rname` from given `Session`.
@@ -170,9 +162,7 @@ def _get(self, session: Session, rname: str) -> Optional[Resource]:
170162
Returns:
171163
The `Resource` for the `rname` if available.
172164
"""
173-
resource: Optional[Resource] = (
174-
session.query(Resource).filter(Resource.rname == rname).first()
175-
)
165+
resource: Optional[Resource] = session.query(Resource).filter(Resource.rname == rname).first()
176166

177167
if resource is not None:
178168
# `Resource` may exist but `rpath` could still be being
@@ -182,8 +172,7 @@ def _get(self, session: Session, rname: str) -> Optional[Resource]:
182172
while not Path(str(resource.rpath)).exists():
183173
if time() - start >= timeout:
184174
raise RpathTimeoutError(
185-
f"For resource: '{rname}' the rpath does not exist "
186-
f"after {timeout} seconds."
175+
f"For resource: '{rname}' the rpath does not exist " f"after {timeout} seconds."
187176
)
188177
sleep(0.1)
189178

@@ -274,3 +263,12 @@ def update(
274263
# but lets just add it to the cache.
275264
res = self.add(rname=rname, fpath=fpath, action=action)
276265
return res
266+
267+
def list_all(self) -> List[Resource]:
268+
"""List all resources currently in the cache.
269+
270+
Returns:
271+
List of all Resource objects in the cache.
272+
"""
273+
with self.sessionLocal() as session:
274+
return session.query(Resource).all()

src/pybiocfilecache/db/db_config.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,7 @@ def create_schema(cache_dir: str) -> Tuple[Engine, sessionmaker]:
7272
Returns:
7373
A tuple of sqlalchemy engine and session maker.
7474
"""
75-
engine = create_engine(
76-
f"sqlite:///{cache_dir}", connect_args={"check_same_thread": False}
77-
)
75+
engine = create_engine(f"sqlite:///{cache_dir}", connect_args={"check_same_thread": False})
7876

7977
Base.metadata.create_all(bind=engine, checkfirst=True)
8078
sessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

src/pybiocfilecache/utils.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,7 @@ def copy_or_move(
6060
"""
6161

6262
if action not in ["copy", "move", "asis"]:
63-
raise ValueError(
64-
f"Action must be either 'move', 'copy' or 'asis', provided {action}."
65-
)
63+
raise ValueError(f"Action must be either 'move', 'copy' or 'asis', provided {action}.")
6664

6765
try:
6866
if action == "copy":
@@ -84,6 +82,4 @@ def setup_logging(loglevel):
8482
loglevel (int): minimum loglevel for emitting messages
8583
"""
8684
logformat = "[%(asctime)s] %(levelname)s:%(name)s:%(message)s"
87-
logging.basicConfig(
88-
level=loglevel, stream=sys.stdout, format=logformat, datefmt="%Y-%m-%d %H:%M:%S"
89-
)
85+
logging.basicConfig(level=loglevel, stream=sys.stdout, format=logformat, datefmt="%Y-%m-%d %H:%M:%S")

tests/test_cache.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ def test_add_get_operations():
4141
frec3 = open(rec3.rpath, "r").read().strip()
4242
assert frec3 == "test2"
4343

44+
rtrip = bfc.list_all()
45+
assert len(rtrip) == 3
46+
4447
bfc.purge()
4548

4649

0 commit comments

Comments
 (0)