[Tutor] Parsing through decorators program

Mary Morris marris1031 at gmail.com
Thu Jul 15 17:03:53 CEST 2010


So my new internship asked me to write a program that would parse through
all of our source code on an svn server, find all the decorators and print
them. I found all of the decorators by finding the '@' symbol.  What I still
have to do is write something that will make the computer print all of the
names of the decorators that it finds.  So far, this is what I have written:

# Written by Mary Morris
# July 2010
#





import os
import warnings
import traceback





##
#
#
def get_py_files(path):
ret = []
 if path[-1] != '/':
path += '/'
 files = os.listdir(path)
for file in files:
if file == '.svn':
continue
 if os.path.isdir(path + file + '/'):
temp = get_py_files(path + file + '/')
for ele in temp:
ret.append(ele)
 else:
ext = file.split('.')[-1]
if ext != 'py':
continue
 temp = path + file
temp = temp.replace('/', '\\')
 ret.append(temp)
 return ret










if __name__ == '__main__':
imports = {}
 files = get_py_files(os.getcwd())
 our_modules = [file.split('\\')[-1][:-3] for file in files]
 for file in files:
# for file in files[:1]:
f = open(file)
lines = f.readlines()
f.close()
 for line in lines:
line = line[:-1]
 #
# Conveniently, "import bleh" and "from bleh import bleh2" both have the
second string (when split) of "bleh"
#
if line.startswith('import') or line.startswith('from'):
line = line.replace('\t', ' ')
temp = [s for s in line.split(' ') if len(s) != 0]
 # This is because FirstAssist uses "import os, sys" (and similar) all over
the place...
if temp[1].find('@') != -1:
for ele in temp[1:]:
ele = ele.replace('@', '')
if not ele in imports.keys():
# imports[ele] = [file.split('\\')[-1]]
imports[ele] = [file]
else:
# imports[ele] += [file.split('\\')[-1]]
imports[ele] += [file]
else:
if not temp[1] in imports.keys():
# imports[temp[1]] = [file.split('\\')[-1]]
imports[temp[1]] = [file]
else:
# imports[temp[1]] += [file.split('\\')[-1]]
imports[temp[1]] += [file]
  external_imports = [im for im in imports.keys() if not im in our_modules]
 dependencies = []



Could you give me suggestions on how to make my program print the names of
the decorators?
Thanks,
Mary
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100715/056d3faf/attachment.html>


More information about the Tutor mailing list