Skip to content

Parsers

BaseParser

Base protocol for Parser classes

Attributes:

Name Type Description
_METADATA_STANDARD MetadataStandard

a class variable defining the metadata standard a parser supports.

fetcher BaseFetcher

a fetcher instance responsible for fetching content, mime type, and size by making requests.

Source code in offchain/metadata/parsers/base_parser.py
class BaseParser(Protocol):
    """Base protocol for Parser classes

    Attributes:
        _METADATA_STANDARD (MetadataStandard): a class variable defining the metadata standard a parser supports.
        fetcher (BaseFetcher): a fetcher instance responsible for fetching content,
            mime type, and size by making requests.
    """  # noqa: E501

    _METADATA_STANDARD: MetadataStandard

    fetcher: BaseFetcher

    def __init__(self, fetcher: BaseFetcher) -> None:
        pass

    def parse_metadata(  # type: ignore[no-untyped-def]
        self, token: Token, raw_data: dict, *args, **kwargs  # type: ignore[type-arg]
    ) -> Optional[Metadata]:
        """Given a token and raw data returned from the token uri, return a normalized Metadata object.

        Args:
            token (Token): token to process metadata for.
            raw_data (dict): raw data returned from token uri.

        Returns:
            Optional[Metadata]: normalized metadata object, if successfully parsed.
        """  # noqa: E501
        pass

    def should_parse_token(  # type: ignore[no-untyped-def]
        self, token: Token, raw_data: Optional[dict], *args, **kwargs  # type: ignore[type-arg]  # noqa: E501
    ) -> bool:
        """Return whether or not a collection parser should parse a given token.

        Args:
            token (Token): the token whose metadata needs to be parsed.
            raw_data (dict): raw data returned from token uri.

        Returns:
            bool: whether or not the collection parser handles this token.
        """
        pass

    @abstractmethod
    async def _gen_parse_metadata_impl(  # type: ignore[no-untyped-def]
        self, token: Token, raw_data: dict, *args, **kwargs  # type: ignore[type-arg]
    ):
        raise NotImplementedError("Not implemented")

    async def gen_parse_metadata(  # type: ignore[no-untyped-def]
        self, token: Token, raw_data: dict, *args, **kwargs  # type: ignore[type-arg]
    ) -> Optional[Metadata]:
        """Given a token and raw data returned from the token uri, return a normalized Metadata object.

        Args:
            token (Token): token to process metadata for.
            raw_data (dict): raw data returned from token uri.

        Returns:
            Optional[Metadata]: normalized metadata object, if successfully parsed.
        """  # noqa: E501
        try:
            return await self._gen_parse_metadata_impl(token, raw_data, *args, **kwargs)  # type: ignore[no-any-return]  # noqa: E501
        except NotImplementedError:
            logger.warn(
                f"{self.__class__.__name__} doesn't implement gen_parse_metadata, fallback to legacy parse_metadata"  # noqa: E501
            )
            if pytest.is_running:
                raise
            return self.parse_metadata(token, raw_data, *args, **kwargs)

gen_parse_metadata(token, raw_data, *args, **kwargs) async

Given a token and raw data returned from the token uri, return a normalized Metadata object.

Parameters:

Name Type Description Default
token Token

token to process metadata for.

required
raw_data dict

raw data returned from token uri.

required

Returns:

Type Description
Optional[Metadata]

Optional[Metadata]: normalized metadata object, if successfully parsed.

Source code in offchain/metadata/parsers/base_parser.py
async def gen_parse_metadata(  # type: ignore[no-untyped-def]
    self, token: Token, raw_data: dict, *args, **kwargs  # type: ignore[type-arg]
) -> Optional[Metadata]:
    """Given a token and raw data returned from the token uri, return a normalized Metadata object.

    Args:
        token (Token): token to process metadata for.
        raw_data (dict): raw data returned from token uri.

    Returns:
        Optional[Metadata]: normalized metadata object, if successfully parsed.
    """  # noqa: E501
    try:
        return await self._gen_parse_metadata_impl(token, raw_data, *args, **kwargs)  # type: ignore[no-any-return]  # noqa: E501
    except NotImplementedError:
        logger.warn(
            f"{self.__class__.__name__} doesn't implement gen_parse_metadata, fallback to legacy parse_metadata"  # noqa: E501
        )
        if pytest.is_running:
            raise
        return self.parse_metadata(token, raw_data, *args, **kwargs)

parse_metadata(token, raw_data, *args, **kwargs)

Given a token and raw data returned from the token uri, return a normalized Metadata object.

Parameters:

Name Type Description Default
token Token

token to process metadata for.

required
raw_data dict

raw data returned from token uri.

required

Returns:

Type Description
Optional[Metadata]

Optional[Metadata]: normalized metadata object, if successfully parsed.

Source code in offchain/metadata/parsers/base_parser.py
def parse_metadata(  # type: ignore[no-untyped-def]
    self, token: Token, raw_data: dict, *args, **kwargs  # type: ignore[type-arg]
) -> Optional[Metadata]:
    """Given a token and raw data returned from the token uri, return a normalized Metadata object.

    Args:
        token (Token): token to process metadata for.
        raw_data (dict): raw data returned from token uri.

    Returns:
        Optional[Metadata]: normalized metadata object, if successfully parsed.
    """  # noqa: E501
    pass

should_parse_token(token, raw_data, *args, **kwargs)

Return whether or not a collection parser should parse a given token.

Parameters:

Name Type Description Default
token Token

the token whose metadata needs to be parsed.

required
raw_data dict

raw data returned from token uri.

required

Returns:

Name Type Description
bool bool

whether or not the collection parser handles this token.

Source code in offchain/metadata/parsers/base_parser.py
def should_parse_token(  # type: ignore[no-untyped-def]
    self, token: Token, raw_data: Optional[dict], *args, **kwargs  # type: ignore[type-arg]  # noqa: E501
) -> bool:
    """Return whether or not a collection parser should parse a given token.

    Args:
        token (Token): the token whose metadata needs to be parsed.
        raw_data (dict): raw data returned from token uri.

    Returns:
        bool: whether or not the collection parser handles this token.
    """
    pass

CollectionParser

Base class for collection parsers

All parsers that handle collection-based metadata parsing will need to inherit from this base class.

Attributes:

Name Type Description
_COLLECTION_ADDRESSES list[str]

list of collection addresses that a parser class handles.

_METADATA_STANDARD MetadataStandard

(MetadataStandard): metadata standard of all metadata returned by this class of parser. Defaults to MetadataStandard.COLLECTION_STANDARD.

fetcher BaseFetcher

a fetcher instance for making network requests.

contract_caller ContractCaller

a contract caller instance for fetching data from contracts.

Source code in offchain/metadata/parsers/collection/collection_parser.py
class CollectionParser(BaseParser):
    """Base class for collection parsers

    All parsers that handle collection-based metadata parsing will need to
    inherit from this base class.

    Attributes:
        _COLLECTION_ADDRESSES (list[str]): list of collection addresses that a parser class handles.
        _METADATA_STANDARD: (MetadataStandard): metadata standard of all metadata returned by this class of parser.
            Defaults to MetadataStandard.COLLECTION_STANDARD.
        fetcher (BaseFetcher, optional): a fetcher instance for making network requests.
        contract_caller (ContractCaller, optional): a contract caller instance for fetching data from contracts.
    """  # noqa: E501

    _COLLECTION_ADDRESSES: list[str]
    _METADATA_STANDARD: MetadataStandard = MetadataStandard.COLLECTION_STANDARD

    def __init__(  # type: ignore[no-untyped-def]
        self,
        fetcher: Optional[BaseFetcher] = None,
        contract_caller: Optional[ContractCaller] = None,
        *args,
        **kwargs  # noqa: E501
    ) -> None:
        self.contract_caller = contract_caller or ContractCaller()
        self.fetcher = fetcher or MetadataFetcher()

    def should_parse_token(self, token: Token, *args, **kwargs) -> bool:  # type: ignore[no-untyped-def]  # noqa: E501
        """Return whether or not a collection parser should parse a given token.

        Args:
            token (Token): the token whose metadata needs to be parsed.

        Returns:
            bool: whether or not the collection parser handles this token.
        """
        return token.collection_address.lower() in [
            address.lower() for address in self._COLLECTION_ADDRESSES
        ]  # noqa: E501

should_parse_token(token, *args, **kwargs)

Return whether or not a collection parser should parse a given token.

Parameters:

Name Type Description Default
token Token

the token whose metadata needs to be parsed.

required

Returns:

Name Type Description
bool bool

whether or not the collection parser handles this token.

Source code in offchain/metadata/parsers/collection/collection_parser.py
def should_parse_token(self, token: Token, *args, **kwargs) -> bool:  # type: ignore[no-untyped-def]  # noqa: E501
    """Return whether or not a collection parser should parse a given token.

    Args:
        token (Token): the token whose metadata needs to be parsed.

    Returns:
        bool: whether or not the collection parser handles this token.
    """
    return token.collection_address.lower() in [
        address.lower() for address in self._COLLECTION_ADDRESSES
    ]  # noqa: E501

SchemaParser

Base class for schema parsers

All parsers that handle schema-based metadata parsing will need to inherit from this base class.

Attributes:

Name Type Description
fetcher BaseFetcher

a fetcher instance for making network requests

Source code in offchain/metadata/parsers/schema/schema_parser.py
class SchemaParser(BaseParser):
    """Base class for schema parsers

    All parsers that handle schema-based metadata parsing will need to inherit from this base class.

    Attributes:
        fetcher (BaseFetcher, optional): a fetcher instance for making network requests
    """  # noqa: E501

    def __init__(self, fetcher: Optional[BaseFetcher] = None, *args, **kwargs) -> None:  # type: ignore[no-untyped-def]  # noqa: E501
        self.fetcher = fetcher or MetadataFetcher()