E.6.1 Basisklassen und Funktionen
import c_store
import c_bsio
import c_search
import c_glimpsearch
Klasse c_bsio
class c_bsio:
fname=""
fd=0
def __init__(self,filename,mode="rw"):
self.fname=filename
self.mode=mode
def open(self):
self.fd=open(self.fname,self.mode)
return self.fd
def close(self):
return self.fd.close()
def read(self,size):
return self.fd.read(size)
def readline(self):
return self.fd.readline()
def write(self,buff):
return self.fd.write(buff)
def seek(self,offset,whence):
return self.fd.seek(offset,whence)
def tell(self):
return self.fd.tell()
Klasse c_sgmlread
import regex
import string
import c_bsio
class c_sgmlread(c_bsio.c_bsio):
def __init__(self,fname):
c_bsio.c_bsio.__init__(self,fname,"r")
self.open()
def __del__(self):
self.close()
def seek_begin(self):
self.fd.seek(0,0)
def seek_end(self):
self.fd.seek(0,2)
def get_doctype(self):
self.oldpos=self.tell()
self.seek(0,0)
buff=" "
erg=""
while regex.search("<!DOCTYPE",buff)==-1 and buff!="":
buff=self.fd.readline()
if buff =="":
erg= ""
else:
self.fd.seek(-len(buff),1)
buff=""
while buff!=">" and buff!="[":
buff=self.read(1)
erg=erg+buff
if len(erg)>0 and erg[len(erg)-1]=="[":
erg=erg[0:-2]
erg=erg+">"
self.seek(self.oldpos,0)
return erg
def get_dtdname(self):
dtyp=self.get_doctype()
return string.split(dtyp[1:-1])[1]
def get_dtdfile(self):
dtyp=self.get_doctype()
return string.split(dtyp[1:-1])[3][1:-1]
def get_dtd(self):
import string
doctypetag=self.get_doctype()
doctypetag=doctypetag[2:-1]
return string.split(doctypetag," ")
def get_tag(self,str):
buff=" "
while regex.search("<"+str+"*",buff)==-1 and regex.search("</"+str+"*",buff)==-1 and buff!="":
buff=self.fd.readline()
if buff!="":
self.fd.seek(-len(buff)+regex.search("<"+str,buff)-1,1)
if regex.search("</"+str+" ",buff)!=-1 or regex.search("</"+str+">",buff)!=-1:
self.fd.seek(1,1)
erg=""
buff=""
while buff!=">":
buff=self.fd.read(1)
erg=erg+buff
else:
erg=""
return erg
def get_nexttag(self):
buff=" "
while regex.search("<",buff)==-1 and buff!="":
buff=self.fd.readline()
if buff!="":
self.fd.seek(-len(buff)+regex.search("<",buff),1)
erg=""
buff=""
while buff!=">":
buff=self.fd.read(1)
erg=erg+buff
else:
erg=""
return erg
def get_tagcontents(self,str):
erg=self.get_tag(str)
if erg=="</"+str+">":
erg=self.get_tag(str)
erg1=""
buff=" "
while regex.search("</"+str+" ",buff)==-1 and regex.search("</"+str+">",buff)==-1 and buff!="":
buff=self.fd.readline()
erg1=erg1+buff
erg1=erg1[:regex.search("</"+str+">",erg1)+len(str)+3]
return erg+erg1
def split_tag(tagstr):
ergdict={}
tagstr=tagstr[1:-1]
pos=string.find(tagstr," ")
tag=tagstr[:pos]
tagstr=tagstr[pos:]
ergdict["TAG"]=tag
while len(tagstr)!=0:
tagstr=string.strip(tagstr)
pos1 = string.find(tagstr,"=")
attrib = string.strip( tagstr[:pos1] )
tagstr = tagstr[pos1+1:]
tagstr=string.strip(tagstr)
pos3 = string.find(tagstr," ")
if pos3 == -1:
pos3=len(tagstr)
wert = string.strip( tagstr[:pos3] )
tagstr = tagstr[pos3+1:]
ergdict[attrib]=wert
return ergdict
Verwaltungsfunktionen
from sc_globals import *
import pgpy
class c_maintenance:
def vacuum(self):
self.conn=pgpy.pgconn(DL_PGHOST,DL_PGPORT,DL_DBNAME)
res=self.conn.exec_query("vacuum;")
erg=res.commandstatus()
if str(erg)[0:6]!="VACUUM":
return -1
else:
return 0
def delete_bs(self,ident):
import os
os.system("rm "+"-f "+DL_BSOUTPATH+str(ident)+".bs")
conn=pgpy.pgconn(DL_PGHOST,DL_PGPORT,DL_DBNAME)
res=self.conn.exec_query(
"update doc_description set bs_stored='f' where docnr="+
str(ident)+";")
erg=res.commandstatus()
if str(erg)[0:6]!="UPDATE":
return -1
else:
return 0
def delete_doc(self,ident):
self.delete_bs(ident)
conn=pgpy.pgconn(DL_PGHOST,DL_PGPORT,DL_DBNAME)
res=self.conn.exec_query(
"delete from doc_elements* where docnr=" + str(ident)+";")
res=self.conn.exec_query(
"delete from doc_methods where docnr=" + str(ident)+";")
res=self.conn.exec_query(
"update doc_description set sgml_stored='f' where docnr="+
str(ident)+";")
erg=res.commandstatus()
if str(erg)[0:6]!="UPDATE":
return -1
else:
return 0
def sql_exec(self,querystr):
conn=pgpy.pgconn(DL_PGHOST,DL_PGPORT,DL_DBNAME)
res=self.conn.exec_query(querystr)
if res.get_tuple_count()==0:
return -1
else:
erg=res.get_resultlist(1,"|",1,0)
return erg
return erg
def dali_reinit(self):
import time
print "-------------------------------------------------------"
print "Sie sind dabei die komplette DALI Datenbank zu loeschen."
print ""
print " Ich warte jetzt mal 20 Sekunden, wenn Sie es sich anders"
print ""
print "ueberlegt haben, sollten sie ein paarmal CTRL-C druecken."
print "--------------------------------------------------------"
time.sleep(20)
conn=pgpy.pgconn(DL_PGHOST,DL_PGPORT,DL_DBNAME)
res=self.conn.exec_query("drop table doc_description;")
res=self.conn.exec_query("drop table doc_elements;")
res=self.conn.exec_query("drop table doc_bin;")
res=self.conn.exec_query("drop table doc_tags;")
res=self.conn.exec_query("drop table doc_text;")
res=self.conn.exec_query("drop table doc_methods;")
res=self.conn.exec_query("drop table dtd;")
res=self.conn.exec_query("drop table local_doc_ids;")
res=self.conn.exec_query("CREATE TABLE local_doc_ids (docnr int4, dtdnr int4) archive = none;")
res=self.conn.exec_query("CREATE TABLE doc_elements (docnr int4, elementnr int4, brother int4, son int4, contents text) archive = none;")
res=self.conn.exec_query("CREATE TABLE dtd (dtdnr int4, dtdid text,publicname text, systemname text) archive = none;")
res=self.conn.exec_query("CREATE TABLE doc_tags () inherits ( doc_elements) archive = none;")
res=self.conn.exec_query("CREATE TABLE doc_text () inherits ( doc_elements) archive = none;")
res=self.conn.exec_query("CREATE TABLE doc_bin () inherits ( doc_elements) archive = none;")
res=self.conn.exec_query("CREATE TABLE doc_description (docnr int4, dtdnr int4, title text, author text, owner text, ident text, pubkey text, info text, metadtdnr int4, bs_stored bool, sgml_stored bool) archive = none;")
res=self.conn.exec_query("CREATE TABLE doc_methods (docnr int4, meth_class text, meth_location text, meth_mimetype text) archive = none;")
res=self.conn.exec_query("INSERT INTO local_doc_ids VALUES (100,1);")
self.read_sgmlcat(self)
def read_sgmlcat(self):
import c_bsio
import class_funcs
import string
fname=DL_SGMLCATPATH+DL_SGMLCATNAME
fd=c_bsio.c_bsio(fname,"r")
fd.open()
buff=" "
while buff!="":
buff=fd.readline()
if buff[0:6]=="PUBLIC":
newdtdid=class_funcs.get_newdtdid()
publictup=string.split(buff,"\"")
dtdid=string.strip(publictup[2])
publicname=string.strip(publictup[1])
systemname=DL_SGMLCATPATH+dtdid
query="insert into dtd values ("+str(newdtdid)+","
query=query+"'"+dtdid+"',"
query=query+"'"+publicname+"',"
query=query+"'"+systemname+"');"
res=conn.exec_query(query)
if res.commandstatus()[0:6]!="INSERT":
print "Fehler bei INSERT in c_maintenance.read_sgmlcat"
fd.close()