def __init__(self, path, oid, **kwargs): """ kwargs allows users to pass arbitrary information into a Node that will later be output in jsonData(). It allows for more advanced configuration than the default path handling that jsTree currently allows. For example, users may want to pass "attr" or some other valid jsTree options. Example: >>> from dictobj import * >>> node = Node('a', None) >>> node._items == {'text': 'a', 'children': MutableDictionaryObject({})} True >>> node.jsonData() == {'text': 'a'} True >>> import jstree >>> from dictobj import * >>> node = jstree.Node('a', 1) >>> node._items == {'text': 'a', 'children': MutableDictionaryObject({}), 'li_attr': DictionaryObject({'id': 1})} True >>> d = node.jsonData() >>> d == {'text': 'a', 'li_attr': {'id': 1}} True >>> import jstree >>> node = jstree.Node('a', 5, icon="folder", state = {'opened': True}) >>> node._items == {'text': 'a', 'state': DictionaryObject({'opened': True}), 'children': MutableDictionaryObject({}), 'li_attr': DictionaryObject({'id': 5}), 'icon': 'folder'} True >>> node.jsonData() == {'text': 'a', 'state': {'opened': True}, 'li_attr': {'id': 5}, 'icon': 'folder'} True """ super(Node, self).__init__() children = kwargs.get('children', {}) if any( filter(lambda key: not isinstance(children[key], Node), children)): raise TypeError("One or more children were not instances of '%s'" % Node.__name__) if 'children' in kwargs: del kwargs['children'] self._items['children'] = dictobj.MutableDictionaryObject(children) if oid is not None: li_attr = kwargs.get('li_attr', {}) li_attr['id'] = oid kwargs['li_attr'] = li_attr self._items.update(dictobj.DictionaryObject(**kwargs)) self._items['text'] = path
def __init__(self, path, oid, **kwargs): """ kwargs allows users to pass arbitrary information into a Node that will later be output in jsonData(). It allows for more advanced configuration than the default path handling that JSTree currently allows. For example, users may want to pass "attr" or some other valid jsTree options. Example: >>> node = Node('a', None) >>> assert node._items == {'text': 'a', 'children': dictobj.MutableDictionaryObject({})} >>> assert node.jsonData() == {'text': 'a'} >>> node = Node('a', 1) >>> assert node._items == {'text': 'a', 'children': dictobj.MutableDictionaryObject({}), 'li_attr': dictobj.DictionaryObject({'id': 1}), 'id': 1} >>> assert node.jsonData() == {'text': 'a', 'id': 1, 'li_attr': {'id': 1}} >>> node = Node('a', 5, icon="folder", state = {'opened': True}) >>> assert node._items == {'text': 'a', 'id': 5, 'state': dictobj.DictionaryObject({'opened': True}), 'children': dictobj.MutableDictionaryObject({}), 'li_attr': dictobj.DictionaryObject({'id': 5}), 'icon': 'folder'} >>> assert node.jsonData() == {'text': 'a', 'state': {'opened': True}, 'id': 5, 'li_attr': {'id': 5}, 'icon': 'folder'} """ super().__init__() children = kwargs.get('children', {}) if len([key for key in children if not isinstance(children[key], Node)]): raise TypeError( f"One or more children were not instances of '{Node.__name__}'") if 'children' in kwargs: del kwargs['children'] self._items['children'] = dictobj.MutableDictionaryObject(children) if oid is not None: li_attr = kwargs.get('li_attr', {}) li_attr['id'] = oid kwargs['li_attr'] = li_attr self._items['id'] = oid self._items.update(dictobj.DictionaryObject(**kwargs)) self._items['text'] = path