{"id":16243531,"url":"https://github.com/flyte/ast-to-xml","last_synced_at":"2026-03-07T20:01:27.297Z","repository":{"id":149757240,"uuid":"354268414","full_name":"flyte/ast-to-xml","owner":"flyte","description":"Converts a Python abstract source tree (AST) to an XML representation. Uses lxml to enable full XPath searching.","archived":false,"fork":false,"pushed_at":"2025-04-03T13:21:48.000Z","size":20,"stargazers_count":1,"open_issues_count":1,"forks_count":2,"subscribers_count":1,"default_branch":"develop","last_synced_at":"2025-10-07T05:04:27.723Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/flyte.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2021-04-03T10:59:43.000Z","updated_at":"2025-04-03T13:21:53.000Z","dependencies_parsed_at":null,"dependency_job_id":"901f7967-0770-4fe0-a33f-219000e8b34e","html_url":"https://github.com/flyte/ast-to-xml","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/flyte/ast-to-xml","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flyte%2Fast-to-xml","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flyte%2Fast-to-xml/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flyte%2Fast-to-xml/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flyte%2Fast-to-xml/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/flyte","download_url":"https://codeload.github.com/flyte/ast-to-xml/tar.gz/refs/heads/develop","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flyte%2Fast-to-xml/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29988073,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-01T22:42:38.399Z","status":"ssl_error","status_checked_at":"2026-03-01T22:41:51.863Z","response_time":124,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-10T14:15:15.192Z","updated_at":"2026-03-07T20:01:27.277Z","avatar_url":"https://github.com/flyte.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ast-to-xml\n\nConverts a Python abstract source tree (AST) to an XML representation. Uses lxml to enable full XPath searching.\n\n## Installation\n\n```bash\npip install ast-to-xml\n```\n\n## Usage\n\nGiven the following code as `code.py`:\n\n```python\nSOME_CONSTANT = \"some constant\"\n\ndef a_function(arg1, arg2, kwarg1=None):\n    def a_subfunction(arg1, kwarg1=None):\n        print(\"Hello, World!\")\n\nclass SomeClass:\n    def __init__(self, arg1):\n        self.arg1 = arg1\n\n    def a_method(self, arg1, kwarg1=None):\n        pass\n```\n\nParse the code and convert to XML:\n\n```python\nimport ast\n\nfrom ast_to_xml import visit_node\n\n\nwith open(\"code.py\") as code_file:\n    ast_tree = ast.parse(code_file.read())\n\nxml_tree = visit_node(ast_tree)\n```\n\n\u003cdetails\u003e\n  \u003csummary\u003eClick to expand rendered XML\u003c/summary\u003e\n\n```xml\n\u003cModule\u003e\n  \u003cbody\u003e\n    \u003cAssign lineno=\"3\" col_offset=\"0\" end_lineno=\"3\" end_col_offset=\"31\"\u003e\n      \u003ctargets\u003e\n        \u003cName id=\"SOME_CONSTANT\" lineno=\"3\" col_offset=\"0\" end_lineno=\"3\" end_col_offset=\"13\"\u003e\n          \u003cctx\u003e\n            \u003cStore/\u003e\n          \u003c/ctx\u003e\n        \u003c/Name\u003e\n      \u003c/targets\u003e\n      \u003cvalue\u003e\n        \u003cConstant lineno=\"3\" col_offset=\"16\" end_lineno=\"3\" end_col_offset=\"31\"\u003e\n          \u003cvalue type=\"str\"\u003esome constant\u003c/value\u003e\n          \u003ckind type=\"NoneType\"\u003eNone\u003c/kind\u003e\n        \u003c/Constant\u003e\n      \u003c/value\u003e\n      \u003ctype_comment type=\"NoneType\"\u003eNone\u003c/type_comment\u003e\n    \u003c/Assign\u003e\n    \u003cFunctionDef name=\"a_function\" lineno=\"5\" col_offset=\"0\" end_lineno=\"7\" end_col_offset=\"30\"\u003e\n      \u003cargs\u003e\n        \u003carguments\u003e\n          \u003cposonlyargs/\u003e\n          \u003cargs\u003e\n            \u003carg lineno=\"5\" col_offset=\"15\" end_lineno=\"5\" end_col_offset=\"19\"\u003e\n              \u003carg type=\"str\"\u003earg1\u003c/arg\u003e\n              \u003cannotation type=\"NoneType\"\u003eNone\u003c/annotation\u003e\n              \u003ctype_comment type=\"NoneType\"\u003eNone\u003c/type_comment\u003e\n            \u003c/arg\u003e\n            \u003carg lineno=\"5\" col_offset=\"21\" end_lineno=\"5\" end_col_offset=\"25\"\u003e\n              \u003carg type=\"str\"\u003earg2\u003c/arg\u003e\n              \u003cannotation type=\"NoneType\"\u003eNone\u003c/annotation\u003e\n              \u003ctype_comment type=\"NoneType\"\u003eNone\u003c/type_comment\u003e\n            \u003c/arg\u003e\n            \u003carg lineno=\"5\" col_offset=\"27\" end_lineno=\"5\" end_col_offset=\"33\"\u003e\n              \u003carg type=\"str\"\u003ekwarg1\u003c/arg\u003e\n              \u003cannotation type=\"NoneType\"\u003eNone\u003c/annotation\u003e\n              \u003ctype_comment type=\"NoneType\"\u003eNone\u003c/type_comment\u003e\n            \u003c/arg\u003e\n          \u003c/args\u003e\n          \u003cvararg type=\"NoneType\"\u003eNone\u003c/vararg\u003e\n          \u003ckwonlyargs/\u003e\n          \u003ckw_defaults/\u003e\n          \u003ckwarg type=\"NoneType\"\u003eNone\u003c/kwarg\u003e\n          \u003cdefaults\u003e\n            \u003cConstant lineno=\"5\" col_offset=\"34\" end_lineno=\"5\" end_col_offset=\"38\"\u003e\n              \u003cvalue type=\"NoneType\"\u003eNone\u003c/value\u003e\n              \u003ckind type=\"NoneType\"\u003eNone\u003c/kind\u003e\n            \u003c/Constant\u003e\n          \u003c/defaults\u003e\n        \u003c/arguments\u003e\n      \u003c/args\u003e\n      \u003cbody\u003e\n        \u003cFunctionDef name=\"a_subfunction\" lineno=\"6\" col_offset=\"4\" end_lineno=\"7\" end_col_offset=\"30\"\u003e\n          \u003cargs\u003e\n            \u003carguments\u003e\n              \u003cposonlyargs/\u003e\n              \u003cargs\u003e\n                \u003carg lineno=\"6\" col_offset=\"22\" end_lineno=\"6\" end_col_offset=\"26\"\u003e\n                  \u003carg type=\"str\"\u003earg1\u003c/arg\u003e\n                  \u003cannotation type=\"NoneType\"\u003eNone\u003c/annotation\u003e\n                  \u003ctype_comment type=\"NoneType\"\u003eNone\u003c/type_comment\u003e\n                \u003c/arg\u003e\n                \u003carg lineno=\"6\" col_offset=\"28\" end_lineno=\"6\" end_col_offset=\"34\"\u003e\n                  \u003carg type=\"str\"\u003ekwarg1\u003c/arg\u003e\n                  \u003cannotation type=\"NoneType\"\u003eNone\u003c/annotation\u003e\n                  \u003ctype_comment type=\"NoneType\"\u003eNone\u003c/type_comment\u003e\n                \u003c/arg\u003e\n              \u003c/args\u003e\n              \u003cvararg type=\"NoneType\"\u003eNone\u003c/vararg\u003e\n              \u003ckwonlyargs/\u003e\n              \u003ckw_defaults/\u003e\n              \u003ckwarg type=\"NoneType\"\u003eNone\u003c/kwarg\u003e\n              \u003cdefaults\u003e\n                \u003cConstant lineno=\"6\" col_offset=\"35\" end_lineno=\"6\" end_col_offset=\"39\"\u003e\n                  \u003cvalue type=\"NoneType\"\u003eNone\u003c/value\u003e\n                  \u003ckind type=\"NoneType\"\u003eNone\u003c/kind\u003e\n                \u003c/Constant\u003e\n              \u003c/defaults\u003e\n            \u003c/arguments\u003e\n          \u003c/args\u003e\n          \u003cbody\u003e\n            \u003cExpr lineno=\"7\" col_offset=\"8\" end_lineno=\"7\" end_col_offset=\"30\"\u003e\n              \u003cvalue\u003e\n                \u003cCall lineno=\"7\" col_offset=\"8\" end_lineno=\"7\" end_col_offset=\"30\"\u003e\n                  \u003cfunc\u003e\n                    \u003cName id=\"print\" lineno=\"7\" col_offset=\"8\" end_lineno=\"7\" end_col_offset=\"13\"\u003e\n                      \u003cctx\u003e\n                        \u003cLoad/\u003e\n                      \u003c/ctx\u003e\n                    \u003c/Name\u003e\n                  \u003c/func\u003e\n                  \u003cargs\u003e\n                    \u003cConstant lineno=\"7\" col_offset=\"14\" end_lineno=\"7\" end_col_offset=\"29\"\u003e\n                      \u003cvalue type=\"str\"\u003eHello, World!\u003c/value\u003e\n                      \u003ckind type=\"NoneType\"\u003eNone\u003c/kind\u003e\n                    \u003c/Constant\u003e\n                  \u003c/args\u003e\n                  \u003ckeywords/\u003e\n                \u003c/Call\u003e\n              \u003c/value\u003e\n            \u003c/Expr\u003e\n          \u003c/body\u003e\n          \u003cdecorator_list/\u003e\n          \u003creturns type=\"NoneType\"\u003eNone\u003c/returns\u003e\n          \u003ctype_comment type=\"NoneType\"\u003eNone\u003c/type_comment\u003e\n        \u003c/FunctionDef\u003e\n      \u003c/body\u003e\n      \u003cdecorator_list/\u003e\n      \u003creturns type=\"NoneType\"\u003eNone\u003c/returns\u003e\n      \u003ctype_comment type=\"NoneType\"\u003eNone\u003c/type_comment\u003e\n    \u003c/FunctionDef\u003e\n    \u003cClassDef name=\"SomeClass\" lineno=\"9\" col_offset=\"0\" end_lineno=\"14\" end_col_offset=\"12\"\u003e\n      \u003cbases/\u003e\n      \u003ckeywords/\u003e\n      \u003cbody\u003e\n        \u003cFunctionDef name=\"__init__\" lineno=\"10\" col_offset=\"4\" end_lineno=\"11\" end_col_offset=\"24\"\u003e\n          \u003cargs\u003e\n            \u003carguments\u003e\n              \u003cposonlyargs/\u003e\n              \u003cargs\u003e\n                \u003carg lineno=\"10\" col_offset=\"17\" end_lineno=\"10\" end_col_offset=\"21\"\u003e\n                  \u003carg type=\"str\"\u003eself\u003c/arg\u003e\n                  \u003cannotation type=\"NoneType\"\u003eNone\u003c/annotation\u003e\n                  \u003ctype_comment type=\"NoneType\"\u003eNone\u003c/type_comment\u003e\n                \u003c/arg\u003e\n                \u003carg lineno=\"10\" col_offset=\"23\" end_lineno=\"10\" end_col_offset=\"27\"\u003e\n                  \u003carg type=\"str\"\u003earg1\u003c/arg\u003e\n                  \u003cannotation type=\"NoneType\"\u003eNone\u003c/annotation\u003e\n                  \u003ctype_comment type=\"NoneType\"\u003eNone\u003c/type_comment\u003e\n                \u003c/arg\u003e\n              \u003c/args\u003e\n              \u003cvararg type=\"NoneType\"\u003eNone\u003c/vararg\u003e\n              \u003ckwonlyargs/\u003e\n              \u003ckw_defaults/\u003e\n              \u003ckwarg type=\"NoneType\"\u003eNone\u003c/kwarg\u003e\n              \u003cdefaults/\u003e\n            \u003c/arguments\u003e\n          \u003c/args\u003e\n          \u003cbody\u003e\n            \u003cAssign lineno=\"11\" col_offset=\"8\" end_lineno=\"11\" end_col_offset=\"24\"\u003e\n              \u003ctargets\u003e\n                \u003cAttribute lineno=\"11\" col_offset=\"8\" end_lineno=\"11\" end_col_offset=\"17\"\u003e\n                  \u003cvalue\u003e\n                    \u003cName id=\"self\" lineno=\"11\" col_offset=\"8\" end_lineno=\"11\" end_col_offset=\"12\"\u003e\n                      \u003cctx\u003e\n                        \u003cLoad/\u003e\n                      \u003c/ctx\u003e\n                    \u003c/Name\u003e\n                  \u003c/value\u003e\n                  \u003cattr type=\"str\"\u003earg1\u003c/attr\u003e\n                  \u003cctx\u003e\n                    \u003cStore/\u003e\n                  \u003c/ctx\u003e\n                \u003c/Attribute\u003e\n              \u003c/targets\u003e\n              \u003cvalue\u003e\n                \u003cName id=\"arg1\" lineno=\"11\" col_offset=\"20\" end_lineno=\"11\" end_col_offset=\"24\"\u003e\n                  \u003cctx\u003e\n                    \u003cLoad/\u003e\n                  \u003c/ctx\u003e\n                \u003c/Name\u003e\n              \u003c/value\u003e\n              \u003ctype_comment type=\"NoneType\"\u003eNone\u003c/type_comment\u003e\n            \u003c/Assign\u003e\n          \u003c/body\u003e\n          \u003cdecorator_list/\u003e\n          \u003creturns type=\"NoneType\"\u003eNone\u003c/returns\u003e\n          \u003ctype_comment type=\"NoneType\"\u003eNone\u003c/type_comment\u003e\n        \u003c/FunctionDef\u003e\n        \u003cFunctionDef name=\"a_method\" lineno=\"13\" col_offset=\"4\" end_lineno=\"14\" end_col_offset=\"12\"\u003e\n          \u003cargs\u003e\n            \u003carguments\u003e\n              \u003cposonlyargs/\u003e\n              \u003cargs\u003e\n                \u003carg lineno=\"13\" col_offset=\"17\" end_lineno=\"13\" end_col_offset=\"21\"\u003e\n                  \u003carg type=\"str\"\u003eself\u003c/arg\u003e\n                  \u003cannotation type=\"NoneType\"\u003eNone\u003c/annotation\u003e\n                  \u003ctype_comment type=\"NoneType\"\u003eNone\u003c/type_comment\u003e\n                \u003c/arg\u003e\n                \u003carg lineno=\"13\" col_offset=\"23\" end_lineno=\"13\" end_col_offset=\"27\"\u003e\n                  \u003carg type=\"str\"\u003earg1\u003c/arg\u003e\n                  \u003cannotation type=\"NoneType\"\u003eNone\u003c/annotation\u003e\n                  \u003ctype_comment type=\"NoneType\"\u003eNone\u003c/type_comment\u003e\n                \u003c/arg\u003e\n                \u003carg lineno=\"13\" col_offset=\"29\" end_lineno=\"13\" end_col_offset=\"35\"\u003e\n                  \u003carg type=\"str\"\u003ekwarg1\u003c/arg\u003e\n                  \u003cannotation type=\"NoneType\"\u003eNone\u003c/annotation\u003e\n                  \u003ctype_comment type=\"NoneType\"\u003eNone\u003c/type_comment\u003e\n                \u003c/arg\u003e\n              \u003c/args\u003e\n              \u003cvararg type=\"NoneType\"\u003eNone\u003c/vararg\u003e\n              \u003ckwonlyargs/\u003e\n              \u003ckw_defaults/\u003e\n              \u003ckwarg type=\"NoneType\"\u003eNone\u003c/kwarg\u003e\n              \u003cdefaults\u003e\n                \u003cConstant lineno=\"13\" col_offset=\"36\" end_lineno=\"13\" end_col_offset=\"40\"\u003e\n                  \u003cvalue type=\"NoneType\"\u003eNone\u003c/value\u003e\n                  \u003ckind type=\"NoneType\"\u003eNone\u003c/kind\u003e\n                \u003c/Constant\u003e\n              \u003c/defaults\u003e\n            \u003c/arguments\u003e\n          \u003c/args\u003e\n          \u003cbody\u003e\n            \u003cPass lineno=\"14\" col_offset=\"8\" end_lineno=\"14\" end_col_offset=\"12\"/\u003e\n          \u003c/body\u003e\n          \u003cdecorator_list/\u003e\n          \u003creturns type=\"NoneType\"\u003eNone\u003c/returns\u003e\n          \u003ctype_comment type=\"NoneType\"\u003eNone\u003c/type_comment\u003e\n        \u003c/FunctionDef\u003e\n      \u003c/body\u003e\n      \u003cdecorator_list/\u003e\n    \u003c/ClassDef\u003e\n  \u003c/body\u003e\n  \u003ctype_ignores/\u003e\n\u003c/Module\u003e\n```\n\u003c/details\u003e\n\nYou may now query `xml_tree` with xpath to get details of various sections of the Python source code:\n\n```python\n\u003e\u003e\u003e # All function definitions\n\u003e\u003e\u003e xml_tree.xpath(\"//FunctionDef\")\n[\u003cElement FunctionDef at 0x7f89780e41c0\u003e, \u003cElement FunctionDef at 0x7f89780e42c0\u003e, \u003cElement FunctionDef at 0x7f89780e4280\u003e, \u003cElement FunctionDef at 0x7f89780e4100\u003e]\n\n\u003e\u003e\u003e # Get the line numbers of the `s_subfunction` function\n\u003e\u003e\u003e xml_tree.xpath(\"./body/FunctionDef[name='a_function']/body/FunctionDef[name='a_subfunction']\")[0].attrib\n{'lineno': '6', 'col_offset': '4', 'end_lineno': '7', 'end_col_offset': '30'}\n\n\u003e\u003e\u003e # The above can be made simpler, since there's only one function called `a_subfunction`:\n\u003e\u003e\u003e xml_tree.xpath(\"//FunctionDef[name='a_subfunction']\")[0].attrib\n{'lineno': '6', 'col_offset': '4', 'end_lineno': '7', 'end_col_offset': '30'}\n\n\u003e\u003e\u003e # Get all of the methods within `SomeClass`:\n\u003e\u003e\u003e xml_tree.xpath(\"./body/ClassDef[name='SomeClass']//FunctionDef\")\n[\u003cElement FunctionDef at 0x7f89785fcd80\u003e, \u003cElement FunctionDef at 0x7f89780e4200\u003e]\n```\n\nUse the `source()`, `file_source()` and `module_source()` functions to get source code for the given xpath query:\n\n```python\n\u003e\u003e\u003e source, _ = file_source(\"code.py\", \"//FunctionDef[name='a_subfunction']\")[0]\n\u003e\u003e\u003e print(source)\ndef a_subfunction(arg1, kwarg1=None):\n    print(\"Hello, World!\")\n```\n\nThe first arguments to these three functions differ:\n- `source()` - the actual code as a string\n- `file_source()` - a string filesystem path to the source file\n- `module_source()` - an importable module path as a string, or the improted module object itself\n\nThe second argument is always the xpath query string, and you may set `dedent=False` to preserve the indentation.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflyte%2Fast-to-xml","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fflyte%2Fast-to-xml","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflyte%2Fast-to-xml/lists"}