Skip to content
Jiri Hnidek edited this page Jun 27, 2014 · 11 revisions

Instances of VerseNode class are used for creating new nodes at Verse server. When other Verse client creates new node at Verse server and corresponding command is received, then VerseNode object is automatically created and stored in dictionary of session: session.nodes.

Creating of Nodes

Client can create new VerseNode and new node at Verse server with following code:

import vrsent
session = vrsent.VerseSession(hostname='localhost', service='12345', \
    callback_thread=True, username='joe', password='pass')
node = vrsent.VerseNode(session, custom_type=100)
print(node)

Note: When you create new node with VerseNode(), then node create command is sent to Verse server and many things are undefined in the instance: node.id, node.parent etc. until node create command is received from the server. The new instance is also stored in queue of node in session object until node create command is received from server, but it is safe to work with this node as it would be already shared at Verse server. You can create new tag groups and layers of this node, change parent node, etc. These changes will be sent to the Verse server, when the node create command will be received.

Destroying of Nodes

Owner of VerseNode can be destroyed this node using node.destroy(). The reference at this node is removed from the dictionary of nodes, when node destroy command is received from Verse server.

Parent Node

A parent node can be changed with property parent: node.parent = other_node

Child Nodes

Each node has dictionary of all child nodes:

for child_node in node.child_nodes.values():
    print(child_node)

Owner and Permissions

A node has property owner. It could be used to get current owner: node_owner = node.owner . It returns reference at VerseUser node. This owner property could be also used for setting new owner of the node. Only current owner of node can change owner of the node.

Access permission are stored in dictionary node.perms.

Note: Setting permission has not been implemented yet.

Node Priority

Node priority can be changed with property prio.

Locking of Node

User with write permission to the node can lock this node using node.lock(). A locker of node can unlock node with node.unlock(). Each node has also property node.locked that returns True, when node is locked, otherwise it return False. Property node.locker returns reference at current locker (avatar) of node, when node is locked, otherwise it returns None.

VerseTagGroups and VerseLayers

As node can be used for sharing tag groups and layers, the instance of VerseNode contains dictionary of VerseTagGroups and VerseLayers instances. VerseTagGroups are stored in node.tag_groups and VerseLayers in node.layers.

Custom VerseNodes

The big advantage of VerseNode is support of subclassing of VerseNode. Subclass of VerseNode could be created with following code:

import vrsent
session = vrsent.VerseSession(hostname='localhost', service='12345', \
    callback_thread=True, username='joe', password='pass')

TEST_NODE_CUSTOM_TYPE = 219

class TestNode(vrsent.VerseNode):
    """
    Subclass of VerseNode
    """

    custom_type = TEST_NODE_CUSTOM_TYPE

    @classmethod
    def _receive_node_create(cls, session, node_id, parent_id, \
             user_id, custom_type):
        """
        Custom callback method called, when this custom_type of
        VerseNode is created by verse server and appropriate
        command is received.
        """
        print("Custom method of VerseNode subclass TestNode.")
        return super(TestNode, cls)._receive_node_create(session, \
            node_id, parent_id, user_id, custom_type)

Very important part of VerseNode subclassing is class attribute custom_type. When this attribute is not set in class, then subclass of VerseNode will not work correctly.

New custom subclass of VerseNode can be created with following code:

# First method of creating instance of TestNode
node01 = VerseNode(session=session, custom_type=TEST_NODE_CUSTOM_TYPE)
# More straightforward method of creating TestNode instance
node02 = TestNode(session=session)

It is possible to create custom methods for all callback methods of VerseNode, but it is important to call method of parent class VerseNode using super() at the end of custom method and this method should return result of parent method.

Callback Methods

Following methods could be customized in subclasses of VerseNode:

def __str__(self):
    pass
@classmethod
def _receive_node_create(cls, session, node_id, parent_id, \
        user_id, custom_type):
    pass
@classmethod
def _receive_node_destroy(cls, session, node_id):
    pass
@classmethod
def _receive_node_link(cls, session, parent_node_id, child_node_id):
    pass
@classmethod
def _receive_node_lock(cls, session, node_id, avatar_id):
    pass
@classmethod
def _receive_node_unlock(cls, session, node_id, avatar_id):
   pass
@classmethod
def _receive_node_owner(cls, session, node_id, user_id):
    pass
@classmethod
def _receive_node_perm(cls, session, node_id, user_id, perm):
    pass

It is possible to create other custom methods in subclass, but it is not recommended.