# HG changeset patch # User Paul Boddie # Date 1516141779 -3600 # Node ID 02d8d279f84a9b13a1304443be52467b754616b2 # Parent 77bcb48a11e8747d22634c083ab668f408f1e4bd Parameterised user interface commands and values, employing a function to help generate suitable help text strings. diff -r 77bcb48a11e8 -r 02d8d279f84a imip_text_client.py --- a/imip_text_client.py Sat Jan 13 17:26:42 2018 +0100 +++ b/imip_text_client.py Tue Jan 16 23:29:39 2018 +0100 @@ -38,6 +38,51 @@ import vRecurrence import sys, os +# User interface definitions. + +ATTENDANCE_COMMANDS = ("A", "attend", "attendance") +ATTENDEE_COMMANDS = ("a", "attendee") +CANCEL_COMMANDS = ("c", "cancel") +CHANGE_COMMANDS = ("C", "changes") +CLASSIFICATION_COMMANDS = ("c", "class", "classification") +FINISH_COMMANDS = ("f", "finish") +HELP_COMMANDS = ("h", "help", "?") +LIST_COMMANDS = ("l", "list", "show") +OPERATION_COMMANDS = ("o", "ops", "operations") +PERIOD_COMMANDS = ("p", "period") +QUIT_COMMANDS = ("q", "quit", "exit") +RECURRENCEID_COMMANDS = ("RECURRENCE-ID", "RID") +RESET_COMMANDS = ("r", "reload", "reset", "restart") +RULE_COMMANDS = ("rr", "rule", "rrule") +SUGGESTED_ATTENDEE_COMMANDS = ("as", "attendee-suggested", "suggested-attendee") +SUGGESTED_PERIOD_COMMANDS = ("ps", "period-suggested", "suggested-period") +SUMMARY_COMMANDS = ("s", "summary") +UID_COMMANDS = ("UID",) +UNCANCEL_COMMANDS = ("u", "uncancel", "restore") + +CANCEL_COMMANDS = ("R", "remove", "cancel") +PUBLISH_COMMANDS = ("P", "publish") +SEND_COMMANDS = ("S", "send") +UPDATE_COMMANDS = ("U", "update") + +EDIT_COMMANDS = ("e", "edit") +REMOVE_COMMANDS = ("r", "remove") + +ACCEPTED_VALUES = ("a", "accept", "accepted", "attend") +DECLINED_VALUES = ("d", "decline", "declined") +TENTATIVE_VALUES = ("t", "tentative") + +def commandlist(l, option=None): + + "Show 'l' as a command list string employing any given 'option'." + + if option: + l2 = [] + for s in l: + l2.append("%s %s" % (s, option)) + return "\n".join(l2) + return ", ".join(l) + # User interface functions. echo = False @@ -542,11 +587,22 @@ "Edit the attendee at 'index'." t = self.can_edit_attendee(index) - if t: - attendees = self.state.get("attendees") - attendee, attr = t - del attendees[attendee] - attendee = input_with_default("Attendee (%s)? ", attendee) + if not t: + return + + attendees = self.state.get("attendees") + old_attendee, attr = t + + attendee = input_with_default("Attendee (%s) (or return)? ", old_attendee) + + # Remove the old attendee if null or a replacement is given. + + if attendee.strip() or not old_attendee: + del attendees[old_attendee] + + # Add any replacement. + + if attendee.strip(): attendees[attendee] = attr def edit_period(self, index, args=None): @@ -591,7 +647,7 @@ # Edit an existing selector. - if cmd in ("e", "edit"): + if cmd in EDIT_COMMANDS: if isinstance(selector, vRecurrence.LimitSelector): add_rule_selector_count(selectors, args, selector) elif isinstance(selector, vRecurrence.Pattern): @@ -601,7 +657,7 @@ # Remove an existing selector. - elif cmd in ("r", "remove"): + elif cmd in REMOVE_COMMANDS: del selectors[index] # Exit if requested or after a successful @@ -1095,55 +1151,67 @@ print_title("Editing commands") print print """\ -a [ ] -attendee [ ] +%(ATTENDEE_NEW_COMMANDS)s Add attendee -A, attend, attendance - Change attendance/participation - -a -attendee +%(ATTENDEE_COMMANDS)s Select attendee from list -as +%(ATTENDANCE_COMMANDS)s + Change attendance/participation + +%(SUGGESTED_ATTENDEE_COMMANDS)s Add suggested attendee from list -f, finish +%(FINISH_COMMANDS)s Finish editing, confirming changes, proceeding to messaging -h, help, ? +%(HELP_COMMANDS)s Show this help message -l, list, show +%(LIST_COMMANDS)s List/show all event details -p, period [ new ] +%(PERIOD_NEW_COMMANDS)s Add new period -p -period +%(PERIOD_COMMANDS)s Select period from list -ps +%(SUGGESTED_PERIOD_COMMANDS)s Add or remove suggested period from list -q, quit, exit +%(QUIT_COMMANDS)s Exit/quit this program -r, reload, reset, restart +%(RESET_COMMANDS)s Reset event periods (return to editing mode, if already finished) -rule, rrule +%(RULE_NEW_COMMANDS)s Add a period recurrence rule -rule -rrule +%(RULE_COMMANDS)s Select period recurrence rule selector from list -s, summary +%(SUMMARY_COMMANDS)s Set event summary -""" +""" % { + "ATTENDEE_NEW_COMMANDS" : commandlist(ATTENDEE_COMMANDS, "[ ]"), + "ATTENDEE_COMMANDS" : commandlist(ATTENDEE_COMMANDS, ""), + "ATTENDANCE_COMMANDS" : commandlist(ATTENDANCE_COMMANDS), + "SUGGESTED_ATTENDEE_COMMANDS" : commandlist(SUGGESTED_ATTENDEE_COMMANDS, ""), + "FINISH_COMMANDS" : commandlist(FINISH_COMMANDS), + "HELP_COMMANDS" : commandlist(HELP_COMMANDS), + "LIST_COMMANDS" : commandlist(LIST_COMMANDS), + "PERIOD_NEW_COMMANDS" : commandlist(PERIOD_COMMANDS, "[ new ]"), + "PERIOD_COMMANDS" : commandlist(PERIOD_COMMANDS, ""), + "SUGGESTED_PERIOD_COMMANDS" : commandlist(SUGGESTED_PERIOD_COMMANDS, ""), + "QUIT_COMMANDS" : commandlist(QUIT_COMMANDS), + "RESET_COMMANDS" : commandlist(RESET_COMMANDS), + "RULE_NEW_COMMANDS" : commandlist(RULE_COMMANDS), + "RULE_COMMANDS" : commandlist(RULE_COMMANDS, ""), + "SUMMARY_COMMANDS" : commandlist(SUMMARY_COMMANDS), + } print_title("Messaging commands") print @@ -1218,44 +1286,44 @@ # Check the status of the periods. - if cmd in ("c", "class", "classification"): + if cmd in CLASSIFICATION_COMMANDS: cl.show_period_classification() print - elif cmd in ("C", "changes"): + elif cmd in CHANGE_COMMANDS: cl.show_changes() print # Finish editing. - elif cmd in ("f", "finish"): + elif cmd in FINISH_COMMANDS: cl.finish() # Help. - elif cmd in ("h", "?", "help"): + elif cmd in HELP_COMMANDS: show_commands() # Show object details. - elif cmd in ("l", "list", "show"): + elif cmd in LIST_COMMANDS: cl.show_object() print # Show the operations. - elif cmd in ("o", "ops", "operations"): + elif cmd in OPERATION_COMMANDS: cl.show_operations() print # Quit or exit. - elif cmd in ("q", "quit", "exit"): + elif cmd in QUIT_COMMANDS: break # Restart editing. - elif cmd in ("r", "reload", "reset", "restart"): + elif cmd in RESET_COMMANDS: obj = cl.load_object(obj.get_uid(), obj.get_recurrenceid()) if not obj: obj = cl.new_object() @@ -1265,11 +1333,11 @@ # Show UID details. - elif cmd == "UID": + elif cmd in UID_COMMANDS: filename = get_text_arg(s) write(obj.get_uid(), filename) - elif cmd == "RECURRENCE-ID": + elif cmd in RECURRENCEID_COMMANDS: filename = get_text_arg(s) write(obj.get_recurrenceid() or "", filename) @@ -1279,21 +1347,21 @@ # Show messages. - if cmd in ("P", "publish"): + if cmd in PUBLISH_COMMANDS: filename = get_text_arg(s) cl.show_publish_message(plain=not filename, filename=filename) - elif cmd in ("R", "remove", "cancel"): + elif cmd in CANCEL_COMMANDS: filename = get_text_arg(s) cl.show_cancel_message(plain=not filename, filename=filename) - elif cmd in ("U", "update"): + elif cmd in UPDATE_COMMANDS: filename = get_text_arg(s) cl.show_update_message(plain=not filename, filename=filename) # Definitive finishing action. - elif cmd in ("S", "send"): + elif cmd in SEND_COMMANDS: # Send update and cancellation messages. @@ -1337,7 +1405,7 @@ # Add or edit attendee. - if cmd in ("a", "attendee"): + if cmd in ATTENDEE_COMMANDS: value = next_arg(args) index = to_int_or_none(value) @@ -1367,9 +1435,9 @@ cmd = next_arg(args) if not cmd: cmd = read_input("Attendee: (e)dit, (r)emove (or return)> ") - if cmd in ("e", "edit"): + if cmd in EDIT_COMMANDS: cl.edit_attendee(index) - elif cmd in ("r", "remove"): + elif cmd in REMOVE_COMMANDS: cl.remove_attendees([index]) # Exit if requested or after a successful @@ -1386,7 +1454,7 @@ # Add suggested attendee (using index). - elif cmd in ("as", "attendee-suggested", "suggested-attendee"): + elif cmd in SUGGESTED_ATTENDEE_COMMANDS: value = next_arg(args) index = to_int_or_none(value) @@ -1398,7 +1466,7 @@ # Edit attendance. - elif cmd in ("A", "attend", "attendance"): + elif cmd in ATTENDANCE_COMMANDS: if not cl.is_attendee() and cl.is_organiser(): cl.add_attendee(cl.user) @@ -1413,11 +1481,11 @@ cmd = next_arg(args) if not cmd: cmd = read_input("Attendance: (a)ccept, (d)ecline, (t)entative (or return)> ") - if cmd in ("a", "accept", "accepted", "attend"): + if cmd in ACCEPTED_VALUES: cl.edit_attendance("ACCEPTED") - elif cmd in ("d", "decline", "declined"): + elif cmd in DECLINED_VALUES: cl.edit_attendance("DECLINED") - elif cmd in ("t", "tentative"): + elif cmd in TENTATIVE_VALUES: cl.edit_attendance("TENTATIVE") # Exit if requested or after a successful operation. @@ -1433,7 +1501,7 @@ # Add or edit period. - elif cmd in ("p", "period"): + elif cmd in PERIOD_COMMANDS: value = next_arg(args) index = to_int_or_none(value) @@ -1455,12 +1523,13 @@ cmd = next_arg(args) if not cmd: - cmd = read_input("Period: (e)dit, (c)ancel, (u)ncancel (or return)> ") - if cmd in ("e", "edit"): + cmd = read_input("Period: (c)ancel, (e)dit, (u)ncancel (or return)> ") + + if cmd in CANCEL_COMMANDS: + cl.cancel_periods([index]) + elif cmd in EDIT_COMMANDS: cl.edit_period(index, args) - elif cmd in ("c", "cancel"): - cl.cancel_periods([index]) - elif cmd in ("u", "uncancel", "restore"): + elif cmd in UNCANCEL_COMMANDS: cl.cancel_periods([index], False) # Exit if requested or after a successful @@ -1477,7 +1546,7 @@ # Apply suggested period (using index). - elif cmd in ("ps", "period-suggested", "suggested-period"): + elif cmd in SUGGESTED_PERIOD_COMMANDS: value = next_arg(args) index = to_int_or_none(value) @@ -1489,7 +1558,7 @@ # Specify a recurrence rule. - elif cmd in ("rule", "rrule"): + elif cmd in RULE_COMMANDS: value = next_arg(args) index = to_int_or_none(value) @@ -1506,7 +1575,7 @@ # Set the summary. - elif cmd in ("s", "summary"): + elif cmd in SUMMARY_COMMANDS: cl.edit_summary(get_text_arg(s)) cl.show_object() print