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
22 changes: 16 additions & 6 deletions src/gcpath/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

CACHE_DIR = Path.home() / ".gcpath"
CACHE_FILE = CACHE_DIR / "cache.json"
CACHE_VERSION = 1
CACHE_VERSION = 2
DEFAULT_CACHE_TTL_HOURS = 72


Expand All @@ -38,7 +38,9 @@ class CacheInfo:
scope: Optional[str] = None


def _hierarchy_to_dict(hierarchy: Hierarchy, scope: Optional[str] = None) -> Dict[str, Any]:
def _hierarchy_to_dict(
hierarchy: Hierarchy, scope: Optional[str] = None
) -> Dict[str, Any]:
"""Serializes the Hierarchy object to a dictionary."""
organizations_data = []

Expand All @@ -53,6 +55,8 @@ def _hierarchy_to_dict(hierarchy: Hierarchy, scope: Optional[str] = None) -> Dic
"display_name": project.display_name,
"parent": project.parent,
"folder_name": project.folder.name if project.folder else None,
"labels": project.labels,
"tags": project.tags,
}

if project.organization:
Expand All @@ -76,6 +80,8 @@ def _hierarchy_to_dict(hierarchy: Hierarchy, scope: Optional[str] = None) -> Dic
"display_name": folder.display_name,
"ancestors": folder.ancestors,
"parent": folder.parent,
"labels": folder.labels,
"tags": folder.tags,
}
for name, folder in org_node.folders.items()
}
Expand All @@ -101,7 +107,7 @@ def _dict_to_hierarchy(data: Dict[str, Any]) -> Optional[Hierarchy]:
"""Deserializes a dictionary to a Hierarchy object."""
if data.get("version") != CACHE_VERSION:
logger.warning(
f"Cache version mismatch (expected {CACHE_VERSION}, got {data.get('version')}). Ignoring cache."
"Cache version mismatch (expected %s). Ignoring cache.", CACHE_VERSION
)
return None

Expand Down Expand Up @@ -130,6 +136,8 @@ def _dict_to_hierarchy(data: Dict[str, Any]) -> Optional[Hierarchy]:
ancestors=folder_data["ancestors"],
parent=folder_data["parent"],
organization=node,
labels=folder_data.get("labels", {}),
tags=folder_data.get("tags", {}),
)
node.folders[folder_name] = folder

Expand All @@ -145,6 +153,8 @@ def _dict_to_hierarchy(data: Dict[str, Any]) -> Optional[Hierarchy]:
parent=p_data["parent"],
organization=node,
folder=parent_folder,
labels=p_data.get("labels", {}),
tags=p_data.get("tags", {}),
)
projects.append(project)

Expand All @@ -157,6 +167,8 @@ def _dict_to_hierarchy(data: Dict[str, Any]) -> Optional[Hierarchy]:
parent=p_data["parent"],
organization=None,
folder=None,
labels=p_data.get("labels", {}),
tags=p_data.get("tags", {}),
)
projects.append(project)

Expand Down Expand Up @@ -257,9 +269,7 @@ def get_cache_info(
if timestamp_str:
try:
cached_time = datetime.fromisoformat(timestamp_str)
age_seconds = (
datetime.now(timezone.utc) - cached_time
).total_seconds()
age_seconds = (datetime.now(timezone.utc) - cached_time).total_seconds()
except (ValueError, TypeError):
pass

Expand Down
Loading
Loading