-
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
Apr 24, 2019
1 parent
789714a
commit 326892d
Showing
9 changed files
with
598 additions
and
1 deletion.
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,105 @@ | ||
""" | ||
Provide implementation of the command line interface's batch commands. | ||
""" | ||
import asyncio | ||
import sys | ||
|
||
import click | ||
from remme import Remme | ||
|
||
from cli.batch.forms import ListBatchesForm | ||
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, | ||
dict_to_pretty_json, | ||
print_errors, | ||
print_result, | ||
) | ||
|
||
loop = asyncio.get_event_loop() | ||
|
||
|
||
@click.group('batch', chain=True) | ||
def batch_commands(): | ||
""" | ||
Provide commands for working with batches. | ||
""" | ||
pass | ||
|
||
|
||
def _split_ids(ctx, param, value): | ||
""" | ||
Convert string to list. Remove '[', ']' and split by ','. | ||
""" | ||
if value is None: | ||
return | ||
|
||
if value[0] != '[' or value[-1] != ']': | ||
click.echo(dict_to_pretty_json({ | ||
'ids': ['Not a valid list.'], | ||
})) | ||
sys.exit(FAILED_EXIT_FROM_COMMAND_CODE) | ||
value = value[1:-1] | ||
values = [c.strip() for c in value.split(',')] | ||
|
||
return values | ||
|
||
|
||
@click.option('--ids', type=str, required=False, help=BATCH_IDS_ARGUMENT_HELP_MESSAGE, callback=_split_ids) | ||
@click.option('--start', type=str, required=False, help=BATCH_START_ARGUMENT_HELP_MESSAGE) | ||
@click.option('--limit', type=int, required=False, help=BATCH_LIMIT_ARGUMENT_HELP_MESSAGE) | ||
@click.option('--head', type=str, required=False, help=BATCH_HEAD_ARGUMENT_HELP_MESSAGE) | ||
@click.option('--reverse', type=str, required=False, help=BATCH_REVERSE_ARGUMENT_HELP_MESSAGE) | ||
@click.option('--node-url', type=str, required=False, 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 = ListBatchesForm().load({ | ||
'ids': ids, | ||
'start': start, | ||
'limit': limit, | ||
'head': head, | ||
'reverse': reverse, | ||
'node_url': node_url, | ||
}) | ||
|
||
if errors: | ||
print_errors(errors) | ||
sys.exit(FAILED_EXIT_FROM_COMMAND_CODE) | ||
|
||
ids = arguments.get('ids') | ||
start = arguments.get('start') | ||
limit = arguments.get('limit') | ||
head = arguments.get('head') | ||
reverse = arguments.get('reverse') | ||
node_url = arguments.get('node_url') | ||
|
||
remme = Remme(network_config={ | ||
'node_address': str(node_url) + ':8080', | ||
}) | ||
|
||
query = { | ||
'ids': ids, | ||
'start': start, | ||
'limit': limit, | ||
'head': head, | ||
'reverse': reverse, | ||
} | ||
|
||
batch_service = Batch(service=remme) | ||
batches = loop.run_until_complete(batch_service.get_list(query)) | ||
|
||
print_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,87 @@ | ||
""" | ||
Provide forms for command line interface's batch commands. | ||
""" | ||
import re | ||
|
||
from marshmallow import ( | ||
Schema, | ||
ValidationError, | ||
fields, | ||
validates, | ||
) | ||
|
||
from cli.constants import ( | ||
DOMAIN_NAME_REGEXP, | ||
HEADER_SIGNATURE_REGEXP, | ||
) | ||
|
||
|
||
class ListBatchesForm(Schema): | ||
""" | ||
List batches validation form. | ||
""" | ||
|
||
ids = fields.List(fields.String(), allow_none=True, required=False) | ||
start = fields.String(allow_none=True, required=False) | ||
limit = fields.Integer(allow_none=True, required=False) | ||
head = fields.String(allow_none=True, required=False) | ||
reverse = fields.String(allow_none=True, required=False) | ||
node_url = fields.String(allow_none=True, required=False) | ||
|
||
@validates('ids') | ||
def validate_ids(self, ids): | ||
""" | ||
Validate batch identifiers. | ||
""" | ||
if ids is not None: | ||
for batch_id in ids: | ||
if re.match(pattern=HEADER_SIGNATURE_REGEXP, string=batch_id) is None: | ||
raise ValidationError(f'The following batch id `{batch_id}` is invalid.') | ||
|
||
@validates('start') | ||
def validate_start(self, start): | ||
""" | ||
Validate start. | ||
""" | ||
if start is not None and re.match(pattern=HEADER_SIGNATURE_REGEXP, string=start) is None: | ||
raise ValidationError(f'The following batch id `{start}` is invalid.') | ||
|
||
@validates('limit') | ||
def validate_limit(self, limit): | ||
""" | ||
Validate limit. | ||
""" | ||
if limit is not None and limit < 0: | ||
raise ValidationError("Limit can't be negative.") | ||
|
||
@validates('head') | ||
def validate_head(self, head): | ||
""" | ||
Validate head. | ||
""" | ||
if head is not None and re.match(pattern=HEADER_SIGNATURE_REGEXP, string=head) is None: | ||
raise ValidationError(f'The following block id `{head}` is invalid.') | ||
|
||
@validates('reverse') | ||
def validate_reverse(self, reverse): | ||
""" | ||
Validate reverse. | ||
""" | ||
if reverse is not None and reverse not in ('true', 'false'): | ||
raise ValidationError("Invalid reverse field. Should be either 'true' or 'false'.") | ||
|
||
@validates('node_url') | ||
def validate_node_url(self, node_url): | ||
""" | ||
Validate node URL. | ||
If node URL is localhost, it means client didn't passed any URL, so nothing to validate. | ||
""" | ||
if node_url == 'localhost': | ||
return | ||
|
||
if 'http' in node_url or 'https' in node_url: | ||
raise ValidationError(f'Pass the following node URL `{node_url}` without protocol (http, https, etc.).') | ||
|
||
if re.match(pattern=DOMAIN_NAME_REGEXP, string=node_url) is None: | ||
raise ValidationError(f'The following node URL `{node_url}` is invalid.') |
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 = 'Parameter to list batches starting from.' | ||
BATCH_LIMIT_ARGUMENT_HELP_MESSAGE = 'Parameter to limit amount of batches.' | ||
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,21 @@ | ||
""" | ||
Provide implementation of the batch interfaces. | ||
""" | ||
|
||
|
||
class BatchInterface: | ||
""" | ||
Implements batch interface. | ||
""" | ||
|
||
async def get_list(self, query=None): | ||
""" | ||
Get a list of batches from REMChain. | ||
Arguments: | ||
query (dict, optional): dictionary with specific parameters | ||
Returns: | ||
List of batches. | ||
""" | ||
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,34 @@ | ||
""" | ||
Provide implementation of the batch service. | ||
""" | ||
from accessify import implements | ||
|
||
from cli.batch.interfaces import BatchInterface | ||
|
||
|
||
@implements(BatchInterface) | ||
class Batch: | ||
""" | ||
Implements batch interface. | ||
""" | ||
|
||
def __init__(self, service): | ||
""" | ||
Constructor. | ||
Arguments: | ||
service: object to interact with Remme core API. | ||
""" | ||
self.service = service | ||
|
||
async def get_list(self, query=None): | ||
""" | ||
Get all batches from REMChain. | ||
Arguments: | ||
query (dict, optional): dictionary with specific parameters | ||
Returns: | ||
List of batches. | ||
""" | ||
return await self.service.blockchain_info.get_batches(query=query) |
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.