Package 03 / pyarchgraph.adapters
Adapters
Connects analysis to the command line, files, Python parsing, and graph-library operations.
Its role
An adapter connects a core contract to an interface or technology. A driving adapter starts the workflow; a driven adapter provides a capability the core asks for.
Start and present
The CLI turns command-line choices into a request and presents the report.
Observe source
Filesystem and Python parsing components provide source and import observations.
Query graphs
The NetworkX component supplies graph operations through domain contracts.
The CLI, filesystem, Python AST, and NetworkX components remain independent. Helpers inside one component may work together. main.ApplicationFactory, outside these packages, connects the concrete parts.
Follow a CLI run
Run pyarchgraph demo from the parent of this example directory.
# demo/orders.py
import inventory# demo/inventory.py
import ordersTurn CLI choices into a request
CliApplicationreads roots, exclusions, gate, detail level, optional package depth, and an optional config path.TomlOptionsReadertranslates an explicitly selected TOML file and CLI choices intoAnalysisOptions. The CLI calls theProjectAnalyzerport.Discover the files
When the application requests it,
FileSystemProjectAccesslocates roots and supplies packaging clues.FileSystemSourceDiscoveryinventories sources, exclusions, bindings, and target declarations. It recognises ordinary package/module precedence within a root.Read imports without running the project
AstImportFactSourcecoordinatesSourceReader,PythonAstParser, andModuleImportExtractor. The two statements become twoImportFactDraftvalues, with locations and context. The application later assigns final fact identities.Supply the graph operations
During view analysis,
NetworkXGraphAlgorithmsprepares graphs and returnsNetworkXGraphHandleobjects. These find groups whose nodes can all reach one another and a witness cycle when asked. Domain code decides what that cycle means and which evidence supports it.Present the result
JsonReportRendererturns the returned report into JSON for standard output.CliExitCodePolicysees complete coverage and a gate error finding, so this example exits with1.
Collection reports source read and parse failures as diagnostics, allowing a partial report. Invocation or extension failures are printed to standard error and return exit code 2 without a JSON report.
How parts connect
Package analysis uses the same filesystem, parser and graph adapters. Select a package gate with --gate package-structural, --gate package-non-typing or --gate package-module-body. --package-max-depth N takes a positive integer and caps dotted package names; omitting it keeps immediate packages. The option is carried through AnalysisOptions; TOML remains focused on ownership and target boundaries.
JsonReportRenderer presents schema 0.8 using the same generic serialization. Each view gains dependencies: null when not requested, or an array of all requested relationships with readable source evidence. The three default package views always provide the array, which can be empty. These relationships are independent of --details component-edges, which controls evidence inside cycle findings.
| Contract | Adapter relationship |
|---|---|
ProjectAnalyzer | The CLI calls this application input port; AnalyseProject implements it. |
ProjectAccess | FileSystemProjectAccess supplies locations and metadata observations. |
SourceDiscovery | FileSystemSourceDiscovery supplies an inventory for a root. |
ImportFactSource | AstImportFactSource supplies import drafts and collection diagnostics. |
GraphAlgorithms / GraphHandle | The NetworkX classes supply the domain's graph operations. |
The AST component also defines small local contracts—SourceTextReader, AstParser, and ImportExtractor—for its own collaborators. Syntax-tree details stay inside that component.
The composition root, ApplicationFactory, chooses defaults or accepts supplied collaborators, then builds an analyser or CLI. The small __main__.main entry point starts the CLI.
| Exit code | Meaning |
|---|---|
0 | Complete analysis; no error findings in the gate. |
1 | Complete analysis; the gate has error findings. |
2 | Incomplete analysis, invalid invocation, or extension failure. |
Configuration is read only when selected with --config. Packaging metadata is inspected as data, and analysed source is parsed without execution. The tool does not discover runtime dependencies by importing the project.
Module map
Choose a module to jump to its classes below. Package initialisers are intentionally empty; callers import from the defining modules.
| Module | Responsibility |
|---|---|
driving/cli/application.py | Command-line arguments, analysis invocation, and exit-code policy. |
driving/cli/configuration.py | Explicit TOML configuration translated into analysis options. |
driving/cli/rendering.py | Analysis reports presented as JSON. |
driven/filesystem/project.py | Real project locations and literal packaging metadata. |
driven/filesystem/discovery.py | Source inventory, exclusions, bindings, and target declarations. |
driven/python_ast.py | Source reading and parsing into import fact drafts. |
driven/networkx_graph.py | NetworkX implementations of the domain graph contracts. |
Every class
All 18 classes in this package, including services, contracts, an exception, and internal helpers. Names link to the source file; descriptions match the role summaries in the code.
driving/cli/application.py
Command-line arguments, analysis invocation, and exit-code policy.
CliExitCodePolicy- Translate analysis completeness and gate findings into a CLI exit code.
CliApplication- Run analysis from command-line choices and present the resulting report.
driving/cli/configuration.py
Explicit TOML configuration translated into analysis options.
TomlOptionsReader- Combine CLI choices and explicit TOML configuration into analysis options.
driving/cli/rendering.py
Analysis reports presented as JSON.
JsonReportRenderer- Present an analysis report and any extension findings as JSON.
driven/filesystem/project.py
Real project locations and literal packaging metadata.
FileSystemProjectAccess- Locate source roots and read packaging metadata for analysis.
driven/filesystem/discovery.py
Source inventory, exclusions, bindings, and target declarations.
FileSystemSourceDiscovery- Inventory source modules, import bindings and target declarations on disk.
_DiscoverySessionInternal helper- Gather files, target declarations and diagnostics for one filesystem scan.
driven/python_ast.py
Source reading and parsing into import fact drafts.
SourceTextReader- Provide Python source text for import collection.
AstParser- Provide the syntax tree used to collect imports from Python source.
ImportExtractor- Describe explicit imports and their context as import fact drafts.
NonRegularSourceError- Signal that an input cannot be read as a regular source file.
SourceReader- Read Python source text, following only symlinks to regular files.
PythonAstParser- Expose Python syntax for import collection without executing the source.
_SourceTextInternal helper- Recover source excerpts and character columns for import fact drafts.
ModuleImportExtractor- Describe each explicit import and its context as an import fact draft.
AstImportFactSource- Collect import fact drafts and source diagnostics without executing code.
driven/networkx_graph.py
NetworkX implementations of the domain graph contracts.
NetworkXGraphHandle- Answer cycle queries for one prepared dependency graph using NetworkX.
NetworkXGraphAlgorithms- Prepare dependency graphs for cycle checks using NetworkX.