Skip to content

Commit 6371d03

Browse files
committed
lightree initial version
0 parents  commit 6371d03

File tree

16 files changed

+1749
-0
lines changed

16 files changed

+1749
-0
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* @leonardbinet

.gitignore

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
*.py[cod]
2+
.idea
3+
env/*
4+
.python-version
5+
6+
# C extensions
7+
*.so
8+
9+
# Packages
10+
*.egg
11+
*.egg-info
12+
dist
13+
build
14+
eggs
15+
parts
16+
bin
17+
var
18+
sdist
19+
develop-eggs
20+
.installed.cfg
21+
lib
22+
lib64
23+
__pycache__
24+
\.vscode/*
25+
26+
# Installer logs
27+
pip-log.txt
28+
29+
# Unit test / coverage reports
30+
.coverage
31+
.tox
32+
nosetests.xml
33+
34+
# Translations
35+
*.mo
36+
37+
# Mr Developer
38+
.mr.developer.cfg
39+
.project
40+
.pydevproject
41+
*.swp

LICENSE

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Even though all code has been rewritten and inner structure revamped, lots of interfaces are inspired by treelib:
2+
https://github.com/caesar0301/treelib
3+
4+
MIT License
5+
6+
Copyright (c) 2020 Léonard Binet [email protected]
7+
8+
Permission is hereby granted, free of charge, to any person obtaining a copy
9+
of this software and associated documentation files (the "Software"), to deal
10+
in the Software without restriction, including without limitation the rights
11+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
copies of the Software, and to permit persons to whom the Software is
13+
furnished to do so, subject to the following conditions:
14+
15+
The above copyright notice and this permission notice shall be included in all
16+
copies or substantial portions of the Software.
17+
18+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
SOFTWARE.

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include README*

Makefile

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
.PHONY : develop clean clean_pyc lint-diff black test coverage
2+
3+
clean:
4+
-python setup.py clean
5+
6+
clean_pyc:
7+
-find ./tests -name "*.py[co]" -exec rm {} \;
8+
-find ./lighttree -name "*.py[co]" -exec rm {} \;
9+
10+
lint-diff:
11+
git diff upstream/master --name-only -- "*.py" | xargs flake8
12+
13+
black:
14+
black lighttree tests setup.py
15+
16+
develop:
17+
-python -m pip install -e .
18+
19+
test:
20+
-python -m unittest
21+
22+
coverage:
23+
-coverage run --source=./lighttree -m unittest
24+
coverage report

README.md

Whitespace-only changes.

lighttree/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from .tree import Tree
2+
from .node import Node
3+
4+
__all__ = ["Tree", "Node"]

lighttree/exceptions.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class NotFoundNodeError(Exception):
2+
"""Requested node identifier is not present in tree"""
3+
4+
pass
5+
6+
7+
class MultipleRootError(Exception):
8+
"""Tree root is already declared."""
9+
10+
pass
11+
12+
13+
class DuplicatedNodeError(Exception):
14+
"""Node identifier already exists in tree."""
15+
16+
pass

lighttree/node.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env python
2+
3+
from __future__ import unicode_literals
4+
from future.utils import python_2_unicode_compatible, string_types
5+
6+
import uuid
7+
8+
9+
@python_2_unicode_compatible
10+
class Node(object):
11+
def __init__(self, identifier=None, auto_uuid=False):
12+
"""
13+
:param identifier: node identifier, must be unique per tree
14+
"""
15+
if not isinstance(identifier, string_types):
16+
raise ValueError(
17+
"Identifier must be a string type, provided type is <%s>"
18+
% type(identifier)
19+
)
20+
if identifier is None:
21+
if not auto_uuid:
22+
raise ValueError("Required identifier")
23+
identifier = uuid.uuid4()
24+
self.identifier = identifier
25+
26+
def line_repr(self, **kwargs):
27+
"""Control how node is displayed in tree representation.
28+
"""
29+
return self.identifier
30+
31+
def serialize(self, *args, **kwargs):
32+
return {"identifier": self.identifier}
33+
34+
@classmethod
35+
def deserialize(cls, d, *args, **kwargs):
36+
if not isinstance(d, dict):
37+
raise ValueError("Deserialization requires a dict.")
38+
return cls._deserialize(d, *args, **kwargs)
39+
40+
@classmethod
41+
def _deserialize(cls, d, *args, **kwargs):
42+
return cls(d.get("identifier"))
43+
44+
def __str__(self):
45+
return "%s, id=%s" % (self.__class__.__name__, self.identifier)
46+
47+
def __repr__(self):
48+
return self.__str__()

0 commit comments

Comments
 (0)