3 from __future__ import with_statement
10 def tag_parser(file, tag):
13 >>> for todo in tag_parser(nothing, "@todo"):
16 >>> one = ["@todo Help!"]
17 >>> for todo in tag_parser(one, "@todo"):
21 >>> mixed = ["one", "@todo two", "three"]
22 >>> for todo in tag_parser(mixed, "@todo"):
26 >>> embedded = ["one @todo two", "three"]
27 >>> for todo in tag_parser(embedded, "@todo"):
31 >>> continuation = ["one", "@todo two", " three"]
32 >>> for todo in tag_parser(continuation, "@todo"):
36 >>> series = ["one", "@todo two", "@todo three"]
37 >>> for todo in tag_parser(series, "@todo"):
45 for lineNumber, line in enumerate(file):
46 column = line.find(tag)
49 yield "\n".join (currentTodo)
50 prefix = line[0:column]
51 currentTodo = ["%d: %s" % (lineNumber+1, line[column:].strip())]
52 elif prefix is not None and len(prefix)+1 < len(line) and line.startswith(prefix) and line[len(prefix)].isspace():
53 currentTodo.append (line[len(prefix):].rstrip())
55 yield "\n".join (currentTodo)
59 yield "\n".join (currentTodo)
62 def tag_finder(filename, tag):
65 with open(filename) as file:
66 body = "\n".join (tag_parser(file, tag))
69 output = "No %s's for %s" % (tag, filename) if verbose else ""
71 header = "%s's for %s:\n" % (tag, filename) if verbose else ""
72 output = header + body
73 output += "\n" if verbose else ""
75 return (passed, output)
78 if __name__ == "__main__":
83 opar = optparse.OptionParser()
84 opar.add_option("-v", "--verbose", dest="verbose", help="Toggle verbosity", action="store_true", default=False)
85 options, args = opar.parse_args(sys.argv[1:])
86 verbose = options.verbose
94 bugPassed, bugOutput = tag_finder(filename, "@bug")
95 todoPassed, todoOutput = tag_finder(filename, "@todo")
96 output = "\n".join ([bugOutput, todoOutput])
97 if (not bugPassed and bugsAsError) or (not todoPassed and todosAsError):
99 output = output.strip()
101 completeOutput.append(filename+":\n"+output+"\n\n")
102 print "\n".join(completeOutput)
104 sys.exit(0 if allPassed else 1);