Parse `` /.graphifyrc``. Returns ``{}`` when absent. Returned keys: ``viz_node_limit``, ``query_budget``, ``query_depth`` (ints), ``demote_docs`` / ``query_demote_docs`` (bools), and ``languages`` (``{".inc": ".php", ...}``) — each present only when the file sets it. Unknown ke
(root: Path)
| 122 | |
| 123 | |
| 124 | def load_graphifyrc(root: Path) -> dict: |
| 125 | """Parse ``<root>/.graphifyrc``. Returns ``{}`` when absent. |
| 126 | |
| 127 | Returned keys: ``viz_node_limit``, ``query_budget``, ``query_depth`` (ints), |
| 128 | ``demote_docs`` / ``query_demote_docs`` (bools), and ``languages`` |
| 129 | (``{".inc": ".php", ...}``) — each present only when the file sets it. |
| 130 | Unknown keys are ignored so a newer graphify's options do not break an |
| 131 | older one. Malformed lines raise ``ValueError`` naming the line. |
| 132 | """ |
| 133 | rc_path = Path(root) / RC_FILENAME |
| 134 | if not rc_path.is_file(): |
| 135 | return {} |
| 136 | |
| 137 | cfg: dict = {} |
| 138 | languages: dict[str, str] = {} |
| 139 | # utf-8-sig: Windows PowerShell 5.1 (`Set-Content -Encoding UTF8`) and older |
| 140 | # Notepad save UTF-8 with a BOM. Read as plain utf-8, the BOM became part of |
| 141 | # the first key, which then matched nothing and was ignored without a word. |
| 142 | content = rc_path.read_text(encoding="utf-8-sig") |
| 143 | for line_num, raw in enumerate(content.splitlines(), 1): |
| 144 | line = raw.strip() |
| 145 | if not line or line.startswith("#"): |
| 146 | continue |
| 147 | if "=" not in line: |
| 148 | raise ValueError(f"Invalid line {line_num} in {rc_path}: {raw!r} (expected key=value)") |
| 149 | key, val = line.split("=", 1) |
| 150 | key = key.strip() |
| 151 | val = val.strip() |
| 152 | if key == "viz_node_limit": |
| 153 | try: |
| 154 | parsed_val = int(val) |
| 155 | if parsed_val < 0: |
| 156 | raise ValueError("must be a non-negative integer") |
| 157 | cfg["viz_node_limit"] = parsed_val |
| 158 | except ValueError as exc: |
| 159 | raise ValueError( |
| 160 | f"Invalid viz_node_limit in {rc_path} at line {line_num}: {val!r}. " |
| 161 | f"Must be a non-negative integer." |
| 162 | ) from exc |
| 163 | elif key in {"query_budget", "query_depth"}: |
| 164 | requirement = "a positive integer" if key == "query_budget" else "an integer from 1 to 6" |
| 165 | try: |
| 166 | parsed_val = int(val) |
| 167 | if parsed_val <= 0 or (key == "query_depth" and parsed_val > 6): |
| 168 | raise ValueError(requirement) |
| 169 | cfg[key] = parsed_val |
| 170 | except ValueError as exc: |
| 171 | raise ValueError( |
| 172 | f"Invalid {key} in {rc_path} at line {line_num}: {val!r}. " |
| 173 | f"Must be {requirement}." |
| 174 | ) from exc |
| 175 | elif key in {"demote_docs", "query_demote_docs"}: |
| 176 | flag = _BOOL_VALUES.get(val.lower()) |
| 177 | if flag is None: |
| 178 | raise ValueError( |
| 179 | f"Invalid {key} in {rc_path} at line {line_num}: {val!r}. " |
| 180 | f"Must be true or false." |
| 181 | ) |