Skip to content

Commit b9d314e

Browse files
committed
Enhance serialization in CodeTide by adding support for cached elements and IDs, and update defaults for serialization paths.
1 parent 679debc commit b9d314e

File tree

3 files changed

+55
-5
lines changed

3 files changed

+55
-5
lines changed

codetide/__init__.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
from codetide.core.defaults import DEFAULT_SERIALIZATION_PATH, LANGUAGE_EXTENSIONS, DEFAULT_MAX_CONCURRENT_TASKS, DEFAULT_BATCH_SIZE
1+
from codetide.core.defaults import (
2+
DEFAULT_SERIALIZATION_PATH, DEFAULT_MAX_CONCURRENT_TASKS,
3+
DEFAULT_BATCH_SIZE, DEFAULT_CACHED_ELEMENTS_FILE, DEFAULT_CACHED_IDS_FILE,
4+
LANGUAGE_EXTENSIONS
5+
)
26
from codetide.core.models import CodeFileModel, CodeBase
37
from codetide.core.common import readFile, writeFile
48

@@ -12,6 +16,7 @@
1216
import logging
1317
import asyncio
1418
import time
19+
import json
1520
import os
1621

1722
logging.basicConfig(
@@ -84,18 +89,35 @@ async def from_path(
8489

8590
return codebase
8691

87-
def serialize(self, filepath :Optional[Union[str, Path]]=DEFAULT_SERIALIZATION_PATH):
92+
def serialize(self, filepath :Optional[Union[str, Path]]=DEFAULT_SERIALIZATION_PATH, include_codebase_cached_elements :bool=False, include_cached_ids :bool=False):
8893
if not os.path.exists(filepath):
8994
os.makedirs(os.path.split(filepath)[0], exist_ok=True)
9095
writeFile(self.model_dump_json(indent=4), filepath)
96+
if include_codebase_cached_elements or include_cached_ids:
97+
dir_path = Path(os.path.split(filepath))[0]
98+
if include_codebase_cached_elements:
99+
cached_elements_path = dir_path / DEFAULT_CACHED_ELEMENTS_FILE
100+
writeFile(self.codebase.serialize_cache_elements(), cached_elements_path)
101+
102+
if include_cached_ids:
103+
cached_ids_path = dir_path / DEFAULT_CACHED_IDS_FILE
104+
writeFile(json.dumps(self.codebase.unique_ds, indent=4), cached_ids_path)
91105

92106
@classmethod
93107
def deserialize(cls, filepath :Optional[Union[str, Path]]=DEFAULT_SERIALIZATION_PATH)->"CodeTide":
94108
if not os.path.exists(filepath):
95109
raise FileNotFoundError(f"{filepath} is not a valid path")
96110

97111
kwargs = readFile(filepath)
98-
return cls(**kwargs)
112+
tideInstance = cls(**kwargs)
113+
114+
# dir_path = Path(os.path.split(filepath))[0]
115+
# cached_elements_path = dir_path / DEFAULT_CACHED_ELEMENTS_FILE
116+
# if os.path.exists(cached_elements_path):
117+
# cached_elements = json.loads(readFile(cached_elements_path))
118+
# tideInstance.codebase._cached_elements = cached_elements
119+
120+
return tideInstance
99121

100122
def _organize_files_by_language(
101123
self,

codetide/core/defaults.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,6 @@
5050

5151
DEFAULT_ENCODING = "utf8"
5252

53-
DEFAULT_SERIALIZATION_PATH = "./storage/tide.json"
53+
DEFAULT_SERIALIZATION_PATH = "./storage/tide.json"
54+
DEFAULT_CACHED_ELEMENTS_FILE = "cached_elements.json"
55+
DEFAULT_CACHED_IDS_FILE = "cached_ids.json"

codetide/core/models.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from pydantic import BaseModel, Field, computed_field, field_validator
44
from typing import Any, Dict, List, Optional, Literal, Union
55
from collections import defaultdict
6+
import json
67

78
class BaseCodeElement(BaseModel):
89
file_path: str = ""
@@ -684,4 +685,29 @@ def get(self, unique_id :Union[str, List[str]], degree :int=0, as_string :bool=F
684685
return codeContext.as_list_str()
685686

686687
else:
687-
return codeContext
688+
return codeContext
689+
690+
def serialize_cache_elements(self, indent :int=4)->str:
691+
return json.dumps(
692+
{
693+
key: value.model_dump()
694+
for key, value in self._cached_elements
695+
}
696+
)
697+
698+
def deserialize_cache_elements(self, contents :str):
699+
self._cached_elements = json.loads(contents)
700+
### TODO need to handle model validates and so on
701+
# return json.dumps(
702+
# {
703+
# key: value.model_dump()
704+
# for key, value in self._cached_elements
705+
# }
706+
# )
707+
708+
709+
def unique_ds(self)->List[str]:
710+
if not self._cached_elements:
711+
self._build_cached_elements()
712+
713+
return list(self._cached_elements.keys())

0 commit comments

Comments
 (0)