Source code for plynx.base.resource

"""Templates for PLynx Resources and utils."""
from collections import namedtuple
from typing import Any, Dict, Optional

from plynx.constants import NodeResources

[docs]PreviewObject = namedtuple('PreviewObject', ['fp', 'resource_id'])
[docs]def _force_decode(byte_array): try: return byte_array.decode("utf-8") except UnicodeDecodeError: return f"# not a UTF-8 sequence:\n{byte_array}" return "Failed to decode the sequence"
[docs]class BaseResource: """Base Resource class"""
[docs] DISPLAY_RAW: bool = False
[docs] DISPLAY_THUMBNAIL: bool = False
def __init__(self): pass @staticmethod
[docs] def prepare_input(filename: str, preview: bool = False) -> Dict[str, str]: # pylint: disable=unused-argument """Resource preprocessor""" return {NodeResources.INPUT: filename}
@staticmethod
[docs] def prepare_output(filename: str, preview: bool = False) -> Dict[str, str]: """Prepare output""" if not preview: # Create file with open(filename, 'a'): pass return {NodeResources.OUTPUT: filename}
@staticmethod
[docs] def preprocess_input(value: Any) -> Any: """Resource preprocessor""" return value
@staticmethod
[docs] def postprocess_output(value: Any) -> Any: """Resource postprocessor""" return value
@classmethod
[docs] def preview(cls, preview_object: PreviewObject) -> str: """Preview Resource""" # TODO escape html code for security reasons data = _force_decode(preview_object.fp.read()) return f"<pre>{data}</pre>"
@classmethod
[docs] def thumbnail(cls, output: Any) -> Optional[str]: # pylint: disable=unused-argument """Thumbnail preview Resource""" return None