master.py

#

Copyright (c) 2011 The Substance Project. All Rights Reserved. Licensed under the GNU LGPL. For full terms see the file COPYING or visit http://fsf.org

Contributors:

Clemens Klokmose: clemens@klokmose.net

#

The Substance environment is booted and loaded by importing the substance.environment.bootstrap lib

from substance.environment.bootstrap import *
#

In order for us to share something on the network, we need to import a Sharer facet

from substance.std.sharing.sharer import Sharer
#

Our base class is a facet that is going to be installed on the root of our Substance tree

class Master(Facet):
    
#

A facet has a constructor like any other Python class

    def __init__(self):
        Facet.__init__(self, "Master")
    
#

In addition to a constructor, a facet has an instantiate method that is called when the facet is installed on a node

    def instantiate(self, with_state):
#

To create a new child note, simply call new_child on local, this returns the path to the new node. We can now add values to that node calling the new_value handler from the CoreValue facet on the new node. local is a shorthand for accessing the node this facet is installed on. new_child is a handler on the CoreStructural facet, and local. new_child is equivalent to writing local.CoreStructural.new_child. All the handlers of the core facets can be accessed without specifying the facet.

        self.st = local.new_child(self, "SharedSubtree", "Our shared subtree")
        self.st.new_value(self, "SomeValue", "A description", "foo")
        self.st.new_value(self, "AnotherValue", "Another description", 42)
#

Now to share this single node subtree we have made, we add a sharer facet to the node. Again the add_facet handler is a handler on a core facet, and we can just write self.st.add_facet instead of self.st.CoreFacet.add_facet.

        self.st.add_facet(self, Sharer(), True, True)
#

Now we share the subtree by calling the share handler on the newly installed Sharer facet with a name, description and application domain

        self.st.Sharer.share(self, "SomeShare", "The master environment", "examples.substance")
        
boot_std_default(name = "Master", description = "Testing sharing")
master = Master()
boot.add_facet(master, environment / "app")
done_boot()