2016-07-06 08:17:38 +02:00
|
|
|
import sys
|
2016-09-15 20:47:32 +02:00
|
|
|
import argparse
|
2016-11-11 19:40:19 +01:00
|
|
|
import json
|
2016-12-21 20:55:43 +01:00
|
|
|
from lbrynet import conf
|
2016-09-20 22:58:30 +02:00
|
|
|
from lbrynet.lbrynet_daemon.auth.client import LBRYAPIClient
|
2016-11-11 19:40:19 +01:00
|
|
|
from jsonrpc.common import RPCError
|
|
|
|
|
|
|
|
|
2016-07-06 08:17:38 +02:00
|
|
|
|
2016-11-11 19:40:19 +01:00
|
|
|
help_msg = "Usage: lbrynet-cli method kwargs\n" \
|
2016-07-06 09:02:55 +02:00
|
|
|
+ "Examples: " \
|
2016-11-11 19:40:19 +01:00
|
|
|
+ "lbrynet-cli resolve_name name=what\n" \
|
2016-07-06 09:02:55 +02:00
|
|
|
+ "lbrynet-cli get_balance\n" \
|
2016-11-11 19:40:19 +01:00
|
|
|
+ "lbrynet-cli help function=resolve_name\n" \
|
2016-07-06 09:02:55 +02:00
|
|
|
+ "\n******lbrynet-cli functions******\n"
|
|
|
|
|
2016-07-06 08:17:38 +02:00
|
|
|
|
2016-09-15 20:47:32 +02:00
|
|
|
def guess_type(x):
|
|
|
|
if '.' in x:
|
|
|
|
try:
|
|
|
|
return float(x)
|
|
|
|
except ValueError:
|
|
|
|
# not a float
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
return int(x)
|
|
|
|
except ValueError:
|
|
|
|
return x
|
|
|
|
|
2016-09-16 04:20:00 +02:00
|
|
|
|
|
|
|
def get_params_from_kwargs(params):
|
|
|
|
params_for_return = {}
|
|
|
|
for i in params:
|
|
|
|
eq_pos = i.index('=')
|
|
|
|
k, v = i[:eq_pos], i[eq_pos+1:]
|
|
|
|
params_for_return[k] = guess_type(v)
|
|
|
|
return params_for_return
|
|
|
|
|
|
|
|
|
2016-07-06 08:17:38 +02:00
|
|
|
def main():
|
2016-09-21 09:49:52 +02:00
|
|
|
api = LBRYAPIClient.config()
|
2016-07-06 08:20:18 +02:00
|
|
|
|
|
|
|
try:
|
2016-09-21 09:49:52 +02:00
|
|
|
status = api.daemon_status()
|
|
|
|
assert status.get('code', False) == "started"
|
2016-10-26 09:16:33 +02:00
|
|
|
except Exception:
|
|
|
|
try:
|
2016-12-21 20:55:43 +01:00
|
|
|
conf.settings.update({'use_auth_http': not conf.settings.use_auth_http})
|
2016-10-26 09:16:33 +02:00
|
|
|
api = LBRYAPIClient.config()
|
|
|
|
status = api.daemon_status()
|
|
|
|
assert status.get('code', False) == "started"
|
|
|
|
except Exception:
|
|
|
|
print "lbrynet-daemon isn't running"
|
|
|
|
sys.exit(1)
|
2016-07-06 08:20:18 +02:00
|
|
|
|
2016-09-15 20:47:32 +02:00
|
|
|
parser = argparse.ArgumentParser()
|
2016-09-16 04:20:00 +02:00
|
|
|
parser.add_argument('method', nargs=1)
|
2016-09-15 21:04:42 +02:00
|
|
|
parser.add_argument('params', nargs=argparse.REMAINDER, default=None)
|
2016-09-15 20:47:32 +02:00
|
|
|
args = parser.parse_args()
|
2016-09-16 04:20:00 +02:00
|
|
|
|
2016-09-15 20:47:32 +02:00
|
|
|
meth = args.method[0]
|
|
|
|
params = {}
|
2016-09-16 04:20:00 +02:00
|
|
|
|
2016-11-11 19:40:19 +01:00
|
|
|
if len(args.params) > 1:
|
|
|
|
params = get_params_from_kwargs(args.params)
|
|
|
|
elif len(args.params) == 1:
|
|
|
|
try:
|
|
|
|
params = json.loads(args.params[0])
|
|
|
|
except ValueError:
|
2016-09-16 04:20:00 +02:00
|
|
|
params = get_params_from_kwargs(args.params)
|
2016-07-06 09:02:55 +02:00
|
|
|
|
|
|
|
msg = help_msg
|
|
|
|
for f in api.help():
|
|
|
|
msg += f + "\n"
|
|
|
|
|
|
|
|
if meth in ['--help', '-h', 'help']:
|
|
|
|
print msg
|
|
|
|
sys.exit(1)
|
|
|
|
|
2016-07-06 08:17:38 +02:00
|
|
|
if meth in api.help():
|
2016-07-06 09:02:55 +02:00
|
|
|
try:
|
|
|
|
if params:
|
2016-10-26 09:16:33 +02:00
|
|
|
result = LBRYAPIClient.config(service=meth, params=params)
|
2016-07-06 09:02:55 +02:00
|
|
|
else:
|
2016-10-26 09:16:33 +02:00
|
|
|
result = LBRYAPIClient.config(service=meth, params=params)
|
2016-09-21 09:49:52 +02:00
|
|
|
print json.dumps(result, sort_keys=True)
|
2016-11-11 19:40:19 +01:00
|
|
|
except RPCError as err:
|
2016-10-20 21:52:37 +02:00
|
|
|
# TODO: The api should return proper error codes
|
|
|
|
# and messages so that they can be passed along to the user
|
|
|
|
# instead of this generic message.
|
|
|
|
# https://app.asana.com/0/158602294500137/200173944358192
|
2016-07-06 09:02:55 +02:00
|
|
|
print "Something went wrong, here's the usage for %s:" % meth
|
|
|
|
print api.help({'function': meth})
|
2016-11-11 19:40:19 +01:00
|
|
|
print "Here's the traceback for the error you encountered:"
|
|
|
|
print err.msg
|
|
|
|
|
2016-07-06 08:17:38 +02:00
|
|
|
else:
|
2016-07-06 09:02:55 +02:00
|
|
|
print "Unknown function"
|
|
|
|
print msg
|
2016-07-06 08:17:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2016-07-14 05:44:02 +02:00
|
|
|
main()
|