Skip to content
This repository has been archived by the owner on Feb 22, 2020. It is now read-only.

Commit

Permalink
fix(cli): fix cli client required
Browse files Browse the repository at this point in the history
  • Loading branch information
hanhxiao committed Sep 12, 2019
1 parent 1d87dfc commit 563a48c
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 11 deletions.
30 changes: 27 additions & 3 deletions gnes/cli/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,29 @@
import argparse


class ActionNoYes(argparse.Action):
def __init__(self, option_strings, dest, default=None, required=False, help=None):

if default is None:
raise ValueError('you must provide a default with yes/no action')
if len(option_strings) != 1:
raise ValueError('only single argument is allowed with yes/no action')
opt = option_strings[0]
if not opt.startswith('--'):
raise ValueError('yes/no arguments must be prefixed with --')

opt = opt[2:]
opts = ['--' + opt, '--no-' + opt]
super(ActionNoYes, self).__init__(opts, dest, nargs=0, const=None,
default=default, required=required, help=help)

def __call__(self, parser, namespace, values, option_strings=None):
if option_strings.startswith('--no-'):
setattr(namespace, self.dest, False)
else:
setattr(namespace, self.dest, True)


def resolve_py_path(path):
import os
if not os.path.exists(path):
Expand Down Expand Up @@ -54,7 +77,8 @@ def set_base_parser():
'It enables large-scale index and semantic search for text-to-text, image-to-image, '
'video-to-video and any content form. Visit %s for tutorials and documentations.' % (
colored('GNES v%s: Generic Neural Elastic Search' % __version__, 'green'),
colored('https://gnes.ai', 'cyan', attrs=['underline'])))
colored('https://gnes.ai', 'cyan', attrs=['underline'])),
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('-v', '--version', action='version', version='%(prog)s ' + __version__)
parser.add_argument('--verbose', action='store_true', default=False,
help='turn on detailed logging for debug')
Expand Down Expand Up @@ -152,7 +176,7 @@ def set_service_parser(parser=None):
parser.add_argument('--parallel_type', type=ParallelType.from_string, choices=list(ParallelType),
default=ParallelType.PUSH_NONBLOCK,
help='parallel type of the concurrent services')
parser.add_argument('--check_version', action='store_true', default=False,
parser.add_argument('--check_version', action=ActionNoYes, default=True,
help='comparing the GNES and proto version of incoming message with local setup, '
'mismatch raise an exception')
parser.add_argument('--identity', type=str, default=str(uuid.uuid4()).split('-')[0],
Expand Down Expand Up @@ -283,7 +307,7 @@ def set_frontend_parser(parser=None):
read_only=True)
parser.add_argument('--max_concurrency', type=int, default=10,
help='maximum concurrent connections allowed')
parser.add_argument('--show_route_table', action='store_true', default=False,
parser.add_argument('--route_table', action=ActionNoYes, default=True,
help='showing a route table with time cost after receiving the result')
return parser

Expand Down
2 changes: 1 addition & 1 deletion gnes/service/frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def remove_envelope(self, m: 'gnes_pb2.Message'):
resp = m.response
resp.request_id = m.envelope.request_id
m.envelope.routes[0].end_time.GetCurrentTime()
if self.args.show_route_table:
if self.args.route_table:
self.logger.info('route: %s' % router2str(m))
route_time = []
k = m.envelope.routes[0]
Expand Down
2 changes: 1 addition & 1 deletion tests/test_client_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def test_cli(self):
])

args = set_frontend_parser().parse_args([
'--show_route_table'

])

p_args = set_router_parser().parse_args([
Expand Down
2 changes: 1 addition & 1 deletion tests/test_dict_indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def setUp(self):
def test_pymode(self):
os.unsetenv('http_proxy')
os.unsetenv('https_proxy')
args = set_frontend_parser().parse_args(['--show_route_table'])
args = set_frontend_parser().parse_args([])

p_args = set_preprocessor_parser().parse_args([
'--port_in', str(args.port_out),
Expand Down
2 changes: 1 addition & 1 deletion tests/test_grpc_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def setUp(self):
'--grpc_port', '9999',
'--port_in', str(self.s_args.port_out),
'--port_out', str(self.s_args.port_in),
'--show_route_table'

])

def test_grpc_empty_service(self):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_service_mgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def _test_multiple_router(self, backend='thread', num_parallel=5):
def _test_grpc_multiple_router(self, backend='thread', num_parallel=5):
args = set_frontend_parser().parse_args([
'--grpc_host', '127.0.0.1',
'--show_route_table'

])

p_args = set_router_parser().parse_args([
Expand All @@ -55,7 +55,7 @@ def _test_grpc_multiple_router(self, backend='thread', num_parallel=5):
def _test_grpc_multiple_pub(self, backend='thread', num_parallel=5):
args = set_frontend_parser().parse_args([
'--grpc_host', '127.0.0.1',
'--show_route_table'

])

p_args = set_router_parser().parse_args([
Expand Down
4 changes: 2 additions & 2 deletions tests/test_stream_grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def setUp(self):
def test_bm_frontend(self):
args = set_frontend_parser().parse_args([
'--grpc_host', '127.0.0.1',
'--show_route_table'

])

p_args = set_router_parser().parse_args([
Expand All @@ -65,7 +65,7 @@ def test_bm_frontend(self):
def test_grpc_frontend(self):
args = set_frontend_parser().parse_args([
'--grpc_host', '127.0.0.1',
'--show_route_table'

])

p_args = set_router_parser().parse_args([
Expand Down

0 comments on commit 563a48c

Please sign in to comment.