Parse a schema and its imports before generated files are written.
(
file_path: Path,
import_paths: List[Path],
cache: Dict[Path, Schema],
visiting: Set[Path],
)
| 210 | |
| 211 | |
| 212 | def collect_schema_graph( |
| 213 | file_path: Path, |
| 214 | import_paths: List[Path], |
| 215 | cache: Dict[Path, Schema], |
| 216 | visiting: Set[Path], |
| 217 | ) -> Optional[List[Tuple[Path, Schema]]]: |
| 218 | """Parse a schema and its imports before generated files are written.""" |
| 219 | file_path = file_path.resolve() |
| 220 | if file_path in visiting: |
| 221 | print(f"Import error: Circular import detected: {file_path}", file=sys.stderr) |
| 222 | return None |
| 223 | if file_path in cache: |
| 224 | return [] |
| 225 | visiting.add(file_path) |
| 226 | try: |
| 227 | schema = parse_idl_file(file_path) |
| 228 | except OSError as e: |
| 229 | print(f"Error reading {file_path}: {e}", file=sys.stderr) |
| 230 | visiting.remove(file_path) |
| 231 | return None |
| 232 | except (FrontendError, ValueError) as e: |
| 233 | print(f"Error: {e}", file=sys.stderr) |
| 234 | visiting.remove(file_path) |
| 235 | return None |
| 236 | cache[file_path] = schema |
| 237 | entries = [(file_path, schema)] |
| 238 | for imp in schema.imports: |
| 239 | import_path = resolve_import_path(imp.path, file_path, import_paths) |
| 240 | if import_path is None: |
| 241 | searched = [str(file_path.parent)] |
| 242 | searched.extend(str(p) for p in import_paths) |
| 243 | line = imp.location.line if imp.location else imp.line |
| 244 | column = imp.location.column if imp.location else imp.column |
| 245 | print( |
| 246 | f"Import error: Import not found: {imp.path}\n" |
| 247 | f" at line {line}, column {column}\n" |
| 248 | f" Searched in: {', '.join(searched)}", |
| 249 | file=sys.stderr, |
| 250 | ) |
| 251 | visiting.remove(file_path) |
| 252 | return None |
| 253 | imp.resolved_path = str(import_path) |
| 254 | imported = collect_schema_graph(import_path, import_paths, cache, visiting) |
| 255 | if imported is None: |
| 256 | visiting.remove(file_path) |
| 257 | return None |
| 258 | entries.extend(imported) |
| 259 | visiting.remove(file_path) |
| 260 | return entries |
| 261 | |
| 262 | |
| 263 | def validate_kotlin_import_packages(graph: List[Tuple[Path, Schema]]) -> bool: |
no test coverage detected