home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1393 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  8.8 KB  |  251 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import socket
  5. import re
  6. version = '1.0'
  7.  
  8. def dequote(str):
  9.     quotechars = '\'"'
  10.     while len(str) and str[0] in quotechars:
  11.         str = str[1:]
  12.     while len(str) and str[-1] in quotechars:
  13.         str = str[0:-1]
  14.     return str
  15.  
  16.  
  17. def enquote(str):
  18.     return '"' + str.replace('"', '\\"') + '"'
  19.  
  20.  
  21. class Connection:
  22.     
  23.     def __init__(self, hostname = 'localhost', port = 2628):
  24.         self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  25.         self.sock.connect((hostname, port))
  26.         self.rfile = self.sock.makefile('rt')
  27.         self.wfile = self.sock.makefile('wt', 0)
  28.         self.saveconnectioninfo()
  29.  
  30.     
  31.     def getresultcode(self):
  32.         line = self.rfile.readline().strip()
  33.         (code, text) = line.split(' ', 1)
  34.         return [
  35.             int(code),
  36.             text]
  37.  
  38.     
  39.     def get200result(self):
  40.         (code, text) = self.getresultcode()
  41.         if code < 200 or code >= 300:
  42.             raise Exception, "Got '%s' when 200-class response expected" % text
  43.         code >= 300
  44.         return [
  45.             code,
  46.             text]
  47.  
  48.     
  49.     def get100block(self):
  50.         data = []
  51.         while None:
  52.             line = self.rfile.readline().strip()
  53.             if line == '.':
  54.                 break
  55.             
  56.             continue
  57.             return '\n'.join(data)
  58.  
  59.     
  60.     def get100result(self):
  61.         (code, text) = self.getresultcode()
  62.         if code < 100 or code >= 200:
  63.             raise Exception, "Got '%s' when 100-class response expected" % code
  64.         code >= 200
  65.         bodylines = self.get100block().split('\n')
  66.         code2 = self.get200result()[0]
  67.         return [
  68.             code,
  69.             bodylines,
  70.             code2]
  71.  
  72.     
  73.     def get100dict(self):
  74.         dict = { }
  75.         for line in self.get100result()[1]:
  76.             (key, val) = line.split(' ', 1)
  77.             dict[key] = dequote(val)
  78.         
  79.         return dict
  80.  
  81.     
  82.     def saveconnectioninfo(self):
  83.         (code, string) = self.get200result()
  84.         (capstr, msgid) = re.search('<(.*)> (<.*>)$', string).groups()
  85.         self.capabilities = capstr.split('.')
  86.         self.messageid = msgid
  87.  
  88.     
  89.     def getcapabilities(self):
  90.         return self.capabilities
  91.  
  92.     
  93.     def getmessageid(self):
  94.         return self.messageid
  95.  
  96.     
  97.     def getdbdescs(self):
  98.         if hasattr(self, 'dbdescs'):
  99.             return self.dbdescs
  100.         self.sendcommand('SHOW DB')
  101.         self.dbdescs = self.get100dict()
  102.         return self.dbdescs
  103.  
  104.     
  105.     def getstratdescs(self):
  106.         if hasattr(self, 'stratdescs'):
  107.             return self.stratdescs
  108.         self.sendcommand('SHOW STRAT')
  109.         self.stratdescs = self.get100dict()
  110.         return self.stratdescs
  111.  
  112.     
  113.     def getdbobj(self, dbname):
  114.         if not hasattr(self, 'dbobjs'):
  115.             self.dbobjs = { }
  116.         
  117.         if self.dbobjs.has_key(dbname):
  118.             return self.dbobjs[dbname]
  119.         if dbname != '*' and dbname != '!' and dbname not in self.dbdescs.keys():
  120.             raise Exception, "Invalid database name '%s'" % dbname
  121.         dbname not in self.dbdescs.keys()
  122.         self.dbobjs[dbname] = Database(self, dbname)
  123.         return self.dbobjs[dbname]
  124.  
  125.     
  126.     def sendcommand(self, command):
  127.         self.wfile.write(command + '\n')
  128.  
  129.     
  130.     def define(self, database, word):
  131.         self.getdbdescs()
  132.         if database != '*' and database != '!' and database not in self.getdbdescs():
  133.             raise Exception, "Invalid database '%s' specified" % database
  134.         database not in self.getdbdescs()
  135.         self.sendcommand('DEFINE ' + enquote(database) + ' ' + enquote(word))
  136.         code = self.getresultcode()[0]
  137.         retval = []
  138.         if code == 552:
  139.             return []
  140.         if code != 150:
  141.             raise Exception, 'Unknown code %d' % code
  142.         code != 150
  143.         while None:
  144.             (code, text) = self.getresultcode()
  145.             if code != 151:
  146.                 break
  147.             
  148.             (resultword, resultdb) = re.search('^"(.+)" (\\S+)', text).groups()
  149.             defstr = self.get100block()
  150.             continue
  151.             return retval
  152.  
  153.     
  154.     def match(self, database, strategy, word):
  155.         self.getstratdescs()
  156.         self.getdbdescs()
  157.         if strategy not in self.getstratdescs().keys():
  158.             raise Exception, "Invalid strategy '%s'" % strategy
  159.         strategy not in self.getstratdescs().keys()
  160.         if database != '*' and database != '!' and database not in self.getdbdescs().keys():
  161.             raise Exception, "Invalid database name '%s'" % database
  162.         database not in self.getdbdescs().keys()
  163.         self.sendcommand('MATCH %s %s %s' % (enquote(database), enquote(strategy), enquote(word)))
  164.         code = self.getresultcode()[0]
  165.         if code == 552:
  166.             return []
  167.         if code != 152:
  168.             raise Exception, 'Unexpected code %d' % code
  169.         code != 152
  170.         retval = []
  171.         for matchline in self.get100block().split('\n'):
  172.             (matchdict, matchword) = matchline.split(' ', 1)
  173.             retval.append(Definition(self, self.getdbobj(matchdict), dequote(matchword)))
  174.         
  175.         if self.getresultcode()[0] != 250:
  176.             raise Exception, 'Unexpected end-of-list code %d' % code
  177.         self.getresultcode()[0] != 250
  178.         return retval
  179.  
  180.  
  181.  
  182. class Database:
  183.     
  184.     def __init__(self, dictconn, dbname):
  185.         self.conn = dictconn
  186.         self.name = dbname
  187.  
  188.     
  189.     def getname(self):
  190.         return self.name
  191.  
  192.     
  193.     def getdescription(self):
  194.         if hasattr(self, 'description'):
  195.             return self.description
  196.         if self.getname() == '*':
  197.             self.description = 'All Databases'
  198.         elif self.getname() == '!':
  199.             self.description = 'First matching database'
  200.         else:
  201.             self.description = self.conn.getdbdescs()[self.getname()]
  202.         return self.description
  203.  
  204.     
  205.     def getinfo(self):
  206.         if hasattr(self, 'info'):
  207.             return self.info
  208.         if self.getname() == '*':
  209.             self.info = 'This special database will search all databases on the system.'
  210.         elif self.getname() == '!':
  211.             self.info = 'This special database will return matches from the first matching database.'
  212.         else:
  213.             self.conn.sendcommand('SHOW INFO ' + self.name)
  214.             self.info = '\n'.join(self.conn.get100result()[1])
  215.         return self.info
  216.  
  217.     
  218.     def define(self, word):
  219.         return self.conn.define(self.getname(), word)
  220.  
  221.     
  222.     def match(self, strategy, word):
  223.         return self.conn.match(self.getname(), strategy, word)
  224.  
  225.  
  226.  
  227. class Definition:
  228.     
  229.     def __init__(self, dictconn, db, word, defstr = None):
  230.         self.conn = dictconn
  231.         self.db = db
  232.         self.word = word
  233.         self.defstr = defstr
  234.  
  235.     
  236.     def getdb(self):
  237.         return self.db
  238.  
  239.     
  240.     def getdefstr(self):
  241.         if not self.defstr:
  242.             self.defstr = self.conn.define(self.getdb().getname(), self.word)[0].getdefstr()
  243.         
  244.         return self.defstr
  245.  
  246.     
  247.     def getword(self):
  248.         return self.word
  249.  
  250.  
  251.