1- import json
21import logging
32from contextlib import contextmanager
43from datetime import datetime
@@ -108,6 +107,7 @@ def _setup_database(self) -> None:
108107 """ ),
109108 {"version" : SCHEMA_VERSION },
110109 )
110+ conn .commit ()
111111
112112 return SCHEMA_VERSION
113113
@@ -263,7 +263,7 @@ def add(
263263 Returns:
264264 The `Resource` object added to the cache.
265265 """
266- self ._validate_rname (rname )
266+ # self._validate_rname(rname)
267267 fpath = Path (fpath )
268268
269269 if not fpath .exists ():
@@ -273,7 +273,7 @@ def add(
273273 raise RnameExistsError (f"Resource '{ rname } ' already exists" )
274274
275275 # Generate paths and check size
276- rid = generate_id ()
276+ rid = generate_id (size = len ( self ) )
277277 rpath = self .config .cache_dir / f"{ rid } { fpath .suffix if ext else '' } " if action != "asis" else fpath
278278
279279 # Create resource record
@@ -459,38 +459,38 @@ def validate_resource(self, resource: Resource) -> bool:
459459 logger .error (f"Failed to validate resource: { resource .rname } " , exc_info = e )
460460 return False
461461
462- def export_metadata (self , path : Path ) -> None :
463- """Export cache metadata to JSON file."""
464- data = {
465- "resources" : [
466- {
467- "rname" : r .rname ,
468- "rtype" : r .rtype ,
469- "expires" : r .expires .isoformat () if r .expires else None ,
470- "etag" : r .etag ,
471- }
472- for r in self .list_resources ()
473- ],
474- "export_time" : datetime .now ().isoformat (),
475- }
476-
477- with open (path , "w" ) as f :
478- json .dump (data , f , indent = 2 )
479-
480- def import_metadata (self , path : Path ) -> None :
481- """Import cache metadata from JSON file."""
482- with open (path ) as f :
483- data = json .load (f )
484-
485- with self .get_session () as session :
486- for resource_data in data ["resources" ]:
487- resource = self ._get (session , resource_data ["rname" ])
488- if resource :
489- resource .expires = (
490- datetime .fromisoformat (resource_data ["expires" ]) if resource_data ["expires" ] else None
491- )
492- session .merge (resource )
493- session .commit ()
462+ # def export_metadata(self, path: Path) -> None:
463+ # """Export cache metadata to JSON file."""
464+ # data = {
465+ # "resources": [
466+ # {
467+ # "rname": r.rname,
468+ # "rtype": r.rtype,
469+ # "expires": r.expires.isoformat() if r.expires else None,
470+ # "etag": r.etag,
471+ # }
472+ # for r in self.list_resources()
473+ # ],
474+ # "export_time": datetime.now().isoformat(),
475+ # }
476+
477+ # with open(path, "w") as f:
478+ # json.dump(data, f, indent=2)
479+
480+ # def import_metadata(self, path: Path) -> None:
481+ # """Import cache metadata from JSON file."""
482+ # with open(path) as f:
483+ # data = json.load(f)
484+
485+ # with self.get_session() as session:
486+ # for resource_data in data["resources"]:
487+ # resource = self._get(session, resource_data["rname"])
488+ # if resource:
489+ # resource.expires = (
490+ # datetime.fromisoformat(resource_data["expires"]) if resource_data["expires"] else None
491+ # )
492+ # session.merge(resource)
493+ # session.commit()
494494
495495 def verify_cache (self ) -> Tuple [int , int ]:
496496 """Verify integrity of all cached resources.
@@ -612,3 +612,7 @@ def purge(self, force: bool = False) -> bool:
612612 logger .warning (f"Failed to remove { file } : { file_e } " )
613613
614614 return False
615+
616+ def __len__ (self ):
617+ with self .get_session () as session :
618+ return session .query (Resource ).count ()
0 commit comments