summaryrefslogtreecommitdiff
path: root/server/continuedev/libs/util/paths.py
diff options
context:
space:
mode:
Diffstat (limited to 'server/continuedev/libs/util/paths.py')
-rw-r--r--server/continuedev/libs/util/paths.py148
1 files changed, 148 insertions, 0 deletions
diff --git a/server/continuedev/libs/util/paths.py b/server/continuedev/libs/util/paths.py
new file mode 100644
index 00000000..22e4b5b9
--- /dev/null
+++ b/server/continuedev/libs/util/paths.py
@@ -0,0 +1,148 @@
+import os
+import re
+from typing import Optional
+
+from ..constants.default_config import default_config
+from ..constants.main import (
+ CONTINUE_GLOBAL_FOLDER,
+ CONTINUE_SERVER_FOLDER,
+ CONTINUE_SESSIONS_FOLDER,
+)
+
+
+def find_data_file(filename):
+ datadir = os.path.dirname(__file__)
+ return os.path.abspath(os.path.join(datadir, filename))
+
+
+def getGlobalFolderPath():
+ path = os.path.join(os.path.expanduser("~"), CONTINUE_GLOBAL_FOLDER)
+ os.makedirs(path, exist_ok=True)
+ return path
+
+
+def getSessionsFolderPath():
+ path = os.path.join(getGlobalFolderPath(), CONTINUE_SESSIONS_FOLDER)
+ os.makedirs(path, exist_ok=True)
+ return path
+
+
+def getServerFolderPath():
+ path = os.path.join(getGlobalFolderPath(), CONTINUE_SERVER_FOLDER)
+ os.makedirs(path, exist_ok=True)
+ return path
+
+
+def getDevDataFolderPath():
+ path = os.path.join(getGlobalFolderPath(), "dev_data")
+ os.makedirs(path, exist_ok=True)
+ return path
+
+
+def getDiffsFolderPath():
+ path = os.path.join(getGlobalFolderPath(), "diffs")
+ os.makedirs(path, exist_ok=True)
+ return path
+
+
+def getDevDataFilePath(table_name: str):
+ filepath = os.path.join(getDevDataFolderPath(), f"{table_name}.jsonl")
+ if not os.path.exists(filepath):
+ with open(filepath, "w") as f:
+ f.write("")
+
+ return filepath
+
+
+def getMeilisearchExePath():
+ binary_name = "meilisearch.exe" if os.name == "nt" else "meilisearch"
+ path = os.path.join(getServerFolderPath(), binary_name)
+ return path
+
+
+def getSessionFilePath(session_id: str):
+ path = os.path.join(getSessionsFolderPath(), f"{session_id}.json")
+ os.makedirs(os.path.dirname(path), exist_ok=True)
+ return path
+
+
+def getSessionsListFilePath():
+ path = os.path.join(getSessionsFolderPath(), "sessions.json")
+ os.makedirs(os.path.dirname(path), exist_ok=True)
+ if not os.path.exists(path):
+ with open(path, "w") as f:
+ f.write("[]")
+ return path
+
+
+def migrateConfigFile(existing: str) -> Optional[str]:
+ if existing.strip() == "":
+ return default_config
+
+ migrated = (
+ existing.replace("MaybeProxyOpenAI", "OpenAIFreeTrial")
+ .replace("maybe_proxy_openai", "openai_free_trial")
+ .replace("unused=", "saved=")
+ .replace("medium=", "summarize=")
+ )
+ if migrated != existing:
+ return migrated
+
+ return None
+
+
+def getConfigFilePath() -> str:
+ path = os.path.join(getGlobalFolderPath(), "config.py")
+ os.makedirs(os.path.dirname(path), exist_ok=True)
+
+ if not os.path.exists(path):
+ with open(path, "w") as f:
+ f.write(default_config)
+ else:
+ # Make any necessary migrations
+ with open(path, "r") as f:
+ existing_content = f.read()
+
+ migrated = migrateConfigFile(existing_content)
+
+ if migrated is not None:
+ with open(path, "w") as f:
+ f.write(migrated)
+
+ return path
+
+
+def convertConfigImports(shorten: bool) -> str:
+ path = getConfigFilePath()
+ # Make any necessary migrations
+ with open(path, "r") as f:
+ existing_content = f.read()
+
+ if shorten:
+ migrated = existing_content.replace(
+ "from continuedev.src.continuedev.", "from continuedev."
+ )
+ else:
+ migrated = re.sub(
+ r"(?<!src\.)continuedev\.(?!src)",
+ "continuedev.",
+ existing_content,
+ )
+
+ with open(path, "w") as f:
+ f.write(migrated)
+
+
+def getLogFilePath():
+ path = os.path.join(getGlobalFolderPath(), "continue.log")
+ os.makedirs(os.path.dirname(path), exist_ok=True)
+ return path
+
+
+def getSavedContextGroupsPath():
+ path = os.path.join(getGlobalFolderPath(), "saved_context_groups.json")
+ os.makedirs(os.path.dirname(path), exist_ok=True)
+ if not os.path.exists(path):
+ with open(path, "w") as f:
+ f.write("\{\}")
+ return path