mirror of https://github.com/deadc0de6/catcli
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
208 lines
5.2 KiB
Python
208 lines
5.2 KiB
Python
1 year ago
|
"""
|
||
|
author: deadc0de6 (https://github.com/deadc0de6)
|
||
|
Copyright (c) 2023, deadc0de6
|
||
|
|
||
|
Class that represents a node in the catalog tree
|
||
|
"""
|
||
|
# pylint: disable=W0622
|
||
|
|
||
1 year ago
|
from typing import Dict, Any
|
||
1 year ago
|
from anytree import NodeMixin # type: ignore
|
||
|
|
||
|
|
||
1 year ago
|
_TYPE_TOP = 'top'
|
||
|
_TYPE_FILE = 'file'
|
||
|
_TYPE_DIR = 'dir'
|
||
|
_TYPE_ARC = 'arc'
|
||
|
_TYPE_STORAGE = 'storage'
|
||
|
_TYPE_META = 'meta'
|
||
1 year ago
|
|
||
|
NAME_TOP = 'top'
|
||
|
NAME_META = 'meta'
|
||
|
|
||
|
|
||
1 year ago
|
class NodeAny(NodeMixin): # type: ignore
|
||
|
"""generic node"""
|
||
1 year ago
|
|
||
|
def __init__(self, # type: ignore[no-untyped-def]
|
||
|
parent=None,
|
||
|
children=None):
|
||
1 year ago
|
"""build generic node"""
|
||
1 year ago
|
super().__init__()
|
||
|
self.parent = parent
|
||
|
if children:
|
||
|
self.children = children
|
||
|
|
||
1 year ago
|
def _to_str(self) -> str:
|
||
|
ret = str(self.__class__) + ": " + str(self.__dict__)
|
||
|
if self.children:
|
||
|
ret += '\n'
|
||
|
for child in self.children:
|
||
|
ret += ' child => ' + str(child)
|
||
|
return ret
|
||
|
|
||
|
def __str__(self) -> str:
|
||
|
return self._to_str()
|
||
|
|
||
1 year ago
|
def flagged(self) -> bool:
|
||
|
"""is flagged"""
|
||
1 year ago
|
if not hasattr(self, '_flagged'):
|
||
|
return False
|
||
1 year ago
|
return self._flagged
|
||
|
|
||
|
def flag(self) -> None:
|
||
|
"""flag a node"""
|
||
1 year ago
|
self._flagged = True # pylint: disable=W0201
|
||
1 year ago
|
|
||
|
def unflag(self) -> None:
|
||
|
"""unflag node"""
|
||
1 year ago
|
self._flagged = False # pylint: disable=W0201
|
||
|
delattr(self, '_flagged')
|
||
1 year ago
|
|
||
|
|
||
|
class NodeTop(NodeAny):
|
||
|
"""a top node"""
|
||
|
|
||
|
def __init__(self, # type: ignore[no-untyped-def]
|
||
|
name: str,
|
||
|
children=None):
|
||
|
"""build a top node"""
|
||
|
super().__init__() # type: ignore[no-untyped-call]
|
||
|
self.name = name
|
||
|
self.type = _TYPE_TOP
|
||
|
self.parent = None
|
||
|
if children:
|
||
|
self.children = children
|
||
|
|
||
1 year ago
|
def __str__(self) -> str:
|
||
|
return self._to_str()
|
||
|
|
||
1 year ago
|
|
||
|
class NodeFile(NodeAny):
|
||
|
"""a file node"""
|
||
|
|
||
|
def __init__(self, # type: ignore[no-untyped-def]
|
||
|
name: str,
|
||
|
relpath: str,
|
||
|
size: int,
|
||
|
md5: str,
|
||
|
maccess: float,
|
||
|
parent=None,
|
||
|
children=None):
|
||
|
"""build a file node"""
|
||
|
super().__init__() # type: ignore[no-untyped-call]
|
||
|
self.name = name
|
||
|
self.type = _TYPE_FILE
|
||
|
self.relpath = relpath
|
||
|
self.size = size
|
||
|
self.md5 = md5
|
||
|
self.maccess = maccess
|
||
|
self.parent = parent
|
||
|
if children:
|
||
|
self.children = children
|
||
|
|
||
1 year ago
|
def __str__(self) -> str:
|
||
|
return self._to_str()
|
||
|
|
||
1 year ago
|
|
||
|
class NodeDir(NodeAny):
|
||
|
"""a directory node"""
|
||
|
|
||
|
def __init__(self, # type: ignore[no-untyped-def]
|
||
|
name: str,
|
||
|
relpath: str,
|
||
|
size: int,
|
||
|
maccess: float,
|
||
|
parent=None,
|
||
|
children=None):
|
||
|
"""build a directory node"""
|
||
|
super().__init__() # type: ignore[no-untyped-call]
|
||
|
self.name = name
|
||
|
self.type = _TYPE_DIR
|
||
|
self.relpath = relpath
|
||
|
self.size = size
|
||
|
self.maccess = maccess
|
||
|
self.parent = parent
|
||
|
if children:
|
||
|
self.children = children
|
||
|
|
||
1 year ago
|
def __str__(self) -> str:
|
||
|
return self._to_str()
|
||
|
|
||
1 year ago
|
|
||
|
class NodeArchived(NodeAny):
|
||
|
"""an archived node"""
|
||
|
|
||
|
def __init__(self, # type: ignore[no-untyped-def]
|
||
|
name: str,
|
||
|
relpath: str,
|
||
|
size: int,
|
||
|
md5: str,
|
||
|
archive: str,
|
||
|
parent=None,
|
||
|
children=None):
|
||
|
"""build an archived node"""
|
||
|
super().__init__() # type: ignore[no-untyped-call]
|
||
|
self.name = name
|
||
|
self.type = _TYPE_ARC
|
||
|
self.relpath = relpath
|
||
|
self.size = size
|
||
|
self.md5 = md5
|
||
|
self.archive = archive
|
||
|
self.parent = parent
|
||
|
if children:
|
||
|
self.children = children
|
||
|
|
||
1 year ago
|
def __str__(self) -> str:
|
||
|
return self._to_str()
|
||
|
|
||
1 year ago
|
|
||
|
class NodeStorage(NodeAny):
|
||
|
"""a storage node"""
|
||
|
|
||
|
def __init__(self, # type: ignore[no-untyped-def]
|
||
|
name: str,
|
||
|
free: int,
|
||
|
total: int,
|
||
|
size: int,
|
||
1 year ago
|
ts: float,
|
||
|
attr: str,
|
||
1 year ago
|
parent=None,
|
||
|
children=None):
|
||
|
"""build a storage node"""
|
||
|
super().__init__() # type: ignore[no-untyped-call]
|
||
|
self.name = name
|
||
|
self.type = _TYPE_STORAGE
|
||
|
self.free = free
|
||
|
self.total = total
|
||
|
self.attr = attr
|
||
|
self.size = size
|
||
1 year ago
|
self.ts = ts
|
||
1 year ago
|
self.parent = parent
|
||
|
if children:
|
||
|
self.children = children
|
||
|
|
||
1 year ago
|
def __str__(self) -> str:
|
||
|
return self._to_str()
|
||
|
|
||
1 year ago
|
|
||
|
class NodeMeta(NodeAny):
|
||
|
"""a meta node"""
|
||
|
|
||
|
def __init__(self, # type: ignore[no-untyped-def]
|
||
|
name: str,
|
||
1 year ago
|
attr: Dict[str, Any],
|
||
1 year ago
|
parent=None,
|
||
|
children=None):
|
||
|
"""build a meta node"""
|
||
|
super().__init__() # type: ignore[no-untyped-call]
|
||
|
self.name = name
|
||
|
self.type = _TYPE_META
|
||
|
self.attr = attr
|
||
|
self.parent = parent
|
||
|
if children:
|
||
|
self.children = children
|
||
1 year ago
|
|
||
|
def __str__(self) -> str:
|
||
|
return self._to_str()
|