Пример #1
0
 def __init__(self):
     if THREAD_PROTECTION:
         self.thread = threading.current_thread()
         self.__tree = MainTree(thread=self.thread)
     else:
         self.__tree = MainTree()
     self.__fbank = FiltersBank(self.__tree)
     self.views = {}
     #main is a reserved name for a viewtree. It is the complete viewtree,
     #without anyfilter
     self.views['main'] = ViewTree(self,self.__tree,self.__fbank,static=True)
     if THREAD_PROTECTION:
         self.views['main'].set_thread(self.thread)
Пример #2
0
class Tree():
    '''A thin wrapper to MainTree that adds filtering capabilities.
        It also provides a few methods to operate complex operation on the
        MainTree (e.g, move_node)
    '''


    def __init__(self):
        if THREAD_PROTECTION:
            self.thread = threading.current_thread()
            self.__tree = MainTree(thread=self.thread)
        else:
            self.__tree = MainTree()
        self.__fbank = FiltersBank(self.__tree)
        self.views = {}
        #main is a reserved name for a viewtree. It is the complete viewtree,
        #without anyfilter
        self.views['main'] = ViewTree(self,self.__tree,self.__fbank,static=True)
        if THREAD_PROTECTION:
            self.views['main'].set_thread(self.thread)

    ###### nodes handling ######
    def get_node(self,nid):
        """
        return the node object defined by the Node id nid.
        raises a ValueError if the node doesn't exist in the tree
        """
        return self.__tree.get_node(nid)
    
    def has_node(self,nid):
        return self.__tree.has_node(nid)

    def add_node(self,node,parent_id=None):
        if THREAD_PROTECTION:
            t = threading.current_thread()
            if t != self.thread:
                raise Exception('! could not acces add_node from thread %s' %t)
            node.set_thread(self.thread)
        if IDLE_ADD:
            gobject.idle_add(self.__tree.add_node,node,parent_id)
        else:
            self.__tree.add_node(node,parent_id=parent_id)

    def del_node(self,nid,recursive=False):
        if THREAD_PROTECTION:
            t = threading.current_thread()
            if t != self.thread:
                raise Exception('! could not acces del_node from thread %s' %t)
        if IDLE_ADD:
            gobject.idle_add(self.__tree.remove_node,nid,recursive)
            return True
        else:
            return self.__tree.remove_node(nid,recursive=recursive)

    def refresh_node(self,nid):
        #FIXME: Transform this thread-protection code in a @decorator or in a
        #function, since it's repeated (invernizzi)
        if THREAD_PROTECTION:
            t = threading.current_thread()
            if t != self.thread:
                raise Exception('! could not refres_node from thread %s' %t)
        if IDLE_ADD:
            gobject.idle_add(self.__tree.modify_node,nid)
        else:
            self.__tree.modify_node(nid)
            
    def refresh_all(self):
        for nid in self.__tree.get_all_nodes():
            self.refresh_node(nid)
        
    def move_node(self,nid,new_parent_id=None):
        """
        Move the node to a new parent (dismissing all other parents)
        use pid None to move it to the root
        """
        if THREAD_PROTECTION:
            t = threading.current_thread()
            if t != self.thread:
                raise Exception('! could not move_node from thread %s' %t)
        if self.has_node(nid):
            node = self.get_node(nid)
            if IDLE_ADD:
                gobject.idle_add(node.set_parent,new_parent_id)
            else:
                node.set_parent(new_parent_id)
            toreturn = True
        else:
            toreturn = False
        return toreturn
        
    #if pid is None, nothing is done
    def add_parent(self,nid,new_parent_id=None):
        if THREAD_PROTECTION:
            t = threading.current_thread()
            if t != self.thread:
                raise Exception('! could not add_parent from thread %s' %t)
        if self.has_node(nid):
            node = self.get_node(nid)
            if IDLE_ADD:
                gobject.idle_add(node.add_parent,new_parent_id)
                toreturn = True
            else:
                toreturn = node.add_parent(new_parent_id)
        else:
            toreturn = False
        return toreturn

    ############ Views ############
    #The main view is the bare tree, without any filters on it.
    def get_main_view(self):
        return self.views['main']
        
    def get_viewtree(self,name=None,refresh=True):
        '''This returns a viewtree.
        If name is given and a view of that name already exists,
        that existing view is returned. Else, a view with that name
        is created. If name is None, the view is not remembered.
        If refresh is False, the view is not initialized (meaning that it 
        will probably not reflect the Tree. This is useful as an optimization
        if you plan to apply filter.
        '''
        if name and self.views.has_key(name):
            vt = self.views[name]
        else:
#            print "   -> creating new viewtree %s  - %s" %(name,self.views.keys())
            vt = ViewTree(self,self.__tree,self.__fbank,refresh=refresh)
            if THREAD_PROTECTION:
                vt.set_thread(self.thread)
            if name:
                self.views[name] = vt
        return vt

    ########### Filters bank ######
    def list_filters(self):
        """ List, by name, all available filters """
        return self.__fbank.list_filters()

    def add_filter(self,filter_name,filter_func,parameters=None):
        """
        Adds a filter to the filter bank 
        @filter_name : name to give to the filter
        @filter_func : the function that will filter the nodes
        @parameters : some default parameters fot that filter
        Return True if the filter was added
        Return False if the filter_name was already in the bank
        """
        if THREAD_PROTECTION:
            t = threading.current_thread()
            if t != self.thread:
                raise Exception('! could not add_filter from thread %s' %t)
        return self.__fbank.add_filter(filter_name,filter_func,parameters=parameters)

    def remove_filter(self,filter_name):
        """
        Remove a filter from the bank.
        Only custom filters that were added here can be removed
        Return False if the filter was not removed
        """
        if THREAD_PROTECTION:
            t = threading.current_thread()
            if t != self.thread:
                raise Exception('! could not remove_filter from thread %s' %t)
        return self.__fbank.remove_filter(filter_name)