diff --git a/pyogp/lib/base/agentdomain.py b/pyogp/lib/base/agentdomain.py index aa34051..956ae99 100644 --- a/pyogp/lib/base/agentdomain.py +++ b/pyogp/lib/base/agentdomain.py @@ -33,12 +33,24 @@ class AgentDomain(object): def __init__(self,uri): """initialize the agent domain endpoint""" self.uri = uri + self.credentials = None + self.loginStatus = False log(DEBUG, 'initializing agent domain: %s' %self) def login(self, credentials): """login to the agent domain and return an agent object""" - log(INFO, 'logging in to %s as %s %s' % (self.uri, credentials.firstname, credentials.lastname)) + response = self.post_to_loginuri(credentials) + + self.eval_login_response(response) + + return Agent(self) + + def post_to_loginuri(self, credentials): + """post to login_uri and return response""" + + self.credentials = credentials + log(INFO, 'logging in to %s as %s %s' % (self.uri, self.credentials.firstname, self.credentials.lastname)) serializer = ISerialization(credentials) # convert to string via adapter payload = serializer.serialize() @@ -53,23 +65,43 @@ class AgentDomain(object): try: response = restclient.POST(self.uri, payload, headers=headers) except HTTPError, error: - if e.code==404: + if error.code==404: raise exc.ResourceNotFound(self.uri) else: raise exc.ResourceError(self.uri, error.code, error.msg, error.fp.read(), method="POST") - - seed_cap_url_data = response.body - seed_cap_url = llsd.parse(seed_cap_url_data)['agent_seed_capability'] - self.seed_cap = SeedCapability('seed_cap', seed_cap_url) - - if self.seed_cap is None: - raise exc.UserNotAuthorized(self.credentials) - else: + + return response + + def eval_login_response(self, response): + """ parse the login uri response and return an agent object """ + + seed_cap_url_data = self.parse_login_response(response) + try: + seed_cap_url = seed_cap_url_data['agent_seed_capability'] + self.seed_cap = SeedCapability('seed_cap', seed_cap_url) + self.loginStatus = True log(INFO, 'logged in to %s' % (self.uri)) + except KeyError: + raise exc.UserNotAuthorized(self.credentials) return Agent(self) + def parse_login_response(self, response): + """ parse the login uri response and returns deserialized data """ + + data = llsd.parse(response.body) + log(DEBUG, 'deserialized login response body = %s' % (data)) + try: + seed_cap_url = data['agent_seed_capability'] + self.seed_cap = SeedCapability('seed_cap', seed_cap_url) + self.loginStatus = True + log(INFO, 'logged in to %s' % (self.uri)) + except KeyError: + pass + + return data + class PlaceAvatar(grok.Adapter): """handles placing an avatar for an agent object""" grok.implements(IPlaceAvatar) @@ -105,6 +137,9 @@ class PlaceAvatar(grok.Adapter): #AND THE REST region.details = result + + log(DEBUG, 'Full rez_avatar/place response is: %s' % (result)) + return avatar from interfaces import IEventQueueGet diff --git a/pyogp/lib/base/caps.py b/pyogp/lib/base/caps.py index d38d6d8..57a6216 100644 --- a/pyogp/lib/base/caps.py +++ b/pyogp/lib/base/caps.py @@ -29,21 +29,21 @@ class Capability(object): self.name = name self.public_url = public_url - log(DEBUG, 'instantiated cap %s' %self) + log(INFO, 'instantiated cap %s' %self) def GET(self,custom_headers={}): """call this capability, return the parsed result""" - log(INFO, '%s: GETing %s' %(self.name, self.public_url)) + log(DEBUG, '%s: GETing %s' %(self.name, self.public_url)) try: restclient = getUtility(IRESTClient) response = restclient.GET(self.public_url) except HTTPError, e: - if e.code==404: - raise exc.ResourceNotFound(self.public_url) - else: - raise exc.ResourceError(self.public_url, e.code, e.msg, e.fp.read(), method="GET") + if e.code==404: + raise exc.ResourceNotFound(self.public_url) + else: + raise exc.ResourceError(self.public_url, e.code, e.msg, e.fp.read(), method="GET") # now deserialize the data again, we ask for a utility with the content type # as the name @@ -51,15 +51,21 @@ class Capability(object): content_type = content_type_charset.split(";")[0] # remove the charset part deserializer = queryUtility(IDeserialization,name=content_type) + if deserializer is None: - raise exc.DeserializerNotFound(content_type) + raise exc.DeserializerNotFound(content_type) - return deserializer.deserialize_string(response.body) + data = deserializer.deserialize_string(response.body) + log(DEBUG, 'Get of cap %s response is: %s' % (self.public_url, data)) + + return data def POST(self,payload,custom_headers={}): """call this capability, return the parsed result""" + log(DEBUG, 'Sending to cap %s the following payload: %s' %(self.public_url, payload)) + # serialize the data serializer = ISerialization(payload) content_type = serializer.content_type @@ -72,10 +78,10 @@ class Capability(object): restclient = getUtility(IRESTClient) response = restclient.POST(self.public_url, serialized_payload, headers=headers) except HTTPError, e: - if e.code==404: - raise exc.ResourceNotFound(self.public_url) - else: - raise exc.ResourceError(self.public_url, e.code, e.msg, e.fp.read(), method="POST") + if e.code==404: + raise exc.ResourceNotFound(self.public_url) + else: + raise exc.ResourceError(self.public_url, e.code, e.msg, e.fp.read(), method="POST") # now deserialize the data again, we ask for a utility with the content type # as the name @@ -83,9 +89,14 @@ class Capability(object): content_type = content_type_charset.split(";")[0] # remove the charset part deserializer = queryUtility(IDeserialization,name=content_type) + if deserializer is None: - raise exc.DeserializerNotFound(content_type) - return deserializer.deserialize_string(response.body) + raise exc.DeserializerNotFound(content_type) + + data = deserializer.deserialize_string(response.body) + log(DEBUG, 'Post to cap %s response is: %s' % (self.public_url, data)) + + return data def __repr__(self): return "" %(self.name, self.public_url) diff --git a/pyogp/lib/base/example.py b/pyogp/lib/base/example.py index 22fc022..13014ef 100644 --- a/pyogp/lib/base/example.py +++ b/pyogp/lib/base/example.py @@ -28,7 +28,7 @@ def login(): # would be nice to be able to kill threads with Ctrl-C now wouldnt it? # i have yet to see this actually kill a thread tho.... - signal.signal(signal.SIGINT, sigint_handler) + #signal.signal(signal.SIGINT, sigint_handler) registration.init() parser = OptionParser() @@ -106,6 +106,8 @@ class eventQueueGet(Thread): result = self.eqg() self.logger.debug("it returned: %s", result) + + def sigint_handler(signal, frame): global RUNNING RUNNING = False diff --git a/pyogp/lib/base/regiondomain.py b/pyogp/lib/base/regiondomain.py index 1ee9cd9..4a92c8b 100644 --- a/pyogp/lib/base/regiondomain.py +++ b/pyogp/lib/base/regiondomain.py @@ -77,7 +77,7 @@ class EventQueueGet(grok.Adapter): # let's retrieve the cap we need self.seed_cap = self.context.seed_cap # ISeedCapability - log(DEBUG, 'intitializing region domain event queue via the seed cap: %s' % (self.seed_cap_url)) + log(DEBUG, 'intitializing region domain event queue via the seed cap: %s' % (self.seed_cap)) self.cap = self.seed_cap.get(['EventQueueGet'])['EventQueueGet']