X-Git-Url: http://git.tremily.us/?a=blobdiff_plain;f=nmhive.py;h=b05aa89d7f100f2779cd724f53aa0d419dbef177;hb=919846ad3e6994a8a954128b170a6bbaa1884509;hp=6f9f346777dad836c0c878793fce49f3b53d449f;hpb=fe9c70c04f77ee9f62a11507740e2eb65252bb43;p=nmhive.git diff --git a/nmhive.py b/nmhive.py index 6f9f346..b05aa89 100755 --- a/nmhive.py +++ b/nmhive.py @@ -1,5 +1,7 @@ #!/usr/bin/env python +"""Serve a JSON API for getting/setting notmuch tags with nmbug commits.""" + import json import mailbox import os @@ -8,6 +10,7 @@ import urllib.request import flask import flask_cors +import nmbug import notmuch @@ -43,6 +46,9 @@ def _message_tags(message): @app.route('/mid/', methods=['GET', 'POST']) def message_id_tags(message_id): if flask.request.method == 'POST': + changes = flask.request.get_json() + if not changes: + return flask.Response(status=400) database = notmuch.Database( path=NOTMUCH_PATH, mode=notmuch.Database.MODE.READ_WRITE) @@ -52,7 +58,7 @@ def message_id_tags(message_id): return flask.Response(status=404) database.begin_atomic() message.freeze() - for change in flask.request.get_json(): + for change in changes: if change.startswith('+'): message.add_tag(TAG_PREFIX + change[1:]) elif change.startswith('-'): @@ -64,6 +70,8 @@ def message_id_tags(message_id): tags = _message_tags(message=message) finally: database.close() + nmbug.commit(message='nmhive: {} {}'.format( + message_id, ' '.join(changes))) elif flask.request.method == 'GET': database = notmuch.Database(path=NOTMUCH_PATH) try: @@ -95,4 +103,20 @@ def gmane_message_id(group, article): if __name__ == '__main__': - app.run(host='0.0.0.0') + import argparse + + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument( + '-H', '--host', default='127.0.0.1', + help='The hostname to listen on.') + parser.add_argument( + '-p', '--port', type=int, default=5000, + help='The port to listen on.') + parser.add_argument( + '-d', '--debug', type=bool, default=False, + help='Run Flask in debug mode (e.g. show errors).') + + args = parser.parse_args() + + app.debug = args.debug + app.run(host=args.host, port=args.port)