home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- import socket
- import re
- version = '1.0'
-
- def dequote(str):
- quotechars = '\'"'
- while len(str) and str[0] in quotechars:
- str = str[1:]
- while len(str) and str[-1] in quotechars:
- str = str[0:-1]
- return str
-
-
- def enquote(str):
- return '"' + str.replace('"', '\\"') + '"'
-
-
- class Connection:
-
- def __init__(self, hostname = 'localhost', port = 2628):
- self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- self.sock.connect((hostname, port))
- self.rfile = self.sock.makefile('rt')
- self.wfile = self.sock.makefile('wt', 0)
- self.saveconnectioninfo()
-
-
- def getresultcode(self):
- line = self.rfile.readline().strip()
- (code, text) = line.split(' ', 1)
- return [
- int(code),
- text]
-
-
- def get200result(self):
- (code, text) = self.getresultcode()
- if code < 200 or code >= 300:
- raise Exception, "Got '%s' when 200-class response expected" % text
- code >= 300
- return [
- code,
- text]
-
-
- def get100block(self):
- data = []
- while None:
- line = self.rfile.readline().strip()
- if line == '.':
- break
-
- continue
- return '\n'.join(data)
-
-
- def get100result(self):
- (code, text) = self.getresultcode()
- if code < 100 or code >= 200:
- raise Exception, "Got '%s' when 100-class response expected" % code
- code >= 200
- bodylines = self.get100block().split('\n')
- code2 = self.get200result()[0]
- return [
- code,
- bodylines,
- code2]
-
-
- def get100dict(self):
- dict = { }
- for line in self.get100result()[1]:
- (key, val) = line.split(' ', 1)
- dict[key] = dequote(val)
-
- return dict
-
-
- def saveconnectioninfo(self):
- (code, string) = self.get200result()
- (capstr, msgid) = re.search('<(.*)> (<.*>)$', string).groups()
- self.capabilities = capstr.split('.')
- self.messageid = msgid
-
-
- def getcapabilities(self):
- return self.capabilities
-
-
- def getmessageid(self):
- return self.messageid
-
-
- def getdbdescs(self):
- if hasattr(self, 'dbdescs'):
- return self.dbdescs
- self.sendcommand('SHOW DB')
- self.dbdescs = self.get100dict()
- return self.dbdescs
-
-
- def getstratdescs(self):
- if hasattr(self, 'stratdescs'):
- return self.stratdescs
- self.sendcommand('SHOW STRAT')
- self.stratdescs = self.get100dict()
- return self.stratdescs
-
-
- def getdbobj(self, dbname):
- if not hasattr(self, 'dbobjs'):
- self.dbobjs = { }
-
- if self.dbobjs.has_key(dbname):
- return self.dbobjs[dbname]
- if dbname != '*' and dbname != '!' and dbname not in self.dbdescs.keys():
- raise Exception, "Invalid database name '%s'" % dbname
- dbname not in self.dbdescs.keys()
- self.dbobjs[dbname] = Database(self, dbname)
- return self.dbobjs[dbname]
-
-
- def sendcommand(self, command):
- self.wfile.write(command + '\n')
-
-
- def define(self, database, word):
- self.getdbdescs()
- if database != '*' and database != '!' and database not in self.getdbdescs():
- raise Exception, "Invalid database '%s' specified" % database
- database not in self.getdbdescs()
- self.sendcommand('DEFINE ' + enquote(database) + ' ' + enquote(word))
- code = self.getresultcode()[0]
- retval = []
- if code == 552:
- return []
- if code != 150:
- raise Exception, 'Unknown code %d' % code
- code != 150
- while None:
- (code, text) = self.getresultcode()
- if code != 151:
- break
-
- (resultword, resultdb) = re.search('^"(.+)" (\\S+)', text).groups()
- defstr = self.get100block()
- continue
- return retval
-
-
- def match(self, database, strategy, word):
- self.getstratdescs()
- self.getdbdescs()
- if strategy not in self.getstratdescs().keys():
- raise Exception, "Invalid strategy '%s'" % strategy
- strategy not in self.getstratdescs().keys()
- if database != '*' and database != '!' and database not in self.getdbdescs().keys():
- raise Exception, "Invalid database name '%s'" % database
- database not in self.getdbdescs().keys()
- self.sendcommand('MATCH %s %s %s' % (enquote(database), enquote(strategy), enquote(word)))
- code = self.getresultcode()[0]
- if code == 552:
- return []
- if code != 152:
- raise Exception, 'Unexpected code %d' % code
- code != 152
- retval = []
- for matchline in self.get100block().split('\n'):
- (matchdict, matchword) = matchline.split(' ', 1)
- retval.append(Definition(self, self.getdbobj(matchdict), dequote(matchword)))
-
- if self.getresultcode()[0] != 250:
- raise Exception, 'Unexpected end-of-list code %d' % code
- self.getresultcode()[0] != 250
- return retval
-
-
-
- class Database:
-
- def __init__(self, dictconn, dbname):
- self.conn = dictconn
- self.name = dbname
-
-
- def getname(self):
- return self.name
-
-
- def getdescription(self):
- if hasattr(self, 'description'):
- return self.description
- if self.getname() == '*':
- self.description = 'All Databases'
- elif self.getname() == '!':
- self.description = 'First matching database'
- else:
- self.description = self.conn.getdbdescs()[self.getname()]
- return self.description
-
-
- def getinfo(self):
- if hasattr(self, 'info'):
- return self.info
- if self.getname() == '*':
- self.info = 'This special database will search all databases on the system.'
- elif self.getname() == '!':
- self.info = 'This special database will return matches from the first matching database.'
- else:
- self.conn.sendcommand('SHOW INFO ' + self.name)
- self.info = '\n'.join(self.conn.get100result()[1])
- return self.info
-
-
- def define(self, word):
- return self.conn.define(self.getname(), word)
-
-
- def match(self, strategy, word):
- return self.conn.match(self.getname(), strategy, word)
-
-
-
- class Definition:
-
- def __init__(self, dictconn, db, word, defstr = None):
- self.conn = dictconn
- self.db = db
- self.word = word
- self.defstr = defstr
-
-
- def getdb(self):
- return self.db
-
-
- def getdefstr(self):
- if not self.defstr:
- self.defstr = self.conn.define(self.getdb().getname(), self.word)[0].getdefstr()
-
- return self.defstr
-
-
- def getword(self):
- return self.word
-
-
-