Remove write to file from API and move to CLI

kivy_mainline_and_py3.8
K0nstantine 2018-04-03 17:53:44 -04:00
parent cd69c30143
commit 49b760673e
4 changed files with 36 additions and 45 deletions

View File

@ -121,7 +121,7 @@ def watch(loglevel, broker, rate, name):
log = get_console_log(loglevel) # activate console logging
brokermod = get_brokermod(broker)
watchlists = {
watchlists = json.dumps({
'cannabis': [
'EMH.VN', 'LEAF.TO', 'HVT.VN', 'HMMJ.TO', 'APH.TO',
'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'],
'pharma': ['ATE.VN'],
'indexes': ['SPY', 'DAX', 'QQQ', 'DIA'],
}
})
# broker_conf_path = os.path.join(
# click.get_app_dir('piker'), 'watchlists.json')
# from piker.testing import _quote_streamer as brokermod
@ -179,7 +179,7 @@ def show(ctx, name):
@click.pass_context
def load(ctx, data):
try:
wl.write_watchlists(data, ctx.obj['path'])
wl.write_sorted_json(json.loads(data), ctx.obj['path'])
except (json.JSONDecodeError, IndexError):
click.echo('You have passed an invalid text respresentation of a '
'JSON object. Try again.')
@ -190,8 +190,9 @@ def load(ctx, data):
@click.argument('ticker_name', nargs=1, required=True)
@click.pass_context
def add(ctx, name, ticker_name):
watchlist = ctx.obj['watchlist']
wl.add_ticker(name, ticker_name, watchlist, ctx.obj['path'])
watchlist = wl.add_ticker(name, ticker_name,
ctx.obj['watchlist'])
wl.write_sorted_json(watchlist, ctx.obj['path'])
@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.pass_context
def remove(ctx, name, ticker_name):
watchlist = ctx.obj['watchlist']
wl.remove_ticker(name, ticker_name, watchlist, ctx.obj['path'])
watchlist = wl.remove_ticker(name, ticker_name, ctx.obj['watchlist'])
wl.write_sorted_json(watchlist, ctx.obj['path'])
@watchlists.command(help='delete watchlist group')
@click.argument('name', nargs=1, required=True)
@click.pass_context
def delete(ctx, name):
watchlist = ctx.obj['watchlist']
wl.delete_group(name, watchlist, ctx.obj['path'])
watchlist = wl.delete_group(name, ctx.obj['watchlist'])
wl.write_sorted_json(watchlist, ctx.obj['path'])
@watchlists.command(help='merge a watchlist from another user')
@click.argument('watchlist_to_merge', nargs=1, required=True)
@click.pass_context
def merge(ctx, watchlist_to_merge):
watchlist = ctx.obj['watchlist']
wl.merge_watchlist(watchlist_to_merge, watchlist, ctx.obj['path'])
merged_watchlist = wl.merge_watchlist(watchlist_to_merge,
ctx.obj['watchlist'])
wl.write_sorted_json(merged_watchlist, ctx.obj['path'])
@watchlists.command(help='dump text respresentation of a watchlist to console')
@click.argument('name', nargs=1, required=False)
@click.pass_context
def dump(ctx, name):
watchlist = ctx.obj['watchlist']
click.echo(json.dumps(watchlist))
click.echo(json.dumps(ctx.obj['watchlist']))

View File

@ -26,33 +26,29 @@ def ensure_watchlists(file_path):
return json.load(f) if not os.stat(file_path).st_size == 0 else {}
def write_watchlists(watchlist, path):
write_sorted_json(json.loads(watchlist), path)
def add_ticker(name, ticker_name, watchlist, path):
def add_ticker(name, ticker_name, watchlist):
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:
watchlist[name].remove(str(ticker_name).upper())
if 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:
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)
watchlist_to_merge = json.loads(watchlist_to_merge)
for d in (watchlist, watchlist_to_merge):
for key, value in d.items():
merged_watchlist[key].extend(value)
write_sorted_json(merged_watchlist, path)
return merged_watchlist

View File

@ -100,8 +100,7 @@ def temp_dir():
testing.
"""
with tempfile.TemporaryDirectory() as tempdir:
config_dir = os.path.join(tempdir, 'piker')
yield config_dir
yield os.path.join(tempdir, 'piker')
@pytest.fixture

View File

@ -59,48 +59,43 @@ def test_watchlist_is_read_from_file(piker_dir):
"""
wl_temp = wl.ensure_watchlists(piker_dir)
assert wl_temp == {}
wl_temp2 = '{"AA": ["TEST.CN"]}'
wl.write_watchlists(wl_temp2, piker_dir)
assert json.loads(wl_temp2) == wl.ensure_watchlists(piker_dir)
wl_temp2 = {"AA": ["TEST.CN"]}
wl.write_sorted_json(wl_temp2, 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.
"""
wl.add_ticker('test', 'TEST.CN', {'test': ['TEST2.CN']}, piker_dir)
wl_temp = wl.ensure_watchlists(piker_dir)
wl_temp = wl.add_ticker('test', 'TEST.CN', {'test': ['TEST2.CN']})
assert len(wl_temp['test']) == 2
wl.add_ticker('test2', 'TEST.CN', wl_temp, piker_dir)
wl_temp = wl.ensure_watchlists(piker_dir)
wl_temp = wl.add_ticker('test2', 'TEST.CN', wl_temp)
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
if no tickers left.
"""
wl_temp = {'test': ['TEST.CN', 'TEST2.CN'], 'test2': ['TEST.CN']}
wl.remove_ticker('test', 'TEST.CN', wl_temp, piker_dir)
wl.remove_ticker('test2', 'TEST.CN', wl_temp, piker_dir)
wl_temp = wl.ensure_watchlists(piker_dir)
wl_temp = wl.remove_ticker('test', 'TEST.CN', wl_temp)
wl_temp = wl.remove_ticker('test2', 'TEST.CN', wl_temp)
assert wl_temp == {'test': ['TEST2.CN']}
assert not wl_temp.get('test2')
def test_group_is_deleted(piker_dir):
def test_group_is_deleted():
"""Check that watchlist group is removed.
"""
wl_temp = {'test': ['TEST.CN']}
wl.delete_group('test', wl_temp, piker_dir)
wl_temp = wl.ensure_watchlists(piker_dir)
wl_temp = wl.delete_group('test', wl_temp)
assert not wl_temp.get('test')
def test_watchlist_is_merged(piker_dir):
def test_watchlist_is_merged():
"""Ensure that watchlist is merged.
"""
wl_temp = {'test': ['TEST.CN']}
wl_temp2 = '{"test2": ["TEST2.CN"]}'
wl.merge_watchlist(wl_temp2, wl_temp, piker_dir)
wl_temp3 = wl.ensure_watchlists(piker_dir)
wl_temp3 = wl.merge_watchlist(wl_temp2, wl_temp)
assert wl_temp3 == {'test': ['TEST.CN'], 'test2': ['TEST2.CN']}