feat: renamed Crx to crx. Added proxy support. Minor code improvements.
This commit is contained in:
69
crx/simplenode.py
Normal file
69
crx/simplenode.py
Normal file
@@ -0,0 +1,69 @@
|
||||
from typing import TYPE_CHECKING
|
||||
from logging import Logger
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .connection import Connection
|
||||
|
||||
|
||||
LOGGER = Logger(__name__)
|
||||
|
||||
|
||||
class SimpleNode:
|
||||
_normal_attrs = ['path', '_data', '_connection']
|
||||
|
||||
def __init__(self, path: str, data: dict, connection: 'Connection'):
|
||||
self.path = path
|
||||
self._data = data
|
||||
self._connection = connection
|
||||
|
||||
def download(self, key: str = 'jcr:data') -> bytes:
|
||||
if ':' + key not in self._data:
|
||||
LOGGER.warning(f"Key :{key} is not present, binary probably not available")
|
||||
size = self._data.get(':' + key)
|
||||
if not isinstance(size, int):
|
||||
LOGGER.warning("Size is not present")
|
||||
# TODO value denotes file size, warn/deny large files?
|
||||
return self._connection.download_binary(self.path + '/' + key)
|
||||
|
||||
def __getitem__(self, item):
|
||||
return getattr(self, item)
|
||||
|
||||
def __setitem__(self, key, value):
|
||||
return self.__setattr__(key, value)
|
||||
|
||||
def __setattr__(self, key, value):
|
||||
if key in self._normal_attrs:
|
||||
return super(SimpleNode, self).__setattr__(key, value)
|
||||
if (':' + key) in self._data:
|
||||
value_type = self._data[':' + key]
|
||||
if isinstance(value_type, int):
|
||||
value_type = 'Binary'
|
||||
else:
|
||||
if isinstance(value, str):
|
||||
value_type = 'String'
|
||||
elif isinstance(value, int):
|
||||
value_type = 'Long'
|
||||
elif isinstance(value, bool):
|
||||
value_type = 'Boolean'
|
||||
else:
|
||||
raise ValueError(f"Unknown value type {type(value)!r}")
|
||||
|
||||
self._connection._patch_builder.set_value(self.path + '/' + key, value, value_type)
|
||||
|
||||
def __getattr__(self, item: str):
|
||||
try:
|
||||
return super(SimpleNode, self).__getattr__(item)
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
try:
|
||||
value = self._data.get(item)
|
||||
except KeyError:
|
||||
raise AttributeError()
|
||||
|
||||
if isinstance(value, dict):
|
||||
return self._connection.get_simple_node(self.path + '/' + item)
|
||||
return value
|
||||
|
||||
def __dir__(self):
|
||||
return super(SimpleNode, self).__dir__() + list(self._data.keys())
|
||||
Reference in New Issue
Block a user