AstuteSource / chasten

:dizzy: Chasten Uses XML and XPATH to Check a Python Program's AST for Specified Patterns!
https://pypi.org/project/chasten/
GNU General Public License v2.0
7 stars 8 forks source link

Dump and/or Display the XML Representation for a Python File #31

Closed gkapfham closed 9 months ago

gkapfham commented 1 year ago

Right now the current implementation of chasten uses the pyastgrep package to search through the XML-based representation of a Python source code file.

However, even though pyastgrep supports the dumping of a Python source code file to an XML file, the chasten tool does not also offer that feature.

It would be helpful if the tool also gave a way to dump and/or display the XML representation of a Python source code file. This is particularly useful in the situation when a person is trying to write an XPath expression for a specific feature of the Python AST and it is not working correctly.

Sometimes it is helpful to be able to see the XML representation of the Python source code when you are trying to debug the XPath expression.

gkapfham commented 11 months ago

FIXME: add sabi

MilesF25 commented 11 months ago

How will I be receiving the python source code? Will it be entered manually, or will I have to search for it?

gkapfham commented 11 months ago

Hello @MilesF25, the Python source code is input to the chasten program. Since we've talked about this recently during an in-person meeting, could I trouble you to please share some of what you learned from that conversation here? Are we going to be able to move the resolution of this issue forward? Please update us with your status, okay?

rodriguez03 commented 11 months ago

ElementTree is a library that converts and parses .py files into XML files. I looked through multiple files within chasten and saw that chasten does not do this conversion.

Update: Goal: convert .py files in XML, to dump and display files from AST -> XML files import ElementaryTree library https://docs.python.org/3/library/xml.etree.elementtree.html

rodriguez03 commented 11 months ago

To keep everyone up to date with this issue:

Pyastgrep currently takes .py files and converts them in to an .ast (Abstract Syntax Tree) file while dumping the file into an .xml ![Uploading Screen Shot 2023-09-25 at 9.52.53 PM.png…]() (Extensible Markup Language). An AST file is an intermediate representation of a python programming language, and demonstrates XPATH files to specific python codes. This allows users to simply debug their code and look at a step by step process of how their code is functioning. An example of an AST file is demonstrated through the website below (converts python code into an AST file: https://python-ast-explorer.com Meanwhile an XML (Extensible Markup Language) reads the python file and separates lines into nodes while storing it inside the computers disk. The issue that Chasten is currently facing is the inability to display the XML file to its user. Our goal to resolve this is to create two command lines states as --save XML which will save the directory path of the XML file and --display XML path this will access the file that stores all the XML files and allow the user to view its contents.

An example of an XML file is demonstrated below:

<FunctionDef lineno="829" col_offset="0" type="str" name="datasette_publish">
  <args>
    <arguments>
      <posonlyargs/>
      <args>
        <arg lineno="830" col_offset="4" type="str" arg="database_path">
          <annotation>
            <Name lineno="830" col_offset="19" type="str" id="Path">
              <ctx>
                <Load/>
              </ctx>
            </Name>
          </annotation>
        </arg>
        <arg lineno="839" col_offset="4" type="str" arg="metadata">
          <annotation>
            <Name lineno="839" col_offset="14" type="str" id="Path">
              <ctx>
                <Load/>
              </ctx>
            </Name>
          </annotation>
        </arg>