from site import Site
from retrieval import Retrieval, Factory
from tools import convert
import string, re
class Msc(Site):
def __init__(self, name, database, location):
self.edbmdb = None
Site.__init__(self, name, database, location)
return
def edbminit(self, path, force=0):
if self.edbmdb == None or force:
self.edbmdb = Retrieval(path)
return
def edbmopen(self, name, format=''):
return self.edbmdb.open(name, format, self.Factory)
def edbmquery(self, form, db):
query = []
for idx in db.schema['indexinfo'].keys():
if form.has_key(idx) and form[idx]:
what = convert.iso2ascii(form[idx])
what = string.strip(re.sub(r'\s+', ' ', what))
what = string.replace(what, "'", '"')
what = re.sub(r'[^a-zA-Z0-9*" -]', '', what)
what = re.sub(r'" *(.*?) *"', r'<\1>', what)
what = re.sub(r'" *(.*)$', r'<\1>', what)
quots = string.split(what, '<')
list = [quots[0]]
for quot in quots[1:]:
(q,s) = string.split(quot, '>')
list.append(string.replace(q, ' ', '-'))
list.append(s)
what = string.strip(string.join(list, ''))
what = string.replace(what, ' ', ' & ')
what = string.replace(what, '-', ' ')
query.append('%s=(%s)' % (idx, what))
query = string.join(query, ' & ')
return query
def edbmfind(self, db, query, first, last):
try:
return db.find(query, first, last)
except Exception:
return None
class Factory(Factory):
def make_ti_iso(self, item, name, value):
text = convert.tex2utf8(item[name[:-4]])
return makelinks(text)
make_st_iso = make_pt_iso = make_pu_iso = make_sx_iso = make_ti_iso
def make_msc_section_text(self, item, name, value):
if item.level == 0:
return ''
if item.level == 1:
return item.parent_ti
return item.parent[0].parent[0].ti
def make_msc_section_code(self, item, name, value):
if item.level == 0:
return ''
if item.level == 1:
return item.pa
return item.parent[0].pa
def make_parent_ti(self, item, name, value):
if item.parent:
return item.parent[0].ti
return ''
def make_level(self, item, name, value):
if not item.pa:
return 0
if item.pa[2:] == '-XX':
return 1
return 2
def makelinks(text):
ret = re.sub(r'([0-9][0-9])(-XX)', r'\1\2', text)
ret = re.sub(r'([0-9][0-9][A-Z]xx)', r'\1', ret)
ret = re.sub(r'([0-9][0-9][A-Z])([0-9][0-9])', r'\1\2', ret)
ret = re.sub(r'([0-9][0-9][A-Z])([0-9][0-9])', r'\1\2', ret)
ret = re.sub(r'([0-9][0-9])(-[0-9][0-9])', r'\1\2', ret)
return ret