import os, os.path, sys
import urllib
from datetime import datetime
import pylog_config as cfg
import subprocess,tempfile
from HTMLParser import HTMLParser
def dir_begin(path,hidden):
print '
'
def dir_end(path,hidden):
print '
'
def dir_link(name,f,depth,hidden):
script = sys.argv[0][sys.argv[0].rindex('/')+1:]
print ' '
print ' '
print ' '
def file_link(name,f,depth,hidden):
script = sys.argv[0][sys.argv[0].rindex('/')+1:]
print ' '
print ' '
print ' '
def disp_txt(path):
print ''
f = open(path)
print f.read()
f.close()
print '
'
class BodyParser(HTMLParser):
body=False
src=''
def handle_starttag(self, tag, attrs):
if self.body:
self.src+='<'+tag
for attr in attrs:
self.src+=' '+attr[0]+'="'+attr[1]+'"'
self.src+='>'
if tag=='body':
self.body=True
def handle_endtag(self, tag):
if tag=='body':
self.body=False
if self.body:
self.src+=''+tag+'>'
def handle_data(self, data):
if self.body:
self.src+=data
def handle_entityref(self,ref):
self.handle_charref(ref)
def handle_charref(self,ref):
if self.body:
self.src += '&'+ref+';'
def get_source_html(path):
tmpfile=tempfile.mkstemp(prefix="pylog")[1]
#we use a fork of vim to generate a nice syntax highlighted html file
#TODO this requires further testing
p=subprocess.Popen(["/usr/bin/vim",path,\
"-n",\
"-cTOhtml",\
"-cw! "+tmpfile,\
"-cq!",\
"-cq!"],\
bufsize=2**16,stdout=subprocess.PIPE,stderr=subprocess.PIPE,env={"HOME":os.getcwd()})
p.stdout.close()
p.stderr.close()
# p.stdin.close()
p.wait() #wait for child to terminate
f=open(tmpfile,"r")
bp=BodyParser()
bp.feed(f.read())
src = bp.src
f.close()
#delete tmp file
os.remove(tmpfile)
return src
def disp_source(path):
print ''
print get_source_html(path)
#print source
print '
'
def disp(path):
print ''
f = open(path)
print f.read()
f.close()
print '
'
def get_n_words(fi,n):
i=0
txt=""
c=" "
space=False
while i<40 and len(c)==1:
c = fi.read(1)
txt+=c
if c==" " and not space:
i+=1
space=True
else:
space=False
if i==40:
return (txt,True)
return (txt,False)
def prev(path):
fi = open(path)
txt=get_n_words(fi,40)
print ''+txt[0]
if txt[1]:
print ' ...'
print '
'
fi.close()
def prev_txt(path):
fi = open(path)
txt=get_n_words(fi,40)
print ''+txt[0]
if txt[1]:
print ' ...'
print '
'
fi.close()
def prev_source(path):
return