-
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 18, 2019
1 parent
6a312f8
commit d721d21
Showing
9 changed files
with
230 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 asyncio | ||
import re | ||
import sys | ||
|
||
import click | ||
from remme import Remme | ||
|
||
from cli.batch.help import ( | ||
BATCH_HEAD_ARGUMENT_HELP_MESSAGE, | ||
BATCH_ID_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 ( | ||
BATCH_ID_REGEXP, | ||
BLOCK_ID_REGEXP, | ||
FAILED_EXIT_FROM_COMMAND, | ||
NODE_URL_ARGUMENT_HELP_MESSAGE, | ||
) | ||
from cli.utils import dict_to_pretty_json | ||
|
||
loop = asyncio.get_event_loop() | ||
|
||
|
||
@click.group('batch', chain=True) | ||
def batch_commands(): | ||
""" | ||
Provide commands for working with batches. | ||
""" | ||
pass | ||
|
||
|
||
@click.option('--id', type=str, required=False, help=BATCH_ID_ARGUMENT_HELP_MESSAGE) | ||
@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) | ||
@batch_commands.command('get-list') | ||
def get_batches(id, start, limit, head, reverse, node_url): | ||
""" | ||
Get a list of batches. | ||
""" | ||
for batch_id in (id, start): | ||
if batch_id is not None and re.match(pattern=BATCH_ID_REGEXP, string=batch_id) is None: | ||
click.echo('The following batch id `{batch_id}` is not valid.'.format(batch_id=id)) | ||
sys.exit(FAILED_EXIT_FROM_COMMAND) | ||
|
||
if head is not None and re.match(pattern=BLOCK_ID_REGEXP, string=head) is None: | ||
click.echo('The following batch id `{batch_id}` is not valid.'.format(batch_id=id)) | ||
|
||
if limit is not None and limit < 0: | ||
click.echo("Limit can't be negative.") | ||
sys.exit(FAILED_EXIT_FROM_COMMAND) | ||
|
||
if reverse is not None and reverse not in ('true', 'false'): | ||
click.echo("Invalid reverse field. Should be either 'true' or 'false'") | ||
sys.exit(FAILED_EXIT_FROM_COMMAND) | ||
|
||
if node_url is None: | ||
node_url = 'localhost' | ||
|
||
remme = Remme(private_key_hex=None, network_config={ | ||
'node_address': str(node_url) + ':8080', | ||
}) | ||
|
||
query = { | ||
'id': id, | ||
'start': start, | ||
'limit': limit, | ||
'head': head, | ||
'reverse': reverse, | ||
} | ||
|
||
batch_service = Batch(service=remme) | ||
batches = loop.run_until_complete(batch_service.get_list(query)) | ||
|
||
click.echo(dict_to_pretty_json(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,8 @@ | ||
""" | ||
Provide help messages for command line interface's batch commands. | ||
""" | ||
BATCH_ID_ARGUMENT_HELP_MESSAGE = 'Identifier 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
""" | ||
Provide utils for command line interface. | ||
""" | ||
import json | ||
|
||
|
||
def dict_to_pretty_json(data): | ||
""" | ||
Convert dictionary to string with indents as human readable text. | ||
""" | ||
return json.dumps(data, indent=4, sort_keys=True) |