Skip to content

Commit

Permalink
pass overrides as CLI arg
Browse files Browse the repository at this point in the history
  • Loading branch information
breezykermo committed Apr 6, 2021
1 parent d2d7846 commit f86d7e8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
2 changes: 1 addition & 1 deletion clients/python/clip.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def run():
clip = {
"time": tm,
"title": title,
"text": quote,
"html": quote,
"note": note,
"tags": tags,
"href": url,
Expand Down
33 changes: 21 additions & 12 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,21 @@
parser.add_argument('UPLOAD_DIR', type=str, help='Directory to save uploaded files')
parser.add_argument('-p', '--port', type=int, dest='PORT', default=8888, help='Port for server')
parser.add_argument('-k', '--key', type=str, dest='KEY', default=None, help='Secret key to authenticate clients')
parser.add_argument('-o', '--key-overrides', type=str, dest='OVERRIDES', default=None, help='Key overrides for clips sent to server in the format "original1:new1;original2:new2"')
args = parser.parse_args()

def index_with_fallback(obj, fst, snd):
return obj.get(fst) if obj.get(fst) is not None else obj.get(snd)
def idx(obj, base, overrides):
key = base
for override in overrides:
a,b = override.split(":")
if a == base:
key = b
return obj.get(key) if obj.get(key) is not None else obj.get(base)

overrides = []
if args.OVERRIDES:
overrides = args.OVERRIDES.split(";")
overrides = [x for x in overrides if len(x.split(":")) == 2]


class JSONRequestHandler(BaseHTTPRequestHandler):
Expand Down Expand Up @@ -124,26 +135,24 @@ def do_GET(self):

grouped = defaultdict(list)
for d in data:
grouped[d['href']].append(d)
grouped[idx(d, 'href', overrides)].append(d)

for href, group in sorted(grouped.items(), key=lambda g: -max([d['time'] for d in g[1]])):
for href, group in sorted(grouped.items(), key=lambda g: -max([idx(d, 'time', overrides) for d in g[1]])):
html.append('''
<article>
<h4><a href="{href}">{title}</a></h4>'''.format(href=href, title=group[0].get('title')))
for d in group:
if 'file' in d:
# fname = d['file']['name']
html.append('''
<div class="highlight">
<img src="{src}">
<p>{text}</p>
<div class="tags"><em>{tags}</em></div>
</div>
'''.format(
# src=os.path.join(args.UPLOAD_DIR, fname),
src=d['file']['src'],
text=index_with_fallback(d, 'text', 'note'),
tags=', '.join(d['tags'])
src=idx(d, 'file', overrides)['src'],
text=idx(d, 'text', overrides),
tags=', '.join(idx(d, 'tags', overrides))
))
else:
html.append('''
Expand All @@ -153,9 +162,9 @@ def do_GET(self):
<div class="tags"><em>{tags}</em></div>
</div>
'''.format(
html=index_with_fallback(d, 'html', 'quote'),
note=index_with_fallback(d, 'text', 'note'),
tags=', '.join(d['tags'])
html=idx(d, 'html', overrides),
note=idx(d, 'note', overrides),
tags=', '.join(idx(d, 'tags', overrides))
))
html.append('</article>')

Expand Down

0 comments on commit f86d7e8

Please sign in to comment.