-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuri.py
More file actions
166 lines (143 loc) · 6.25 KB
/
Copy pathuri.py
File metadata and controls
166 lines (143 loc) · 6.25 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
"""URI Routing for DocsHaven — organize knowledge by domain://path."""
from __future__ import annotations
import re
from dataclasses import dataclass
# Valid domains for the knowledge base
VALID_DOMAINS = {"core", "ref", "guide", "lib", "src", "test", "note"}
@dataclass
class URI:
"""Parsed knowledge URI: domain://path/to/doc"""
domain: str
path: str
raw: str
@classmethod
def parse(cls, uri: str) -> URI:
"""Parse 'core://fastapi/deps' into URI(domain='core', path='fastapi/deps')"""
m = re.match(r"^([a-zA-Z0-9_-]+)://(.+)$", uri)
if not m:
raise ValueError(f"Invalid URI format: {uri} (expected domain://path)")
domain = m.group(1).lower()
# Collapse multiple slashes and strip leading/trailing slashes
path = re.sub(r"/+", "/", m.group(2)).strip("/")
if not path:
raise ValueError(f"Empty path in URI: {uri} (expected domain://path)")
# Validate path doesn't contain traversal or injection
if ".." in path or path.startswith("/"):
raise ValueError(f"Invalid URI path: {path}")
if domain not in VALID_DOMAINS:
raise ValueError(f"Unknown domain '{domain}'. Valid: {sorted(VALID_DOMAINS)}")
return cls(domain=domain, path=path, raw=uri)
@classmethod
def create(cls, domain: str, path: str) -> URI:
"""Create URI from domain and path components."""
domain = domain.lower().strip()
path = path.strip("/")
if domain not in VALID_DOMAINS:
raise ValueError(f"Unknown domain '{domain}'. Valid: {sorted(VALID_DOMAINS)}")
return cls(domain=domain, path=path, raw=f"{domain}://{path}")
def to_collection(self) -> str:
"""Map URI to collection name: 'core://fastapi/deps' -> 'core__fastapi'"""
parts = self.path.split("/")
if len(parts) >= 1 and parts[0]:
return f"{self.domain}__{parts[0]}"
return f"{self.domain}__root"
def to_file_pattern(self) -> str:
"""Map URI to file glob pattern for search."""
# Escape glob special characters in path
escaped = self.path.replace("[", "[[]").replace("*", "[*]").replace("?", "[?]")
return f"**/{escaped}*"
def __str__(self) -> str:
return self.raw
def __eq__(self, other):
return isinstance(other, URI) and self.domain == other.domain and self.path == other.path
def __hash__(self):
return hash((self.domain, self.path))
class URIRouter:
"""Maps URIs to collections and provides structured access."""
DOMAIN_DOCS = {
"core": "Core documentation - primary source of truth",
"ref": "Reference materials - API docs, specs",
"guide": "Guides and tutorials",
"lib": "Library documentation",
"src": "Source code annotations",
"test": "Test documentation",
"note": "Personal notes and annotations",
}
def __init__(self, storage):
self.storage = storage
def resolve(self, uri_str: str) -> dict:
"""Resolve a URI to its collection and metadata."""
uri = URI.parse(uri_str)
collection = uri.to_collection()
return {
"uri": str(uri),
"domain": uri.domain,
"path": uri.path,
"collection": collection,
"domain_doc": self.DOMAIN_DOCS.get(uri.domain, ""),
}
def search_by_uri(self, uri_str: str, limit: int = 10) -> list[dict]:
"""Search within a URI scope. Supports wildcards: core://fastapi/*"""
uri = URI.parse(uri_str)
collection = uri.to_collection()
# Check for wildcard pattern (core://fastapi/* or core://*)
if uri.path.endswith("/*") or uri.path == "*":
# Wildcard: search all collections in domain
prefix = f"{uri.domain}__"
result = self.storage.list_collections()
if result.is_err:
return []
collections = [c["name"] for c in result.value if c.get("name", "").startswith(prefix)]
if not collections:
return []
# Extract search term from path (e.g., core://fastapi/* → "fastapi")
search_term = uri.path.rstrip("/*").split("/")[-1] if "/" in uri.path else ""
if not search_term:
# No search term — single query across all domain collections
query_words = [c.split("__")[-1] for c in collections if "__" in c]
result = self.storage.search(
query=" ".join(query_words) if query_words else "",
collections=collections,
limit=limit,
)
return result.value if result.is_ok else []
result = self.storage.search(
query=search_term,
collections=collections,
limit=limit,
)
return result.value if result.is_ok else []
result = self.storage.search(
query=uri.path.split("/")[-1],
collections=[collection],
limit=limit,
)
return result.value if result.is_ok else []
def list_by_domain(self, domain: str) -> list[dict]:
"""List all URIs in a domain."""
if domain not in VALID_DOMAINS:
return [{"error": f"Unknown domain: {domain}"}]
result = self.storage.list_collections()
if result.is_err:
return []
collections = result.value
prefix = f"{domain}__"
return [
{"collection": c["name"], "uri": f"{domain}://{c['name'].replace(prefix, '')}"}
for c in collections
if c.get("name", "").startswith(prefix)
]
def list_all_domains(self) -> dict:
"""List all domains with their collection counts."""
result = self.storage.list_collections()
if result.is_err:
return {}
collections = result.value
domains: dict[str, int] = {}
for c in collections:
name = c.get("name", "")
if "__" in name:
domain = name.split("__")[0]
if domain in VALID_DOMAINS:
domains[domain] = domains.get(domain, 0) + 1
return {d: {"count": domains.get(d, 0), "doc": self.DOMAIN_DOCS.get(d, "")} for d in VALID_DOMAINS}