/misc/todo.cgi

#!/usr/bin/env python
"""
todo.cgi - Find @@ items in a site
License: GPL 2; share and enjoy!
Author: Sean B. Palmer, inamidst.com
@@ Ignore list

---
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Todo Items</title>
<meta name="robots" content="nofollow" />
<meta name="robots" content="noarchive" />
<link rel="stylesheet" type="text/css" href="/trove/pwyky.css" />
</head>
<body>
<h1>Todo Items</h1>
<p>Also: <a href="/inside/misc/todo">source code</a>.</p>
%s
<address>
<a href="http://inamidst.com/sbp/">Sean B. Palmer</a>
</address>
</body>
</html>
"""

import cgitb; cgitb.enable()
import sys, os, re, itertools
import cgiutil as cgiu

suffixen = ('.html', '.txt', '.cgi')
s_todo = r'(?sm)@@[ \n].+?(?:\.(?=[ \n<])|\?(?=[ \n<])|.(?=<)|\n\n)'
r_todo = re.compile(s_todo)

def getTodos(fn, path=None): 
   if path is None: 
      path = fn
   result = ['<ul>']

   f = open(fn)
   msg = '<li><a href="%s">%s</a>: %s</li>'
   for line in f: 
      todos = r_todo.findall(line)
      result.extend(msg % (path, path, t) for t in todos)
   f.close()

   result.extend(['</ul>', ''])
   return result

def todo(site=None): 
   if site is None: 
      site = os.environ.get('DOCUMENT_ROOT') or '/web/inamidst.com'

   if site.endswith('/'): 
      prefix = '/'
   else: prefix = ''

   robotstxt = os.path.join(site, 'robots.txt')
   if os.path.exists(robotstxt): 
      rp = cgiu.RobotsTxt(fn=robotstxt)
   else: rp = None

   result = []
   for (dirpath, dirnames, filenames) in os.walk(site): 
      for name in filenames: 
         fn = os.path.join(dirpath, name)
         path = prefix + fn[len(site):]
         if (not rp) or rp.fetchable(path): 
            if cgiu.any(suffixen, fn.endswith): 
               result.extend(getTodos(fn, path))

   i = __doc__.find('---')
   cgiu.serve(200, __doc__[i+3:] % '\n'.join(result))

def main(): 
   if cgiu.method == 'GET': todo()
   else: cgiu.serve(501, "<p>Only GET is supported.</p>\n")

if __name__=="__main__": 
   main()