Python API
This reference is generated from trailhead docstrings and signatures. It covers the stable extension point (custom language adapters) and the core data/query APIs described in the Extending and HTTP API guides.
Application setup
Source code in src/trailhead/server/app.py
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 | |
Language adapters
Bases: ABC
Parse source files of a specific language into a PropertyGraph.
Subclasses must define a class-level extensions frozenset and implement
:meth:parse. Optionally override :meth:is_available to report whether
the required tree-sitter language package is installed.
Source code in src/trailhead/services/indexing/adapters/base.py
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | |
finalize(graph)
Finalize graph-level relationships after all files are parsed.
Most adapters do not need a post-pass. Adapters that resolve cross-file references can override this hook.
Source code in src/trailhead/services/indexing/adapters/base.py
175 176 177 178 179 180 181 | |
is_available()
classmethod
Return True if the required tree-sitter language package is installed.
Source code in src/trailhead/services/indexing/adapters/base.py
154 155 156 157 | |
matches(path)
classmethod
Return True when this adapter should handle path.
Called only after the file extension already matched. Adapters that own an extension outright should keep the default behavior and return True. Adapters sharing a generic extension can override this hook and inspect path shape and/or file contents.
Source code in src/trailhead/services/indexing/adapters/base.py
159 160 161 162 163 164 165 166 167 168 | |
parse(path, graph)
abstractmethod
Parse path, populate graph, and return the module vertex.
Source code in src/trailhead/services/indexing/adapters/base.py
170 171 172 173 | |
Register adapter for all its declared extensions.
Source code in src/trailhead/services/indexing/adapters/registry.py
15 16 17 18 19 20 21 22 23 | |
Property graph
In-memory TinkerPop-compatible property graph.
Source code in src/trailhead/services/indexing/graph.py
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 | |
A graph vertex: a label plus arbitrary key/value properties.
Source code in src/trailhead/services/indexing/graph.py
17 18 19 20 21 22 23 24 25 26 27 | |
A directed, labeled edge between two vertices, with optional properties.
Source code in src/trailhead/services/indexing/graph.py
30 31 32 33 34 35 36 37 38 39 40 41 | |
Indexing
Recursively index all supported source files found under root.
If graph is provided the vertices and edges are added to it in place and the same object is returned; otherwise a fresh PropertyGraph is created.
Source code in src/trailhead/services/indexing/walker.py
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 | |
Querying
Execute a SQL statement against a read-only connection and return column names and rows.
The connection is opened with SQLite's mode=ro URI flag so the engine
itself enforces the read-only constraint — no string-prefix heuristic is
needed or used.
Source code in src/trailhead/services/indexing/query.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 | |
Return the nearest stored vertices to query_text.
By default external import vertices are excluded from results since they
carry no source location and are rarely useful in search output. Pass
exclude_external=False to include them.
If vector is supplied it is used directly and the local embedding model
is not loaded. This allows callers to delegate embedding to a running
trailhead serve instance and avoid the cold-load penalty.
Source code in src/trailhead/services/indexing/query.py
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 | |
Coverage
Replace the stored coverage with the contents of report.
root is the directory the coverage run used for relative paths; it defaults to the directory that holds the report.
Source code in src/trailhead/services/indexing/coverage.py
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 167 168 169 170 171 172 173 | |
Return (format, {absolute path: {line: hits}}) for a coverage report.
Relative paths in the report are resolved against root (for Cobertura,
against each <source> first).
Source code in src/trailhead/services/indexing/coverage.py
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 | |
Embeddings
Source code in src/trailhead/services/embeddings/generator.py
19 20 21 22 23 24 | |
Generate embeddings for multiple texts with a single model load.
Source code in src/trailhead/services/embeddings/generator.py
27 28 29 30 31 32 33 34 35 36 | |