modules/DICT.py
changeset 32 dcb1b0459f1c
parent 18 3a35dd9adc73
child 33 adf562e77977
equal deleted inserted replaced
31:a707bfd53b26 32:dcb1b0459f1c
     4 def split_and_remove_quotes(str):
     4 def split_and_remove_quotes(str):
     5     pos = string.find(str, " ")
     5     pos = string.find(str, " ")
     6     return (str[ : pos], str[pos + 2 : -1])
     6     return (str[ : pos], str[pos + 2 : -1])
     7 
     7 
     8 class DictError(Exception):
     8 class DictError(Exception):
     9     
     9 
    10     def __init__(self, msg, code):
    10     def __init__(self, msg, code):
    11         self._code = code
    11         self._code = code
    12 
    12 
    13     def get_code(self):
    13     def get_code(self):
    14         return self._code
    14         return self._code
    36 
    36 
    37     def get_database_info(self, db):
    37     def get_database_info(self, db):
    38         return self._interpret_multiline(self._send("SHOW INFO %s" % db))
    38         return self._interpret_multiline(self._send("SHOW INFO %s" % db))
    39 
    39 
    40     def get_server_info(self):
    40     def get_server_info(self):
    41         return self._interpret_multiline(self._send("SHOW SERVER"), "\r\n")
    41         return self._interpret_multiline(self._send("SHOW SERVER"))
    42     
    42 
    43     def get_definition(self, word, db = "*"):
    43     def get_definition(self, word, db = "*"):
    44         try:
    44         try:
    45             return self._interpret_multiresp(self._send("DEFINE %s %s" %
    45             return self._interpret_multiresp(self._send("DEFINE %s %s" %
    46                                                         (db, word),
    46                                                         (db, word),
    47                                                         "\r\n.\r\n250"))
    47                                                         "\r\n.\r\n250"))
    59         except DictError, e:
    59         except DictError, e:
    60             if e.get_code() == 552:
    60             if e.get_code() == 552:
    61                 return []
    61                 return []
    62 
    62 
    63             raise
    63             raise
    64     
    64 
    65     def _send(self, cmd, resp_term = "\r\n.\r\n"):
    65     def _send(self, cmd, resp_term = "\r\n.\r\n"):
    66         self._conn.send(cmd + "\r\n")
    66         self._conn.send(cmd + "\r\n")
    67         self._verify(self._conn.recv(1024 * 16))
    67         self._verify(self._conn.recv(1024 * 16))
    68         return self._receive_response(resp_term)
    68         return self._receive_response(resp_term)
    69 
    69 
    82             if line == ".":
    82             if line == ".":
    83                 resps.append(lines)
    83                 resps.append(lines)
    84                 lines = []
    84                 lines = []
    85             else:
    85             else:
    86                 lines.append(line)
    86                 lines.append(line)
    87                     
    87 
    88         return resps
    88         return resps
    89 
    89 
    90     def _receive_response(self, look_for):
    90     def _receive_response(self, look_for):
    91         resp = self._conn.recv(1024 * 16)
    91         resp = self._conn.recv(1024 * 16)
    92         while string.find(resp, look_for) == -1:
    92         while string.find(resp, look_for) == -1:
    93             resp = resp + self._conn.recv(1024 * 16)
    93             resp = resp + self._conn.recv(1024 * 16)
    94 
    94 
    95         return resp
    95         return resp
    96     
    96 
    97 # ---
    97 # ---
    98 
    98 
    99 if __name__ == "__main__":
    99 if __name__ == "__main__":
   100     from pprint import pprint
   100     from pprint import pprint
   101 
   101