Hi!

Attached is my modified caps.py with logging.
Also attached is example.py as an example of how to configure logging.

cheers,

Christian


-- 
Christian Scholz                          Homepage: http://comlounge.net
COM.lounge                                   blog: http://mrtopf.de/blog
Luetticher Strasse 10                                    Skype: HerrTopf
52064 Aachen                             Video Blog: http://comlounge.tv
Tel: +49 241 400 730 0                           E-Mail [EMAIL PROTECTED]
Fax: +49 241 979 00 850                               IRC: MrTopf, Tao_T

neue Show: TOPFtäglich (http://mrtopf.de/blog/category/topf-taglich/)
# std lib
import urllib2
from logging import getLogger, CRITICAL, ERROR, WARNING, INFO, DEBUG

# ZCA
from zope.interface import implements
from zope.component import queryUtility, adapts, getUtility
import grokcore.component as grok

# related
from indra.base import llsd

# pyogp
from interfaces import ICapability, ISeedCapability
from interfaces import ISerialization, IDeserialization
from network import IRESTClient, HTTPError

logger = getLogger('pyogp.lib.base.caps')
log = logger.log

class Capability(object):
    """models a capability"""
    
    implements(ICapability)
    
    def __init__(self, name, public_url):
        """initialize the capability"""
        
        self.name = name
        self.public_url = public_url
        log(DEBUG, '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))

        # TODO: better errorhandling with own exceptions
        try:
            restclient = getUtility(IRESTClient)
            response = restclient.GET(self.public_url)
        except HTTPError, e:
            print "** failure while calling cap:",
            print e.fp.read(), e.code
            raise
  
        # now deserialize the data again, we ask for a utility with the content 
type
        # as the name
        content_type_charset = response.headers['Content-Type']
        content_type = content_type_charset.split(";")[0] # remove the charset 
part
        
        deserializer = queryUtility(IDeserialization,name=content_type)
        if deserializer is None:
            # TODO: do better error handling here
            raise "deserialization for %s not supported" %(content_type)
        return deserializer.deserialize_string(response.body)


    def POST(self,payload,custom_headers={}):
        """call this capability, return the parsed result"""
        
        # serialize the data
        serializer = ISerialization(payload)
        content_type = serializer.content_type
        serialized_payload = serializer.serialize()
        
        headers = {"Content-type" : content_type}
        headers.update(custom_headers)  # give the user the ability to add 
headers 
        
        # TODO: better errorhandling with own exceptions
        try:
            restclient = getUtility(IRESTClient)
            response = restclient.POST(self.public_url, serialized_payload, 
headers=headers)
        except HTTPError, e:
            print "** failure while calling cap:",
            print e.fp.read(), e.code            
            raise
            
        # now deserialize the data again, we ask for a utility with the content 
type
        # as the name
        content_type_charset = response.headers['Content-Type']
        content_type = content_type_charset.split(";")[0] # remove the charset 
part
        
        deserializer = queryUtility(IDeserialization,name=content_type)
        if deserializer is None:
            # TODO: do better error handling here
            raise Exception("deserialization for %s not supported" 
%(content_type))
        return deserializer.deserialize_string(response.body)
        
    def __repr__(self):
        return "<Capability '%s' for %s>" %(self.name, self.public_url)
        
class SeedCapability(Capability):
    """a seed capability which is able to retrieve other capabilities"""
    
    implements(ISeedCapability)
        
    def get(self, names=[]):
        """if this is a seed cap we can retrieve other caps here"""
        payload = {'caps':names} 
        parsed_result = self.POST(payload)['caps']
        
        caps = {}
        for name in names:
            # TODO: some caps might be seed caps, how do we know? 
            caps[name]=Capability(name, parsed_result[name])
            
        return caps
             
        
    def __repr__(self):
        return "<SeedCapability for %s>" %self.public_url
        
####
#### Serialization adapters
####
class ListLLSDSerializer(grok.Adapter):
    """adapter for serializing a dictionary to LLSD
    
    An example:
    >>> d={'foo':'bar', 'test':1234}
    >>> serializer = ISerialization(d)
    >>> serializer.serialize()
    '<?xml version="1.0" 
?><llsd><map><key>test</key><integer>1234</integer><key>foo</key><string>bar</string></map></llsd>'
    >>> serializer.content_type
    'application/llsd+xml'
    
    """
    grok.implements(ISerialization)
    grok.context(list)
    
    def __init__(self, context):
        self.context = context
        
    def serialize(self):
        """convert the payload to LLSD"""
        return llsd.format_xml(self.context)
        
    @property
    def content_type(self):
        """return the content type of this serializer"""
        return "application/llsd+xml"


class DictLLSDSerializer(grok.Adapter):
    """adapter for serializing a dictionary to LLSD
    
    An example:
    >>> d={'foo':'bar', 'test':1234}
    >>> serializer = ISerialization(d)
    >>> serializer.serialize()
    '<?xml version="1.0" 
?><llsd><map><key>test</key><integer>1234</integer><key>foo</key><string>bar</string></map></llsd>'
    >>> serializer.content_type
    'application/llsd+xml'
    
    """
    grok.implements(ISerialization)
    grok.context(dict)
    
    def __init__(self, context):
        self.context = context
        
    def serialize(self):
        """convert the payload to LLSD"""
        return llsd.format_xml(self.context)
        
    @property
    def content_type(self):
        """return the content type of this serializer"""
        return "application/llsd+xml"
        
class LLSDDeserializer(grok.GlobalUtility):
    """utility for deserializing LLSD data
    
    The deserialization component is defined as a utility because the input
    data can be a string or a file. It might be possible to define this as 
    an adapter on a string but a string is too generic for this. So that's 
    why it is a utility.
    
    You can use it like this:
    
    >>> s='<?xml version="1.0" 
?><llsd><map><key>test</key><integer>1234</integer><key>foo</key><string>bar</string></map></llsd>'
    
    We use queryUtility because this returns None instead of an exception
    when a utility is not registered. We use the content type we received
    as the name of the utility. Another option would of course be to subclas
    string to some LLSDString class and use an adapter. We then would need some
    factory for generating the LLSDString class from whatever came back from
    the HTTP call.
    
    So here is how you use that utility:
    >>> deserializer = 
queryUtility(IDeserialization,name="application/llsd+xml")
    >>> llsd = deserializer.deserialize_string(s)
    >>> llsd
    {'test': 1234, 'foo': 'bar'}
    
    """
    grok.implements(IDeserialization)
    grok.name('application/llsd+xml')
    
    def deserialize_string(self, data):
        """deserialize a string"""
        return llsd.parse(data)
        
    def deserialize_file(self, fp):
        """deserialize a file"""
        data = fp.read()
        return self.deserialize_string(data)
        
# TODO: remove this again! Just a workaround for SVC-2682
grok.global_utility(LLSDDeserializer,
                                  provides=IDeserialization,
                                  name='text/html',
                                  direct=False)

# TODO: remove this again! Just a workaround for SVC-2720
grok.global_utility(LLSDDeserializer,
                                  provides=IDeserialization,
                                  name='application/xml',
                                  direct=False)



from pyogp.lib.base.credentials import PlainPasswordCredential
from pyogp.lib.base.agentdomain import AgentDomain
from pyogp.lib.base.regiondomain import Region
from pyogp.lib.base import registration

from pyogp.lib.base.interfaces import IPlaceAvatar, IEventQueueGet

import getpass, sys
from optparse import OptionParser


class ExampleLogin(object):
    
    def login(self):
        """login an agent and place it on a region"""
        # set up logging
        import logging
        
        
        
        registration.init()
        parser = OptionParser()

        parser.add_option("-a", "--agentdomain", dest="loginuri", 
default="https://login1.aditi.lindenlab.com/cgi-bin/auth.cgi";,
                          help="URI of Agent Domain")
        parser.add_option("-r", "--region", dest="regionuri", 
default="http://sim1.vaak.lindenlab.com:13000";,
                          help="URI of Region to connect to")

        (options, args) = parser.parse_args()

        firstname = args[0]
        lastname = args[1]
        password = getpass.getpass()

        credentials = PlainPasswordCredential(firstname, lastname, password)

        agentdomain = AgentDomain(options.loginuri)
        agent = agentdomain.login(credentials)

        print "logged in, we now have an agent: ", agent

        place = IPlaceAvatar(agentdomain)
        region = Region(options.regionuri)

        print "now we try to place the avatar on a region"
        avatar = place(region)
        print avatar.region.details
        
        # now get an event_queue_get cap
        eqg = IEventQueueGet(agentdomain)
        print "we got an event queue cap: ", eqg.cap

        print "calling it!"
        result = eqg()
        print "returned: %s" %result

        print "calling it!"
        result = eqg()
        print "returned: %s" %result

        print "calling it!"
        result = eqg()
        print "returned: %s" %result
        
        
        #avatar.establish_presence()
        # 
def main():
    return ExampleLogin().login()    

if __name__=="__main__":
    main()
_______________________________________________
Click here to unsubscribe or manage your list subscription:
https://lists.secondlife.com/cgi-bin/mailman/listinfo/pyogp

Reply via email to