2022-07-01 14:28:39 +04:00
|
|
|
"""Exploring files in your server directory"""
|
|
|
|
|
|
|
|
from typing import Union, Optional, Any, List
|
2021-10-15 19:31:47 +04:00
|
|
|
from typing import TYPE_CHECKING
|
2021-10-14 17:56:01 +04:00
|
|
|
|
2022-08-22 09:55:08 +04:00
|
|
|
import lxml.html
|
|
|
|
|
2022-03-18 18:38:36 +04:00
|
|
|
from .atfile import AternosFile, FileType
|
2021-10-15 19:31:47 +04:00
|
|
|
if TYPE_CHECKING:
|
2022-06-23 15:13:56 +04:00
|
|
|
from .atserver import AternosServer
|
|
|
|
|
2021-10-15 19:31:47 +04:00
|
|
|
|
2022-03-18 18:38:36 +04:00
|
|
|
class FileManager:
|
2021-10-14 17:56:01 +04:00
|
|
|
|
MkDocs, Readme, Files API, Automated session saving, v2.0.1
MkDocs: sphinx docstrings rewritten to google, improved config, written the major part of how-to.
Readme: centered title + logo, added badges, features list, updated changelog.
Improved Files API, added automatical session saving and restoring to Client.
Some changes in makefile and gitignore.
License Notice now refers to all contributors.
2022-08-26 16:14:07 +04:00
|
|
|
"""Aternos file manager class
|
|
|
|
for viewing files structure"""
|
2022-06-23 15:13:56 +04:00
|
|
|
|
|
|
|
def __init__(self, atserv: 'AternosServer') -> None:
|
|
|
|
|
MkDocs, Readme, Files API, Automated session saving, v2.0.1
MkDocs: sphinx docstrings rewritten to google, improved config, written the major part of how-to.
Readme: centered title + logo, added badges, features list, updated changelog.
Improved Files API, added automatical session saving and restoring to Client.
Some changes in makefile and gitignore.
License Notice now refers to all contributors.
2022-08-26 16:14:07 +04:00
|
|
|
"""Aternos file manager class
|
|
|
|
for viewing files structure
|
|
|
|
|
|
|
|
Args:
|
|
|
|
atserv (python_aternos.atserver.AternosServer):
|
|
|
|
atserver.AternosServer instance
|
|
|
|
"""
|
|
|
|
|
2022-06-23 15:13:56 +04:00
|
|
|
self.atserv = atserv
|
|
|
|
|
MkDocs, Readme, Files API, Automated session saving, v2.0.1
MkDocs: sphinx docstrings rewritten to google, improved config, written the major part of how-to.
Readme: centered title + logo, added badges, features list, updated changelog.
Improved Files API, added automatical session saving and restoring to Client.
Some changes in makefile and gitignore.
License Notice now refers to all contributors.
2022-08-26 16:14:07 +04:00
|
|
|
def list_dir(self, path: str = '') -> List[AternosFile]:
|
2022-06-23 15:13:56 +04:00
|
|
|
|
|
|
|
"""Requests a list of files
|
|
|
|
in the specified directory
|
|
|
|
|
MkDocs, Readme, Files API, Automated session saving, v2.0.1
MkDocs: sphinx docstrings rewritten to google, improved config, written the major part of how-to.
Readme: centered title + logo, added badges, features list, updated changelog.
Improved Files API, added automatical session saving and restoring to Client.
Some changes in makefile and gitignore.
License Notice now refers to all contributors.
2022-08-26 16:14:07 +04:00
|
|
|
Args:
|
|
|
|
path (str, optional):
|
|
|
|
Directory (an empty string means root)
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
List of atfile.AternosFile objects
|
2022-06-23 15:13:56 +04:00
|
|
|
"""
|
|
|
|
|
|
|
|
path = path.lstrip('/')
|
2022-07-01 14:28:39 +04:00
|
|
|
|
2022-06-23 15:13:56 +04:00
|
|
|
filesreq = self.atserv.atserver_request(
|
|
|
|
f'https://aternos.org/files/{path}', 'GET'
|
|
|
|
)
|
|
|
|
filestree = lxml.html.fromstring(filesreq.content)
|
2022-07-01 14:28:39 +04:00
|
|
|
|
2022-06-23 15:13:56 +04:00
|
|
|
fileslist = filestree.xpath(
|
MkDocs, Readme, Files API, Automated session saving, v2.0.1
MkDocs: sphinx docstrings rewritten to google, improved config, written the major part of how-to.
Readme: centered title + logo, added badges, features list, updated changelog.
Improved Files API, added automatical session saving and restoring to Client.
Some changes in makefile and gitignore.
License Notice now refers to all contributors.
2022-08-26 16:14:07 +04:00
|
|
|
'//div[@class="file" or @class="file clickable"]'
|
2022-06-23 15:13:56 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
files = []
|
|
|
|
for f in fileslist:
|
|
|
|
|
|
|
|
ftype_raw = f.xpath('@data-type')[0]
|
2022-07-01 14:28:39 +04:00
|
|
|
fsize = self.extract_size(
|
|
|
|
f.xpath('./div[@class="filesize"]')
|
|
|
|
)
|
2022-06-23 15:13:56 +04:00
|
|
|
|
MkDocs, Readme, Files API, Automated session saving, v2.0.1
MkDocs: sphinx docstrings rewritten to google, improved config, written the major part of how-to.
Readme: centered title + logo, added badges, features list, updated changelog.
Improved Files API, added automatical session saving and restoring to Client.
Some changes in makefile and gitignore.
License Notice now refers to all contributors.
2022-08-26 16:14:07 +04:00
|
|
|
rm_btn = f.xpath('./div[contains(@class,"js-delete-file")]')
|
|
|
|
dl_btn = f.xpath('./div[contains(@class,"js-download-file")]')
|
|
|
|
clickable = 'clickable' in f.classes
|
|
|
|
is_config = ('server.properties' in path) or ('level.dat' in path)
|
|
|
|
|
2022-06-23 15:13:56 +04:00
|
|
|
files.append(
|
|
|
|
AternosFile(
|
MkDocs, Readme, Files API, Automated session saving, v2.0.1
MkDocs: sphinx docstrings rewritten to google, improved config, written the major part of how-to.
Readme: centered title + logo, added badges, features list, updated changelog.
Improved Files API, added automatical session saving and restoring to Client.
Some changes in makefile and gitignore.
License Notice now refers to all contributors.
2022-08-26 16:14:07 +04:00
|
|
|
atserv=self.atserv,
|
|
|
|
path=f.xpath('@data-path')[0],
|
|
|
|
|
|
|
|
rmable=(len(rm_btn) > 0),
|
|
|
|
dlable=(len(dl_btn) > 0),
|
|
|
|
editable=(clickable and not is_config),
|
|
|
|
|
|
|
|
ftype={'file': FileType.file}.get(
|
|
|
|
ftype_raw, FileType.dir
|
|
|
|
),
|
|
|
|
size=fsize
|
2022-06-23 15:13:56 +04:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
return files
|
|
|
|
|
2022-07-01 14:28:39 +04:00
|
|
|
def extract_size(self, fsize_raw: List[Any]) -> float:
|
|
|
|
|
|
|
|
"""Parses file size from the LXML tree
|
|
|
|
|
MkDocs, Readme, Files API, Automated session saving, v2.0.1
MkDocs: sphinx docstrings rewritten to google, improved config, written the major part of how-to.
Readme: centered title + logo, added badges, features list, updated changelog.
Improved Files API, added automatical session saving and restoring to Client.
Some changes in makefile and gitignore.
License Notice now refers to all contributors.
2022-08-26 16:14:07 +04:00
|
|
|
Args:
|
|
|
|
fsize_raw (List[Any]): XPath parsing result
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
File size in bytes
|
2022-07-01 14:28:39 +04:00
|
|
|
"""
|
|
|
|
|
|
|
|
if len(fsize_raw) > 0:
|
|
|
|
|
|
|
|
fsize_text = fsize_raw[0].text.strip()
|
|
|
|
fsize_num = fsize_text[:fsize_text.rfind(' ')]
|
|
|
|
fsize_msr = fsize_text[fsize_text.rfind(' ') + 1:]
|
|
|
|
|
|
|
|
try:
|
MkDocs, Readme, Files API, Automated session saving, v2.0.1
MkDocs: sphinx docstrings rewritten to google, improved config, written the major part of how-to.
Readme: centered title + logo, added badges, features list, updated changelog.
Improved Files API, added automatical session saving and restoring to Client.
Some changes in makefile and gitignore.
License Notice now refers to all contributors.
2022-08-26 16:14:07 +04:00
|
|
|
return self.convert_size(
|
|
|
|
float(fsize_num),
|
|
|
|
fsize_msr
|
|
|
|
)
|
2022-07-01 14:28:39 +04:00
|
|
|
except ValueError:
|
|
|
|
return -1.0
|
|
|
|
|
|
|
|
return 0.0
|
|
|
|
|
|
|
|
def convert_size(
|
|
|
|
self,
|
|
|
|
num: Union[int, float],
|
|
|
|
measure: str) -> float:
|
2022-06-23 15:13:56 +04:00
|
|
|
|
|
|
|
"""Converts "human" file size to size in bytes
|
|
|
|
|
MkDocs, Readme, Files API, Automated session saving, v2.0.1
MkDocs: sphinx docstrings rewritten to google, improved config, written the major part of how-to.
Readme: centered title + logo, added badges, features list, updated changelog.
Improved Files API, added automatical session saving and restoring to Client.
Some changes in makefile and gitignore.
License Notice now refers to all contributors.
2022-08-26 16:14:07 +04:00
|
|
|
Args:
|
|
|
|
num (Union[int,float]): Size
|
|
|
|
measure (str): Units (B, kB, MB, GB)
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Size in bytes
|
2022-06-23 15:13:56 +04:00
|
|
|
"""
|
|
|
|
|
|
|
|
measure_match = {
|
|
|
|
'B': 1,
|
|
|
|
'kB': 1000,
|
|
|
|
'MB': 1000000,
|
|
|
|
'GB': 1000000000
|
|
|
|
}
|
2022-07-01 14:28:39 +04:00
|
|
|
return measure_match.get(measure, -1) * num
|
2022-06-23 15:13:56 +04:00
|
|
|
|
|
|
|
def get_file(self, path: str) -> Optional[AternosFile]:
|
|
|
|
|
|
|
|
"""Returns :class:`python_aternos.atfile.AternosFile`
|
|
|
|
instance by its path
|
|
|
|
|
MkDocs, Readme, Files API, Automated session saving, v2.0.1
MkDocs: sphinx docstrings rewritten to google, improved config, written the major part of how-to.
Readme: centered title + logo, added badges, features list, updated changelog.
Improved Files API, added automatical session saving and restoring to Client.
Some changes in makefile and gitignore.
License Notice now refers to all contributors.
2022-08-26 16:14:07 +04:00
|
|
|
Args:
|
|
|
|
path (str): Path to the file including its filename
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
atfile.AternosFile object
|
|
|
|
if file has been found,
|
|
|
|
otherwise None
|
2022-06-23 15:13:56 +04:00
|
|
|
"""
|
|
|
|
|
MkDocs, Readme, Files API, Automated session saving, v2.0.1
MkDocs: sphinx docstrings rewritten to google, improved config, written the major part of how-to.
Readme: centered title + logo, added badges, features list, updated changelog.
Improved Files API, added automatical session saving and restoring to Client.
Some changes in makefile and gitignore.
License Notice now refers to all contributors.
2022-08-26 16:14:07 +04:00
|
|
|
filedir = path[:path.rfind('/')]
|
2022-06-23 15:13:56 +04:00
|
|
|
filename = path[path.rfind('/'):]
|
|
|
|
|
MkDocs, Readme, Files API, Automated session saving, v2.0.1
MkDocs: sphinx docstrings rewritten to google, improved config, written the major part of how-to.
Readme: centered title + logo, added badges, features list, updated changelog.
Improved Files API, added automatical session saving and restoring to Client.
Some changes in makefile and gitignore.
License Notice now refers to all contributors.
2022-08-26 16:14:07 +04:00
|
|
|
files = self.list_dir(filedir)
|
2022-06-23 15:13:56 +04:00
|
|
|
|
MkDocs, Readme, Files API, Automated session saving, v2.0.1
MkDocs: sphinx docstrings rewritten to google, improved config, written the major part of how-to.
Readme: centered title + logo, added badges, features list, updated changelog.
Improved Files API, added automatical session saving and restoring to Client.
Some changes in makefile and gitignore.
License Notice now refers to all contributors.
2022-08-26 16:14:07 +04:00
|
|
|
return {
|
|
|
|
'file': f
|
|
|
|
for f in files
|
|
|
|
if f.name == filename
|
|
|
|
}.get('file', None)
|
2022-06-23 15:13:56 +04:00
|
|
|
|
|
|
|
def dl_file(self, path: str) -> bytes:
|
|
|
|
|
|
|
|
"""Returns the file content in bytes (downloads it)
|
|
|
|
|
MkDocs, Readme, Files API, Automated session saving, v2.0.1
MkDocs: sphinx docstrings rewritten to google, improved config, written the major part of how-to.
Readme: centered title + logo, added badges, features list, updated changelog.
Improved Files API, added automatical session saving and restoring to Client.
Some changes in makefile and gitignore.
License Notice now refers to all contributors.
2022-08-26 16:14:07 +04:00
|
|
|
Args:
|
|
|
|
path (str): Path to file including its filename
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
File content
|
2022-06-23 15:13:56 +04:00
|
|
|
"""
|
|
|
|
|
2022-07-01 14:28:39 +04:00
|
|
|
file = self.atserv.atserver_request( # type: ignore
|
2022-06-23 15:13:56 +04:00
|
|
|
'https://aternos.org/panel/ajax/files/download.php'
|
|
|
|
'GET', params={
|
|
|
|
'file': path.replace('/', '%2F')
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
return file.content
|
|
|
|
|
|
|
|
def dl_world(self, world: str = 'world') -> bytes:
|
|
|
|
|
|
|
|
"""Returns the world zip file content
|
|
|
|
by its name (downloads it)
|
|
|
|
|
MkDocs, Readme, Files API, Automated session saving, v2.0.1
MkDocs: sphinx docstrings rewritten to google, improved config, written the major part of how-to.
Readme: centered title + logo, added badges, features list, updated changelog.
Improved Files API, added automatical session saving and restoring to Client.
Some changes in makefile and gitignore.
License Notice now refers to all contributors.
2022-08-26 16:14:07 +04:00
|
|
|
Args:
|
|
|
|
world (str, optional): Name of world
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
ZIP file content
|
2022-06-23 15:13:56 +04:00
|
|
|
"""
|
|
|
|
|
2022-07-01 14:28:39 +04:00
|
|
|
resp = self.atserv.atserver_request( # type: ignore
|
2022-06-23 15:13:56 +04:00
|
|
|
'https://aternos.org/panel/ajax/worlds/download.php'
|
|
|
|
'GET', params={
|
|
|
|
'world': world.replace('/', '%2F')
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2022-07-01 14:28:39 +04:00
|
|
|
return resp.content
|