Skip to content

Commit

Permalink
python: Split apart the Command hierarchy into a backwards-compatible…
Browse files Browse the repository at this point in the history
… version and a forward-looking one.
  • Loading branch information
da-tanabe committed Mar 11, 2021
1 parent 9ccdcad commit 10e4458
Show file tree
Hide file tree
Showing 13 changed files with 845 additions and 390 deletions.
48 changes: 38 additions & 10 deletions python/dazl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,43 @@
"""
This module contains the Python API for interacting with the Ledger API.
"""
from ast import literal_eval
from configparser import ConfigParser
from pathlib import Path

import pkg_resources

__all__ = [
"AIOPartyClient",
"Command",
"ContractData",
"ContractId",
"CreateAndExerciseCommand",
"CreateCommand",
"DazlError",
"ExerciseByKeyCommand",
"ExerciseCommand",
"LOG",
"Network",
"Party",
"SimplePartyClient",
"__version__",
"async_network",
"create",
"create_and_exercise",
"exercise",
"exercise_by_key",
"frozendict",
"run",
"setup_default_logger",
"simple_client",
"write_acs",
]


from ._logging import LOG
from .client import AIOPartyClient, Network, SimplePartyClient, async_network, run, simple_client
from .pretty.table import write_acs
from .prim import ContractData, ContractId, DazlError, FrozenDict as frozendict, Party
from .protocols.commands import (
Command,
from .client.commands import (
CreateAndExerciseCommand,
CreateCommand,
ExerciseByKeyCommand,
Expand All @@ -19,6 +50,9 @@
exercise,
exercise_by_key,
)
from .pretty.table import write_acs
from .prim import ContractData, ContractId, DazlError, FrozenDict as frozendict, Party
from .protocols.commands import Command
from .util.logging import setup_default_logger

try:
Expand All @@ -45,12 +79,6 @@ def _get_version() -> str:
2. Use the value from the local pyproject.toml file (this is used when
running dazl from source).
"""
from ast import literal_eval
from configparser import ConfigParser
from pathlib import Path

import pkg_resources

try:
return pkg_resources.require("dazl")[0].version
except pkg_resources.DistributionNotFound:
Expand Down
2 changes: 1 addition & 1 deletion python/dazl/client/_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

from typing import Awaitable, Callable, TypeVar, Union

from ..protocols.commands import EventHandlerResponse
from ..protocols.events import BaseEvent
from .commands import EventHandlerResponse

E = TypeVar("E", bound=BaseEvent)
T = TypeVar("T")
Expand Down
15 changes: 6 additions & 9 deletions python/dazl/client/_party_client_impl.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Copyright (c) 2017-2021 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

from asyncio import Future, ensure_future, gather, get_event_loop, wait
from concurrent.futures import ALL_COMPLETED
from dataclasses import asdict, dataclass, field, fields, replace
Expand All @@ -20,17 +19,12 @@
TypeVar,
)
import uuid
import warnings

from .. import LOG
from ..damlast.daml_lf_1 import TypeConName
from ..prim import ContractId, Party, TimeDeltaLike, to_timedelta
from ..protocols import LedgerClient, LedgerNetwork
from ..protocols.commands import (
CommandBuilder,
CommandDefaults,
CommandPayload,
EventHandlerResponse,
)
from ..protocols.events import (
ActiveContractSetEvent,
BaseEvent,
Expand All @@ -52,6 +46,7 @@
from ._conn_settings import OAuthSettings, connection_settings
from ._writer_verify import ValidateSerializer
from .bots import Bot, BotCallback, BotCollection, BotFilter
from .commands import CommandBuilder, CommandDefaults, CommandPayload, EventHandlerResponse
from .config import NetworkConfig, PartyConfig
from .ledger import LedgerMetadata
from .state import (
Expand Down Expand Up @@ -455,7 +450,7 @@ def _process_transaction_stream_event(self, event: Any, raise_events: bool) -> F

def write_commands(
self,
commands: EventHandlerResponse,
commands: "EventHandlerResponse",
ignore_errors: bool = False,
workflow_id: "Optional[str]" = None,
deduplication_time: "Optional[TimeDeltaLike]" = None,
Expand All @@ -481,7 +476,9 @@ def write_commands(
"""
if workflow_id is None:
workflow_id = uuid.uuid4().hex
cb = CommandBuilder.coerce(commands, atomic_default=True)
with warnings.catch_warnings():
warnings.simplefilter("ignore", DeprecationWarning)
cb = CommandBuilder.coerce(commands, atomic_default=True)
cb.defaults(
workflow_id=workflow_id,
deduplication_time=(
Expand Down
46 changes: 25 additions & 21 deletions python/dazl/client/_writer_verify.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@
# SPDX-License-Identifier: Apache-2.0

from typing import Any
import warnings

from ..damlast.daml_lf_1 import TypeConName
from ..prim import ContractId
from ..protocols.commands import (
AbstractSerializer,
CreateAndExerciseCommand,
CreateCommand,
ExerciseByKeyCommand,
ExerciseCommand,
)
from ..protocols.commands import AbstractSerializer
from ..values import CanonicalMapper
from .commands import CreateAndExerciseCommand, CreateCommand, ExerciseByKeyCommand, ExerciseCommand


class ValidateSerializer(AbstractSerializer):
Expand All @@ -29,12 +25,16 @@ class ValidateSerializer(AbstractSerializer):
def serialize_create_command(
self, name: "TypeConName", template_args: "Any"
) -> "CreateCommand":
return CreateCommand(template=name, arguments=template_args)
with warnings.catch_warnings():
warnings.simplefilter("ignore", DeprecationWarning)
return CreateCommand(template=name, arguments=template_args)

def serialize_exercise_command(
self, contract_id: "ContractId", choice_name: str, choice_args: "Any"
) -> "ExerciseCommand":
return ExerciseCommand(contract=contract_id, choice=choice_name, arguments=choice_args)
with warnings.catch_warnings():
warnings.simplefilter("ignore", DeprecationWarning)
return ExerciseCommand(contract=contract_id, choice=choice_name, arguments=choice_args)

def serialize_exercise_by_key_command(
self,
Expand All @@ -43,12 +43,14 @@ def serialize_exercise_by_key_command(
choice_name: str,
choice_arguments: "Any",
) -> "ExerciseByKeyCommand":
return ExerciseByKeyCommand(
template=template_name,
contract_key=key_arguments,
choice=choice_name,
choice_argument=choice_arguments,
)
with warnings.catch_warnings():
warnings.simplefilter("ignore", DeprecationWarning)
return ExerciseByKeyCommand(
template=template_name,
contract_key=key_arguments,
choice=choice_name,
choice_argument=choice_arguments,
)

def serialize_create_and_exercise_command(
self,
Expand All @@ -57,9 +59,11 @@ def serialize_create_and_exercise_command(
choice_name: str,
choice_arguments: "Any",
) -> "CreateAndExerciseCommand":
return CreateAndExerciseCommand(
template=template_name,
arguments=create_arguments,
choice=choice_name,
choice_argument=choice_arguments,
)
with warnings.catch_warnings():
warnings.simplefilter("ignore", DeprecationWarning)
return CreateAndExerciseCommand(
template=template_name,
arguments=create_arguments,
choice=choice_name,
choice_argument=choice_arguments,
)
2 changes: 1 addition & 1 deletion python/dazl/client/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
from ..damlast.protocols import SymbolLookup
from ..metrics import MetricEvents
from ..prim import ContractData, ContractId, Party, TimeDeltaLike, to_party
from ..protocols.commands import EventHandlerResponse
from ..protocols.events import (
ContractArchiveEvent,
ContractCreateEvent,
Expand All @@ -70,6 +69,7 @@
from ._network_client_impl import _NetworkImpl
from ._party_client_impl import _PartyClientImpl
from .bots import Bot, BotCollection
from .commands import EventHandlerResponse
from .config import AnonymousNetworkConfig, NetworkConfig, PartyConfig
from .events import EventKey
from .ledger import LedgerMetadata
Expand Down
3 changes: 2 additions & 1 deletion python/dazl/client/bots.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@

from .. import LOG
from ..prim import Party
from ..protocols.commands import Command, CommandBuilder
from ..protocols.commands import Command
from ..protocols.events import BaseEvent
from ..util.asyncio_util import LongRunningAwaitable, Signal, completed, failed, propagate
from .commands import CommandBuilder
from .events import EventKey

if TYPE_CHECKING:
Expand Down
Loading

0 comments on commit 10e4458

Please sign in to comment.