Exemplo n.º 1
0
class XozeContext(object):
    """Context contains information of complete XOZE xml, which defines the moves to be executed in an action, mapping between service and action."""
    def __init__(self, addon_context_files, addon, addon_path,
                 addon_data_path):
        self._addon = addon
        self._addon_path = addon_path
        self._addon_data_path = addon_data_path

        self._xoze = Actions()
        for filepath in addon_context_files:
            self._load_actions(
                file.resolve_file_path(baseDirPath=addon_path,
                                       filename=filepath))
        self._initialize_mvc()

    def _load_actions(self, filepath):
        """loads content of xoze.xml in python objects. this should be STEP 1 for starting application."""
        _filepath = file.resolve_file_path(filepath)
        logging.getLogger().info(
            'reading actions from file: <%s>, which exists: <%s>' %
            (_filepath, file.does_file_exist(_filepath)))

        actions_elem = ET.parse(open(_filepath, "r")).getroot()
        for elem in actions_elem.getchildren():
            if elem.tag == '{0}action'.format(NAMESPACE):
                self._xoze.actions.append(self._load_action(elem))
            elif elem.tag == '{0}service'.format(NAMESPACE):
                self._xoze.services.append(self._load_service(elem))
            elif elem.tag == '{0}view'.format(NAMESPACE):
                self._xoze.views.append(self._load_view(elem))
            else:
                logging.getLogger().error('found UNKNOWN tag: <%s>' % (elem))

    def _load_action(self, elem):
        logging.getLogger().debug('reading action tag: <%s>' %
                                  (elem.attrib['id']))
        action = Action(elem.attrib['id'])
        for move_elem in elem.getchildren():
            action.moves.append(self._load_move(move_elem))
        return action

    def _load_move(self, elem):
        logging.getLogger().debug(
            '----- move from current action: <%s.%s>' %
            (elem.attrib['module'], elem.attrib['function']))
        move = Move(elem.attrib['module'], elem.attrib['function'])
        if (elem.attrib.has_key('view-id')):
            move.set_view_id(elem.attrib['view-id'])
        return move

    def _load_service(self, elem):
        logging.getLogger().debug(
            'reading service tag: <%s> for action: <%s>' %
            (elem.attrib['path'], elem.attrib['action-id']))
        return Service(elem.attrib['path'], elem.attrib['action-id'],
                       elem.attrib['module'], elem.attrib['function'])

    def _load_view(self, elem):
        logging.getLogger().debug('reading view tag: <%s>' %
                                  (elem.attrib['id']))
        view = View(elem.attrib['id'], elem.attrib['module'],
                    elem.attrib['function'])
        for event_elem in elem.getchildren():
            view.events.append(self._load_event(event_elem))
        return view

    def _load_event(self, elem):
        logging.getLogger().debug('----- event from current view: <%s>' %
                                  (elem.attrib['intent']))
        if (elem.attrib.has_key('module') and elem.attrib.has_key('function')):
            if (elem.attrib.has_key('action-id')):
                return Event(elem.attrib['control-id'], elem.attrib['intent'],
                             elem.attrib['action-id'], elem.attrib['module'],
                             elem.attrib['function'])
            else:
                return Event(elem.attrib['control-id'], elem.attrib['intent'],
                             None, elem.attrib['module'],
                             elem.attrib['function'])
        elif (elem.attrib.has_key('action-id')):
            return Event(elem.attrib['control-id'], elem.attrib['intent'],
                         elem.attrib['action-id'])
        else:
            # This case should never happen
            logging.getLogger().error(
                'Loading failed: An event should have at least action-id or module function defined. Exiting now...'
            )
            raise Exception('Loading failed',
                            'event is not having minimum required fields')

    def _initialize_mvc(self):
        """creates controller part MVC which is responsible to initialize and control both Model and View. this should be STEP 2 for starting application."""
        self._action_controller = ActionController(self._xoze.actions,
                                                   self._xoze.views,
                                                   self._addon,
                                                   self._addon_path)

    def get_action_controller(self):
        return self._action_controller

    def do_clean(self):
        """This function clean all objects created by this object and will also call do_clean() functions of its child objects where ever applicable. Call to this function is internal, donot make explicit call."""
        self._action_controller.do_clean()
        del self._addon
        del self._addon_path
        del self._addon_data_path
        del self._xoze
        del self._action_controller
Exemplo n.º 2
0
class XozeContext(object):
    """Context contains information of complete XOZE xml, which defines the moves to be executed in an action, mapping between service and action."""
    
    def __init__(self, addon_context_files, addon, addon_path, addon_data_path):
        self._addon = addon
        self._addon_path = addon_path
        self._addon_data_path = addon_data_path
        
        self._xoze = Actions()
        for filepath in addon_context_files:
            self._load_actions(file.resolve_file_path(baseDirPath=addon_path, filename=filepath))
        self._initialize_mvc()
    
    
    def _load_actions(self, filepath):
        """loads content of xoze.xml in python objects. this should be STEP 1 for starting application."""
        _filepath = file.resolve_file_path(filepath)
        logging.getLogger().info('reading actions from file: <%s>, which exists: <%s>' % (_filepath, file.does_file_exist(_filepath)))
        
        actions_elem = ET.parse(open(_filepath, "r")).getroot()
        for elem in actions_elem.getchildren():
            if elem.tag == '{0}action'.format(NAMESPACE):
                self._xoze.actions.append(self._load_action(elem))
            elif elem.tag == '{0}service'.format(NAMESPACE):
                self._xoze.services.append(self._load_service(elem))
            elif elem.tag == '{0}view'.format(NAMESPACE):
                self._xoze.views.append(self._load_view(elem))
            else:
                logging.getLogger().error('found UNKNOWN tag: <%s>' % (elem))
            
            
    def _load_action(self, elem):
        logging.getLogger().debug('reading action tag: <%s>' % (elem.attrib['id']))
        action = Action(elem.attrib['id'])
        for move_elem in elem.getchildren():
            action.moves.append(self._load_move(move_elem))
        return action

    def _load_move(self, elem):
        logging.getLogger().debug('----- move from current action: <%s.%s>' % (elem.attrib['module'], elem.attrib['function']))
        move = Move(elem.attrib['module'], elem.attrib['function'])
        if(elem.attrib.has_key('view-id')):
            move.set_view_id(elem.attrib['view-id'])
        return move
    
    def _load_service(self, elem):
        logging.getLogger().debug('reading service tag: <%s> for action: <%s>' % (elem.attrib['path'], elem.attrib['action-id']))
        return Service(elem.attrib['path'], elem.attrib['action-id'], elem.attrib['module'], elem.attrib['function'])
    
    
    def _load_view(self, elem):
        logging.getLogger().debug('reading view tag: <%s>' % (elem.attrib['id']))
        view = View(elem.attrib['id'], elem.attrib['module'], elem.attrib['function'])
        for event_elem in elem.getchildren():
            view.events.append(self._load_event(event_elem))
        return view
    
    
    def _load_event(self, elem):
        logging.getLogger().debug('----- event from current view: <%s>' % (elem.attrib['intent']))
        if(elem.attrib.has_key('module') and elem.attrib.has_key('function')):
            if(elem.attrib.has_key('action-id')):
                return Event(elem.attrib['control-id'], elem.attrib['intent'], elem.attrib['action-id'], elem.attrib['module'], elem.attrib['function'])
            else:
                return Event(elem.attrib['control-id'], elem.attrib['intent'], None, elem.attrib['module'], elem.attrib['function'])
        elif(elem.attrib.has_key('action-id')):
            return Event(elem.attrib['control-id'], elem.attrib['intent'], elem.attrib['action-id'])
        else:
            # This case should never happen
            logging.getLogger().error('Loading failed: An event should have at least action-id or module function defined. Exiting now...')
            raise Exception('Loading failed', 'event is not having minimum required fields')
            
        

    def _initialize_mvc(self):
        """creates controller part MVC which is responsible to initialize and control both Model and View. this should be STEP 2 for starting application."""
        self._action_controller = ActionController(self._xoze.actions, self._xoze.views, self._addon, self._addon_path);
        
    
    def get_action_controller(self):
        return self._action_controller
        
        
    def do_clean(self):
        """This function clean all objects created by this object and will also call do_clean() functions of its child objects where ever applicable. Call to this function is internal, donot make explicit call."""
        self._action_controller.do_clean()
        del self._addon
        del self._addon_path
        del self._addon_data_path
        del self._xoze
        del self._action_controller
Exemplo n.º 3
0
 def _initialize_mvc(self):
     """creates controller part MVC which is responsible to initialize and control both Model and View. this should be STEP 2 for starting application."""
     self._action_controller = ActionController(self._xoze.actions,
                                                self._xoze.views,
                                                self._addon,
                                                self._addon_path)
Exemplo n.º 4
0
 def _initialize_mvc(self):
     """creates controller part MVC which is responsible to initialize and control both Model and View. this should be STEP 2 for starting application."""
     self._action_controller = ActionController(self._xoze.actions, self._xoze.views, self._addon, self._addon_path);