95 lines
3.1 KiB
Python
95 lines
3.1 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from typing import Any, Optional, Self, Sequence
|
|
from abc import ABC, abstractmethod
|
|
from os.path import dirname as directory_name, abspath as absolute_path
|
|
from Interfaces.Application.AnPInterface import AnPInterface
|
|
from Utils.Common import Common
|
|
from Utils.Checks import Check
|
|
from Utils.Patterns import RE
|
|
|
|
class FilesAbstract(ABC):
|
|
|
|
def __init__(self:Self, anp:AnPInterface, inputs:Optional[dict[str, Any|None]|Sequence[Any|None]] = None) -> None:
|
|
self.anp:AnPInterface = anp
|
|
root_path:str = directory_name(absolute_path(__file__))
|
|
self._slash:str = Common.get_value("slash", inputs, "/" if "/" in root_path else "\\\\")
|
|
self._root_paths:list[str] = Common.get_value(
|
|
"root_paths",
|
|
inputs,
|
|
[""] + self.get_parents(root_path, 4)
|
|
)
|
|
|
|
def set_root_paths(self:Self, paths:str|Sequence[str]) -> None:
|
|
|
|
path:str
|
|
|
|
for path in Common.get_array(paths):
|
|
if path not in self._root_paths:
|
|
self._root_paths.append(path)
|
|
|
|
def set_slash(self:Self, slash:str) -> None:
|
|
self._slash = slash
|
|
|
|
def fix_path(self:Self, path:str) -> str:
|
|
return RE.SLASHES.sub(self._slash, path)
|
|
|
|
def get_parent(self:Self, path:str) -> str:
|
|
return RE.PARENT_PATH.sub(r'\1', self.fix_path(path))
|
|
|
|
def get_parents(self:Self, path:str, levels:int = 1) -> list[str]:
|
|
|
|
parents:list[str] = [self.fix_path(path)]
|
|
|
|
while len(parents[-1]) > 2 and len(parents) < levels:
|
|
parents.append(RE.PARENT_PATH.sub(r'\1', parents[-1]))
|
|
|
|
return parents
|
|
|
|
@abstractmethod
|
|
def get_absolute_path(self:Self, path:str) -> str|None:pass
|
|
|
|
@abstractmethod
|
|
def exists(self:Self, path:str) -> bool:pass
|
|
|
|
@abstractmethod
|
|
def is_file(self:Self, path:str) -> bool:pass
|
|
|
|
@abstractmethod
|
|
def is_directory(self:Self, path:str) -> bool:pass
|
|
|
|
@abstractmethod
|
|
def load(self:Self, path:str, mode:str = "r") -> str|bytes|None:pass
|
|
|
|
@abstractmethod
|
|
def make_directory(self:Self, path:str) -> bool:pass
|
|
|
|
@abstractmethod
|
|
def save(self:Self, path:str, content:str|bytes) -> bool:pass
|
|
|
|
@abstractmethod
|
|
def delete(self:Self, path:str) -> bool:pass
|
|
|
|
def load_json(self:Self, inputs:Any|None, only_dictionaries:bool = True) -> list[dict[str, Any|None]|Sequence[Any|None]]:
|
|
|
|
results:list[dict[str, Any|None]|Sequence[Any|None]] = []
|
|
|
|
if Check.is_dictionary(inputs):
|
|
results.append(inputs)
|
|
elif Check.is_array(inputs):
|
|
if only_dictionaries:
|
|
for item in inputs:
|
|
results.extend(self.load_json(item, only_dictionaries))
|
|
else:
|
|
results.append(inputs)
|
|
elif Check.is_string(inputs):
|
|
|
|
json:dict[str, Any|None]|Sequence[Any|None]|None = Common.json_decode(inputs)
|
|
|
|
if json is None:
|
|
results.extend(self.load_json(self.load(inputs), only_dictionaries))
|
|
else:
|
|
results.extend(self.load_json(json, only_dictionaries))
|
|
|
|
return results |