#!/usr/bin/env python # -*- coding: utf-8 -*- """ Knowledge Closure.Report Current Progress by kennyluck The (imature) design is as follows: Thread ----> _______ | | ajax.cgi translates to JSON Thread ----> | | ----------------------------> Browser | | Thread ----> | | |_______| Later thread might rewrite previous result """ import sys import os #@@ change this to suitable folder if os.environ['HTTP_HOST'] == 'dig.csail.mit.edu': WRITABLE_DIRECTORY = "/afs/csail.mit.edu/group/dig/www/data/2008/webdav/Knowledge_Closure/" else: WRITABLE_DIRECTORY = "" #import simplejson #I would prefer to use simplejson but it's too hard to install it on # people.csail.mit.edu . dmejson is a one-file-library, which is better. import demjson from xml.dom.minidom import parseString def give_current_progress(): #read at once and close transaction_file = open(WRITABLE_DIRECTORY + "transaction_0.txt") lines = transaction_file.readlines() transaction_file.close() cluster = {} if lines[-1] == "FINISHED\n": cluster["isFinished"] = True lines.pop() else: cluster["isFinished"] = False contents = {} for line in lines: # num, _ , html = line.partition(" ") # assert html, "Missing html, a problem within check.cgi" # parseString(html) # contents["Test_" + str(num)] = html try: paragraph = parseString(line).documentElement contents[paragraph.getAttribute("id")] = line.rstrip() except: print line raise #The third way # li = parseString(line).documentElement #very slow, it seems # element = dict() # element["class"] = li.getAttribute("class") # element["textContent"] = li.firstChild.data # contents[li.getAttribute("id")] = element cluster["contents"] = contents #simplejson.dump(cluster, sys.stdout) print demjson.encode(cluster) sys.stderr = open(WRITABLE_DIRECTORY + "ajax_log.txt", 'w') print "Content-type: application/json" print #@@Redo when error occurs give_current_progress() sys.stderr.close() # time curl -i http://people.csail.mit.edu/kennyluck/Knowledge_Closure/ajax.cgi # real 0m0.148s # user 0m0.005s # sys 0m0.006s # real 0m0.311s 0m0.169s # user 0m0.005s # sys 0m0.006s