Visitor Module

cli_command_parser.conversion.visitor.scoped(func)[source]
class cli_command_parser.conversion.visitor.ScopedVisit[source]

Bases: object

class cli_command_parser.conversion.visitor.ScriptVisitor(smart_loop_handling: bool = True, track_refs: Collection[TrackedRef] = ())[source]

Bases: NodeVisitor

visit_FunctionDef
visit_AsyncFunctionDef
visit_Lambda
visit_ClassDef
visit_While
scopes: ChainMap[str, TrackedValue]
track_callable(module: str, name: str, cb: Callable)[source]
track_refs_to(ref: TrackedRef)[source]

Register a reference that should be tracked.

get_tracked_refs() TrackedRefMap[source]
visit_Import(node: Import)[source]

Processes a import some_module or import some_module as other_name statement.

Parameters:

node – The AST object representing the import statement.

visit_ImportFrom(node: ImportFrom)[source]

Processes a from module import names statement. If the module name matches one from which members were registered to be tracked, then the imported names (and any as aliases) are processed. Members with canonical names that match an item that was registered to be tracked are added to the current scope / variable namespace.

Relative module imports are handled fuzzily - no attempt is made to determine the fully qualified module name for the source file or to resolve what the relative import’s fully qualified module name would be. This may result in incorrect items being tracked if the name matched a tracked name in the matched tracked module.

visit_For(node: For)[source]

Visit a for loop.

visit_AsyncFor(node: For)

Visit a for loop.

resolve_ref(name: RefName, only_visitable: Literal[False] = False) VisitFunc | AstCallable | None[source]
resolve_ref(name: RefName, only_visitable: Literal[True]) VisitFunc | None

Resolve the given reference to a tracked item in the current scope.

Parameters:
  • name – The name of a reference or an AST node that may contain the name of a reference

  • only_visitable – If True, then only return visit functions or None, otherwise include AstCallable objects

Returns:

The resolved reference

visit_withitem(item: withitem)[source]

Visit a single withitem / context expression within a with ...: statement that may include one or more ``withitem``s / content expressions.

visit_Assign(node: Assign)[source]

Visit an assignment statement where one or more variables (stored in Assign.targets) are being assigned one or more values (stored in Assign.value).

visit_Call(node: Call) AstCallable | _NoCallType[source]
class cli_command_parser.conversion.visitor.TrackedRef(name: str)[source]

Bases: object

Represents any class/function/object/variable/etc. that may be imported from a specific module with a specific name in that module. Used to track references to that item.

module
name
cli_command_parser.conversion.visitor.imp_names(imp: Import | ImportFrom) Iterator[tuple[str, str]][source]