Beispiel #1
0
 def __init__(self, project_path, project_name, appmod):
     self.project_name=project_name
     self.project_path=project_path
     self._appmod=appmod
     self._webregistry=WebRegistry(project_path)
             
     self.webapp_project_path = os.path.join( project_path,             "webapp" )
     self.static_project_path = os.path.join( self.webapp_project_path, "static" )
     self.script_project_path = os.path.join( self.static_project_path, "js"     )
     #iterating over objects and creating routers
     
     self.update_registry()
     
     #initialize handlers
     self._handlers = []
     self._handlers.append((r"/", MainRequestHandler))
     
     self._handlers.append((r"/script/([A-Za-z0-9_]+)", ScriptRequestHandler))
     
     self._handlers.append((r"/remote", RemoteRequestHandler))
     self._handlers.append((r"/websocket", AppEventHandler))
     self._handlers.append((r"/project_static/(.*)", tornado.web.StaticFileHandler, {"path":self.static_project_path}))
     
     super(WebApplication,self).__init__(self._handlers,
                                         cookie_secret=base64.b64encode(uuid.uuid4().bytes + uuid.uuid4().bytes))
     
     self.appmod.auth_module.after_login_signal.connect(self.update_registry)
     self.appmod.auth_module.after_logout_signal.connect(self.update_registry)
Beispiel #2
0
class WebApplication(tornado.web.Application):
    """Web application class is inherited from the standard tornado Application. WebApplication class initializes web registry, request handlers
     and routers for remote method execution."""
    
    def __init__(self, project_path, project_name, appmod):
        self.project_name=project_name
        self.project_path=project_path
        self._appmod=appmod
        self._webregistry=WebRegistry(project_path)
                
        self.webapp_project_path = os.path.join( project_path,             "webapp" )
        self.static_project_path = os.path.join( self.webapp_project_path, "static" )
        self.script_project_path = os.path.join( self.static_project_path, "js"     )
        #iterating over objects and creating routers
        
        self.update_registry()
        
        #initialize handlers
        self._handlers = []
        self._handlers.append((r"/", MainRequestHandler))
        
        self._handlers.append((r"/script/([A-Za-z0-9_]+)", ScriptRequestHandler))
        
        self._handlers.append((r"/remote", RemoteRequestHandler))
        self._handlers.append((r"/websocket", AppEventHandler))
        self._handlers.append((r"/project_static/(.*)", tornado.web.StaticFileHandler, {"path":self.static_project_path}))
        
        super(WebApplication,self).__init__(self._handlers,
                                            cookie_secret=base64.b64encode(uuid.uuid4().bytes + uuid.uuid4().bytes))
        
        self.appmod.auth_module.after_login_signal.connect(self.update_registry)
        self.appmod.auth_module.after_logout_signal.connect(self.update_registry)
        
    def update_registry(self, *args, **kwargs):
        """This function updates a registry of web application. This registry contains information about class names, paths to js files
        and routers. This update happens at initialization of web application and on
        login or logout of user. In the latter case the web interface of application must be reconstructed according to user permissions.
        """
        self._webregistry.clear()
        self._webregistry.register(self._appmod)
        for _, obj in self._appmod.iterate_commands(["view", "execute"]):
            self._webregistry.register(obj)
        for _, obj in self._appmod.iterate_devices ("view"):
            self._webregistry.register(obj)
        for _, obj in self._appmod.iterate_modules ("view"):
            self._webregistry.register(obj)
                                        
    def get_provider_code(self, url=r"/remote", timeout=0, namespace='Pyfrid.router'):
        """returns a javascript code of the direct reauest provider. This code is substituted to index.html template."""
        actions={}
        for name,inst in self._webregistry.iterrouters():
            methods = []
            for mname, mvalue in inspect.getmembers(inst):
                if mname.startswith("_"): continue
                if inspect.ismethod(mvalue):
                    args = inspect.getargspec(mvalue)[0]
                    if "handler" in args:
                        args.remove("self")
                        args.remove("handler")
                        arglen = len(args)
                        methods.append({"name":mname, "len":arglen})
            actions[name]=methods
        config = {
            "type": "remoting",
            "url": url,
            "actions": actions
        }
        if timeout:
            config["timeout"] = timeout
        if namespace:
            config["namespace"] = namespace
        return config
    
    def _process_request(self,handler,request):
        return self._webregistry.get_router(request["action"], exc=True)(handler,request)
    
    def process_remote_request(self,handler,request):
        """This function processes a remote request. It is invoked by the remote request handler."""
        if type(request)!=ListType:    
            return json.dumps(self._process_request(handler,request))
        else:
            return json.dumps([self._process_request(handler,req) for req in request])

    def get_script_info(self, name):
        """..."""
        return self._webregistry.get_info(name, exc=True)

    @property
    def index_args(self):
        """..."""
        return {
            "css":self._webregistry.css,    
            "bases":self._webregistry.bases,
            "scripts"    :self._webregistry.scripts,
            "provider_code":self.get_provider_code(),
            "proj_name":self.project_name,
            "appclass":self._appmod.__class__.__name__,
            "appalias":self._appmod.alias
        }
    
    @property
    def appmod(self):
        """..."""
        return self._appmod