Source code for drepr.models.resource

from __future__ import annotations

from abc import ABC, abstractmethod
from dataclasses import dataclass
from enum import Enum
from typing import Optional, TypeAlias, Union

ResourceId: TypeAlias = str


[docs]class ResourceType(Enum): CSV = "csv" JSON = "json" XML = "xml" Spreadsheet = "spreadsheet" NetCDF4 = "netcdf4" NetCDF3 = "netcdf3" GeoTIFF = "geotiff" NPDict = "np-dict" Shapefile = "shapefile" Container = "container"
[docs]@dataclass class CSVProp: delimiter: str = ","
# @dataclass # class SpreadsheetProp: # worksheet: Optional[str] = None
[docs]@dataclass class Resource: id: ResourceId type: ResourceType prop: Optional[CSVProp] = None
[docs] @staticmethod def deserialize(raw: dict): if raw["type"] == ResourceType.CSV.value and raw["prop"] is not None: prop = CSVProp(raw["prop"]["delimiter"]) else: prop = None return Resource(raw["id"], ResourceType(raw["type"]), prop)
[docs]@dataclass class PreprocessResourceOutput(Resource): original_resource_id: str = "" def __init__(self, resource_id: str, original_resource_id: str): super().__init__(id=resource_id, type=ResourceType.Container, prop=None) self.original_resource_id = original_resource_id
[docs] def get_preprocessing_original_resource_id(self): return self.original_resource_id
[docs]class ResourceData(ABC):
[docs] @abstractmethod def to_dict(self): pass
[docs]@dataclass class ResourceDataFile(ResourceData): file: str
[docs] def to_dict(self): return {"file": self.file}
[docs]@dataclass class ResourceDataString(ResourceData): value: Union[str, bytes]
[docs] def as_str(self): if isinstance(self.value, bytes): return self.value.decode() else: assert isinstance(self.value, str) return self.value
[docs] def to_dict(self): return { "string": ( self.value.decode() if isinstance(self.value, bytes) else self.value ) }
[docs]@dataclass class ResourceDataObject(ResourceData): value: dict | list
[docs] def to_dict(self): return {"object": self.value}