Permalink
Browse files

added node_catalogue() returning a dict; sorting node list in brewery…

… runner tool - fixes #23
  • Loading branch information...
1 parent 99daa9f commit c56b07602aa59634f54a21b67affc3e6e7c61121 @Stiivi committed Apr 13, 2012
Showing with 63 additions and 21 deletions.
  1. +17 −20 bin/brewery
  2. +46 −1 brewery/nodes/base.py
View
@@ -20,6 +20,7 @@ import urllib2
import re
import textwrap
import brewery.streams
+from operator import itemgetter
class ToolError(Exception):
"""Just exception"""
@@ -110,11 +111,11 @@ def create_graph(args):
def list_nodes(args):
node_name = args.node
- node_dict = brewery.nodes.node_dictionary()
+ catalogue = brewery.nodes.node_catalogue()
if node_name:
- if node_name in node_dict:
- info = node_dict[node_name].node_info
+ if node_name in catalogue:
+ info = catalogue[node_name]
print "%s - %s\n" % (node_name, info.get("label") or node_name)
@@ -129,24 +130,20 @@ def list_nodes(args):
print "unknown node: %s" % node_name
else:
- nodes = node_dict.values()
+ all_nodes = sorted(catalogue.values(), key=itemgetter("name"))
+
+ types = ["source", "processing", "target"]
+ for t in types:
+ nodes = [node for node in all_nodes if node["type"] == t]
+ print_nodes(nodes, t)
+
+def print_nodes(nodes, label):
+ print "%s nodes:" % label
+ for node in nodes:
+ print " " + node["name"]
+
+ print ""
- print "source nodes:"
- for node in nodes:
- if issubclass(node, brewery.nodes.SourceNode):
- print " " + node.identifier()
-
- print "processing nodes:"
- for node in nodes:
- if not issubclass(node, brewery.nodes.SourceNode) \
- and not issubclass(node, brewery.nodes.TargetNode):
- print " " + node.identifier()
-
- print "target nodes:"
- for node in nodes:
- if issubclass(node, brewery.nodes.TargetNode):
- print " " + node.identifier()
-
def run_pipe(args):
# Collect nodes
View
@@ -6,6 +6,7 @@
__all__ = (
"create_node",
"node_dictionary",
+ "node_catalogue",
"get_node_info",
"NodeFinished",
"Node",
@@ -26,7 +27,9 @@ def create_node(identifier, *args, **kwargs):
return node
def node_dictionary():
- """Return a dictionary containing node name as key and node class as value."""
+ """Return a dictionary containing node name as key and node class as
+ value. This will be depreciated soon in favour of
+ :func:`node_catalogue()`"""
classes = node_subclasses(Node)
dictionary = {}
@@ -42,6 +45,48 @@ def node_dictionary():
return dictionary
+def node_catalogue():
+ """Returns a dictionary of information about all available nodes. Keys are
+ node identifiers, values are dictionaries. The information dictionary contains
+ all the keys from the node's `node_info` dictionary plus keys: `factory`
+ with node class, `type` (if not provided) is set to one of ``source``,
+ ``processing`` or ``target``.
+ """
+
+ classes = node_subclasses(Node)
+
+ catalogue = {}
+
+ for node_class in classes:
+ try:
+ name = node_class.identifier()
+ except AttributeError as e:
+ # If node does not provide identifier, we consider it to be
+ # private or abstract class
+ continue
+
+ # Get copy of node info
+ info = dict(get_node_info(node_class))
+ info["name"] = name
+ info["factory"] = node_class
+
+ # Get node type based on superclass, if not provided
+
+ if "type" not in info:
+ if issubclass(node_class, SourceNode):
+ info["type"] = "source"
+ elif not issubclass(node_class, SourceNode) \
+ and not issubclass(node_class, TargetNode):
+ info["type"] = "processing"
+ elif issubclass(node_class, TargetNode):
+ info["type"] = "target"
+ else:
+ info["type"] = "unknown"
+
+ catalogue[name] = info
+
+ return catalogue
+
def node_subclasses(root, abstract = False):
"""Get all subclasses of node.

0 comments on commit c56b076

Please sign in to comment.