Using pyastgrep as a library #18
Replies: 2 comments 4 replies
-
Hello @spookylukey, thanks for starting this discussion about using
I would also be interested in building a tool that completes the first two aforementioned steps and then also takes these steps:
I should additionally point out that I quickly scanned the documentation for So, the main questions I have are:
Finally, as a reference, I thought it would be useful to point out this tool suite: srcML allows for analysis and manipulation of a program's AST through XML. However, it does not currently support the Python programming language. As such, I would be interested in seeing if it would be possible to use Finally, thanks for all of the time and effort that you have invested in the creation of |
Beta Was this translation helpful? Give feedback.
-
@gkapfham Thanks for your input, that's all really helpful for the discussion. Regarding your questions:
At the moment, it's "Use The Source Luke" - https://github.com/spookylukey/pyastgrep/tree/master/src/pyastgrep . I think you most likely want to do something similar to the search_python_files function. It won't work as it is, however, because if your query expression returns counts instead of XML nodes, it throws an exception (see the test ). So, I think you can use all the lower level functions (
Going from XML to AST should certainly be possible, though not implemented. Going from AST back to Python source is also possible using tools like ast_decompiler . However this has some significantly limitations - AST does not preserve comments or formatting, which means those would be lost on a roundtrip. To avoid that, you'd have to get clever e.g. doing some kind of diff against the original and transformed AST. Tools like LibCST are better suited for code modification tools. For the code modification needs, I'm not sure how to bridge the gap from querying. Being able to go from an AST node to a LibCST node? Being able to do an XPath query against the structures that LibCST produces? |
Beta Was this translation helpful? Give feedback.
-
Currently we're not documenting any APIs for use as a library, but can do so if there is demand. Please share ideas and use cases you'd like to cover.
At the moment, the printing functionality is seeing a lot of flux, but the search is fairly stable.
Beta Was this translation helpful? Give feedback.
All reactions