2021-10-11 15:28:09 +02:00
|
|
|
# standard imports
|
|
|
|
import logging
|
|
|
|
|
|
|
|
logg = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2022-03-01 08:01:56 +01:00
|
|
|
class ContractProcessor:
|
2021-11-29 08:02:12 +01:00
|
|
|
"""Drives the serialization and publishing of contracts, proofs and metadata for the token.
|
|
|
|
|
|
|
|
:param proof: Proof object to publish
|
|
|
|
:type proof: cic.proof.Proof
|
|
|
|
:param attachment: Attachment object to publish
|
|
|
|
:type attachment: cic.attachment.Attachment
|
|
|
|
:param metadata: Metadata object to publish
|
|
|
|
:type metadata: cic.meta.Meta
|
|
|
|
:param writer: Writer interface receiving the output of the processor
|
2022-03-01 08:01:56 +01:00
|
|
|
:type writer: cic.writers.OutputWriter
|
2021-11-29 08:02:12 +01:00
|
|
|
:param extensions: Extension contexts to publish to
|
|
|
|
:type extensions: list of cic.extension.Extension
|
|
|
|
"""
|
2022-03-01 08:01:56 +01:00
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
proof=None,
|
|
|
|
attachment=None,
|
|
|
|
metadata=None,
|
|
|
|
outputs_writer=None,
|
|
|
|
extensions=[],
|
|
|
|
):
|
2021-10-11 12:23:08 +02:00
|
|
|
self.token_address = None
|
2021-10-11 15:28:09 +02:00
|
|
|
self.extensions = extensions
|
2021-10-11 17:39:01 +02:00
|
|
|
self.cores = {
|
2022-03-01 08:01:56 +01:00
|
|
|
"metadata": metadata,
|
|
|
|
"attachment": attachment,
|
|
|
|
"proof": proof,
|
|
|
|
}
|
2021-10-11 15:28:09 +02:00
|
|
|
self.outputs = []
|
2021-10-11 12:23:08 +02:00
|
|
|
self.__outputs_writer = outputs_writer
|
|
|
|
|
|
|
|
def writer(self):
|
2021-11-29 08:02:12 +01:00
|
|
|
"""Return the writer instance that the process is using.
|
|
|
|
|
2022-03-01 08:01:56 +01:00
|
|
|
:rtype: cic.writers.OutputWriter
|
2021-11-29 08:02:12 +01:00
|
|
|
:return: Writer
|
|
|
|
"""
|
2021-10-11 12:23:08 +02:00
|
|
|
return self.__outputs_writer
|
|
|
|
|
2021-10-11 15:28:09 +02:00
|
|
|
def get_outputs(self):
|
2021-11-29 08:02:12 +01:00
|
|
|
"""Return all written outputs.
|
|
|
|
|
|
|
|
This will return nothing unless the process method has been executed.
|
|
|
|
|
|
|
|
:rtype: bytes
|
|
|
|
:return: Outputs
|
|
|
|
"""
|
2021-10-11 15:28:09 +02:00
|
|
|
outputs = []
|
|
|
|
for ext in self.extensions:
|
|
|
|
outputs += ext.outputs
|
|
|
|
outputs += self.outputs
|
|
|
|
return outputs
|
2021-10-11 12:23:08 +02:00
|
|
|
|
2021-10-12 11:27:26 +02:00
|
|
|
def process(self, writer=None):
|
2021-11-29 08:02:12 +01:00
|
|
|
"""Serializes and publishes all token data.
|
|
|
|
|
|
|
|
Calls the process method on each extension. For each extension, the process method on attachment, proof and metadata, in that order, for any of them that have provided at processor object instantiation.
|
|
|
|
|
|
|
|
All output written to the publish writer will also be cached so that it subsequently be recalled using the get_outputs method.
|
|
|
|
|
|
|
|
:param writer: Writer to use for publishing.
|
2022-03-01 08:01:56 +01:00
|
|
|
:type writer: cic.writers.OutputWriter
|
2021-11-29 08:02:12 +01:00
|
|
|
"""
|
2021-10-12 11:27:26 +02:00
|
|
|
|
2021-10-11 12:23:08 +02:00
|
|
|
tasks = [
|
2022-03-01 08:01:56 +01:00
|
|
|
"attachment",
|
|
|
|
"proof",
|
|
|
|
"metadata",
|
|
|
|
]
|
2021-10-11 12:23:08 +02:00
|
|
|
|
2021-10-11 15:28:09 +02:00
|
|
|
for ext in self.extensions:
|
2021-10-21 15:11:05 +02:00
|
|
|
(token_address, token_symbol) = ext.process()
|
2021-10-11 12:23:08 +02:00
|
|
|
|
2021-10-11 15:28:09 +02:00
|
|
|
for task in tasks:
|
2021-10-11 17:39:01 +02:00
|
|
|
a = self.cores.get(task)
|
2022-03-01 08:01:56 +01:00
|
|
|
if a is None:
|
|
|
|
logg.debug(f'skipping missing task receiver "{task}"')
|
2021-10-11 15:28:09 +02:00
|
|
|
continue
|
2022-03-01 08:01:56 +01:00
|
|
|
v = a.process(
|
|
|
|
token_address=token_address,
|
|
|
|
token_symbol=token_symbol,
|
|
|
|
writer=self.__outputs_writer,
|
|
|
|
)
|
2021-10-11 15:28:09 +02:00
|
|
|
self.outputs.append(v)
|