Esempio n. 1
0
    def read_file(self, filename):
        """
        Read a data file and return the data.

        :param str filename: file to read
        
        :returns: graph data
        :rtype: dict
        """
        # expand user home path.
        filename = os.path.expanduser(filename)
        autosave_file = '%s~' % filename

        if not os.path.exists(filename):
            log.error('file %s does not exist.' % filename)
            return False

        if os.path.exists(autosave_file):
            os.remove(autosave_file)
            log.info('removing autosave "%s"' % autosave_file)

        log.info('reading scene file "%s"' % filename)
        raw_data = open(filename).read()
        graph_data = json.loads(raw_data, object_pairs_hook=dict)
        return graph_data
Esempio n. 2
0
    def read_file(self, filename):
        """
        Read a data file and return the data.

        :param str filename: file to read
        
        :returns: graph data
        :rtype: dict
        """
        # expand user home path.
        filename = os.path.expanduser(filename)
        autosave_file = '%s~' % filename

        if not os.path.exists(filename):
            log.error('file %s does not exist.' % filename)
            return False

        if os.path.exists(autosave_file):
            os.remove(autosave_file)
            log.info('removing autosave "%s"' % autosave_file)

        log.info('reading scene file "%s"' % filename)
        raw_data = open(filename).read()
        graph_data = json.loads(raw_data, object_pairs_hook=dict)
        return graph_data
Esempio n. 3
0
    def add_edge(self, src, dest, **kwargs):
        """
        Add an edge connecting two nodes.

        :param src: source node
        :type src: DagNode

        :param dest: destination node
        :type dest: DagNode

        :returns: edge dictionary
        :rtype: dict
        """
        src_attr = kwargs.pop('src_attr', 'output')
        dest_attr = kwargs.pop('dest_attr', 'input')
        weight = kwargs.pop('weight', 1.0)
        edge_type = kwargs.pop('edge_type', 'bezier')
        style = kwargs.pop('style', 'solid')

        if src is None or dest is None:
            log.warning('none type passed.')
            return False

        if not src or not dest:
            log.warning('please specify two nodes to connect.')
            return False

        # don't connect the same node
        if src.name == dest.name:
            log.warning('invalid connection: "%s", "%s"' % (src.name, dest.name))
            return

        conn_str = '%s.%s,%s.%s' % (src.name, src_attr, dest.name, dest_attr)
        if conn_str in self.connections():
            log.warning('connection already exists: %s' % conn_str)
            return 
        
        # edge attributes for nx graph
        edge_attrs = dict(src_id=src.id, dest_id=dest.id, src_attr=src_attr, dest_attr=dest_attr, edge_type=edge_type, style=style)

        src_conn = src.get_connection(src_attr)
        dest_conn = dest.get_connection(dest_attr)
        edge_id_str = '(%s,%s)' % (src.id, dest.id)

        #if edge_id_str not in src_conn._edges and edge_id_str not in dest_conn._edges:            
        # add the nx edge - weight should go here        
        self.network.add_edge(src.id, dest.id, key='attributes', weight=weight, attr_dict=edge_attrs)
        log.info('adding edge: "%s"' % self.edge_nice_name(src.id, dest.id))

        # new edge = {'attributes': {'dest_attr': 'input', 'src_attr': 'output', 'weight': 1}}
        new_edge = self.network.edge[src.id][dest.id]
        #print 'new edge: ', new_edge
        src_conn._edges.append(edge_id_str)
        dest_conn._edges.append(edge_id_str)

        # update the scene
        self.edgesAdded([new_edge.get('attributes')])
        return new_edge
Esempio n. 4
0
    def saveLayout(self, layout):
        """
        Save a named layout.

        :param str layout: layout name to save.
        """
        log.info('saving layout: "%s"' % layout)
        self.setValue("MainWindow/geometry/%s" % layout, self._parent.saveGeometry())
        self.setValue("MainWindow/windowState/%s" % layout, self._parent.saveState())

        for dock in self._parent.findChildren(QtGui.QDockWidget):
            dock_name = dock.objectName()
            self.setValue("%s/geometry/%s" % (dock_name, layout), dock.saveGeometry())
Esempio n. 5
0
 def setDebug(self, val):
     """
     Set the debug value of all child nodes.
     """
     vs = 'true'
     if not val:
         vs = 'false'
     if val != self._debug:
         log.info('setting "%s" debug: %s' % (self.dagnode.name, vs))
         self._debug = val
         for item in self.childItems():
             if hasattr(item, '_debug'):
                 item._debug = val
Esempio n. 6
0
    def enable(self, plugin, enabled=True):
        """
        Enable/disable plugins.
        """
        if not plugin in self._node_data:
            log.error('plugin "%s" not recognized.' % plugin)
            return False

        for plug, plugin_attrs in self._node_data.iteritems():
            if plug == plugin:
                log.info('setting plugin "%s" enabled: %s' % (plugin, str(enabled)))
                self._node_data.get(plugin).update(enabled=enabled)
                return True
        return False
Esempio n. 7
0
    def load_core(self, plugins=[]):
        """
        Load core node types.

        :param list plugins: plugin names to filter.
        """
        log.info('loading plugins...')

        core_path = SCENEGRAPH_CORE
        widget_path = os.path.join(SCENEGRAPH_PATH, 'ui')

        builtins = self._load_core(core_path, plugins=plugins)
        #print '# DEBUG: core nodes loaded: ', builtins
        self.load_widgets(widget_path, plugins=builtins)
Esempio n. 8
0
    def load_core(self, plugins=[]):
        """
        Load core node types.

        :param list plugins: plugin names to filter.
        """
        log.info('loading plugins...')

        core_path = SCENEGRAPH_CORE
        widget_path = os.path.join(SCENEGRAPH_PATH, 'ui')

        builtins = self._load_core(core_path, plugins=plugins)
        #print '# DEBUG: core nodes loaded: ', builtins
        self.load_widgets(widget_path, plugins=builtins)
Esempio n. 9
0
    def load_plugins(self, path=None, plugins=[]):
        """
        Load built-in and external asset types

         .. todo::: load the external plugins as well.

        :param str path: path to scan.
        :param list plugins: plugin names to filter.
        """
        log.info('loading plugins...')

        if path is None:
            path = self.default_plugin_path

        builtins = self._load_builtins(path, plugins=plugins)
Esempio n. 10
0
    def enable(self, plugin, enabled=True):
        """
        Enable/disable plugins.
        """
        if not plugin in self._node_data:
            log.error('plugin "%s" not recognized.' % plugin)
            return False

        for plug, plugin_attrs in self._node_data.iteritems():
            if plug == plugin:
                log.info('setting plugin "%s" enabled: %s' %
                         (plugin, str(enabled)))
                self._node_data.get(plugin).update(enabled=enabled)
                return True
        return False
Esempio n. 11
0
    def load_plugins(self, path=None, plugins=[]):
        """
        Load built-in and external asset types

         .. todo::: load the external plugins as well.

        :param str path: path to scan.
        :param list plugins: plugin names to filter.
        """
        log.info('loading plugins...')

        if path is None:
            path = self.default_plugin_path

        builtins = self._load_builtins(path, plugins=plugins)
Esempio n. 12
0
    def saveLayout(self, layout):
        """
        Save a named layout.

        :param str layout: layout name to save.
        """
        log.info('saving layout: "%s"' % layout)
        self.setValue("MainWindow/geometry/%s" % layout,
                      self._parent.saveGeometry())
        self.setValue("MainWindow/windowState/%s" % layout,
                      self._parent.saveState())

        for dock in self._parent.findChildren(QtGui.QDockWidget):
            dock_name = dock.objectName()
            self.setValue("%s/geometry/%s" % (dock_name, layout),
                          dock.saveGeometry())
Esempio n. 13
0
    def flush(self):
        """
        Flush all currently loaded plugins.
        """
        flush = []
        for attr in globals():
            if not attr.startswith('__'):
                obj = globals()[attr]
                if hasattr(obj, 'dag_types'):
                    flush.append(attr)

        if flush:
            for f in flush:
                globals().pop(f)
                log.info('flushing object: "%s"' % f)

        self._node_data = dict()
        self._default_modules = []
Esempio n. 14
0
    def deleteLayout(self, layout):
        """
        Delete a named layout.

        :param str layout: layout name to restore.
        """
        log.info('deleting layout: "%s"' % layout)
        window_keys = self.window_keys()

        for widget_name in window_keys:
            key_name = '%s/geometry/%s' % (widget_name, layout)
            if key_name in self.allKeys():
                self.remove(key_name)

            if widget_name == 'MainWindow':
                window_state = '%s/windowState/%s' % (widget_name, layout)
                if window_state in self.allKeys():
                    self.remove(window_state)
Esempio n. 15
0
    def flush(self):
        """
        Flush all currently loaded plugins.
        """
        flush = []
        for attr in globals():
            if not attr.startswith('__'):
                obj = globals()[attr]
                if hasattr(obj, 'dag_types'):
                    flush.append(attr)

        if flush:
            for f in flush:
                globals().pop(f)
                log.info('flushing object: "%s"'% f)

        self._node_data = dict()
        self._default_modules = []
Esempio n. 16
0
    def deleteLayout(self, layout):
        """
        Delete a named layout.

        :param str layout: layout name to restore.
        """
        log.info('deleting layout: "%s"' % layout)
        window_keys = self.window_keys()

        for widget_name in window_keys:            
            key_name = '%s/geometry/%s' % (widget_name, layout)
            if key_name in self.allKeys():                    
                self.remove(key_name)        

            if widget_name == 'MainWindow':
                window_state = '%s/windowState/%s' % (widget_name, layout)
                if window_state in self.allKeys():
                    self.remove(window_state)
Esempio n. 17
0
    def load_widgets(self, path=None, plugins=[]):
        """
        Load built-in and external node widgets.

        .. todo:: 
            - load the external plugins as well.

        :param str path: path to scan.
        :param list plugins: plugin names to filter.
        """
        log.info('loading plugin widgets...')

        if path is None:
            path = self.default_plugin_path

        widgets = self._load_widgets(path, plugins=plugins)

        # update the node data attribute with widget classes
        for node_type in widgets:
            
            if node_type in self._node_data:
                #print '# DEBUG: updating node "%s" with widget...' % node_type
                self._node_data.get(node_type).update(widgets.get(node_type))
Esempio n. 18
0
    def load_widgets(self, path=None, plugins=[]):
        """
        Load built-in and external node widgets.

        .. todo:: 
            - load the external plugins as well.

        :param str path: path to scan.
        :param list plugins: plugin names to filter.
        """
        log.info('loading plugin widgets...')

        if path is None:
            path = self.default_plugin_path

        widgets = self._load_widgets(path, plugins=plugins)

        # update the node data attribute with widget classes
        for node_type in widgets:

            if node_type in self._node_data:
                #print '# DEBUG: updating node "%s" with widget...' % node_type
                self._node_data.get(node_type).update(widgets.get(node_type))
Esempio n. 19
0
    def restoreLayout(self, layout):
        """
        Restore a named layout.

        :param str layout: layout name to restore.
        """
        log.info('restoring layout: "%s"' % layout)
        window_keys = self.window_keys()

        for widget_name in window_keys:            
            key_name = '%s/geometry/%s' % (widget_name, layout)
            if widget_name != 'MainWindow':
                dock = self._parent.findChildren(QtGui.QDockWidget, widget_name)
                if dock:
                    dock[0].restoreGeometry(value)
            else:
                if key_name in self.allKeys():                    
                    value = self.value(key_name)
                    self._parent.restoreGeometry(value)

                window_state = '%s/windowState/%s' % (widget_name, layout)
                if window_state in self.allKeys():                    
                    self._parent.restoreState(self.value(window_state))
Esempio n. 20
0
    def restoreLayout(self, layout):
        """
        Restore a named layout.

        :param str layout: layout name to restore.
        """
        log.info('restoring layout: "%s"' % layout)
        window_keys = self.window_keys()

        for widget_name in window_keys:
            key_name = '%s/geometry/%s' % (widget_name, layout)
            if widget_name != 'MainWindow':
                dock = self._parent.findChildren(QtGui.QDockWidget,
                                                 widget_name)
                if dock:
                    dock[0].restoreGeometry(value)
            else:
                if key_name in self.allKeys():
                    value = self.value(key_name)
                    self._parent.restoreGeometry(value)

                window_state = '%s/windowState/%s' % (widget_name, layout)
                if window_state in self.allKeys():
                    self._parent.restoreState(self.value(window_state))
Esempio n. 21
0
 def deleteFile(self):
     """
     Delete the preferences file on disk.
     """
     log.info('deleting settings: "%s"' % self.fileName())
     return os.remove(self.fileName())
Esempio n. 22
0
    def restore(self, data, nodes=True, graph=True):
        """
        Restore current DAG state from data. Also used for restoring graph state for the undo stack.

        :param dict data: dictionary of scene graph data.
        :param bool nodes: restore nodes/edges.
        :param bool graph: restore scene attributes/preferences.
        """
        self.reset()

        graph_data = data.get('graph', [])
        node_data = data.get('nodes', [])
        edge_data = data.get('links', [])
        
        self.updateConsole(msg='restoring %d nodes' % len(node_data))

        # update graph attributes
        for gdata in graph_data:
            if len(gdata):
                if graph or gdata[0] in ['scene', 'api_version']:
                    if len(gdata) > 1:
                        self.network.graph[gdata[0]]=gdata[1]

        # build nodes from data
        if nodes:
            for node_attrs in node_data:
                # get the node type
                node_type = node_attrs.pop('node_type', 'default')

                # add the dag node/widget
                dag_node = self.add_node(node_type, **node_attrs)
                log.debug('building node "%s"' % node_attrs.get('name'))

            # edge : ['src_attr', 'target', 'weight', 'dest_id', 'source', 'dest_attr', 'key', 'src_id']
            for edge in edge_data:

                src_id = edge.get('src_id')
                dest_id = edge.get('dest_id')

                src_attr = edge.get('src_attr')
                dest_attr = edge.get('dest_attr')

                weight = edge.get('weight', 1.0)

                src_dag_nodes = self.get_node(src_id)
                dest_dag_nodes = self.get_node(dest_id)

                if not src_dag_nodes or not dest_dag_nodes:
                    log.warning('cannot parse nodes.')
                    return

                src_dag_node = src_dag_nodes[0]
                dest_dag_node = dest_dag_nodes[0]
                src_string = '%s.%s' % (src_dag_node.name, src_attr)
                dest_string = '%s.%s' % (dest_dag_node.name, dest_attr)

                # TODO: need to get connection node here
                log.info('connecting nodes: "%s" "%s"' % (src_string, dest_string))            
                dag_edge = self.add_edge(src_dag_node, dest_dag_node, src_attr=src_attr, dest_attr=dest_attr, weight=weight)

        #self.handler.scene.clear()
        scene_pos = self.network.graph.get('view_center', (0,0))
        view_scale = self.network.graph.get('view_scale', (1.0, 1.0))

        # update the UI.
        if self.mode == 'ui':
            if graph:
                self.handler.restoreGeometry(pos=scene_pos, scale=view_scale)
                
        self._initialized = 1
Esempio n. 23
0
    def add_edge(self, src, dest, **kwargs):
        """
        Add an edge connecting two nodes.

        :param src: source node
        :type src: DagNode

        :param dest: destination node
        :type dest: DagNode

        :returns: edge dictionary
        :rtype: dict
        """
        src_attr = kwargs.pop('src_attr', 'output')
        dest_attr = kwargs.pop('dest_attr', 'input')
        weight = kwargs.pop('weight', 1.0)
        edge_type = kwargs.pop('edge_type', 'bezier')
        style = kwargs.pop('style', 'solid')

        if src is None or dest is None:
            log.warning('none type passed.')
            return False

        if not src or not dest:
            log.warning('please specify two nodes to connect.')
            return False

        # don't connect the same node
        if src.name == dest.name:
            log.warning('invalid connection: "%s", "%s"' %
                        (src.name, dest.name))
            return

        conn_str = '%s.%s,%s.%s' % (src.name, src_attr, dest.name, dest_attr)
        if conn_str in self.connections():
            log.warning('connection already exists: %s' % conn_str)
            return

        # edge attributes for nx graph
        edge_attrs = dict(src_id=src.id,
                          dest_id=dest.id,
                          src_attr=src_attr,
                          dest_attr=dest_attr,
                          edge_type=edge_type,
                          style=style)

        src_conn = src.get_connection(src_attr)
        dest_conn = dest.get_connection(dest_attr)
        edge_id_str = '(%s,%s)' % (src.id, dest.id)

        #if edge_id_str not in src_conn._edges and edge_id_str not in dest_conn._edges:
        # add the nx edge - weight should go here
        self.network.add_edge(src.id,
                              dest.id,
                              key='attributes',
                              weight=weight,
                              attr_dict=edge_attrs)
        log.info('adding edge: "%s"' % self.edge_nice_name(src.id, dest.id))

        # new edge = {'attributes': {'dest_attr': 'input', 'src_attr': 'output', 'weight': 1}}
        new_edge = self.network.edge[src.id][dest.id]
        #print 'new edge: ', new_edge
        src_conn._edges.append(edge_id_str)
        dest_conn._edges.append(edge_id_str)

        # update the scene
        self.edgesAdded([new_edge.get('attributes')])
        return new_edge
Esempio n. 24
0
    def restore(self, data, nodes=True, graph=True):
        """
        Restore current DAG state from data. Also used for restoring graph state for the undo stack.

        :param dict data: dictionary of scene graph data.
        :param bool nodes: restore nodes/edges.
        :param bool graph: restore scene attributes/preferences.
        """
        self.reset()

        graph_data = data.get('graph', [])
        node_data = data.get('nodes', [])
        edge_data = data.get('links', [])

        self.updateConsole(msg='restoring %d nodes' % len(node_data))

        # update graph attributes
        for gdata in graph_data:
            if len(gdata):
                if graph or gdata[0] in ['scene', 'api_version']:
                    if len(gdata) > 1:
                        self.network.graph[gdata[0]] = gdata[1]

        # build nodes from data
        if nodes:
            for node_attrs in node_data:
                # get the node type
                node_type = node_attrs.pop('node_type', 'default')

                # add the dag node/widget
                dag_node = self.add_node(node_type, **node_attrs)
                log.debug('building node "%s"' % node_attrs.get('name'))

            # edge : ['src_attr', 'target', 'weight', 'dest_id', 'source', 'dest_attr', 'key', 'src_id']
            for edge in edge_data:

                src_id = edge.get('src_id')
                dest_id = edge.get('dest_id')

                src_attr = edge.get('src_attr')
                dest_attr = edge.get('dest_attr')

                weight = edge.get('weight', 1.0)

                src_dag_nodes = self.get_node(src_id)
                dest_dag_nodes = self.get_node(dest_id)

                if not src_dag_nodes or not dest_dag_nodes:
                    log.warning('cannot parse nodes.')
                    return

                src_dag_node = src_dag_nodes[0]
                dest_dag_node = dest_dag_nodes[0]
                src_string = '%s.%s' % (src_dag_node.name, src_attr)
                dest_string = '%s.%s' % (dest_dag_node.name, dest_attr)

                # TODO: need to get connection node here
                log.info('connecting nodes: "%s" "%s"' %
                         (src_string, dest_string))
                dag_edge = self.add_edge(src_dag_node,
                                         dest_dag_node,
                                         src_attr=src_attr,
                                         dest_attr=dest_attr,
                                         weight=weight)

        #self.handler.scene.clear()
        scene_pos = self.network.graph.get('view_center', (0, 0))
        view_scale = self.network.graph.get('view_scale', (1.0, 1.0))

        # update the UI.
        if self.mode == 'ui':
            if graph:
                self.handler.restoreGeometry(pos=scene_pos, scale=view_scale)

        self._initialized = 1
Esempio n. 25
0
 def deleteFile(self):
     """
     Delete the preferences file on disk.
     """
     log.info('deleting settings: "%s"' % self.fileName())
     return os.remove(self.fileName())