Commands in files

This example shows how to distribute your code if you want to separate the node handler classes to external files.

tree.py: Instantiate the CommandTree class in an extra file
1
2
3
from command_tree import CommandTree

tree = CommandTree()
node1.py: Define the node1
1
2
3
4
5
6
7
8
9
from tree import tree

@tree.node()
class Node1(object):

    @tree.leaf()
    @tree.argument()
    def divide(self, arg1):
        return int(arg1) / 2
node2.py: Define the node2
1
2
3
4
5
6
7
8
9
from tree import tree

@tree.node()
class Node2(object):

    @tree.leaf()
    @tree.argument()
    def multiply(self, arg1):
        return int(arg1) * 2
power.py: If you are brave enough, you can define leafs in separated files. But beware of the ‘self’ argument!
1
2
3
4
5
6
from tree import tree

@tree.leaf()
@tree.argument()
def power(self, arg1):
    return int(arg1) * int(arg1)
cli.py: This is the ‘root’ file where you collect the nodes and leafs from other files.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
from tree import tree
from node1 import Node1
from node2 import Node2
from power import power as Power

@tree.root()
class Root(object):

    node1 = Node1
    node2 = Node2

    power = Power

print(tree.execute())