0

for the sake of analysing a python project, I need a tool that given the code and a line number will provide me with the signature of the method the line represents: module_name.[class.]method_name. Due to python indentation parsing it should be really simple but I didn't find such a tool.

(apparently rope doesn't fit my needs)

akiva
  • 2,677
  • 3
  • 31
  • 40
  • Parsing isn't really easy "because of indentation". The indentation stuff requires a clever hack in the lexer, but that isn't particularly hard. You still need a full python parser for the dialect of Python you are using, including all the icky detail on character string escapes (or you'll miss the end of a string literal or comment, and therefore the start of your method). – Ira Baxter Mar 09 '15 at 23:17

1 Answers1

1

Our DMS Software Reengineering Toolkit with its Python Front End can parse both Python 2 and 3, a produce finely detailed ASTs.

You can export those trees as XML if you want and then do as you like. The preferred mode of operation with DMS is for you to write a DMS metaprogram that accomplishes what you want.

An easy thing you could with DMS for your problem is to write a rather simple source pattern that recognizes a method declaration (which automatically gets its source position, since the ASTs capture that), and then walk up the AST to pick off the language containers for class and module name.

Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
  • thanks. but this is not exactly what I need. I sure could write code to parse the AST as well as I could write code to parse the source code itself. my question was whether there is such a parser already exists – akiva Mar 10 '15 at 06:56
  • 1
    Sorry, didn't understand that last bit. Yes, such a parser exists, in more than one place. DMS obviously offers one. I think the Python community has one hiding in the tool called (I think) "2to3". – Ira Baxter Mar 10 '15 at 08:40