def font_defaults(self, platform=None, style='default'): """ Builds a dictionary of font & size defaults by platform. :param str platform: os type :returns: font and font size defaults dictionary. :rtype: dict """ if platform is None: platform = options.PLATFORM defaults = dict() def_font_config = self.config_files(style).get('fonts', None) if not os.path.exists(def_font_config): log.error('config "%s" does not exist.' % def_font_config) return defaults parser = StyleParser(self) data = parser._parse_configs(def_font_config) data = parser._parse_platform_data(data) # substitute attribute names for attr, val in data.iteritems(): attr = re.sub('-', '_', attr) defaults[attr] = val return defaults
def renameNode(self, old_name, new_name): """ Rename a node in the graph params: old_name - (str) name to replace new_name - (str) name to with returns: (object) - renamed node """ if not self.is_valid_name(new_name): log.error('"%s" is not unique' % new_name) return UUID = self.getNodeID(old_name) if UUID: dagnodes = self.get_node(UUID) self.network.node[UUID]['name'] = new_name if dagnodes: # update the scene if self.handler is not None: self.handler.renameNodes(dagnodes[0]) return
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
def rename_connection(self, id, old, new): """ Rename an attribute in the network. params: id - (str) node UUID old - (str) old attribute name new - (str) new attribute name """ if not id in self.network.nodes(): log.error('invalid id: "%s"' % id) return False nn = self.network.node[id] if old in nn: val = nn.pop(old) nn[new] = val # update any connections if self.network.edges(): for edge in self.network.edges(data=True): src_id, dest_id, attrs = edge if id in [src_id, dest_id]: for attr in ['src_attr', 'dest_attr']: val = attrs.get(attr, None) if val is not None: if val == old: print 'updating attribute name: "%s": "%s" ("%s")' % ( attr, new, old) self.network.edge[src_id][dest_id][ 'attributes'][attr] = new return True return False
def rename_connection(self, id, old, new): """ Rename an attribute in the network. params: id - (str) node UUID old - (str) old attribute name new - (str) new attribute name """ if not id in self.network.nodes(): log.error('invalid id: "%s"' % id) return False nn = self.network.node[id] if old in nn: val = nn.pop(old) nn[new] = val # update any connections if self.network.edges(): for edge in self.network.edges(data=True): src_id, dest_id, attrs = edge if id in [src_id, dest_id]: for attr in ['src_attr', 'dest_attr']: val = attrs.get(attr, None) if val is not None: if val == old: print 'updating attribute name: "%s": "%s" ("%s")' % (attr, new, old) self.network.edge[src_id][dest_id]['attributes'][attr] = new return True return False
def add_node(self, node_type='default', **kwargs): """ Creates a node in the parent graph :param node_type: node type. :type node_type: str :returns: DagNode - in standalone mode NodeWidget - in ui mode :rtype: DagNode :rtype: NodeWidget """ # check to see if node type is valid if node_type not in self.node_types(): log.error('invalid node type: "%s"' % node_type) return pos = kwargs.pop('pos', self.grid.coords) # get the default name for the node type and validate it name = self.get_valid_name(self.plug_mgr.default_name(node_type)) if 'name' in kwargs: name = kwargs.pop('name') # parse attributes attributes = dict() for attr, val in kwargs.iteritems(): if util.is_dict(val): attributes[attr]=val # get the dag node from the PluginManager dag = self.plug_mgr.get_dagnode(node_type=node_type, name=name, pos=pos, _graph=self, attributes=attributes, **kwargs) # connect signals dag.nodeNameChanged += self.nodeNameChangedEvent dag.nodePositionChanged += self.nodePositionChangedEvent dag.nodeAttributeUpdated += self.nodeAttributeUpdatedEvent # advance the grid to the next value. self.grid.next() self.dagnodes[dag.id] = dag # todo: figure out why I have to load this (need JSONEncoder) node_data = json.loads(str(dag), object_pairs_hook=dict) # add the node to the networkx graph self.network.add_node(dag.id, **node_data) self.nodesAdded([dag.id]) return dag
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
def get_dagnode(self, node_type, **kwargs): """ Return the appropriate dag node type. :param str node_type: dag node type to return. :returns: dag node subclass. :rtype: DagNode """ if node_type not in self._node_data: log.error('plugin type "%s" is not loaded.' % node_type) return dag = self._node_data.get(node_type).get('dagnode') # assign the node metadata file result = dag(_metadata=self._node_data.get(node_type).get('metadata', None), **kwargs) return result
def get_dagnode(self, node_type, **kwargs): """ Return the appropriate dag node type. :param str node_type: dag node type to return. :returns: dag node subclass. :rtype: DagNode """ if node_type not in self._node_data: log.error('plugin type "%s" is not loaded.' % node_type) return dag = self._node_data.get(node_type).get('dagnode') # assign the node metadata file result = dag(_metadata=self._node_data.get(node_type).get( 'metadata', None), **kwargs) return result
def read(self, filename, force=False): """ Read a graph from a saved scene. :param str filename: file to read :param bool force: force scenes not meeting API_MINIMUM to be read. :returns: current scene. :rtype: str """ # callbacks self.graphAboutToBeRead() graph_data = self.read_file(filename) if not graph_data: log.error('scene "%s" appears to be invalid.' % filename) return False file_data = graph_data.get('graph', []) if len(file_data) > 1: api_ver = [x[1] for x in file_data if x[0] == 'api_version'] if api_ver: if not self.version_check(graph_data): if not force: log.error('scene "%s" requires api version %s ( %s )' % (filename, options.API_MINIMUM, api_ver[0])) return False # restore from state. self.restore(graph_data) # callbacks prefs = dict() for data in graph_data.get('graph').items(): if len(data) > 1: dname, attrs = data if dname == 'preferences': prefs = attrs self.graphRead(**prefs) return self.setScene(filename)
def get_widget(self, dagnode, **kwargs): """ Return the appropriate node type widget. Returns the default widget if one is not defined. :param DagNode dagnode: node type. :returns: node widget subclass. :rtype: NodeWidget """ if dagnode.node_type not in self._node_data: log.error('plugin "%s" is not loaded.' % dagnode.node_type) return if 'widget' not in self._node_data.get(dagnode.node_type): log.error('plugin "%s" widget not loaded.' % dagnode.node_type) return widget = self._node_data.get(dagnode.node_type).get('widget') return widget(dagnode)
def add_node(self, node_type='default', **kwargs): """ Creates a node in the parent graph :param node_type: node type. :type node_type: str :returns: DagNode - in standalone mode NodeWidget - in ui mode :rtype: DagNode :rtype: NodeWidget """ # check to see if node type is valid if node_type not in self.node_types(): log.error('invalid node type: "%s"' % node_type) return pos = kwargs.pop('pos', self.grid.coords) # get the default name for the node type and validate it name = self.get_valid_name(self.plug_mgr.default_name(node_type)) if 'name' in kwargs: name = kwargs.pop('name') # parse attributes attributes = dict() for attr, val in kwargs.iteritems(): if util.is_dict(val): attributes[attr] = val # get the dag node from the PluginManager dag = self.plug_mgr.get_dagnode(node_type=node_type, name=name, pos=pos, _graph=self, attributes=attributes, **kwargs) # connect signals dag.nodeNameChanged += self.nodeNameChangedEvent dag.nodePositionChanged += self.nodePositionChangedEvent dag.nodeAttributeUpdated += self.nodeAttributeUpdatedEvent # advance the grid to the next value. self.grid.next() self.dagnodes[dag.id] = dag # todo: figure out why I have to load this (need JSONEncoder) node_data = json.loads(str(dag), object_pairs_hook=dict) # add the node to the networkx graph self.network.add_node(dag.id, **node_data) self.nodesAdded([dag.id]) return dag