Module opshin.rewrite.rewrite_import_plutusdata

Expand source code
from ast import *
from typing import Optional

from ..util import CompilingNodeTransformer

"""
Checks that there was an import of dataclass if there are any class definitions
"""


class RewriteImportPlutusData(CompilingNodeTransformer):
    step = "Resolving imports and usage of PlutusData and Datum"

    imports_plutus_data = False

    def visit_ImportFrom(self, node: ImportFrom) -> Optional[ImportFrom]:
        if node.module != "pycardano":
            return node
        assert (
            len(node.names) == 2
        ), "The program must contain one 'from pycardano import Datum as Anything, PlutusData'"
        assert (
            node.names[0].name == "Datum"
        ), "The program must contain one 'from pycardano import Datum as Anything, PlutusData'"
        assert (
            node.names[0].asname == "Anything"
        ), "The program must contain one 'from pycardano import Datum as Anything, PlutusData'"
        assert (
            node.names[1].name == "PlutusData"
        ), "The program must contain one 'from pycardano import Datum as Anything, PlutusData'"
        assert (
            node.names[1].asname == None
        ), "The program must contain one 'from pycardano import Datum as Anything, PlutusData'"
        self.imports_plutus_data = True
        return None

    def visit_ClassDef(self, node: ClassDef) -> ClassDef:
        assert (
            len(node.decorator_list) == 1
        ), "Class definitions must have no decorators but @dataclass"
        assert (
            len(node.bases) == 1
        ), "Class definitions must inherit only from PlutusData"
        assert isinstance(
            node.bases[0], Name
        ), "The inheritance must be direct, using the name PlutusData"
        base: Name = node.bases[0]
        assert base.id == "PlutusData", "Class definitions must inherit from PlutusData"
        assert (
            self.imports_plutus_data
        ), "PlutusData must be imported in order to use datum classes"
        return node

Classes

class RewriteImportPlutusData

A :class:NodeVisitor subclass that walks the abstract syntax tree and allows modification of nodes.

The NodeTransformer will walk the AST and use the return value of the visitor methods to replace or remove the old node. If the return value of the visitor method is None, the node will be removed from its location, otherwise it is replaced with the return value. The return value may be the original node in which case no replacement takes place.

Here is an example transformer that rewrites all occurrences of name lookups (foo) to data['foo']::

class RewriteName(NodeTransformer):

   def visit_Name(self, node):
       return Subscript(
           value=Name(id='data', ctx=Load()),
           slice=Constant(value=node.id),
           ctx=node.ctx
       )

Keep in mind that if the node you're operating on has child nodes you must either transform the child nodes yourself or call the :meth:generic_visit method for the node first.

For nodes that were part of a collection of statements (that applies to all statement nodes), the visitor may also return a list of nodes rather than just a single node.

Usually you use the transformer like this::

node = YourTransformer().visit(node)

Expand source code
class RewriteImportPlutusData(CompilingNodeTransformer):
    step = "Resolving imports and usage of PlutusData and Datum"

    imports_plutus_data = False

    def visit_ImportFrom(self, node: ImportFrom) -> Optional[ImportFrom]:
        if node.module != "pycardano":
            return node
        assert (
            len(node.names) == 2
        ), "The program must contain one 'from pycardano import Datum as Anything, PlutusData'"
        assert (
            node.names[0].name == "Datum"
        ), "The program must contain one 'from pycardano import Datum as Anything, PlutusData'"
        assert (
            node.names[0].asname == "Anything"
        ), "The program must contain one 'from pycardano import Datum as Anything, PlutusData'"
        assert (
            node.names[1].name == "PlutusData"
        ), "The program must contain one 'from pycardano import Datum as Anything, PlutusData'"
        assert (
            node.names[1].asname == None
        ), "The program must contain one 'from pycardano import Datum as Anything, PlutusData'"
        self.imports_plutus_data = True
        return None

    def visit_ClassDef(self, node: ClassDef) -> ClassDef:
        assert (
            len(node.decorator_list) == 1
        ), "Class definitions must have no decorators but @dataclass"
        assert (
            len(node.bases) == 1
        ), "Class definitions must inherit only from PlutusData"
        assert isinstance(
            node.bases[0], Name
        ), "The inheritance must be direct, using the name PlutusData"
        base: Name = node.bases[0]
        assert base.id == "PlutusData", "Class definitions must inherit from PlutusData"
        assert (
            self.imports_plutus_data
        ), "PlutusData must be imported in order to use datum classes"
        return node

Ancestors

Class variables

var imports_plutus_data
var step

Methods

def visit(self, node)

Inherited from: CompilingNodeTransformer.visit

Visit a node.

def visit_ClassDef(self, node: ast.ClassDef) ‑> ast.ClassDef
def visit_ImportFrom(self, node: ast.ImportFrom) ‑> ast.ImportFrom | None