-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Artem Saprykin
committed
May 8, 2019
1 parent
bb56429
commit 751cf24
Showing
10 changed files
with
741 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
""" | ||
Provide implementation of the command line interface's batch commands. | ||
""" | ||
import sys | ||
|
||
import click | ||
from remme import Remme | ||
|
||
from cli.batch.forms import GetBatchesListForm | ||
from cli.batch.help import ( | ||
BATCH_HEAD_ARGUMENT_HELP_MESSAGE, | ||
BATCH_IDS_ARGUMENT_HELP_MESSAGE, | ||
BATCH_LIMIT_ARGUMENT_HELP_MESSAGE, | ||
BATCH_REVERSE_ARGUMENT_HELP_MESSAGE, | ||
BATCH_START_ARGUMENT_HELP_MESSAGE, | ||
) | ||
from cli.batch.service import Batch | ||
from cli.constants import ( | ||
FAILED_EXIT_FROM_COMMAND_CODE, | ||
NODE_URL_ARGUMENT_HELP_MESSAGE, | ||
) | ||
from cli.utils import ( | ||
default_node_url, | ||
print_errors, | ||
print_result, | ||
) | ||
|
||
|
||
@click.group('batch', chain=True) | ||
def batch_commands(): | ||
""" | ||
Provide commands for working with batch. | ||
""" | ||
pass | ||
|
||
|
||
@click.option('--ids', required=False, type=str, help=BATCH_IDS_ARGUMENT_HELP_MESSAGE) | ||
@click.option('--start', required=False, type=str, help=BATCH_START_ARGUMENT_HELP_MESSAGE) | ||
@click.option('--limit', required=False, type=int, help=BATCH_LIMIT_ARGUMENT_HELP_MESSAGE) | ||
@click.option('--head', required=False, type=str, help=BATCH_HEAD_ARGUMENT_HELP_MESSAGE) | ||
@click.option('--reverse', required=False, is_flag=True, help=BATCH_REVERSE_ARGUMENT_HELP_MESSAGE) | ||
@click.option('--node-url', required=False, type=str, help=NODE_URL_ARGUMENT_HELP_MESSAGE, default=default_node_url()) | ||
@batch_commands.command('get-list') | ||
def get_batches(ids, start, limit, head, reverse, node_url): | ||
""" | ||
Get a list of batches. | ||
""" | ||
arguments, errors = GetBatchesListForm().load({ | ||
'ids': ids, | ||
'start': start, | ||
'limit': limit, | ||
'head': head, | ||
'reverse': reverse, | ||
'node_url': node_url, | ||
}) | ||
|
||
if errors: | ||
print_errors(errors=errors) | ||
sys.exit(FAILED_EXIT_FROM_COMMAND_CODE) | ||
|
||
batch_ids = arguments.get('ids') | ||
start = arguments.get('start') | ||
limit = arguments.get('limit') | ||
head = arguments.get('head') | ||
node_url = arguments.get('node_url') | ||
|
||
remme = Remme(network_config={ | ||
'node_address': str(node_url) + ':8080', | ||
}) | ||
|
||
batches, errors = Batch(service=remme).get_list( | ||
batch_ids=batch_ids, | ||
start=start, | ||
limit=limit, | ||
head=head, | ||
reverse=reverse, | ||
) | ||
|
||
if errors is not None: | ||
print_errors(errors=errors) | ||
sys.exit(FAILED_EXIT_FROM_COMMAND_CODE) | ||
|
||
print_result(result=batches) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
""" | ||
Provide forms for command line interface's batch commands. | ||
""" | ||
from marshmallow import ( | ||
Schema, | ||
fields, | ||
validate, | ||
) | ||
|
||
from cli.generic.forms.fields import ( | ||
BatchIdentifierField, | ||
BatchIdentifiersListField, | ||
NodeUrlField, | ||
) | ||
|
||
|
||
class GetBatchesListForm(Schema): | ||
""" | ||
Get a list of batch form. | ||
""" | ||
|
||
ids = BatchIdentifiersListField(allow_none=True, required=False) | ||
start = BatchIdentifierField(allow_none=True, required=False) | ||
head = BatchIdentifierField(allow_none=True, required=False) | ||
limit = fields.Integer( | ||
allow_none=True, | ||
strict=True, | ||
required=False, | ||
validate=[ | ||
validate.Range(min=1, error='Limit must be greater than 0.'), | ||
], | ||
) | ||
node_url = NodeUrlField(required=False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
""" | ||
Provide help messages for command line interface's batch commands. | ||
""" | ||
BATCH_IDS_ARGUMENT_HELP_MESSAGE = 'Identifiers to get a list of batches by.' | ||
BATCH_START_ARGUMENT_HELP_MESSAGE = 'Batch identifier to get a list of batches starting from.' | ||
BATCH_LIMIT_ARGUMENT_HELP_MESSAGE = 'Maximum amount of batches to return.' | ||
BATCH_HEAD_ARGUMENT_HELP_MESSAGE = 'Block identifier to get a list of batches from.' | ||
BATCH_REVERSE_ARGUMENT_HELP_MESSAGE = 'Parameter to reverse result.' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
""" | ||
Provide implementation of the batch interfaces. | ||
""" | ||
|
||
|
||
class BatchInterface: | ||
""" | ||
Implements batch interface. | ||
""" | ||
|
||
def get_list(self, batch_ids, start, limit, head, reverse): | ||
""" | ||
Get a list of batches. | ||
A list of batches could be filtered by batch identifiers, start identifier, limit, head identifier, | ||
reverse. | ||
Arguments: | ||
batch_ids (list, optional): identifiers to get a list of batches by. | ||
start (string, optional): batch identifier to get a list of batches starting from. | ||
limit (int, optional): maximum amount of batches to return. | ||
head (string, optional): block identifier to get a list of batches from. | ||
reverse (string, optional): parameter to reverse result. | ||
""" | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
""" | ||
Provide implementation of the batch. | ||
""" | ||
import asyncio | ||
|
||
from accessify import implements | ||
from aiohttp_json_rpc import RpcGenericServerDefinedError | ||
|
||
from cli.batch.interfaces import BatchInterface | ||
|
||
loop = asyncio.get_event_loop() | ||
|
||
|
||
@implements(BatchInterface) | ||
class Batch: | ||
""" | ||
Implements batch. | ||
""" | ||
|
||
def __init__(self, service): | ||
""" | ||
Constructor. | ||
Arguments: | ||
service: object to interact with Remme core API. | ||
""" | ||
self.service = service | ||
|
||
def get_list(self, batch_ids, start, limit, head, reverse): | ||
""" | ||
Get a list of batches. | ||
Arguments: | ||
batch_ids (list, optional): identifiers to get a list of batches by. | ||
start (string, optional): batch identifier to get a list of batches starting from. | ||
limit (int, optional): maximum amount of batches to return. | ||
head (string, optional): block identifier to get a list of batches from. | ||
reverse (string, optional): parameter to reverse result. | ||
""" | ||
reverse = '' if reverse else 'false' | ||
|
||
try: | ||
batches = loop.run_until_complete( | ||
self.service.blockchain_info.get_batches(query={ | ||
'ids': batch_ids, | ||
'start': start, | ||
'limit': limit, | ||
'head': head, | ||
'reverse': reverse, | ||
}), | ||
) | ||
|
||
except RpcGenericServerDefinedError as error: | ||
return None, str(error.message) | ||
|
||
except Exception as error: | ||
return None, str(error) | ||
|
||
return batches, None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.