pyBiocFileCache 0.8.0.dev0__py3-none-any.whl → 0.8.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pybiocfilecache/cache.py +93 -45
- pybiocfilecache/config.py +1 -2
- pybiocfilecache/utils.py +7 -7
- pybiocfilecache-0.8.1.dist-info/METADATA +154 -0
- pybiocfilecache-0.8.1.dist-info/RECORD +12 -0
- pybiocfilecache-0.8.1.dist-info/licenses/AUTHORS.md +5 -0
- pybiocfilecache-0.8.0.dev0.dist-info/METADATA +0 -21
- pybiocfilecache-0.8.0.dev0.dist-info/RECORD +0 -12
- pybiocfilecache-0.8.0.dev0.dist-info/licenses/AUTHORS.md +0 -4
- {pybiocfilecache-0.8.0.dev0.dist-info → pybiocfilecache-0.8.1.dist-info}/WHEEL +0 -0
- {pybiocfilecache-0.8.0.dev0.dist-info → pybiocfilecache-0.8.1.dist-info}/licenses/LICENSE.txt +0 -0
pybiocfilecache/cache.py
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
1
3
|
import logging
|
|
2
4
|
import shutil
|
|
5
|
+
from collections.abc import Iterator
|
|
3
6
|
from contextlib import contextmanager
|
|
4
7
|
from datetime import datetime, timezone
|
|
5
8
|
from pathlib import Path
|
|
6
9
|
from time import sleep, time
|
|
7
|
-
from typing import Any,
|
|
10
|
+
from typing import Any, Literal
|
|
8
11
|
|
|
9
12
|
from biocframe import BiocFrame
|
|
10
13
|
from sqlalchemy import create_engine, func, text
|
|
@@ -36,11 +39,11 @@ def _now() -> datetime:
|
|
|
36
39
|
return datetime.now(timezone.utc).replace(tzinfo=None)
|
|
37
40
|
|
|
38
41
|
|
|
39
|
-
def _normalize_datetime(dt:
|
|
42
|
+
def _normalize_datetime(dt: datetime | None) -> datetime | None:
|
|
40
43
|
"""Normalize an optional datetime to naive UTC."""
|
|
41
44
|
if dt is not None and dt.tzinfo is not None:
|
|
42
45
|
return dt.astimezone(timezone.utc).replace(tzinfo=None)
|
|
43
|
-
|
|
46
|
+
|
|
44
47
|
return dt
|
|
45
48
|
|
|
46
49
|
|
|
@@ -53,7 +56,7 @@ class BiocFileCache:
|
|
|
53
56
|
- Cleanup of expired resources
|
|
54
57
|
"""
|
|
55
58
|
|
|
56
|
-
def __init__(self, cache_dir:
|
|
59
|
+
def __init__(self, cache_dir: str | Path | None = None, config: CacheConfig | None = None):
|
|
57
60
|
"""Initialize cache with optional configuration.
|
|
58
61
|
|
|
59
62
|
Args:
|
|
@@ -98,6 +101,7 @@ class BiocFileCache:
|
|
|
98
101
|
self.SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=self.engine)
|
|
99
102
|
|
|
100
103
|
with self.engine.connect() as conn:
|
|
104
|
+
self._ensure_autoincrement(conn)
|
|
101
105
|
result = conn.execute(
|
|
102
106
|
text("""
|
|
103
107
|
SELECT value FROM metadata
|
|
@@ -120,15 +124,52 @@ class BiocFileCache:
|
|
|
120
124
|
|
|
121
125
|
return SCHEMA_VERSION
|
|
122
126
|
|
|
123
|
-
def
|
|
127
|
+
def _ensure_autoincrement(self, conn) -> None:
|
|
128
|
+
"""Migrate legacy resource table without AUTOINCREMENT so SQLite never reuses deleted row IDs."""
|
|
129
|
+
row = conn.execute(text("SELECT sql FROM sqlite_master WHERE type='table' AND name='resource'")).fetchone()
|
|
130
|
+
if row is None or not row[0] or "AUTOINCREMENT" in row[0].upper():
|
|
131
|
+
return
|
|
132
|
+
|
|
133
|
+
dup_count = conn.execute(
|
|
134
|
+
text("SELECT COUNT(*) - COUNT(DISTINCT rid) FROM resource WHERE rid IS NOT NULL")
|
|
135
|
+
).scalar()
|
|
136
|
+
unique_clause = ", UNIQUE (rid)" if not dup_count else ""
|
|
137
|
+
|
|
138
|
+
cols = "id, rid, rname, create_time, access_time, rpath, rtype, fpath, last_modified_time, etag, expires"
|
|
139
|
+
conn.execute(
|
|
140
|
+
text(
|
|
141
|
+
f"""
|
|
142
|
+
CREATE TABLE resource_new (
|
|
143
|
+
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
|
144
|
+
rid TEXT,
|
|
145
|
+
rname TEXT,
|
|
146
|
+
create_time DATETIME DEFAULT (CURRENT_TIMESTAMP),
|
|
147
|
+
access_time DATETIME DEFAULT (CURRENT_TIMESTAMP),
|
|
148
|
+
rpath TEXT,
|
|
149
|
+
rtype TEXT,
|
|
150
|
+
fpath TEXT,
|
|
151
|
+
last_modified_time DATETIME,
|
|
152
|
+
etag TEXT,
|
|
153
|
+
expires DATETIME{unique_clause}
|
|
154
|
+
)
|
|
155
|
+
"""
|
|
156
|
+
)
|
|
157
|
+
)
|
|
158
|
+
conn.execute(text(f"INSERT INTO resource_new ({cols}) SELECT {cols} FROM resource"))
|
|
159
|
+
conn.execute(text("DROP TABLE resource"))
|
|
160
|
+
conn.execute(text("ALTER TABLE resource_new RENAME TO resource"))
|
|
161
|
+
conn.execute(text("CREATE INDEX IF NOT EXISTS ix_resource_id ON resource (id)"))
|
|
162
|
+
conn.commit()
|
|
163
|
+
|
|
164
|
+
def _resolve_rpath(self, rpath: str, rtype: str | None) -> Path:
|
|
124
165
|
"""Resolve a stored resource path against cache_dir when relative."""
|
|
125
166
|
path = Path(rpath)
|
|
126
167
|
if rtype in ("relative", "web") and not path.is_absolute():
|
|
127
168
|
return self.config.cache_dir / path
|
|
128
|
-
|
|
169
|
+
|
|
129
170
|
return path
|
|
130
171
|
|
|
131
|
-
def _get_detached_resource(self, session: Session, obj:
|
|
172
|
+
def _get_detached_resource(self, session: Session, obj: Resource | Metadata) -> dict | None:
|
|
132
173
|
"""Get a detached copy of a resource."""
|
|
133
174
|
if obj is None:
|
|
134
175
|
return None
|
|
@@ -141,7 +182,7 @@ class BiocFileCache:
|
|
|
141
182
|
|
|
142
183
|
return obj_dict
|
|
143
184
|
|
|
144
|
-
def __enter__(self) ->
|
|
185
|
+
def __enter__(self) -> BiocFileCache:
|
|
145
186
|
return self
|
|
146
187
|
|
|
147
188
|
def __exit__(self, exc_type, exc_val, exc_tb) -> None:
|
|
@@ -226,11 +267,12 @@ class BiocFileCache:
|
|
|
226
267
|
|
|
227
268
|
def get(
|
|
228
269
|
self,
|
|
229
|
-
rname:
|
|
230
|
-
rid:
|
|
270
|
+
rname: str | None = None,
|
|
271
|
+
rid: str | None = None,
|
|
231
272
|
wait: bool = True,
|
|
232
273
|
timeout: float = 30,
|
|
233
|
-
|
|
274
|
+
update_access_time: bool = True,
|
|
275
|
+
) -> dict | None:
|
|
234
276
|
"""Get resource by name or id from cache.
|
|
235
277
|
|
|
236
278
|
Args:
|
|
@@ -248,6 +290,11 @@ class BiocFileCache:
|
|
|
248
290
|
timeout:
|
|
249
291
|
Maximum seconds to wait when `wait=True`. Defaults to 30.
|
|
250
292
|
|
|
293
|
+
update_access_time:
|
|
294
|
+
Whether to update `access_time` in the database when the
|
|
295
|
+
cached file exists. Set to False for read-only lookups.
|
|
296
|
+
Defaults to True.
|
|
297
|
+
|
|
251
298
|
"""
|
|
252
299
|
if rname is None and rid is None:
|
|
253
300
|
raise ValueError("either 'rname' or 'rid' must be provided.")
|
|
@@ -259,8 +306,8 @@ class BiocFileCache:
|
|
|
259
306
|
resource = session.query(Resource).filter(Resource.rname == rname).first()
|
|
260
307
|
|
|
261
308
|
if resource is not None:
|
|
309
|
+
resolved_path = self._resolve_rpath(resource.rpath, resource.rtype)
|
|
262
310
|
if wait:
|
|
263
|
-
resolved_path = self._resolve_rpath(resource.rpath, resource.rtype)
|
|
264
311
|
start = time()
|
|
265
312
|
while not resolved_path.exists():
|
|
266
313
|
if time() - start >= timeout:
|
|
@@ -270,9 +317,10 @@ class BiocFileCache:
|
|
|
270
317
|
)
|
|
271
318
|
sleep(0.1)
|
|
272
319
|
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
320
|
+
if update_access_time and resolved_path.exists():
|
|
321
|
+
resource.access_time = _now()
|
|
322
|
+
session.commit()
|
|
323
|
+
|
|
276
324
|
return self._get_detached_resource(session, resource)
|
|
277
325
|
|
|
278
326
|
return None
|
|
@@ -281,19 +329,19 @@ class BiocFileCache:
|
|
|
281
329
|
self,
|
|
282
330
|
session: Session,
|
|
283
331
|
rname: str,
|
|
284
|
-
fpath:
|
|
332
|
+
fpath: str | Path,
|
|
285
333
|
rtype: Literal["local", "web", "relative"] = "relative",
|
|
286
334
|
action: Literal["copy", "move", "asis"] = "copy",
|
|
287
|
-
expires:
|
|
335
|
+
expires: datetime | None = None,
|
|
288
336
|
download: bool = True,
|
|
289
337
|
ext: bool = True,
|
|
290
|
-
) ->
|
|
338
|
+
) -> tuple[Resource, Path | None]:
|
|
291
339
|
"""Insert a resource record and store its file within an active session."""
|
|
292
340
|
if session.query(Resource).filter(Resource.rname == rname).first() is not None:
|
|
293
341
|
raise FileExistsError(f"Resource '{rname}' already exists")
|
|
294
342
|
|
|
295
343
|
fpath_val = Path(fpath) if rtype != "web" else fpath
|
|
296
|
-
tmp_dir:
|
|
344
|
+
tmp_dir: Path | None = None
|
|
297
345
|
|
|
298
346
|
if rtype == "web":
|
|
299
347
|
outpath = download_web_file(str(fpath_val), None, download)
|
|
@@ -321,7 +369,7 @@ class BiocFileCache:
|
|
|
321
369
|
expires=_normalize_datetime(expires),
|
|
322
370
|
)
|
|
323
371
|
|
|
324
|
-
copied_target:
|
|
372
|
+
copied_target: Path | None = None
|
|
325
373
|
try:
|
|
326
374
|
session.add(resource)
|
|
327
375
|
session.flush()
|
|
@@ -333,12 +381,12 @@ class BiocFileCache:
|
|
|
333
381
|
copied_target = target_path
|
|
334
382
|
|
|
335
383
|
resource.etag = calculate_file_hash(target_path, self.config.hash_algorithm)
|
|
336
|
-
|
|
384
|
+
|
|
337
385
|
return resource, copied_target
|
|
338
386
|
except Exception:
|
|
339
387
|
if copied_target is not None and copied_target.exists():
|
|
340
388
|
copied_target.unlink(missing_ok=True)
|
|
341
|
-
|
|
389
|
+
|
|
342
390
|
raise
|
|
343
391
|
finally:
|
|
344
392
|
if tmp_dir is not None:
|
|
@@ -347,10 +395,10 @@ class BiocFileCache:
|
|
|
347
395
|
def add(
|
|
348
396
|
self,
|
|
349
397
|
rname: str,
|
|
350
|
-
fpath:
|
|
398
|
+
fpath: str | Path,
|
|
351
399
|
rtype: Literal["local", "web", "relative"] = "relative",
|
|
352
400
|
action: Literal["copy", "move", "asis"] = "copy",
|
|
353
|
-
expires:
|
|
401
|
+
expires: datetime | None = None,
|
|
354
402
|
download: bool = True,
|
|
355
403
|
ext: bool = True,
|
|
356
404
|
) -> dict:
|
|
@@ -400,7 +448,7 @@ class BiocFileCache:
|
|
|
400
448
|
ext=ext,
|
|
401
449
|
)
|
|
402
450
|
session.commit()
|
|
403
|
-
|
|
451
|
+
|
|
404
452
|
return self._get_detached_resource(session, resource)
|
|
405
453
|
except FileExistsError:
|
|
406
454
|
session.rollback()
|
|
@@ -409,15 +457,15 @@ class BiocFileCache:
|
|
|
409
457
|
session.rollback()
|
|
410
458
|
raise Exception("Failed to add resource") from e
|
|
411
459
|
|
|
412
|
-
def add_batch(self, resources:
|
|
460
|
+
def add_batch(self, resources: list[dict[str, Any]]) -> BiocFrame:
|
|
413
461
|
"""Add multiple resources in a single transaction.
|
|
414
462
|
|
|
415
463
|
Args:
|
|
416
464
|
resources:
|
|
417
465
|
List of resources to add.
|
|
418
466
|
"""
|
|
419
|
-
added_resources:
|
|
420
|
-
copied_targets:
|
|
467
|
+
added_resources: list[Resource] = []
|
|
468
|
+
copied_targets: list[Path] = []
|
|
421
469
|
with self.get_session() as session:
|
|
422
470
|
try:
|
|
423
471
|
for resource_info in resources:
|
|
@@ -427,25 +475,25 @@ class BiocFileCache:
|
|
|
427
475
|
copied_targets.append(copied_target)
|
|
428
476
|
|
|
429
477
|
session.commit()
|
|
430
|
-
|
|
478
|
+
|
|
431
479
|
return BiocFrame(
|
|
432
480
|
convert_to_columnar([self._get_detached_resource(session, r) for r in added_resources])
|
|
433
481
|
)
|
|
434
482
|
except Exception as e:
|
|
435
483
|
logger.error("Failed to add batch of resources", exc_info=e)
|
|
436
484
|
session.rollback()
|
|
437
|
-
|
|
485
|
+
|
|
438
486
|
for target in copied_targets:
|
|
439
487
|
target.unlink(missing_ok=True)
|
|
440
|
-
|
|
488
|
+
|
|
441
489
|
raise
|
|
442
490
|
|
|
443
491
|
def update(
|
|
444
492
|
self,
|
|
445
|
-
rname:
|
|
446
|
-
fpath:
|
|
493
|
+
rname: str | None = None,
|
|
494
|
+
fpath: str | Path = None,
|
|
447
495
|
action: Literal["copy", "move", "asis"] = "copy",
|
|
448
|
-
rid:
|
|
496
|
+
rid: str | None = None,
|
|
449
497
|
) -> dict:
|
|
450
498
|
"""Update an existing resource.
|
|
451
499
|
|
|
@@ -469,7 +517,7 @@ class BiocFileCache:
|
|
|
469
517
|
"""
|
|
470
518
|
if rname is None and rid is None:
|
|
471
519
|
raise ValueError("either 'rname' or 'rid' must be provided.")
|
|
472
|
-
|
|
520
|
+
|
|
473
521
|
if fpath is None:
|
|
474
522
|
raise ValueError("'fpath' must be provided.")
|
|
475
523
|
|
|
@@ -497,7 +545,7 @@ class BiocFileCache:
|
|
|
497
545
|
if resource.rtype == "local":
|
|
498
546
|
resource.rtype = "relative"
|
|
499
547
|
resource.rpath = f"{generate_uuid()}_{fpath.name}"
|
|
500
|
-
|
|
548
|
+
|
|
501
549
|
target_path = self._resolve_rpath(resource.rpath, resource.rtype)
|
|
502
550
|
copy_or_move(fpath, target_path, resource.rname, action, False)
|
|
503
551
|
|
|
@@ -506,15 +554,15 @@ class BiocFileCache:
|
|
|
506
554
|
resource.etag = calculate_file_hash(target_path, self.config.hash_algorithm)
|
|
507
555
|
|
|
508
556
|
session.commit()
|
|
509
|
-
|
|
557
|
+
|
|
510
558
|
return self._get_detached_resource(session, resource)
|
|
511
559
|
|
|
512
560
|
except Exception as e:
|
|
513
561
|
session.rollback()
|
|
514
|
-
|
|
562
|
+
|
|
515
563
|
raise Exception("Failed to update resource") from e
|
|
516
564
|
|
|
517
|
-
def remove(self, rname:
|
|
565
|
+
def remove(self, rname: str | None = None, rid: str | None = None) -> None:
|
|
518
566
|
"""Remove a resource from cache by name or id.
|
|
519
567
|
|
|
520
568
|
Removes both the cached file and its database entry.
|
|
@@ -554,7 +602,7 @@ class BiocFileCache:
|
|
|
554
602
|
identifier = rid if rid is not None else rname
|
|
555
603
|
raise Exception(f"Failed to remove resource '{identifier}'") from e
|
|
556
604
|
|
|
557
|
-
def list_resources(self, rtype:
|
|
605
|
+
def list_resources(self, rtype: str | None = None, expired: bool | None = None) -> BiocFrame:
|
|
558
606
|
"""List resources in the cache with optional filtering.
|
|
559
607
|
|
|
560
608
|
Args:
|
|
@@ -593,7 +641,7 @@ class BiocFileCache:
|
|
|
593
641
|
resources = query.all()
|
|
594
642
|
return BiocFrame(convert_to_columnar([self._get_detached_resource(session, r) for r in resources]))
|
|
595
643
|
|
|
596
|
-
def validate_resource(self, resource:
|
|
644
|
+
def validate_resource(self, resource: Resource | dict) -> bool:
|
|
597
645
|
"""Validate resource integrity.
|
|
598
646
|
|
|
599
647
|
Args:
|
|
@@ -620,7 +668,7 @@ class BiocFileCache:
|
|
|
620
668
|
try:
|
|
621
669
|
resolved_path = self._resolve_rpath(rpath, rtype)
|
|
622
670
|
current_hash = calculate_file_hash(resolved_path, self.config.hash_algorithm)
|
|
623
|
-
|
|
671
|
+
|
|
624
672
|
return current_hash == etag
|
|
625
673
|
except Exception as e:
|
|
626
674
|
logger.error(f"Failed to validate resource: {rname}", exc_info=e)
|
|
@@ -659,7 +707,7 @@ class BiocFileCache:
|
|
|
659
707
|
# session.merge(resource)
|
|
660
708
|
# session.commit()
|
|
661
709
|
|
|
662
|
-
def verify_cache(self) ->
|
|
710
|
+
def verify_cache(self) -> tuple[int, int]:
|
|
663
711
|
"""Verify integrity of all cached resources.
|
|
664
712
|
|
|
665
713
|
Returns:
|
|
@@ -672,7 +720,7 @@ class BiocFileCache:
|
|
|
672
720
|
valid += 1
|
|
673
721
|
else:
|
|
674
722
|
invalid += 1
|
|
675
|
-
|
|
723
|
+
|
|
676
724
|
return valid, invalid
|
|
677
725
|
|
|
678
726
|
def search(self, query: str, field: str = "rname", exact: bool = False) -> BiocFrame:
|
|
@@ -703,7 +751,7 @@ class BiocFileCache:
|
|
|
703
751
|
|
|
704
752
|
return BiocFrame(convert_to_columnar([self._get_detached_resource(session, r) for r in resources]))
|
|
705
753
|
|
|
706
|
-
def get_stats(self) ->
|
|
754
|
+
def get_stats(self) -> dict[str, Any]:
|
|
707
755
|
"""Get statistics about the cache."""
|
|
708
756
|
with self.get_session() as session:
|
|
709
757
|
total = session.query(Resource).count()
|
pybiocfilecache/config.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
from dataclasses import dataclass
|
|
2
2
|
from datetime import timedelta
|
|
3
3
|
from pathlib import Path
|
|
4
|
-
from typing import Optional
|
|
5
4
|
|
|
6
5
|
__author__ = "Jayaram Kancherla"
|
|
7
6
|
__copyright__ = "Jayaram Kancherla"
|
|
@@ -28,6 +27,6 @@ class CacheConfig:
|
|
|
28
27
|
"""
|
|
29
28
|
|
|
30
29
|
cache_dir: Path
|
|
31
|
-
cleanup_interval:
|
|
30
|
+
cleanup_interval: timedelta | None = None # timedelta(days=30)
|
|
32
31
|
rname_pattern: str = r"^[a-zA-Z0-9_-]+$"
|
|
33
32
|
hash_algorithm: str = "md5"
|
pybiocfilecache/utils.py
CHANGED
|
@@ -7,7 +7,7 @@ import uuid
|
|
|
7
7
|
import zlib
|
|
8
8
|
from pathlib import Path
|
|
9
9
|
from shutil import copy2, move
|
|
10
|
-
from typing import
|
|
10
|
+
from typing import Literal
|
|
11
11
|
from urllib.parse import urlparse
|
|
12
12
|
|
|
13
13
|
__author__ = "Jayaram Kancherla"
|
|
@@ -43,7 +43,7 @@ def calculate_file_hash(path: Path, algorithm: str = "md5") -> str:
|
|
|
43
43
|
with open(path, "rb") as f:
|
|
44
44
|
for chunk in iter(lambda: f.read(4096), b""):
|
|
45
45
|
hasher.update(chunk)
|
|
46
|
-
|
|
46
|
+
|
|
47
47
|
return hasher.hexdigest()
|
|
48
48
|
|
|
49
49
|
|
|
@@ -89,13 +89,13 @@ def copy_or_move(
|
|
|
89
89
|
raise Exception(f"Failed to store resource '{rname}' from '{source}' to '{target}'") from e
|
|
90
90
|
|
|
91
91
|
|
|
92
|
-
def download_web_file(url: str, filename:
|
|
92
|
+
def download_web_file(url: str, filename: str | None = None, download: bool = True) -> Path:
|
|
93
93
|
"""Download a web resource into a temporary directory."""
|
|
94
|
-
|
|
94
|
+
|
|
95
95
|
if not filename:
|
|
96
96
|
parsed_name = Path(urlparse(str(url)).path).name
|
|
97
97
|
filename = parsed_name or "download"
|
|
98
|
-
|
|
98
|
+
|
|
99
99
|
tmp_dir = create_tmp_dir()
|
|
100
100
|
outpath = tmp_dir / filename
|
|
101
101
|
if download:
|
|
@@ -106,7 +106,7 @@ def download_web_file(url: str, filename: Optional[str] = None, download: bool =
|
|
|
106
106
|
return outpath
|
|
107
107
|
|
|
108
108
|
|
|
109
|
-
def convert_to_columnar(list_of_dicts:
|
|
109
|
+
def convert_to_columnar(list_of_dicts: list[dict]):
|
|
110
110
|
if not list_of_dicts:
|
|
111
111
|
return {}
|
|
112
112
|
|
|
@@ -116,5 +116,5 @@ def convert_to_columnar(list_of_dicts: List[dict]):
|
|
|
116
116
|
for row in list_of_dicts:
|
|
117
117
|
for col in column_names:
|
|
118
118
|
result[col].append(row.get(col))
|
|
119
|
-
|
|
119
|
+
|
|
120
120
|
return result
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: pybiocfilecache
|
|
3
|
+
Version: 0.8.1
|
|
4
|
+
Summary: File based cache for resources and metadata
|
|
5
|
+
Project-URL: Homepage, https://github.com/BiocPy/pybiocfilecache
|
|
6
|
+
Project-URL: Documentation, https://biocpy.github.io/pybiocfilecache
|
|
7
|
+
Project-URL: Repository, https://github.com/BiocPy/pybiocfilecache
|
|
8
|
+
Project-URL: Bug Tracker, https://github.com/BiocPy/pybiocfilecache/issues
|
|
9
|
+
Author-email: Jayaram Kancherla <jayaram.kancherla@gmail.com>
|
|
10
|
+
License: The MIT License (MIT)
|
|
11
|
+
|
|
12
|
+
Copyright (c) 2021 Genentech, Inc.
|
|
13
|
+
|
|
14
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
15
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
16
|
+
in the Software without restriction, including without limitation the rights
|
|
17
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
18
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
19
|
+
furnished to do so, subject to the following conditions:
|
|
20
|
+
|
|
21
|
+
The above copyright notice and this permission notice shall be included in all
|
|
22
|
+
copies or substantial portions of the Software.
|
|
23
|
+
|
|
24
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
25
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
26
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
27
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
28
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
29
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
30
|
+
SOFTWARE.
|
|
31
|
+
License-File: AUTHORS.md
|
|
32
|
+
License-File: LICENSE.txt
|
|
33
|
+
Keywords: biocfilecache,cache,file-cache,resource-management,sqlite
|
|
34
|
+
Classifier: Development Status :: 4 - Beta
|
|
35
|
+
Classifier: Intended Audience :: Developers
|
|
36
|
+
Classifier: Intended Audience :: Science/Research
|
|
37
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
38
|
+
Classifier: Operating System :: OS Independent
|
|
39
|
+
Classifier: Programming Language :: Python :: 3
|
|
40
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
41
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
42
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
43
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
44
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
45
|
+
Classifier: Topic :: Scientific/Engineering :: Bio-Informatics
|
|
46
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
47
|
+
Requires-Python: >=3.9
|
|
48
|
+
Requires-Dist: biocframe
|
|
49
|
+
Requires-Dist: sqlalchemy
|
|
50
|
+
Provides-Extra: testing
|
|
51
|
+
Requires-Dist: pytest; extra == 'testing'
|
|
52
|
+
Requires-Dist: pytest-cov; extra == 'testing'
|
|
53
|
+
Requires-Dist: setuptools; extra == 'testing'
|
|
54
|
+
Description-Content-Type: text/markdown
|
|
55
|
+
|
|
56
|
+
[](https://pypi.org/project/pyBiocFileCache/)
|
|
57
|
+

|
|
58
|
+

|
|
59
|
+
|
|
60
|
+
# pyBiocFileCache
|
|
61
|
+
|
|
62
|
+
`pyBiocFileCache` is a Python package that provides a file caching system with resource validation, expiration management, and SQLite-backed metadata. Compatible with the [BiocFileCache R package](https://github.com/Bioconductor/BiocFileCache).
|
|
63
|
+
|
|
64
|
+
## Installation
|
|
65
|
+
|
|
66
|
+
Install from [PyPI](https://pypi.org/project/pyBiocFileCache/):
|
|
67
|
+
|
|
68
|
+
```bash
|
|
69
|
+
pip install pybiocfilecache
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## Quick Start
|
|
73
|
+
|
|
74
|
+
```python
|
|
75
|
+
from pybiocfilecache import BiocFileCache
|
|
76
|
+
|
|
77
|
+
# Initialize cache
|
|
78
|
+
cache = BiocFileCache("path/to/cache/directory")
|
|
79
|
+
|
|
80
|
+
# Add a file to cache
|
|
81
|
+
resource = cache.add("myfile", "path/to/file.txt")
|
|
82
|
+
|
|
83
|
+
# Retrieve a file from cache
|
|
84
|
+
resource = cache.get("myfile")
|
|
85
|
+
|
|
86
|
+
# Use the cached file
|
|
87
|
+
print(resource["rpath"]) # Path to cached file
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Advanced Usage
|
|
91
|
+
|
|
92
|
+
### Configuration
|
|
93
|
+
|
|
94
|
+
```python
|
|
95
|
+
from datetime import timedelta
|
|
96
|
+
from pathlib import Path
|
|
97
|
+
|
|
98
|
+
from pybiocfilecache import BiocFileCache, CacheConfig
|
|
99
|
+
|
|
100
|
+
# Create custom configuration
|
|
101
|
+
config = CacheConfig(
|
|
102
|
+
cache_dir=Path("cache_directory"),
|
|
103
|
+
cleanup_interval=timedelta(days=7),
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
# Initialize cache with configuration
|
|
107
|
+
cache = BiocFileCache(config=config)
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### Resource Management
|
|
111
|
+
|
|
112
|
+
```python
|
|
113
|
+
from datetime import datetime, timedelta, timezone
|
|
114
|
+
|
|
115
|
+
# Add file with expiration
|
|
116
|
+
resource = cache.add(
|
|
117
|
+
"myfile",
|
|
118
|
+
"path/to/file.txt",
|
|
119
|
+
expires=datetime.now(timezone.utc) + timedelta(days=30),
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
# Immediate lookup without waiting on missing files
|
|
123
|
+
record = cache.get(rid="BFC1", wait=False)
|
|
124
|
+
|
|
125
|
+
# List resources
|
|
126
|
+
resources = cache.list_resources()
|
|
127
|
+
|
|
128
|
+
# Search resources (exact or substring match)
|
|
129
|
+
results = cache.search("myfile", field="rname")
|
|
130
|
+
|
|
131
|
+
# Update resource by name or rid
|
|
132
|
+
cache.update("myfile", "path/to/new_file.txt")
|
|
133
|
+
cache.update(rid="BFC1", fpath="path/to/new_file.txt")
|
|
134
|
+
|
|
135
|
+
# Remove resource by name or rid
|
|
136
|
+
cache.remove("myfile")
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
### Cache Statistics and Maintenance
|
|
140
|
+
|
|
141
|
+
```python
|
|
142
|
+
# Get cache statistics
|
|
143
|
+
stats = cache.get_stats()
|
|
144
|
+
print(stats)
|
|
145
|
+
|
|
146
|
+
# Clean up expired resources
|
|
147
|
+
removed_count = cache.cleanup()
|
|
148
|
+
|
|
149
|
+
# Verify integrity of cached files
|
|
150
|
+
valid, invalid = cache.verify_cache()
|
|
151
|
+
|
|
152
|
+
# Purge entire cache
|
|
153
|
+
cache.purge()
|
|
154
|
+
```
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
pybiocfilecache/__init__.py,sha256=kClwDkzgGPYAyDW-fuo5LI4lKCruYJhDHvw0lq9u4b0,652
|
|
2
|
+
pybiocfilecache/cache.py,sha256=-5R14I5np3WRKLpGpxvMhuLez1bII_NF6WXurkMc2gk,30906
|
|
3
|
+
pybiocfilecache/config.py,sha256=4tm0aAiNdxOn3rqJNHHgAWRNV8c8iDB1FLMnK9tuA0c,772
|
|
4
|
+
pybiocfilecache/const.py,sha256=c9167eSCGBp0wF1xuAEvr-fbYCzp0lcOtKF9sqxlLF8,26
|
|
5
|
+
pybiocfilecache/models.py,sha256=6CFAmSLDhIBjBGU2leV6B4MxEZQQnJE0xrF55CRT6mw,2582
|
|
6
|
+
pybiocfilecache/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
7
|
+
pybiocfilecache/utils.py,sha256=HzONrEmukpHVJkXKMrvVVV2DSwn2vynK_o72dggKtCE,3247
|
|
8
|
+
pybiocfilecache-0.8.1.dist-info/METADATA,sha256=GjytX5aIdeS1Ek9nc_uxeWm_o4wAHqCupssmNVGldmo,5169
|
|
9
|
+
pybiocfilecache-0.8.1.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
|
|
10
|
+
pybiocfilecache-0.8.1.dist-info/licenses/AUTHORS.md,sha256=ptvMLokf9Knfp0Ng-dHSgtEimkXWZhjZAR_o2aJm9bw,161
|
|
11
|
+
pybiocfilecache-0.8.1.dist-info/licenses/LICENSE.txt,sha256=bV5MJ4ezdCnC4yREyxU30iel5hFq8T8kGqobu4Kc-WM,1082
|
|
12
|
+
pybiocfilecache-0.8.1.dist-info/RECORD,,
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.5
|
|
2
|
-
Name: pybiocfilecache
|
|
3
|
-
Version: 0.8.0.dev0
|
|
4
|
-
Summary: File based cache for resources and metadata
|
|
5
|
-
Author-email: Jayaram Kancherla <jayaram.kancherla@gmail.com>
|
|
6
|
-
License-File: AUTHORS.md
|
|
7
|
-
License-File: LICENSE.txt
|
|
8
|
-
Requires-Python: >=3.12
|
|
9
|
-
Requires-Dist: biocframe>=0.7.3
|
|
10
|
-
Requires-Dist: importlib-metadata>=9.0.0; python_full_version < '3.8'
|
|
11
|
-
Requires-Dist: sqlalchemy>=2.0.52
|
|
12
|
-
Provides-Extra: testing
|
|
13
|
-
Requires-Dist: pytest-cov>=7.1.0; extra == 'testing'
|
|
14
|
-
Requires-Dist: pytest>=9.1.1; extra == 'testing'
|
|
15
|
-
Requires-Dist: setuptools>=84.0.0; extra == 'testing'
|
|
16
|
-
Description-Content-Type: text/markdown
|
|
17
|
-
|
|
18
|
-
```{include} ../README.md
|
|
19
|
-
:relative-docs: docs/
|
|
20
|
-
:relative-images:
|
|
21
|
-
```
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
pybiocfilecache/__init__.py,sha256=kClwDkzgGPYAyDW-fuo5LI4lKCruYJhDHvw0lq9u4b0,652
|
|
2
|
-
pybiocfilecache/cache.py,sha256=EJ8seT-OeW2uaTr58HK_qyXtfSR6P6sf4zfPrPn6WmY,29136
|
|
3
|
-
pybiocfilecache/config.py,sha256=bado4Hg6QFIfV46zKwx6Q9AoV__G-e8fghylnk5I-nk,803
|
|
4
|
-
pybiocfilecache/const.py,sha256=c9167eSCGBp0wF1xuAEvr-fbYCzp0lcOtKF9sqxlLF8,26
|
|
5
|
-
pybiocfilecache/models.py,sha256=6CFAmSLDhIBjBGU2leV6B4MxEZQQnJE0xrF55CRT6mw,2582
|
|
6
|
-
pybiocfilecache/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
7
|
-
pybiocfilecache/utils.py,sha256=fzr00U8D3yGp09gYJfhvHoQrt5P5KbVpLNsNkJfB4hY,3282
|
|
8
|
-
pybiocfilecache-0.8.0.dev0.dist-info/METADATA,sha256=HrmOTJC2Ueqi4QPfSqnEZVJ260PWQ8nzL7vNqq7sOD4,681
|
|
9
|
-
pybiocfilecache-0.8.0.dev0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
|
|
10
|
-
pybiocfilecache-0.8.0.dev0.dist-info/licenses/AUTHORS.md,sha256=6nnc80t6djIXiPNai5Mumh8ROiq0E19XOlhCGgKc8Uw,71
|
|
11
|
-
pybiocfilecache-0.8.0.dev0.dist-info/licenses/LICENSE.txt,sha256=bV5MJ4ezdCnC4yREyxU30iel5hFq8T8kGqobu4Kc-WM,1082
|
|
12
|
-
pybiocfilecache-0.8.0.dev0.dist-info/RECORD,,
|
|
File without changes
|
{pybiocfilecache-0.8.0.dev0.dist-info → pybiocfilecache-0.8.1.dist-info}/licenses/LICENSE.txt
RENAMED
|
File without changes
|