diff options
author | Robin Obůrka <r.oburka@gmail.com> | 2016-03-06 00:36:00 +0100 |
---|---|---|
committer | Robin Obůrka <r.oburka@gmail.com> | 2016-03-06 01:38:30 +0100 |
commit | cd123c2282344bcd5ce35ad56cb5b6774fdc92d4 (patch) | |
tree | 779c613aa5edaeb186004bbcd3fb25b36649a408 /todoist | |
parent | Add --indent parameter (diff) | |
download | todoist-cd123c2282344bcd5ce35ad56cb5b6774fdc92d4.tar.xz |
Provide --lines command with small refactoring of necessary parts
Diffstat (limited to 'todoist')
-rwxr-xr-x | todoist | 46 |
1 files changed, 31 insertions, 15 deletions
@@ -64,6 +64,9 @@ def arg_parser(): parser.add_argument("-i", "--indent", help="Set indentation of task (default is 1)", type=int) + parser.add_argument("-l", "--lines", + help="Use every line of stdin as one task", + action="store_true") parser.add_argument("msg", metavar="MSG", nargs='*', @@ -109,6 +112,17 @@ def find_candidate(name, storage): else: return candidates[0][1], None +def store_task(api, msg, kwargs_dict): + if not msg.strip(): + print("Warning: Empty message skipped", file=sys.stderr) + return + + status = api.add_item(msg, **kwargs_dict) + if "error" in status: + print("Store failed:", file=sys.stderr) + print(status["error"], file=sys.stderr) + sys.exit(1) + def main(): args = arg_parser() @@ -138,17 +152,11 @@ def main(): pprint(data, f) sys.exit(0) + ## Prepare data structures + kwargs = {} projects = build_project_list(data) collaborators = build_collaborators_list(data) - ## Prepare data - if not args.msg: - msg = sys.stdin.read().strip() - else: - msg = " ".join(args.msg) - - ## Prepare arguments - kwargs = {} ## Set date if not args.no_due_date: kwargs["date_string"] = args.date @@ -177,13 +185,21 @@ def main(): sys.exit(1) kwargs["responsible_uid"] = aid - ## Store task - status = api.add_item(msg, **kwargs) - if "error" in status: - print("Store failed:", file=sys.stderr) - print(status["error"], file=sys.stderr) - sys.exit(1) - + ## It is decided about storing tasks at this point + if args.msg: + msg = " ".join(args.msg) + store_task(api, msg, kwargs) + else: ## Read from stdin + if args.lines: + for line in sys.stdin: + if not line.strip(): + continue + store_task(api, line, kwargs) + else: + msg = sys.stdin.read().strip() + store_task(api, msg, kwargs) + + ## All operations done - commit and sync them api.commit() if __name__ == "__main__": |