')
return "\n".join(parts)
# TODO: Find the proper one...
def escape(text):
"""
"""
return text.replace("&", "&").replace("<","<")
def referenceList(func, references):
"""
"""
MAX_REFS_TO_DISPLAY = 5
parts = []
parts.append(heading(3, "references", "References:"))
uniqueReferences = list(sets.Set([r.strip() for r in references]))
parts.append("
")
for reference in uniqueReferences[:MAX_REFS_TO_DISPLAY]:
if func.newName:
# TODO: Check this substitution handles all cases.
reference = re.sub("(\W|^)%s(\W|$)" % func.name,
r"\1%s\2" % func.newName.replace("~",""),
reference)
parts.append('
%s
'
% escape(reference))
if len(uniqueReferences) > MAX_REFS_TO_DISPLAY:
parts.append('
""" % (sourceURL.replace("-linenums.html",""),
"funcs-%s.txt" % sourceURL.replace("-linenums.html",""))
sections = []
toc = []
sortedFuncs = [(f.newName.lower() or f.name.lower(), f)
for f in funcs.values()]
sortedFuncs.sort()
for dummy, f in sortedFuncs:
tocEntry, section = formatFunction(f, sourceURL)
toc.append(tocEntry)
sections.append(section)
references = getReferences(source, f.name)
if references:
sections.append(referenceList(f, references))
print "
"
print table(toc, 4)
print "
"
print "\n".join(sections)
print ""
def usage():
"""
"""
print "Usage: %s (-d | -u )" % sys.argv[0]
raise SystemExit
# TODO: Make this method of Function?
def getArgsSig(f):
"""
Based on function/constructor args?
"""
if f.args:
fargs = f.args.split(",")
else:
fargs = []
return fargs
def findSignatureMatches(targetFunction, allFunctions):
"""
"""
# TODO: Refactor copy & pasting...
# TODO: Cache any of this stuff?
sigMatches = []
# Method signatures
for mf in allFunctions:
if mf.signature == targetFunction.signature:
# This is strongest match so we not check anything else?
sigMatches.append(mf)
# Constructor/function arg count signatures
if len(sigMatches) > 1:
for mf in sigMatches:
if not (len(getArgsSig(mf)) == len(getArgsSig(targetFunction))):
sigMatches.remove(mf)
# Constructor/function source code match
if len(sigMatches) > 1:
matchSources = [mf.source for mf in sigMatches]
closeSources = difflib.get_close_matches(f.source,
matchSources,
n = 2)
if len(closeSources) == 1:
sigMatches = [sigMatches[matchSources.index(closeSources[0])]]
# TODO: Handle multiple closematches?
# First method source code match
if (len(sigMatches) > 1) and f.methods:
# TODO: Check methods are present.
# TODO: Check against all methods?
matchSources = [mf.methods[0].source for mf in sigMatches]
closeSources = difflib.get_close_matches(f.methods[0].source,
matchSources,
n = 2)
if len(closeSources) == 1:
sigMatches = [sigMatches[matchSources.index(closeSources[0])]]
# TODO: Handle multiple close matches?
# Close method signatures
if not sigMatches:
allSigs = [mf.signature for mf in allFunctions]
closeSigs = difflib.get_close_matches(f.signature,
allSigs,
n = 2)
if len(closeSigs) == 1:
sigMatches = [allFunctions[allSigs.index(closeSigs[0])]]
# TODO: Handle multiple close matches?
return sigMatches
if __name__ == "__main__":
try:
option = sys.argv[1]
except IndexError:
usage()
else:
if option == "-d":
try:
sourceFilename1 = sys.argv[2]
except IndexError:
usage()
elif option =="-u":
try:
sourceFilename1 = sys.argv[2]
sourceFilename2 = sys.argv[3]
except IndexError:
usage()
else:
usage()
source1 = open(sourceFilename1).read()
funcs1 = extractFunctionInfo(source1)
exportFuncs("funcs-%s.txt" % sourceFilename1, funcs1)
if option == "-d": # "d"ocument
linenumsFilename = "%s-linenums.html" % sourceFilename1
generateNumberedSource(sourceFilename1, linenumsFilename)
generateDocumentation(source1, funcs1, linenumsFilename)
elif option == "-u": # "u"pgrade
source2 = open(sourceFilename2).read()
funcs2 = extractFunctionInfo(source2)
allFuncs1 = funcs1.values()
allFuncs2 = funcs2.values()
itemMatched = True
while allFuncs1 and allFuncs2 and itemMatched:
itemMatched = False
for f in allFuncs1[:]:
sigMatches = findSignatureMatches(f, allFuncs2)
if len(sigMatches) == 1:
itemMatched = True
matchedF = sigMatches[0]
allFuncs2.remove(matchedF)
allFuncs1.remove(f)
matchedF.update(f)
# TODO: Allow for manual confirmation.
#print "\n------------------------------"
#print f.name, matchedF.name, f.newName
#print f.source
#print matchedF.source
if allFuncs1 and not allFuncs2:
print "%d items exist in old file but not in new file." % \
len(allFuncs1)
if allFuncs2 and not allFuncs1:
print "%d items exist in new file but not in old file." % \
len(allFuncs2)
if allFuncs1 and allFuncs2:
print "%d items in old file and "\
"%d items in new file not matched."% (len(allFuncs1),
len(allFuncs2))
exportFuncs("funcs-%s.txt" % sourceFilename2, funcs2)
else:
usage()