PyArchGraph Package guides

Package 03 / pyarchgraph.adapters

Adapters

Connects analysis to the command line, files, Python parsing, and graph-library operations.

7 modules18 classesWalkthrough + class index

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.

The same two-file example in all three guides
# demo/orders.py
import inventory
# demo/inventory.py
import orders
  1. Turn CLI choices into a request

    CliApplication reads roots, exclusions, gate, detail level, optional package depth, and an optional config path. TomlOptionsReader translates an explicitly selected TOML file and CLI choices into AnalysisOptions. The CLI calls the ProjectAnalyzer port.

  2. Discover the files

    When the application requests it, FileSystemProjectAccess locates roots and supplies packaging clues. FileSystemSourceDiscovery inventories sources, exclusions, bindings, and target declarations. It recognises ordinary package/module precedence within a root.

  3. Read imports without running the project

    AstImportFactSource coordinates SourceReader, PythonAstParser, and ModuleImportExtractor. The two statements become two ImportFactDraft values, with locations and context. The application later assigns final fact identities.

  4. Supply the graph operations

    During view analysis, NetworkXGraphAlgorithms prepares graphs and returns NetworkXGraphHandle objects. 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.

  5. Present the result

    JsonReportRenderer turns the returned report into JSON for standard output. CliExitCodePolicy sees complete coverage and a gate error finding, so this example exits with 1.

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.

Core contracts and concrete adapters
ContractAdapter relationship
ProjectAnalyzerThe CLI calls this application input port; AnalyseProject implements it.
ProjectAccessFileSystemProjectAccess supplies locations and metadata observations.
SourceDiscoveryFileSystemSourceDiscovery supplies an inventory for a root.
ImportFactSourceAstImportFactSource supplies import drafts and collection diagnostics.
GraphAlgorithms / GraphHandleThe 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.

CLI outcomes
Exit codeMeaning
0Complete analysis; no error findings in the gate.
1Complete analysis; the gate has error findings.
2Incomplete 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.

Where each responsibility lives
ModuleResponsibility
driving/cli/application.pyCommand-line arguments, analysis invocation, and exit-code policy.
driving/cli/configuration.pyExplicit TOML configuration translated into analysis options.
driving/cli/rendering.pyAnalysis reports presented as JSON.
driven/filesystem/project.pyReal project locations and literal packaging metadata.
driven/filesystem/discovery.pySource inventory, exclusions, bindings, and target declarations.
driven/python_ast.pySource reading and parsing into import fact drafts.
driven/networkx_graph.pyNetworkX 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.