pyoro.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 import socket, json
00003 
00004 class OroServerError(Exception):
00005         def __init__(self, value):
00006                 self.value = value
00007         def __str__(self):
00008                 return repr(self.value)
00009         
00010 class Oro(object):
00011         def __init__(self, host, port):
00012                 #create an INET, STREAMing socket
00013                 self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
00014 
00015                 #now connect to the oro server
00016                 self.s.connect((host, port))
00017                 self.server = self.s.makefile()
00018                 
00019                 #get the list of methods currenlty implemented by the server
00020                 try:            
00021                         res = self.call_server(["listSimpleMethods"])
00022                         self.rpc_methods = [(t.split('(')[0], len(t.split(','))) for t in res]
00023                 except OroServerError:
00024                         print('Cannot initialize the oro connector! Smthg wrong with the server!')
00025                         self.server.close()
00026                         self.s.close()                  
00027                         exit()                     
00028                 
00029                 #add the the Oro class all the methods the server declares
00030                 for m in self.rpc_methods:
00031                         self.add_methods(m)
00032 
00033         def __del__(self):
00034                 close()
00035         
00036         def call_server(self, req):
00037                 for r in req:           
00038                         self.server.write(r)
00039                         self.server.write("\n")
00040                         
00041                 self.server.write("#end#\n")
00042                 self.server.flush()
00043                 
00044                 status = self.server.readline().rstrip('\n')
00045                 
00046                 if status == 'ok':
00047                         raw = self.server.readline().rstrip('\n')
00048                         self.server.readline() #remove the trailing #end#
00049                         if raw == '':
00050                                 return
00051                         #print "Raw: " + raw
00052                         res = encode8bits(json.loads(raw))
00053                         #print "After JSON loading: " + str(res)
00054                         return res
00055                 else:
00056                         msg = self.server.readline().rstrip('\n') + ": " + self.server.readline().rstrip('\n')
00057                         self.server.readline() #remove the trailing #end#
00058                         raise OroServerError(msg)
00059                 
00060         def add_methods(self, m):
00061                 def innermethod(*args):
00062                         req = ["%s" % m[0]]
00063                         for a in args:
00064                                 req.append(a)
00065                         return self.call_server(req)
00066                                 
00067                 innermethod.__doc__ = "This method is a proxy for the oro-server %s method." % m[0]
00068                 innermethod.__name__ = m[0]
00069                 setattr(self,innermethod.__name__,innermethod)
00070         
00071         def close(self):
00072                 print('Closing the connection to ORO...')
00073                 self.server.close()
00074                 self.s.close()
00075                 print('Done. Bye bye!')
00076 
00077 
00078 def encode8bits(o):
00079         if isinstance(o, list):
00080                 return [encode8bits(o2) for o2 in o]
00081         return o.encode('ascii')
00082 
00083 
00084 if __name__ == '__main__':
00085 
00086         HOST = 'localhost'      # ORO-server host
00087         PORT = 6969             # ORO-server port
00088 
00089         try:
00090                 oro = Oro(HOST, PORT)
00091                 
00092                 oro.add("[johnny rdf:type Human, johnny rdfs:label \"A que Johnny\"]")
00093                 oro.add("[hrp2 rdf:type Robot]")
00094                 print(oro.lookup("A que Johnny")[0])
00095                 print(oro.getSimilarities("johnny", "hrp2"))
00096                 print(oro.getDifferences("johnny", "hrp2"))
00097         except OroServerError as ose:
00098                 print('Oups! An error occured!')
00099                 print(ose)
00100         finally:
00101                 oro.close()
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Friends


oro_ros
Author(s): Séverin Lemaignan
autogenerated on Thu May 23 2013 14:57:24