Ticket #2332: tg2_pyamf.patch

File tg2_pyamf.patch, 5.3 kB (added by thijs, 9 months ago)
  • main/TGandPyAMF.rst

    old new  
    88.. contents:: Table of Contents 
    99    :depth: 2 
    1010 
    11 PyAMF provides a simple way to talk to Flex applications using the binary AMF protocol.   The main advantages of AMF are that it: 
     11PyAMF provides a simple way to talk to Flash Player applications using the binary AMF protocol.   The main advantages of AMF are that it: 
    1212 
    1313 #. Provides native Flash ActionScript representations of your data, so instantiating it on the client side is almost instantaneous.   
    1414 #. Is understood by the RemoteObject in Flex, so it is very easy to implement remote procedure calls from the Flex client. 
    1515 
    16 PyAMF provides serialization of a variety of native python types, from strings, lists, and dictionaries to datetime objects, elementtree elements, and custom classes.  And a lot of this is automatic, and very cool.  Return a dictionary from your pyamf service, and you'll get an ActionScript hash/object on the other side. 
     16PyAMF provides serialization of a variety of native Python types, from strings, lists, and dictionaries to datetime objects, elementtree elements, and custom classes.  And a lot of this is automatic, and very cool.  Return a dictionary from your PyAMF service, and you'll get an ActionScript hash/object on the other side. 
    1717 
    18 PyAMF provides a simple WSGI Application that can be used to setup RPC style service easily in Python.   And because TG2 supports WSGI from top-to-bottom, it's very simple to setup a TG2 app that contains web-services for your flex applications.  All you need to do is: 
     18PyAMF provides a simple WSGI Application that can be used to setup RPC style service easily in Python.   And because TG2 supports WSGI from top-to-bottom, it's very simple to setup a TG2 app that contains web-services for your Flex applications.  All you need to do is: 
    1919 
    2020 #. Install a bunch of stuff and setup a TG2 app 
    2121 #. Create a PyAMF gateway for your services 
     
    2727Installing Stuff: 
    2828---------------------- 
    2929 
    30 If you haven't installed TG2, you'll need to do that first (http:docs.turbogears.org/2.0/RoughDocs/DownloadInstall).  You'll need TG2 version that's newer than February 20, 2007 for this to work.  All released versions of TG2 should work, but early SVN versions may need to be updated.  Once you've got an up-to-date version of TG2,  you'll need to install PyAMF, which you can do by:: 
     30If you haven't installed TG2, you'll need to do that first (http:docs.turbogears.org/2.0/main/DownloadInstall).  You'll need TG2 version that's newer than February 20, 2007 for this to work.  All released versions of TG2 should work, but early SVN versions may need to be updated.  Once you've got an up-to-date version of TG2,  you'll need to install PyAMF, which you can do by:: 
    3131 
    3232  easy_install pyamf 
    3333 
     
    4848 
    4949 from pyamf.remoting.gateway.wsgi import WSGIGateway 
    5050  
    51  Class Services(object): 
     51 class Services(object): 
    5252  
    53     def echo(data): 
     53    def echo(self, data): 
    5454        return "Turbogears gateway says:" + str(data) 
    5555   
    56     def sum(a, b): 
     56    def sum(self, a, b): 
    5757        return a + b 
    5858 
    59     def scramble(text): 
     59    def scramble(self, text): 
    6060        from random import shuffle 
    6161        s = [x for x in text] 
    6262        shuffle(s) 
     
    6868 GatewayController = WSGIGateway(services) 
    6969 
    7070This sets up a GatewayController WSGI app that has three services that  
    71 can be called from flex: echo, sum, and scramble, which each do exactly what  
     71can be called from Flex: echo, sum, and scramble, which each do exactly what  
    7272they say they do.  
    7373 
    7474Setup a controller that uses the GatewayController WSGI app: 
     
    8888Of course, you'll need to import use_wsgi_app from tg, and your  
    8989GatewayController from wherever you put it. But once you've done those  
    9090things you'll have a AMF Gateway mounted at /gateway which you can use from  
    91 flex.  
     91Flex.  
    9292 
    9393    
    9494Create a Flex Client 
     
    114114        <mx:Button click="remoteObj.scramble(c.text)" label="Scramble"/> 
    115115        <mx:TextInput id="c" text="She sells seashells by the seashore" width="100%"/> 
    116116    </mx:HBox> 
    117     <mx:Text id="result" width="100%" height="100%"/> 
     117    <mx:Text id="result" width="100%" height="100%" /> 
    118118 
    119119    <mx:Script> 
    120120    <![CDATA[ 
     
    131131    } 
    132132 
    133133    private function remoteFault(fault:FaultEvent): void { 
    134         result.text = ObjectUtil.toString(fault)
     134        result.text = fault.fault.faultDetail
    135135    } 
    136136    ]]> 
    137137    </mx:Script> 
    138138    </mx:Application>  
    139139 
    140 You can paste that into a new Flex Builder project (or use the free SDK to create a project with the text editor of your choice).  You can then put the HTML and SWF files generated by the builder into your TG2 project's static directory (wherever you want them to be available) at which point you should be able to browse there, get your Flex app, and use it to connect to the web services you just created.  
     140You can paste that into a new Flex Builder project (or use the free SDK to create a project with the text editor of your choice).  You can then put the HTML and SWF files generated by Flex Builder into your TG2 project's static directory (wherever you want them to be available) at which point you should be able to browse there, get your Flex app, and use it to connect to the web services you just created.  
    141141  
    142142 
    143143