Source code for pyfileconf.io.file.load.parsers.kwargs

import ast

[docs]class KeywordExtractor(ast.NodeVisitor):
[docs] def __init__(self): self.kwargs = {}
[docs] def visit_keyword(self, node): self.kwargs[node.arg] = node.value self.generic_visit(node)
[docs]def extract_keywords_from_ast(node: ast.AST) -> dict: ke = KeywordExtractor() ke.visit(node) return ke.kwargs
[docs]class KeywordByNameExtractor(KeywordExtractor):
[docs] def __init__(self, name: str): self.name = name super().__init__()
[docs] def visit_keyword(self, node): if node.arg == self.name: super().visit_keyword(node)
[docs]def extract_keywords_from_ast_by_name(node: ast.AST, name: str) -> dict: ke = KeywordByNameExtractor(name) ke.visit(node) return ke.kwargs