|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import argparse |
| 4 | +import doorstop |
| 5 | +import os |
| 6 | +import subprocess |
| 7 | +import sys |
| 8 | +import tempfile |
| 9 | + |
| 10 | +""" A script to review Doorstop items and add extra fields: confidence |
| 11 | +level and optional comments. """ |
| 12 | + |
| 13 | +parser = argparse.ArgumentParser( |
| 14 | + prog='review', |
| 15 | + description='Reviews Doorstop items and adds confidence and comment fields') |
| 16 | + |
| 17 | +parser.add_argument('itemname', help="Name of item to review e.g. TS1") |
| 18 | +parser.add_argument('-c', '--confidence', help="Specify the level of confidence (0.0-1.0)", type=float) |
| 19 | +parser.add_argument('-m', '--comment', help="Specify comment text (will overwrite)") |
| 20 | + |
| 21 | +args = parser.parse_args() |
| 22 | + |
| 23 | +tree = doorstop.build() |
| 24 | + |
| 25 | +def is_valid_confidence(n): |
| 26 | + return n>=0.0 and n<=1.0 |
| 27 | + |
| 28 | + |
| 29 | +try: |
| 30 | + item = tree.find_item(args.itemname) |
| 31 | +except doorstop.common.DoorstopError as e: |
| 32 | + print(e) |
| 33 | + sys.exit(1) |
| 34 | + |
| 35 | +print(f"Found {item}") |
| 36 | + |
| 37 | +existing_comment = item.get("review-comments") |
| 38 | + |
| 39 | +if args.comment: |
| 40 | + # replace comment with it |
| 41 | + item.set("review-comments", args.comment) |
| 42 | + print(f"Setting comment on {args.itemname} to CLI-supplied value") |
| 43 | +else: |
| 44 | + editor = os.getenv("EDITOR") |
| 45 | + if editor is None: |
| 46 | + print("Warning: The EDITOR environment variable is not set. " + |
| 47 | + "If you want to add a comment, set EDITOR to a text editor and re-run " + |
| 48 | + "this command, or use the -m option") |
| 49 | + else: |
| 50 | + fd, path = tempfile.mkstemp() |
| 51 | + try: |
| 52 | + if existing_comment: |
| 53 | + # Edit or replace |
| 54 | + with os.fdopen(fd, 'w') as tmpfile: |
| 55 | + tmpfile.write(existing_comment.strip()) |
| 56 | + tmpfile.write("\n") |
| 57 | + else: |
| 58 | + with os.fdopen(fd, 'w') as tmpfile: |
| 59 | + tmpfile.write(f"Add your comment on {item} here.") |
| 60 | + tmpfile.write("\n") |
| 61 | + |
| 62 | + # Spawn editor |
| 63 | + subprocess.run([editor, path]) |
| 64 | + |
| 65 | + with open(path, 'r') as tmpfile: |
| 66 | + new_comment = tmpfile.read() |
| 67 | + item.set("review-comments", new_comment) |
| 68 | + finally: |
| 69 | + os.remove(path) |
| 70 | + |
| 71 | +# now prompt for a confidence level |
| 72 | +existing_confidence = item.get("confidence") |
| 73 | + |
| 74 | +if existing_confidence is None: |
| 75 | + existing_confidence = 0 |
| 76 | + |
| 77 | +if args.confidence: |
| 78 | + if is_valid_confidence(args.confidence): |
| 79 | + new_confidence_int = args.confidence |
| 80 | + else: |
| 81 | + print(f"Error: Supplied confidence level {args.confidence} is not in the range 0.0 to 1.0.") |
| 82 | + sys.exit(3) |
| 83 | +else: |
| 84 | + while True: |
| 85 | + print(f"\nPlease enter your confidence level (in %) that {item}'s sub-requirements are sufficient to satisfy item {item}.") |
| 86 | + |
| 87 | + new_confidence_text = input(f"Please enter a number between 0 and 1 inclusive. (Default: {existing_confidence}):") |
| 88 | + |
| 89 | + if new_confidence_text == "": |
| 90 | + new_confidence_int = existing_confidence |
| 91 | + break |
| 92 | + try: |
| 93 | + new_confidence_int = float(new_confidence_text) |
| 94 | + except Exception as e: |
| 95 | + print(f"Can't interpret that value as a number.") |
| 96 | + continue |
| 97 | + |
| 98 | + if is_valid_confidence(new_confidence_int): |
| 99 | + break |
| 100 | + else: |
| 101 | + print(f"Please enter a number between 0 and 1.") |
| 102 | + |
| 103 | +print(f"Setting new confidence value of {new_confidence_int} to {item}") |
| 104 | +item.set("confidence", new_confidence_int) |
| 105 | + |
| 106 | +print(f"Setting {item} as reviewed.") |
| 107 | +item.review() |
0 commit comments