Skip to content

Commit 1735900

Browse files
committed
Add serialization and deserialization methods to CodeTide class and define default serialization path
1 parent 79d3825 commit 1735900

2 files changed

Lines changed: 17 additions & 3 deletions

File tree

codetide/__init__.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
from codetide.core.defaults import LANGUAGE_EXTENSIONS, DEFAULT_MAX_CONCURRENT_TASKS, DEFAULT_BATCH_SIZE
1+
from codetide.core.defaults import DEFAULT_SERIALIZATION_PATH, LANGUAGE_EXTENSIONS, DEFAULT_MAX_CONCURRENT_TASKS, DEFAULT_BATCH_SIZE
22
from codetide.core.models import CodeFileModel, CodeBase
3-
from codetide.core.common import readFile
3+
from codetide.core.common import readFile, writeFile
44

55
from codetide.parsers import BaseParser
66
from codetide import parsers
@@ -12,6 +12,7 @@
1212
import logging
1313
import asyncio
1414
import time
15+
import os
1516

1617
logging.basicConfig(
1718
level=logging.INFO,
@@ -82,6 +83,19 @@ async def from_path(
8283
logger.info(f"CodeBase initialized with {len(results)} files processed in {time.time() - st:.2f}s")
8384

8485
return codebase
86+
87+
def serialize(self, filepath :Optional[Union[str, Path]]=DEFAULT_SERIALIZATION_PATH):
88+
if not os.path.exists(filepath):
89+
os.makedirs(os.path.split(filepath)[0], exist_ok=True)
90+
writeFile(self.model_dump_json(indent=4), filepath)
91+
92+
@classmethod
93+
def deserialize(cls, filepath :Optional[Union[str, Path]]=DEFAULT_SERIALIZATION_PATH)->"CodeTide":
94+
if not os.path.exists(filepath):
95+
raise FileNotFoundError(f"{filepath} is not a valid path")
96+
97+
kwargs = readFile(filepath)
98+
return cls(**kwargs)
8599

86100
def _organize_files_by_language(
87101
self,

codetide/core/defaults.py

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

5151
DEFAULT_ENCODING = "utf8"
5252

53-
DEFAULT_ENCODINGS = ['utf-8', 'latin1', 'cp1252']
53+
DEFAULT_SERIALIZATION_PATH = "./storage/tide.json"

0 commit comments

Comments
 (0)