Navegador

Source Blame History 490 lines
b45288f… lmata 1 """
b45288f… lmata 2 HCL/Terraform parser — extracts resources, data sources, providers,
b45288f… lmata 3 variables, outputs, modules, and locals from .tf files using tree-sitter.
b45288f… lmata 4 """
b45288f… lmata 5
b45288f… lmata 6 import logging
b45288f… lmata 7 import re
b45288f… lmata 8 from pathlib import Path
b45288f… lmata 9
b45288f… lmata 10 from navegador.graph.schema import EdgeType, NodeLabel
b45288f… lmata 11 from navegador.graph.store import GraphStore
b45288f… lmata 12 from navegador.ingestion.parser import LanguageParser
b45288f… lmata 13
b45288f… lmata 14 logger = logging.getLogger(__name__)
b45288f… lmata 15
b45288f… lmata 16 # Patterns for reference extraction from expression text
b45288f… lmata 17 _VAR_REF = re.compile(r"\bvar\.(\w+)")
b45288f… lmata 18 _LOCAL_REF = re.compile(r"\blocal\.(\w+)")
b45288f… lmata 19 _MODULE_REF = re.compile(r"\bmodule\.(\w+)")
b45288f… lmata 20 _DATA_REF = re.compile(r"\bdata\.(\w+)\.(\w+)")
b45288f… lmata 21 _RESOURCE_REF = re.compile(
b45288f… lmata 22 r"(?<!\bdata\.)" # exclude data.resource_type references (handled by _DATA_REF)
b45288f… lmata 23 r"\b(aws_\w+|google_\w+|azurerm_\w+|azuread_\w+|oci_\w+|digitalocean_\w+"
b45288f… lmata 24 r"|cloudflare_\w+|helm_\w+|kubernetes_\w+|null_\w+|random_\w+"
b45288f… lmata 25 r"|local_\w+|tls_\w+|template_\w+|archive_\w+|external_\w+)\.(\w+)"
b45288f… lmata 26 )
b45288f… lmata 27
b45288f… lmata 28
b45288f… lmata 29 def _get_hcl_language():
b45288f… lmata 30 try:
b45288f… lmata 31 import tree_sitter_hcl as tshcl # type: ignore[import]
b45288f… lmata 32 from tree_sitter import Language
b45288f… lmata 33
b45288f… lmata 34 return Language(tshcl.language())
b45288f… lmata 35 except ImportError as e:
b45288f… lmata 36 raise ImportError("Install tree-sitter-hcl: pip install tree-sitter-hcl") from e
b45288f… lmata 37
b45288f… lmata 38
b45288f… lmata 39 def _node_text(node, source: bytes) -> str:
b45288f… lmata 40 return source[node.start_byte : node.end_byte].decode("utf-8", errors="replace")
b45288f… lmata 41
b45288f… lmata 42
b45288f… lmata 43 def _string_lit_text(node, source: bytes) -> str:
b45288f… lmata 44 """Extract the inner text from a string_lit node (strips quotes)."""
b45288f… lmata 45 for child in node.children:
b45288f… lmata 46 if child.type == "template_literal":
b45288f… lmata 47 return _node_text(child, source)
b45288f… lmata 48 # Fallback: strip surrounding quotes from the full text
b45288f… lmata 49 text = _node_text(node, source)
b45288f… lmata 50 return text.strip('"').strip("'")
b45288f… lmata 51
b45288f… lmata 52
b45288f… lmata 53 class HCLParser(LanguageParser):
b45288f… lmata 54 """Parses HCL/Terraform files into the navegador graph."""
b45288f… lmata 55
b45288f… lmata 56 def __init__(self) -> None:
b45288f… lmata 57 from tree_sitter import Parser # type: ignore[import]
b45288f… lmata 58
b45288f… lmata 59 self._parser = Parser(_get_hcl_language())
b45288f… lmata 60
b45288f… lmata 61 def parse_file(self, path: Path, repo_root: Path, store: GraphStore) -> dict[str, int]:
b45288f… lmata 62 source = path.read_bytes()
b45288f… lmata 63 tree = self._parser.parse(source)
b45288f… lmata 64 rel_path = str(path.relative_to(repo_root))
b45288f… lmata 65
b45288f… lmata 66 store.create_node(
b45288f… lmata 67 NodeLabel.File,
b45288f… lmata 68 {
b45288f… lmata 69 "name": path.name,
b45288f… lmata 70 "path": rel_path,
b45288f… lmata 71 "language": "hcl",
b45288f… lmata 72 "line_count": source.count(b"\n"),
b45288f… lmata 73 },
b45288f… lmata 74 )
b45288f… lmata 75
b45288f… lmata 76 stats = {"functions": 0, "classes": 0, "edges": 0}
b45288f… lmata 77 self._walk(tree.root_node, source, rel_path, store, stats)
b45288f… lmata 78 return stats
b45288f… lmata 79
b45288f… lmata 80 # ── AST walker ────────────────────────────────────────────────────────────
b45288f… lmata 81
b45288f… lmata 82 def _walk(self, node, source: bytes, file_path: str, store: GraphStore, stats: dict) -> None:
b45288f… lmata 83 """Walk the top-level body looking for block nodes."""
b45288f… lmata 84 for child in node.children:
b45288f… lmata 85 if child.type == "body":
b45288f… lmata 86 for body_child in child.children:
b45288f… lmata 87 if body_child.type == "block":
b45288f… lmata 88 self._handle_block(body_child, source, file_path, store, stats)
b45288f… lmata 89 elif child.type == "block":
b45288f… lmata 90 self._handle_block(child, source, file_path, store, stats)
b45288f… lmata 91
b45288f… lmata 92 def _handle_block(
b45288f… lmata 93 self, node, source: bytes, file_path: str, store: GraphStore, stats: dict
b45288f… lmata 94 ) -> None:
b45288f… lmata 95 """Dispatch a block based on its block-type identifier."""
b45288f… lmata 96 block_type = None
b45288f… lmata 97 labels: list[str] = []
b45288f… lmata 98 body_node = None
b45288f… lmata 99
b45288f… lmata 100 for child in node.children:
b45288f… lmata 101 if child.type == "identifier" and block_type is None:
b45288f… lmata 102 block_type = _node_text(child, source)
b45288f… lmata 103 elif child.type == "string_lit":
b45288f… lmata 104 labels.append(_string_lit_text(child, source))
b45288f… lmata 105 elif child.type == "body":
b45288f… lmata 106 body_node = child
b45288f… lmata 107
b45288f… lmata 108 if not block_type:
b45288f… lmata 109 return
b45288f… lmata 110
b45288f… lmata 111 if block_type == "resource" and len(labels) >= 2:
b45288f… lmata 112 self._handle_resource(node, source, file_path, store, stats, labels, body_node)
b45288f… lmata 113 elif block_type == "data" and len(labels) >= 2:
b45288f… lmata 114 self._handle_data(node, source, file_path, store, stats, labels, body_node)
b45288f… lmata 115 elif block_type == "provider" and len(labels) >= 1:
b45288f… lmata 116 self._handle_provider(node, source, file_path, store, stats, labels, body_node)
b45288f… lmata 117 elif block_type == "variable" and len(labels) >= 1:
b45288f… lmata 118 self._handle_variable(node, source, file_path, store, stats, labels, body_node)
b45288f… lmata 119 elif block_type == "output" and len(labels) >= 1:
b45288f… lmata 120 self._handle_output(node, source, file_path, store, stats, labels, body_node)
b45288f… lmata 121 elif block_type == "module" and len(labels) >= 1:
b45288f… lmata 122 self._handle_module(node, source, file_path, store, stats, labels, body_node)
b45288f… lmata 123 elif block_type == "locals":
b45288f… lmata 124 self._handle_locals(node, source, file_path, store, stats, body_node)
b45288f… lmata 125 elif block_type == "terraform":
b45288f… lmata 126 pass # Configuration block, skip
b45288f… lmata 127 else:
b45288f… lmata 128 logger.debug("Skipping unknown HCL block type: %s", block_type)
b45288f… lmata 129
b45288f… lmata 130 # ── Handlers ──────────────────────────────────────────────────────────────
b45288f… lmata 131
b45288f… lmata 132 def _handle_resource(
b45288f… lmata 133 self,
b45288f… lmata 134 node,
b45288f… lmata 135 source: bytes,
b45288f… lmata 136 file_path: str,
b45288f… lmata 137 store: GraphStore,
b45288f… lmata 138 stats: dict,
b45288f… lmata 139 labels: list[str],
b45288f… lmata 140 body_node,
b45288f… lmata 141 ) -> None:
b45288f… lmata 142 name = f"{labels[0]}.{labels[1]}"
b45288f… lmata 143 store.create_node(
b45288f… lmata 144 NodeLabel.Class,
b45288f… lmata 145 {
b45288f… lmata 146 "name": name,
b45288f… lmata 147 "file_path": file_path,
b45288f… lmata 148 "line_start": node.start_point[0] + 1,
b45288f… lmata 149 "line_end": node.end_point[0] + 1,
b45288f… lmata 150 "docstring": "",
b45288f… lmata 151 "semantic_type": "terraform_resource",
b45288f… lmata 152 },
b45288f… lmata 153 )
b45288f… lmata 154 store.create_edge(
b45288f… lmata 155 NodeLabel.File,
b45288f… lmata 156 {"path": file_path},
b45288f… lmata 157 EdgeType.CONTAINS,
b45288f… lmata 158 NodeLabel.Class,
b45288f… lmata 159 {"name": name, "file_path": file_path},
b45288f… lmata 160 )
b45288f… lmata 161 stats["classes"] += 1
b45288f… lmata 162 stats["edges"] += 1
b45288f… lmata 163
b45288f… lmata 164 if body_node:
b45288f… lmata 165 self._extract_references(
b45288f… lmata 166 body_node, source, file_path, name, NodeLabel.Class, store, stats
b45288f… lmata 167 )
b45288f… lmata 168
b45288f… lmata 169 def _handle_data(
b45288f… lmata 170 self,
b45288f… lmata 171 node,
b45288f… lmata 172 source: bytes,
b45288f… lmata 173 file_path: str,
b45288f… lmata 174 store: GraphStore,
b45288f… lmata 175 stats: dict,
b45288f… lmata 176 labels: list[str],
b45288f… lmata 177 body_node,
b45288f… lmata 178 ) -> None:
b45288f… lmata 179 name = f"{labels[0]}.{labels[1]}"
b45288f… lmata 180 store.create_node(
b45288f… lmata 181 NodeLabel.Class,
b45288f… lmata 182 {
b45288f… lmata 183 "name": name,
b45288f… lmata 184 "file_path": file_path,
b45288f… lmata 185 "line_start": node.start_point[0] + 1,
b45288f… lmata 186 "line_end": node.end_point[0] + 1,
b45288f… lmata 187 "docstring": "",
b45288f… lmata 188 "semantic_type": "terraform_data",
b45288f… lmata 189 },
b45288f… lmata 190 )
b45288f… lmata 191 store.create_edge(
b45288f… lmata 192 NodeLabel.File,
b45288f… lmata 193 {"path": file_path},
b45288f… lmata 194 EdgeType.CONTAINS,
b45288f… lmata 195 NodeLabel.Class,
b45288f… lmata 196 {"name": name, "file_path": file_path},
b45288f… lmata 197 )
b45288f… lmata 198 stats["classes"] += 1
b45288f… lmata 199 stats["edges"] += 1
b45288f… lmata 200
b45288f… lmata 201 if body_node:
b45288f… lmata 202 self._extract_references(
b45288f… lmata 203 body_node, source, file_path, name, NodeLabel.Class, store, stats
b45288f… lmata 204 )
b45288f… lmata 205
b45288f… lmata 206 def _handle_provider(
b45288f… lmata 207 self,
b45288f… lmata 208 node,
b45288f… lmata 209 source: bytes,
b45288f… lmata 210 file_path: str,
b45288f… lmata 211 store: GraphStore,
b45288f… lmata 212 stats: dict,
b45288f… lmata 213 labels: list[str],
b45288f… lmata 214 body_node,
b45288f… lmata 215 ) -> None:
b45288f… lmata 216 name = labels[0]
b45288f… lmata 217 store.create_node(
b45288f… lmata 218 NodeLabel.Class,
b45288f… lmata 219 {
b45288f… lmata 220 "name": name,
b45288f… lmata 221 "file_path": file_path,
b45288f… lmata 222 "line_start": node.start_point[0] + 1,
b45288f… lmata 223 "line_end": node.end_point[0] + 1,
b45288f… lmata 224 "docstring": "",
b45288f… lmata 225 "semantic_type": "terraform_provider",
b45288f… lmata 226 },
b45288f… lmata 227 )
b45288f… lmata 228 store.create_edge(
b45288f… lmata 229 NodeLabel.File,
b45288f… lmata 230 {"path": file_path},
b45288f… lmata 231 EdgeType.CONTAINS,
b45288f… lmata 232 NodeLabel.Class,
b45288f… lmata 233 {"name": name, "file_path": file_path},
b45288f… lmata 234 )
b45288f… lmata 235 stats["classes"] += 1
b45288f… lmata 236 stats["edges"] += 1
b45288f… lmata 237
b45288f… lmata 238 if body_node:
b45288f… lmata 239 self._extract_references(
b45288f… lmata 240 body_node, source, file_path, name, NodeLabel.Class, store, stats
b45288f… lmata 241 )
b45288f… lmata 242
b45288f… lmata 243 def _handle_variable(
b45288f… lmata 244 self,
b45288f… lmata 245 node,
b45288f… lmata 246 source: bytes,
b45288f… lmata 247 file_path: str,
b45288f… lmata 248 store: GraphStore,
b45288f… lmata 249 stats: dict,
b45288f… lmata 250 labels: list[str],
b45288f… lmata 251 body_node,
b45288f… lmata 252 ) -> None:
b45288f… lmata 253 name = labels[0]
b45288f… lmata 254 store.create_node(
b45288f… lmata 255 NodeLabel.Variable,
b45288f… lmata 256 {
b45288f… lmata 257 "name": name,
b45288f… lmata 258 "file_path": file_path,
b45288f… lmata 259 "line_start": node.start_point[0] + 1,
b45288f… lmata 260 "line_end": node.end_point[0] + 1,
b45288f… lmata 261 "semantic_type": "terraform_variable",
b45288f… lmata 262 },
b45288f… lmata 263 )
b45288f… lmata 264 store.create_edge(
b45288f… lmata 265 NodeLabel.File,
b45288f… lmata 266 {"path": file_path},
b45288f… lmata 267 EdgeType.CONTAINS,
b45288f… lmata 268 NodeLabel.Variable,
b45288f… lmata 269 {"name": name, "file_path": file_path},
b45288f… lmata 270 )
b45288f… lmata 271 stats["functions"] += 1
b45288f… lmata 272 stats["edges"] += 1
b45288f… lmata 273
b45288f… lmata 274 def _handle_output(
b45288f… lmata 275 self,
b45288f… lmata 276 node,
b45288f… lmata 277 source: bytes,
b45288f… lmata 278 file_path: str,
b45288f… lmata 279 store: GraphStore,
b45288f… lmata 280 stats: dict,
b45288f… lmata 281 labels: list[str],
b45288f… lmata 282 body_node,
b45288f… lmata 283 ) -> None:
b45288f… lmata 284 name = labels[0]
b45288f… lmata 285 store.create_node(
b45288f… lmata 286 NodeLabel.Variable,
b45288f… lmata 287 {
b45288f… lmata 288 "name": name,
b45288f… lmata 289 "file_path": file_path,
b45288f… lmata 290 "line_start": node.start_point[0] + 1,
b45288f… lmata 291 "line_end": node.end_point[0] + 1,
b45288f… lmata 292 "semantic_type": "terraform_output",
b45288f… lmata 293 },
b45288f… lmata 294 )
b45288f… lmata 295 store.create_edge(
b45288f… lmata 296 NodeLabel.File,
b45288f… lmata 297 {"path": file_path},
b45288f… lmata 298 EdgeType.CONTAINS,
b45288f… lmata 299 NodeLabel.Variable,
b45288f… lmata 300 {"name": name, "file_path": file_path},
b45288f… lmata 301 )
b45288f… lmata 302 stats["functions"] += 1
b45288f… lmata 303 stats["edges"] += 1
b45288f… lmata 304
b45288f… lmata 305 if body_node:
b45288f… lmata 306 self._extract_references(
b45288f… lmata 307 body_node, source, file_path, name, NodeLabel.Variable, store, stats
b45288f… lmata 308 )
b45288f… lmata 309
b45288f… lmata 310 def _handle_module(
b45288f… lmata 311 self,
b45288f… lmata 312 node,
b45288f… lmata 313 source: bytes,
b45288f… lmata 314 file_path: str,
b45288f… lmata 315 store: GraphStore,
b45288f… lmata 316 stats: dict,
b45288f… lmata 317 labels: list[str],
b45288f… lmata 318 body_node,
b45288f… lmata 319 ) -> None:
b45288f… lmata 320 name = labels[0]
b45288f… lmata 321 source_attr = ""
b45288f… lmata 322 if body_node:
b45288f… lmata 323 source_attr = self._get_attribute_value(body_node, "source", source)
b45288f… lmata 324
b45288f… lmata 325 store.create_node(
b45288f… lmata 326 NodeLabel.Module,
b45288f… lmata 327 {
b45288f… lmata 328 "name": name,
b45288f… lmata 329 "file_path": file_path,
b45288f… lmata 330 "line_start": node.start_point[0] + 1,
b45288f… lmata 331 "line_end": node.end_point[0] + 1,
b45288f… lmata 332 "semantic_type": "terraform_module",
b45288f… lmata 333 "source": source_attr,
b45288f… lmata 334 },
b45288f… lmata 335 )
b45288f… lmata 336 store.create_edge(
b45288f… lmata 337 NodeLabel.File,
b45288f… lmata 338 {"path": file_path},
b45288f… lmata 339 EdgeType.CONTAINS,
b45288f… lmata 340 NodeLabel.Module,
b45288f… lmata 341 {"name": name, "file_path": file_path},
b45288f… lmata 342 )
b45288f… lmata 343 stats["classes"] += 1
b45288f… lmata 344 stats["edges"] += 1
b45288f… lmata 345
b45288f… lmata 346 if body_node:
b45288f… lmata 347 self._extract_references(
b45288f… lmata 348 body_node, source, file_path, name, NodeLabel.Module, store, stats
b45288f… lmata 349 )
b45288f… lmata 350
b45288f… lmata 351 def _handle_locals(
b45288f… lmata 352 self,
b45288f… lmata 353 node,
b45288f… lmata 354 source: bytes,
b45288f… lmata 355 file_path: str,
b45288f… lmata 356 store: GraphStore,
b45288f… lmata 357 stats: dict,
b45288f… lmata 358 body_node,
b45288f… lmata 359 ) -> None:
b45288f… lmata 360 if not body_node:
b45288f… lmata 361 return
b45288f… lmata 362
b45288f… lmata 363 for child in body_node.children:
b45288f… lmata 364 if child.type == "attribute":
b45288f… lmata 365 attr_name = None
b45288f… lmata 366 for attr_child in child.children:
b45288f… lmata 367 if attr_child.type == "identifier":
b45288f… lmata 368 attr_name = _node_text(attr_child, source)
b45288f… lmata 369 break
b45288f… lmata 370
b45288f… lmata 371 if not attr_name:
b45288f… lmata 372 continue
b45288f… lmata 373
b45288f… lmata 374 store.create_node(
b45288f… lmata 375 NodeLabel.Variable,
b45288f… lmata 376 {
b45288f… lmata 377 "name": attr_name,
b45288f… lmata 378 "file_path": file_path,
b45288f… lmata 379 "line_start": child.start_point[0] + 1,
b45288f… lmata 380 "line_end": child.end_point[0] + 1,
b45288f… lmata 381 "semantic_type": "terraform_local",
b45288f… lmata 382 },
b45288f… lmata 383 )
b45288f… lmata 384 store.create_edge(
b45288f… lmata 385 NodeLabel.File,
b45288f… lmata 386 {"path": file_path},
b45288f… lmata 387 EdgeType.CONTAINS,
b45288f… lmata 388 NodeLabel.Variable,
b45288f… lmata 389 {"name": attr_name, "file_path": file_path},
b45288f… lmata 390 )
b45288f… lmata 391 stats["functions"] += 1
b45288f… lmata 392 stats["edges"] += 1
b45288f… lmata 393
b45288f… lmata 394 # Extract references from the attribute expression
b45288f… lmata 395 self._extract_references(
b45288f… lmata 396 child, source, file_path, attr_name, NodeLabel.Variable, store, stats
b45288f… lmata 397 )
b45288f… lmata 398
b45288f… lmata 399 # ── Reference extraction ──────────────────────────────────────────────────
b45288f… lmata 400
b45288f… lmata 401 def _extract_references(
b45288f… lmata 402 self,
b45288f… lmata 403 node,
b45288f… lmata 404 source: bytes,
b45288f… lmata 405 file_path: str,
b45288f… lmata 406 from_name: str,
b45288f… lmata 407 from_label: str,
b45288f… lmata 408 store: GraphStore,
b45288f… lmata 409 stats: dict,
b45288f… lmata 410 ) -> None:
b45288f… lmata 411 """Scan expression text for var.X, local.X, module.X, data.T.N, and resource references."""
b45288f… lmata 412 text = _node_text(node, source)
b45288f… lmata 413
b45288f… lmata 414 # var.xxx → REFERENCES edge to terraform_variable
b45288f… lmata 415 for match in _VAR_REF.finditer(text):
b45288f… lmata 416 var_name = match.group(1)
b45288f… lmata 417 store.create_edge(
b45288f… lmata 418 from_label,
b45288f… lmata 419 {"name": from_name, "file_path": file_path},
b45288f… lmata 420 EdgeType.REFERENCES,
b45288f… lmata 421 NodeLabel.Variable,
b45288f… lmata 422 {"name": var_name, "file_path": file_path},
b45288f… lmata 423 )
b45288f… lmata 424 stats["edges"] += 1
b45288f… lmata 425
b45288f… lmata 426 # local.xxx → REFERENCES edge to terraform_local
b45288f… lmata 427 for match in _LOCAL_REF.finditer(text):
b45288f… lmata 428 local_name = match.group(1)
b45288f… lmata 429 store.create_edge(
b45288f… lmata 430 from_label,
b45288f… lmata 431 {"name": from_name, "file_path": file_path},
b45288f… lmata 432 EdgeType.REFERENCES,
b45288f… lmata 433 NodeLabel.Variable,
b45288f… lmata 434 {"name": local_name, "file_path": file_path},
b45288f… lmata 435 )
b45288f… lmata 436 stats["edges"] += 1
b45288f… lmata 437
b45288f… lmata 438 # module.xxx → REFERENCES edge to terraform_module
b45288f… lmata 439 for match in _MODULE_REF.finditer(text):
b45288f… lmata 440 mod_name = match.group(1)
b45288f… lmata 441 store.create_edge(
b45288f… lmata 442 from_label,
b45288f… lmata 443 {"name": from_name, "file_path": file_path},
b45288f… lmata 444 EdgeType.REFERENCES,
b45288f… lmata 445 NodeLabel.Module,
b45288f… lmata 446 {"name": mod_name, "file_path": file_path},
b45288f… lmata 447 )
b45288f… lmata 448 stats["edges"] += 1
b45288f… lmata 449
b45288f… lmata 450 # data.type.name → DEPENDS_ON edge to terraform_data
b45288f… lmata 451 for match in _DATA_REF.finditer(text):
b45288f… lmata 452 data_name = f"{match.group(1)}.{match.group(2)}"
b45288f… lmata 453 store.create_edge(
b45288f… lmata 454 from_label,
b45288f… lmata 455 {"name": from_name, "file_path": file_path},
b45288f… lmata 456 EdgeType.DEPENDS_ON,
b45288f… lmata 457 NodeLabel.Class,
b45288f… lmata 458 {"name": data_name, "file_path": file_path},
b45288f… lmata 459 )
b45288f… lmata 460 stats["edges"] += 1
b45288f… lmata 461
b45288f… lmata 462 # resource_type.resource_name → DEPENDS_ON edge to terraform_resource
b45288f… lmata 463 for match in _RESOURCE_REF.finditer(text):
b45288f… lmata 464 resource_name = f"{match.group(1)}.{match.group(2)}"
b45288f… lmata 465 store.create_edge(
b45288f… lmata 466 from_label,
b45288f… lmata 467 {"name": from_name, "file_path": file_path},
b45288f… lmata 468 EdgeType.DEPENDS_ON,
b45288f… lmata 469 NodeLabel.Class,
b45288f… lmata 470 {"name": resource_name, "file_path": file_path},
b45288f… lmata 471 )
b45288f… lmata 472 stats["edges"] += 1
b45288f… lmata 473
b45288f… lmata 474 # ── Helpers ───────────────────────────────────────────────────────────────
b45288f… lmata 475
b45288f… lmata 476 def _get_attribute_value(self, body_node, attr_name: str, source: bytes) -> str:
b45288f… lmata 477 """Extract the string value of a named attribute from a body node."""
b45288f… lmata 478 for child in body_node.children:
b45288f… lmata 479 if child.type == "attribute":
b45288f… lmata 480 ident = None
b45288f… lmata 481 expr = None
b45288f… lmata 482 for attr_child in child.children:
b45288f… lmata 483 if attr_child.type == "identifier":
b45288f… lmata 484 ident = _node_text(attr_child, source)
b45288f… lmata 485 elif attr_child.type == "expression" or attr_child.is_named:
b45288f… lmata 486 expr = attr_child
b45288f… lmata 487 if ident == attr_name and expr is not None:
b45288f… lmata 488 text = _node_text(expr, source).strip().strip('"').strip("'")
b45288f… lmata 489 return text
b45288f… lmata 490 return ""

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button