1

I'm trying to write a Python script which should basically act as a text parsing command line tool. The intent of the script is to search for the function name which is provided as the input argument to the script and also the functions which call the function (my input argument) and give the script names containing the functions along with the line numbers as the output.

I have figured out how to get where the input function name is called in the scripts but don't know how to get the name of the functions which call the actual function that I'm searching for. Can anyone let me know how to proceed?

import os, fnmatch

searchStr = "PressButton"
directory = 'C:\TFSMap\FalconPlatformTests\GLib'

# in which file to search.
pattern = "*_Main.php"


a = {}

def find_files(directory, pattern):

for root, dirs, files in os.walk(directory):
    for basename in files:
        if fnmatch.fnmatch(basename, pattern):
            filename = os.path.join(root, basename)
            yield filename

    for dir in dirs:
        find_files(directory+'/'+dir, pattern)


for filename in find_files(directory,pattern):
i = 0
with open(filename) as Sid:
    values = ''
    for line in Sid:
       i = i +1
       if searchStr in line:
            if filename in a:
                values = a[filename]
                a[filename] = values +','+ i.__str__()
            else:
                values = i.__str__()
                a[filename] = i.__str__()
    if values != "":
        print (filename+' ===== '+values)
sunny
  • 21
  • 3
  • 5
    Please don't post code in screenshots. They can't be searched or copied and offer poor usability. Instead, paste the code as text directly into your question. If you select it and click the `{}` button or Ctrl+K the code block will be indented by four spaces, which will cause it to be rendered as code. – ChrisGPT was on strike Aug 15 '17 at 18:26
  • https://stackoverflow.com/questions/3987041/python-run-function-from-the-command-line – whackamadoodle3000 Aug 15 '17 at 18:27
  • Sorry for the inconvenience, i have managed to upload the code in text form, thanks to your advice. – sunny Aug 15 '17 at 18:31

1 Answers1

0

If I understand correctly what you are trying to do, then it requires Python grammar parsing. You aren't just simply searching for strings, but understanding the code to the point of being able to tell that there is a reference to function x in function y. This may be more complex than you think, you need to be able to form a parse tree, decide how far up that tree you want to show when you find a reference, etc. Not to mention edge cases (e.g. If there is a call to function x in a lambda function, inside of what function will you report it being?)

In any case, you may want to use a Python parser. Python gives access to its internal parser in the parser module, and there are other python-parsing-tools that you can use.

Note: I assume you are doing this for academic purposes, since essentially all IDE's provide this functionality.

Ramon
  • 1,169
  • 11
  • 25