Repository navigation
Expand file tree
/
Copy pathpaths.py
More file actions
114 lines (88 loc) · 3.29 KB
/
Copy pathpaths.py
File metadata and controls
114 lines (88 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
"""Filesystem layout for MiniCS.
Everything the user owns lives inside a single home directory (``~/.minics`` by
default). Keeping the layout in one place means the rest of the application
never has to build paths by hand and can be pointed at a temporary directory in
tests via the ``MINICS_HOME`` environment variable.
"""
from __future__ import annotations
import os
from dataclasses import dataclass
from pathlib import Path
ENV_HOME = "MINICS_HOME"
DEFAULT_HOME_NAME = ".minics"
#: Sub-directories that are created on first run.
STRUCTURE: tuple[str, ...] = (
"documents",
"documents/markdown",
"documents/originals",
"vectordb",
"graphdb",
"logs",
"cache",
)
def default_home() -> Path:
"""Return the configured home directory, honouring ``MINICS_HOME``."""
override = os.environ.get(ENV_HOME)
if override:
return Path(override).expanduser().resolve()
return (Path.home() / DEFAULT_HOME_NAME).resolve()
@dataclass(frozen=True)
class MinicsPaths:
"""Resolved paths for a MiniCS installation."""
root: Path
# -- directories -----------------------------------------------------
@property
def documents(self) -> Path:
return self.root / "documents"
@property
def markdown_dir(self) -> Path:
"""Converted, LLM-cleaned markdown copies of imported documents."""
return self.documents / "markdown"
@property
def originals_dir(self) -> Path:
"""Untouched uploaded files, kept for provenance/re-extraction."""
return self.documents / "originals"
@property
def vectordb(self) -> Path:
"""ChromaDB persistent directory."""
return self.root / "vectordb"
@property
def graphdb(self) -> Path:
"""Ladybug graph database directory."""
return self.root / "graphdb"
@property
def graph_file(self) -> Path:
"""Ladybug database file (Ladybug expects a file, not a directory)."""
return self.graphdb / "minics.graph"
@property
def logs(self) -> Path:
return self.root / "logs"
@property
def cache(self) -> Path:
return self.root / "cache"
# -- files -----------------------------------------------------------
@property
def config_file(self) -> Path:
return self.root / "config.json"
@property
def database(self) -> Path:
"""Main SQLite database file."""
4699
return self.root / "minics.sqlite3"
# -- helpers ---------------------------------------------------------
@property
def all_dirs(self) -> tuple[Path, ...]:
return (self.root, *(self.root / name for name in STRUCTURE))
def ensure(self) -> MinicsPaths:
"""Create the directory structure if it does not exist yet."""
for directory in self.all_dirs:
directory.mkdir(parents=True, exist_ok=True)
return self
def exists(self) -> bool:
return self.root.exists() and self.database.exists()
def __str__(self) -> str: # pragma: no cover - cosmetic
return str(self.root)
def get_paths(root: str | os.PathLike[str] | None = None) -> MinicsPaths:
"""Build a :class:`MinicsPaths` for ``root`` (defaults to the user home)."""
if root is None:
return MinicsPaths(default_home())
return MinicsPaths(Path(root).expanduser().resolve())