#!../flask/bin/python """ usage: bug open [options] If no arguments are given it will open your $EDITOR where the first line is the summary following a newline and then the body of the report. Both are required. -h, --help Print this help text -s, --summary STRING A short summary of the bug -b, --body STRING The long description of the bug """ import os, re, tempfile, subprocess from docopt import docopt import json, requests from bug_show import show_ticket if __name__ == '__main__': print(docopt(__doc__)) def call(args): print(args) api_endpoint = args['--uri'] + '/api/1.0/ticket' if args['--summary']: summary = args['--summary'] else: summary = '' if args['--body']: body = args['--body'] else: body = '' if not(summary and body): (summary, body) = editor_prompt(summary, body) ticket = { 'summary': summary, 'body': body, 'user_nickname': 'demi' } headers = { 'Content-Type': 'application/json', 'Accept': 'text/plain' } payload = json.dumps(ticket) r = requests.post(api_endpoint, data=payload, headers=headers) t = json.loads(r.text).get('ticket') print(t) print(show_ticket(t)) def editor_prompt(summary, body): editor = os.environ.get('EDITOR','vim') message='' if summary: message += summary if body: message += '\n\n' + body message += """ # Please enter the summary on a single line, followed # by an empty line, then followed by the body of the # ticket. # # Both the summary and body are required. If either of # them are missing, or they aren't separated properly # the submission will be aborted. """ tmp = tempfile.NamedTemporaryFile() tmp.write(message.encode("utf-8")) tmp.flush() regx = re.compile('^(.+?)\n\n(.+)$', re.S) subprocess.call([editor, tmp.name]) tmp.seek(0) data = tmp.read().decode("utf-8") tmp.close() data = data[:-263] # Strip the commented out message data = data.lstrip().rstrip() # Strip opening and ending whitespace regmatch = regx.match(data) if len(regmatch.groups()) != 2: exit("Error: summary and body not separated properly, aborting") summary = regmatch.group(1) body = regmatch.group(2) return (summary, body)