예제 #1
0
파일: toolkit.py 프로젝트: dimitrs/rctk
    def __init__(self, app, debug=False, polling=0, 
                 title="RCTK", *args, **kw):
        super(Toolkit, self).__init__()
        self.app = app
        self._queue = []
        self._controls = {}
        self._root = Root(self)
        self.debug = debug
        self.polling = polling
        self.title = title

        self.args = args
        self.kw = kw
        self.timers = TimerManager(self)
        self.startupdir = os.getcwd() 
예제 #2
0
파일: toolkit.py 프로젝트: dimitrs/rctk
class Toolkit(object):

    def __init__(self, app, debug=False, polling=0, 
                 title="RCTK", *args, **kw):
        super(Toolkit, self).__init__()
        self.app = app
        self._queue = []
        self._controls = {}
        self._root = Root(self)
        self.debug = debug
        self.polling = polling
        self.title = title

        self.args = args
        self.kw = kw
        self.timers = TimerManager(self)
        self.startupdir = os.getcwd() 

    def serve(self, name):
        # serve a app-specific, dynamic resource
        if name.startswith('dynamic/'):
            elements = name.split('/')
            resource = getResourceRegistry().get_resource(elements[1], elements)
            return (resource.type, resource.data)
        raise KeyError(name)

    def add_control(self, control):
        self._controls[control.id] = control

    def create_control(self, control, **extra):
        ## assert control.id in self._controls ?
        ## XXX everything lives in the same namespace; a property with name
        ## 'id' or 'action' will break this needlessly!
        taskdata = dict(control=control.name, id=control.id, action='create')
        taskdata.update(control.data())
        taskdata.update(extra)

        self.queue(Task("Create " + repr(control), taskdata))
        control.state = Control.CREATED

    def call(self, control, method, *args):
        taskdata = dict(control=control.name, id=control.id, action="call")
        taskdata['method'] = method
        taskdata['args'] = args
        self.queue(Task("Call" + repr(control), taskdata))

    def root(self):
        return self._root

    def queue(self, item):
        """ queue a new item """
        self._queue.append(item)

    def handle(self, method, **args):
        if method == "start":
            if len(self._controls) > 1:
                # app is already running, resume session by restoring UI
                ## restore is a recursive process, start with the root
                ## and toplevels
                self._root.restore()
                ## iterate over toplevels 
                #for id, c in self._controls.items():
                #    c.restore()
            else:
                self.app.run(self)

            return dict(state="started", 
                        config=dict(debug=self.debug,
                                    polling=self.polling,
                                    title=self.title)
                       )
        if method == "task" and 'queue' in args:
            queue = json.loads(args['queue'])
            for task in queue:
                tasktype = task['method']
                id = int(task['id'])
                if tasktype == "event":
                    eventtype = task.get('type')
                    if eventtype == "timer":
                        self.timers.fire(id)
                    else:
                        control = self._controls[id]

                        ## A disabled control shouldn't receive events
                        if not control.enabled:
                            continue

                        dispatcher(eventtype, control, **un_unicode(task.get('data', {})))
                elif tasktype == "sync":
                    control = self._controls[id]
                    control.sync(**un_unicode(task.get('data', {})))

        ## any other case, including "pop" which is not handled explicitly
        ## right now.
        if self._queue:
            tasks = [x.task() for x in self._queue]
            self._queue = []
            return tasks
        return []

    def set_timer(self, handler, millis):
        """ set a timer which fires once after "millis" milliseconds 

            returns a reference that can be used to delete the timer
            if neccesary
        """
        return self.timers.set_timer(handler, millis)