This repository has been archived on 2024-07-30. You can view files and clone it, but cannot push or open issues or pull requests.
python-aternos/python_aternos/atfile.py

148 lines
3.1 KiB
Python
Raw Normal View History

2022-03-18 17:38:36 +03:00
import enum
import lxml.html
from typing import Union
2021-10-15 18:31:47 +03:00
from typing import TYPE_CHECKING
2022-03-18 17:38:36 +03:00
from .aterrors import FileError
2021-10-15 18:31:47 +03:00
if TYPE_CHECKING:
2022-03-18 17:38:36 +03:00
from .atserver import AternosServer
2021-10-15 18:31:47 +03:00
2022-03-18 17:38:36 +03:00
class FileType(enum.IntEnum):
"""File or dierctory"""
2022-03-18 17:38:36 +03:00
file = 0
directory = 1
class AternosFile:
"""File class which contains info about its path, type and size
:param atserv: :class:`python_aternos.atserver.AternosServer` instance
:type atserv: python_aternos.atserver.AternosServer
:param path: Path to the file
:type path: str
:param name: Filename
:type name: str
:param ftype: File or directory
:type ftype: python_aternos.atfile.FileType
:param size: File size, defaults to 0
:type size: Union[int,float], optional
"""
2021-10-14 17:41:57 +03:00
def __init__(
2021-10-15 18:31:47 +03:00
self, atserv:'AternosServer',
path:str, name:str, ftype:FileType=FileType.file,
2022-03-25 15:45:38 +03:00
size:Union[int,float]=0) -> None:
self.atserv = atserv
self._path = path.lstrip('/')
self._name = name
2022-03-25 15:45:38 +03:00
self._full = path + name
self._ftype = ftype
2021-10-14 17:41:57 +03:00
self._size = float(size)
2021-10-14 17:41:57 +03:00
def delete(self) -> None:
"""Deletes the file"""
self.atserv.atserver_request(
'https://aternos.org/panel/ajax/delete.php',
2022-03-25 15:45:38 +03:00
'POST', data={'file': self._full},
sendtoken=True
)
2022-03-18 17:38:36 +03:00
def get_content(self) -> bytes:
2022-03-25 15:45:38 +03:00
"""Requests file content in bytes (downloads it)
:raises FileError: If downloading
the file is not allowed by Aternos
:return: File content
:rtype: bytes
"""
file = self.atserv.atserver_request(
2022-03-25 15:45:38 +03:00
'https://aternos.org/panel/ajax/files/download.php',
2022-03-18 17:38:36 +03:00
'GET', params={
2022-03-25 15:45:38 +03:00
'file': self._full
2022-03-18 17:38:36 +03:00
}
)
2022-03-25 15:45:38 +03:00
if file.content == b'{"success":false}':
raise FileError('Unable to download the file. Try to get text')
return file.content
2022-03-18 17:38:36 +03:00
def set_content(self, value:bytes) -> None:
2022-03-25 15:45:38 +03:00
"""Modifies the file content
:param value: New content
:type value: bytes
"""
self.atserv.atserver_request(
f'https://aternos.org/panel/ajax/save.php',
2022-03-18 17:38:36 +03:00
'POST', data={
2022-03-25 15:45:38 +03:00
'file': self._full,
2022-03-18 17:38:36 +03:00
'content': value
}, sendtoken=True
)
2022-03-18 17:38:36 +03:00
def get_text(self) -> str:
2022-03-25 15:45:38 +03:00
"""Requests editing the file as a text
(try it if downloading is disallowed)
:return: File text content
:rtype: str
"""
editor = self.atserv.atserver_request(
f'https://aternos.org/files/{self._full.lstrip("/")}', 'GET'
)
edittree = lxml.html.fromstring(editor.content)
editblock = edittree.xpath('//div[@id="editor"]')[0]
return editblock.text_content()
2022-03-18 17:38:36 +03:00
def set_text(self, value:str) -> None:
2022-03-25 15:45:38 +03:00
"""Modifies the file content,
but unlike set_content takes
a string as a new value
:param value: New content
:type value: str
"""
2022-03-18 17:38:36 +03:00
self.set_content(value.encode('utf-8'))
@property
def path(self):
return self._path
@property
2021-10-14 17:41:57 +03:00
def name(self) -> str:
return self._name
2022-03-25 15:45:38 +03:00
@property
def full(self) -> str:
return self._full
@property
def is_dir(self) -> bool:
2022-03-18 17:38:36 +03:00
if self._ftype == FileType.directory:
return True
return False
@property
def is_file(self) -> bool:
2022-03-18 17:38:36 +03:00
if self._ftype == FileType.file:
return True
return False
@property
2021-10-14 17:41:57 +03:00
def size(self) -> float:
return self._size