Remove write to file from API and move to CLI
parent
cd69c30143
commit
49b760673e
27
piker/cli.py
27
piker/cli.py
|
@ -121,7 +121,7 @@ def watch(loglevel, broker, rate, name):
|
||||||
log = get_console_log(loglevel) # activate console logging
|
log = get_console_log(loglevel) # activate console logging
|
||||||
brokermod = get_brokermod(broker)
|
brokermod = get_brokermod(broker)
|
||||||
|
|
||||||
watchlists = {
|
watchlists = json.dumps({
|
||||||
'cannabis': [
|
'cannabis': [
|
||||||
'EMH.VN', 'LEAF.TO', 'HVT.VN', 'HMMJ.TO', 'APH.TO',
|
'EMH.VN', 'LEAF.TO', 'HVT.VN', 'HMMJ.TO', 'APH.TO',
|
||||||
'CBW.VN', 'TRST.CN', 'VFF.TO', 'ACB.TO', 'ABCN.VN',
|
'CBW.VN', 'TRST.CN', 'VFF.TO', 'ACB.TO', 'ABCN.VN',
|
||||||
|
@ -135,7 +135,7 @@ def watch(loglevel, broker, rate, name):
|
||||||
'dad': ['GM', 'TSLA', 'DOL.TO', 'CIM', 'SPY', 'SHOP.TO'],
|
'dad': ['GM', 'TSLA', 'DOL.TO', 'CIM', 'SPY', 'SHOP.TO'],
|
||||||
'pharma': ['ATE.VN'],
|
'pharma': ['ATE.VN'],
|
||||||
'indexes': ['SPY', 'DAX', 'QQQ', 'DIA'],
|
'indexes': ['SPY', 'DAX', 'QQQ', 'DIA'],
|
||||||
}
|
})
|
||||||
# broker_conf_path = os.path.join(
|
# broker_conf_path = os.path.join(
|
||||||
# click.get_app_dir('piker'), 'watchlists.json')
|
# click.get_app_dir('piker'), 'watchlists.json')
|
||||||
# from piker.testing import _quote_streamer as brokermod
|
# from piker.testing import _quote_streamer as brokermod
|
||||||
|
@ -179,7 +179,7 @@ def show(ctx, name):
|
||||||
@click.pass_context
|
@click.pass_context
|
||||||
def load(ctx, data):
|
def load(ctx, data):
|
||||||
try:
|
try:
|
||||||
wl.write_watchlists(data, ctx.obj['path'])
|
wl.write_sorted_json(json.loads(data), ctx.obj['path'])
|
||||||
except (json.JSONDecodeError, IndexError):
|
except (json.JSONDecodeError, IndexError):
|
||||||
click.echo('You have passed an invalid text respresentation of a '
|
click.echo('You have passed an invalid text respresentation of a '
|
||||||
'JSON object. Try again.')
|
'JSON object. Try again.')
|
||||||
|
@ -190,8 +190,9 @@ def load(ctx, data):
|
||||||
@click.argument('ticker_name', nargs=1, required=True)
|
@click.argument('ticker_name', nargs=1, required=True)
|
||||||
@click.pass_context
|
@click.pass_context
|
||||||
def add(ctx, name, ticker_name):
|
def add(ctx, name, ticker_name):
|
||||||
watchlist = ctx.obj['watchlist']
|
watchlist = wl.add_ticker(name, ticker_name,
|
||||||
wl.add_ticker(name, ticker_name, watchlist, ctx.obj['path'])
|
ctx.obj['watchlist'])
|
||||||
|
wl.write_sorted_json(watchlist, ctx.obj['path'])
|
||||||
|
|
||||||
|
|
||||||
@watchlists.command(help='remove ticker from watchlist')
|
@watchlists.command(help='remove ticker from watchlist')
|
||||||
|
@ -199,29 +200,29 @@ def add(ctx, name, ticker_name):
|
||||||
@click.argument('ticker_name', nargs=1, required=True)
|
@click.argument('ticker_name', nargs=1, required=True)
|
||||||
@click.pass_context
|
@click.pass_context
|
||||||
def remove(ctx, name, ticker_name):
|
def remove(ctx, name, ticker_name):
|
||||||
watchlist = ctx.obj['watchlist']
|
watchlist = wl.remove_ticker(name, ticker_name, ctx.obj['watchlist'])
|
||||||
wl.remove_ticker(name, ticker_name, watchlist, ctx.obj['path'])
|
wl.write_sorted_json(watchlist, ctx.obj['path'])
|
||||||
|
|
||||||
|
|
||||||
@watchlists.command(help='delete watchlist group')
|
@watchlists.command(help='delete watchlist group')
|
||||||
@click.argument('name', nargs=1, required=True)
|
@click.argument('name', nargs=1, required=True)
|
||||||
@click.pass_context
|
@click.pass_context
|
||||||
def delete(ctx, name):
|
def delete(ctx, name):
|
||||||
watchlist = ctx.obj['watchlist']
|
watchlist = wl.delete_group(name, ctx.obj['watchlist'])
|
||||||
wl.delete_group(name, watchlist, ctx.obj['path'])
|
wl.write_sorted_json(watchlist, ctx.obj['path'])
|
||||||
|
|
||||||
|
|
||||||
@watchlists.command(help='merge a watchlist from another user')
|
@watchlists.command(help='merge a watchlist from another user')
|
||||||
@click.argument('watchlist_to_merge', nargs=1, required=True)
|
@click.argument('watchlist_to_merge', nargs=1, required=True)
|
||||||
@click.pass_context
|
@click.pass_context
|
||||||
def merge(ctx, watchlist_to_merge):
|
def merge(ctx, watchlist_to_merge):
|
||||||
watchlist = ctx.obj['watchlist']
|
merged_watchlist = wl.merge_watchlist(watchlist_to_merge,
|
||||||
wl.merge_watchlist(watchlist_to_merge, watchlist, ctx.obj['path'])
|
ctx.obj['watchlist'])
|
||||||
|
wl.write_sorted_json(merged_watchlist, ctx.obj['path'])
|
||||||
|
|
||||||
|
|
||||||
@watchlists.command(help='dump text respresentation of a watchlist to console')
|
@watchlists.command(help='dump text respresentation of a watchlist to console')
|
||||||
@click.argument('name', nargs=1, required=False)
|
@click.argument('name', nargs=1, required=False)
|
||||||
@click.pass_context
|
@click.pass_context
|
||||||
def dump(ctx, name):
|
def dump(ctx, name):
|
||||||
watchlist = ctx.obj['watchlist']
|
click.echo(json.dumps(ctx.obj['watchlist']))
|
||||||
click.echo(json.dumps(watchlist))
|
|
||||||
|
|
|
@ -26,33 +26,29 @@ def ensure_watchlists(file_path):
|
||||||
return json.load(f) if not os.stat(file_path).st_size == 0 else {}
|
return json.load(f) if not os.stat(file_path).st_size == 0 else {}
|
||||||
|
|
||||||
|
|
||||||
def write_watchlists(watchlist, path):
|
def add_ticker(name, ticker_name, watchlist):
|
||||||
write_sorted_json(json.loads(watchlist), path)
|
|
||||||
|
|
||||||
|
|
||||||
def add_ticker(name, ticker_name, watchlist, path):
|
|
||||||
watchlist.setdefault(name, []).append(str(ticker_name).upper())
|
watchlist.setdefault(name, []).append(str(ticker_name).upper())
|
||||||
write_sorted_json(watchlist, path)
|
return watchlist
|
||||||
|
|
||||||
|
|
||||||
def remove_ticker(name, ticker_name, watchlist, path):
|
def remove_ticker(name, ticker_name, watchlist):
|
||||||
if name in watchlist:
|
if name in watchlist:
|
||||||
watchlist[name].remove(str(ticker_name).upper())
|
watchlist[name].remove(str(ticker_name).upper())
|
||||||
if watchlist[name] == []:
|
if watchlist[name] == []:
|
||||||
del watchlist[name]
|
del watchlist[name]
|
||||||
write_sorted_json(watchlist, path)
|
return watchlist
|
||||||
|
|
||||||
|
|
||||||
def delete_group(name, watchlist, path):
|
def delete_group(name, watchlist):
|
||||||
if name in watchlist:
|
if name in watchlist:
|
||||||
del watchlist[name]
|
del watchlist[name]
|
||||||
write_sorted_json(watchlist, path)
|
return watchlist
|
||||||
|
|
||||||
|
|
||||||
def merge_watchlist(watchlist_to_merge, watchlist, path):
|
def merge_watchlist(watchlist_to_merge, watchlist):
|
||||||
merged_watchlist = defaultdict(list)
|
merged_watchlist = defaultdict(list)
|
||||||
watchlist_to_merge = json.loads(watchlist_to_merge)
|
watchlist_to_merge = json.loads(watchlist_to_merge)
|
||||||
for d in (watchlist, watchlist_to_merge):
|
for d in (watchlist, watchlist_to_merge):
|
||||||
for key, value in d.items():
|
for key, value in d.items():
|
||||||
merged_watchlist[key].extend(value)
|
merged_watchlist[key].extend(value)
|
||||||
write_sorted_json(merged_watchlist, path)
|
return merged_watchlist
|
||||||
|
|
|
@ -100,8 +100,7 @@ def temp_dir():
|
||||||
testing.
|
testing.
|
||||||
"""
|
"""
|
||||||
with tempfile.TemporaryDirectory() as tempdir:
|
with tempfile.TemporaryDirectory() as tempdir:
|
||||||
config_dir = os.path.join(tempdir, 'piker')
|
yield os.path.join(tempdir, 'piker')
|
||||||
yield config_dir
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
|
|
|
@ -59,48 +59,43 @@ def test_watchlist_is_read_from_file(piker_dir):
|
||||||
"""
|
"""
|
||||||
wl_temp = wl.ensure_watchlists(piker_dir)
|
wl_temp = wl.ensure_watchlists(piker_dir)
|
||||||
assert wl_temp == {}
|
assert wl_temp == {}
|
||||||
wl_temp2 = '{"AA": ["TEST.CN"]}'
|
wl_temp2 = {"AA": ["TEST.CN"]}
|
||||||
wl.write_watchlists(wl_temp2, piker_dir)
|
wl.write_sorted_json(wl_temp2, piker_dir)
|
||||||
assert json.loads(wl_temp2) == wl.ensure_watchlists(piker_dir)
|
assert wl_temp2 == wl.ensure_watchlists(piker_dir)
|
||||||
|
|
||||||
|
|
||||||
def test_new_ticker_added(piker_dir):
|
def test_new_ticker_added():
|
||||||
"""Ensure that a new ticker is added to a watchlist for both cases.
|
"""Ensure that a new ticker is added to a watchlist for both cases.
|
||||||
"""
|
"""
|
||||||
wl.add_ticker('test', 'TEST.CN', {'test': ['TEST2.CN']}, piker_dir)
|
wl_temp = wl.add_ticker('test', 'TEST.CN', {'test': ['TEST2.CN']})
|
||||||
wl_temp = wl.ensure_watchlists(piker_dir)
|
|
||||||
assert len(wl_temp['test']) == 2
|
assert len(wl_temp['test']) == 2
|
||||||
wl.add_ticker('test2', 'TEST.CN', wl_temp, piker_dir)
|
wl_temp = wl.add_ticker('test2', 'TEST.CN', wl_temp)
|
||||||
wl_temp = wl.ensure_watchlists(piker_dir)
|
|
||||||
assert wl_temp['test2']
|
assert wl_temp['test2']
|
||||||
|
|
||||||
|
|
||||||
def test_ticker_is_removed(piker_dir):
|
def test_ticker_is_removed():
|
||||||
"""Verify that passed in ticker is removed and that a group is removed
|
"""Verify that passed in ticker is removed and that a group is removed
|
||||||
if no tickers left.
|
if no tickers left.
|
||||||
"""
|
"""
|
||||||
wl_temp = {'test': ['TEST.CN', 'TEST2.CN'], 'test2': ['TEST.CN']}
|
wl_temp = {'test': ['TEST.CN', 'TEST2.CN'], 'test2': ['TEST.CN']}
|
||||||
wl.remove_ticker('test', 'TEST.CN', wl_temp, piker_dir)
|
wl_temp = wl.remove_ticker('test', 'TEST.CN', wl_temp)
|
||||||
wl.remove_ticker('test2', 'TEST.CN', wl_temp, piker_dir)
|
wl_temp = wl.remove_ticker('test2', 'TEST.CN', wl_temp)
|
||||||
wl_temp = wl.ensure_watchlists(piker_dir)
|
|
||||||
assert wl_temp == {'test': ['TEST2.CN']}
|
assert wl_temp == {'test': ['TEST2.CN']}
|
||||||
assert not wl_temp.get('test2')
|
assert not wl_temp.get('test2')
|
||||||
|
|
||||||
|
|
||||||
def test_group_is_deleted(piker_dir):
|
def test_group_is_deleted():
|
||||||
"""Check that watchlist group is removed.
|
"""Check that watchlist group is removed.
|
||||||
"""
|
"""
|
||||||
wl_temp = {'test': ['TEST.CN']}
|
wl_temp = {'test': ['TEST.CN']}
|
||||||
wl.delete_group('test', wl_temp, piker_dir)
|
wl_temp = wl.delete_group('test', wl_temp)
|
||||||
wl_temp = wl.ensure_watchlists(piker_dir)
|
|
||||||
assert not wl_temp.get('test')
|
assert not wl_temp.get('test')
|
||||||
|
|
||||||
|
|
||||||
def test_watchlist_is_merged(piker_dir):
|
def test_watchlist_is_merged():
|
||||||
"""Ensure that watchlist is merged.
|
"""Ensure that watchlist is merged.
|
||||||
"""
|
"""
|
||||||
wl_temp = {'test': ['TEST.CN']}
|
wl_temp = {'test': ['TEST.CN']}
|
||||||
wl_temp2 = '{"test2": ["TEST2.CN"]}'
|
wl_temp2 = '{"test2": ["TEST2.CN"]}'
|
||||||
wl.merge_watchlist(wl_temp2, wl_temp, piker_dir)
|
wl_temp3 = wl.merge_watchlist(wl_temp2, wl_temp)
|
||||||
wl_temp3 = wl.ensure_watchlists(piker_dir)
|
|
||||||
assert wl_temp3 == {'test': ['TEST.CN'], 'test2': ['TEST2.CN']}
|
assert wl_temp3 == {'test': ['TEST.CN'], 'test2': ['TEST2.CN']}
|
||||||
|
|
Loading…
Reference in New Issue