Metadata-Version: 2.1
Name: textree
Version: 0.0.1
Summary: Create nodes as specified in the tree text.
Home-page: https://github.com/textree/textree
Author: ytyaru
Author-email: pypi1@outlook.jp
License: GNU Affero General Public License Version 3 (AGPL-3.0)
Keywords: tree,text,node,textprocess,texttotree,texttonode,treetotext,treetonode,mapper,ObjectToTextMapper,serializer,deserializer,generator,convertor
Platform: UNKNOWN
Classifier: Development Status :: 3 - Alpha
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 2.7
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: GNU Affero General Public License v3
Classifier: Operating System :: OS Independent
Classifier: Intended Audience :: Developers
Classifier: Topic :: Software Development :: Libraries
Classifier: Topic :: Text Processing
Classifier: Topic :: Utilities
Requires-Python: >=2.7
Description-Content-Type: text/markdown

[ja](./ReadMe.ja.md)

# TexTree

Create nodes as specified in the tree text.

# Features

* Compatible with python2,3
* Data can be assigned to nodes
* Convert between text and node objects

# Requirement

* <time datetime="2019-12-26T10:00:00+0900">2019-12-26</time>
* [Raspbierry Pi](https://ja.wikipedia.org/wiki/Raspberry_Pi) 4 Model B Rev 1.2
* [Raspbian](https://ja.wikipedia.org/wiki/Raspbian) buster 10.0 2019-09-26 <small>[setup](http://ytyaru.hatenablog.com/entry/2019/12/25/222222)</small>
* bash 5.0.3(1)-release
* Python 2.7.16
* Python 3.7.3

# Installation

```sh
pip install textree
```

# Usage

## Foundations

Convert from text to node object.

```python
import textree
tree_text = """
A
	A1
		A11
			A111
			A112
	A2
B
"""
tree = TexTree()
root = tree.to_node(tree_text)
print(root, root.Name)
for node in tree.Nodes:
    print(node.Line)
print(tree.to_text())
```

## Reference

Convert between text and objects.

```python
root = tree.to_node(tree_text)
       tree.to_text()
```

Reference and assignment.

```python
node.Name
node.Parent
node.Children
```
```python
node.Name = 'NewName'
node.Parent = Node('Parent')
node.Children.append(Node('Child'))
```

Move.

```python
node.to_first()
node.to_last()
node.to_next()
node.to_prev()
```

Get.

```python
Node.Path.select(root, 'A/A1/A11')
Node.Path.select(A, 'A1/A11')
```

Insertion / deletion.

```python
node.insert_first(Node('new'))
node.insert_last(Node('new'))
node.insert_next(Node('new'))
node.insert_prev(Node('new'))
```
```python
node.delete()
```

Update.

```python
node = Node.Path.select(root, 'A/A1/A11')
node.Name = 'UpdateName'
```

There are many others. Refer to the [code](./src/py3/textree.py) or [API list](./doc/memo/apis_py3.txt) for details.

## Attribute of Node

Attributes can be assigned to the same line.

```python
import textree
tree_text = """
A	attrA
	A1	attrA1
		A11	attrA11
			A111	attrA111
			A112	attrA112
	A2	attrA2
B	attrB
"""
tree = TexTree()
root = tree.to_node(tree_text)
print(root, root.Name)
for node in tree.Nodes:
    print(node.Name, node,Attr)
```

### Attribute of RootNode

Attributes can be assigned to RootNode.

```python
import textree
tree_text = """
<ROOT>	root_attr
A	attrA
	A1	attrA1
		A11	attrA11
			A111	attrA111
			A112	attrA112
	A2	attrA2
B	attrB
"""
tree = TexTree()
root = tree.to_node(tree_text)
print(root, root.Name, root.Attr)
for node in tree.Nodes:
    print(node.Name, node,Attr)
```

### Deserialization of attributes

The user can embed the attribute analysis code freely. Of course you can also write code to serialize to text.

The following code gives the node `my_name`.

```python
class MyNodeDeserializer(NodeDeserializer):
    def deserialize(self, ana, parent, parents=Node):
        node = Node(ana.Line, parent=parent)
        node.my_name = 'My name is ' + node.Name
        return node
```
```python
tree = TexTree(node_deserializer=MyNodeDeserializer())
root = tree.to_node(tree_text)
for node in tree.Nodes:
    print(node.my_name)
```
```python
class MyNodeAttributeSerializer(NodeAttributeSerializer):
    def serialize(self, attr): return 'my_name=' + attr
```
```python
tree = TexTree(node_deserializer=MyNodeDeserializer(), node_serializer=NodeSerializer(MyNodeAttributeSerializer()))
root = tree.to_node(tree_text)
for node in tree.Nodes:
    print(node.my_name)
print(tree.to_text())
```

# Note

* Alpha version. Checking installation

# Author

　ytyaru

* [![github](http://www.google.com/s2/favicons?domain=github.com)](https://github.com/ytyaru "github")
* [![hatena](http://www.google.com/s2/favicons?domain=www.hatena.ne.jp)](http://ytyaru.hatenablog.com/ytyaru "hatena")
* [![mastodon](http://www.google.com/s2/favicons?domain=mstdn.jp)](https://mstdn.jp/web/accounts/233143 "mastdon")

# License

This software is [AGPLv3](https://www.gnu.org/licenses/agpl-3.0.en.html) licensed. (GNU Affero General Public License v3) `agpl-3.0`

[![agpl-3.0](./doc/res/AGPLv3.svg "agpl-3.0")](https://www.gnu.org/licenses/agpl-3.0.en.html)


