paul@1375 | 1 | #!/usr/bin/env python |
paul@1375 | 2 | |
paul@1375 | 3 | from email import message_from_file |
paul@1375 | 4 | from imiptools import parse_args |
paul@1375 | 5 | from imiptools.client import ClientForObject |
paul@1375 | 6 | from imiptools.config import settings |
paul@1375 | 7 | from imiptools.content import get_objects_from_itip, have_itip_part, parse_itip_part |
paul@1375 | 8 | from imiptools.data import get_address, get_main_period, get_recurrence_periods, get_value |
paul@1375 | 9 | from imiptools.dates import get_datetime_item, get_time, to_timezone |
paul@1375 | 10 | from imiptools.editing import EditingClient, PeriodError |
paul@1375 | 11 | from imiptools.mail import Messenger |
paul@1375 | 12 | from imiptools.stores import get_journal, get_store |
paul@1375 | 13 | from imiptools.utils import decode_part, message_as_string |
paul@1375 | 14 | import sys |
paul@1375 | 15 | |
paul@1375 | 16 | # User interface functions. |
paul@1375 | 17 | |
paul@1375 | 18 | echo = False |
paul@1375 | 19 | |
paul@1375 | 20 | def read_input(label): |
paul@1375 | 21 | s = raw_input(label).strip() |
paul@1375 | 22 | if echo: |
paul@1375 | 23 | print s |
paul@1375 | 24 | return s |
paul@1375 | 25 | |
paul@1375 | 26 | def input_with_default(label, default): |
paul@1375 | 27 | return read_input(label % default) or default |
paul@1375 | 28 | |
paul@1375 | 29 | def print_title(text): |
paul@1375 | 30 | print text |
paul@1375 | 31 | print len(text) * "-" |
paul@1375 | 32 | |
paul@1375 | 33 | def write(s, filename): |
paul@1375 | 34 | f = filename and open(filename, "w") or None |
paul@1375 | 35 | try: |
paul@1375 | 36 | print >>(f or sys.stdout), s |
paul@1375 | 37 | finally: |
paul@1375 | 38 | if f: |
paul@1375 | 39 | f.close() |
paul@1375 | 40 | |
paul@1375 | 41 | # Interpret an input file containing a calendar resource. |
paul@1375 | 42 | |
paul@1375 | 43 | def get_objects(filename): |
paul@1375 | 44 | |
paul@1375 | 45 | "Return objects provided by 'filename'." |
paul@1375 | 46 | |
paul@1375 | 47 | f = open(filename) |
paul@1375 | 48 | try: |
paul@1375 | 49 | msg = message_from_file(f) |
paul@1375 | 50 | finally: |
paul@1375 | 51 | f.close() |
paul@1375 | 52 | |
paul@1375 | 53 | all_objects = [] |
paul@1375 | 54 | |
paul@1375 | 55 | for part in msg.walk(): |
paul@1375 | 56 | itip = parse_itip_part(part) |
paul@1375 | 57 | method = itip and get_value(itip, "METHOD") |
paul@1375 | 58 | |
paul@1375 | 59 | # Ignore cancelled objects since only active objects are of interest. |
paul@1375 | 60 | |
paul@1375 | 61 | if method != "CANCEL": |
paul@1375 | 62 | all_objects += get_objects_from_itip(itip, ["VEVENT"]) |
paul@1375 | 63 | |
paul@1375 | 64 | return all_objects |
paul@1375 | 65 | |
paul@1375 | 66 | def show_objects(objects, user, store): |
paul@1375 | 67 | |
paul@1375 | 68 | """ |
paul@1375 | 69 | Show details of 'objects', accessed by the given 'user' in the given |
paul@1375 | 70 | 'store'. |
paul@1375 | 71 | """ |
paul@1375 | 72 | |
paul@1375 | 73 | for index, obj in enumerate(objects): |
paul@1375 | 74 | recurrenceid = obj.get_recurrenceid() |
paul@1375 | 75 | recurrence_label = recurrenceid and " %s" % recurrenceid or "" |
paul@1375 | 76 | print "(%d) Summary: %s (%s%s)" % (index, obj.get_value("SUMMARY"), obj.get_uid(), recurrence_label) |
paul@1375 | 77 | |
paul@1375 | 78 | def show_attendee(attendee_item, index): |
paul@1375 | 79 | |
paul@1375 | 80 | "Show the 'attendee_item' (value and attributes) at 'index'." |
paul@1375 | 81 | |
paul@1375 | 82 | attendee, attr = attendee_item |
paul@1375 | 83 | partstat = attr.get("PARTSTAT") |
paul@1375 | 84 | print "(%d) %s%s" % (index, attendee, partstat and " (%s)" % partstat or "") |
paul@1375 | 85 | |
paul@1375 | 86 | def show_attendees_raw(attendee_map): |
paul@1375 | 87 | |
paul@1375 | 88 | "Show the 'attendee_map' in a simple raw form." |
paul@1375 | 89 | |
paul@1375 | 90 | for attendee, attr in attendee_map.items(): |
paul@1375 | 91 | print attendee |
paul@1375 | 92 | |
paul@1375 | 93 | def show_periods(periods, errors=None): |
paul@1375 | 94 | |
paul@1375 | 95 | "Show 'periods' with any indicated 'errors'." |
paul@1375 | 96 | |
paul@1375 | 97 | main = get_main_period(periods) |
paul@1375 | 98 | if main: |
paul@1375 | 99 | show_period(main, 0, errors) |
paul@1375 | 100 | |
paul@1375 | 101 | recurrences = get_recurrence_periods(periods) |
paul@1375 | 102 | if recurrences: |
paul@1375 | 103 | print |
paul@1375 | 104 | print_title("Recurrences") |
paul@1375 | 105 | for index, p in enumerate(recurrences): |
paul@1375 | 106 | show_period(p, index + 1, errors) |
paul@1375 | 107 | |
paul@1375 | 108 | def show_period(p, index, errors=None): |
paul@1375 | 109 | |
paul@1375 | 110 | "Show period 'p' at 'index' with any indicated 'errors'." |
paul@1375 | 111 | |
paul@1375 | 112 | errors = errors and errors.get(index) |
paul@1375 | 113 | if p.replacement: |
paul@1375 | 114 | if p.cancelled: |
paul@1375 | 115 | label = "Cancelled" |
paul@1375 | 116 | else: |
paul@1375 | 117 | label = "Replaced" |
paul@1375 | 118 | else: |
paul@1375 | 119 | if p.new_replacement: |
paul@1375 | 120 | label = "To replace" |
paul@1375 | 121 | elif p.recurrenceid: |
paul@1375 | 122 | label = "Retained" |
paul@1375 | 123 | else: |
paul@1375 | 124 | label = "New" |
paul@1375 | 125 | |
paul@1375 | 126 | error_label = errors and " (errors: %s)" % ", ".join(errors) or "" |
paul@1375 | 127 | print "(%d) %s%s:" % (index, label, error_label), p.get_start(), p.get_end(), p.origin |
paul@1375 | 128 | |
paul@1375 | 129 | def show_periods_raw(periods): |
paul@1375 | 130 | |
paul@1375 | 131 | "Show 'periods' in a simple raw form." |
paul@1375 | 132 | |
paul@1375 | 133 | periods = periods[:] |
paul@1375 | 134 | periods.sort() |
paul@1375 | 135 | map(show_period_raw, periods) |
paul@1375 | 136 | |
paul@1375 | 137 | def show_period_raw(p): |
paul@1375 | 138 | |
paul@1375 | 139 | "Show period 'p' in a simple raw form." |
paul@1375 | 140 | |
paul@1375 | 141 | print p.get_start(), p.get_end(), p.origin |
paul@1375 | 142 | |
paul@1375 | 143 | def show_attendee_changes(new, modified, unmodified, removed): |
paul@1375 | 144 | |
paul@1375 | 145 | "Show 'new', 'modified', 'unmodified' and 'removed' periods." |
paul@1375 | 146 | |
paul@1375 | 147 | print |
paul@1375 | 148 | print_title("Changes to attendees") |
paul@1375 | 149 | print |
paul@1375 | 150 | print "New:" |
paul@1375 | 151 | show_attendees_raw(new) |
paul@1375 | 152 | print |
paul@1375 | 153 | print "Modified:" |
paul@1375 | 154 | show_attendees_raw(modified) |
paul@1375 | 155 | print |
paul@1375 | 156 | print "Unmodified:" |
paul@1375 | 157 | show_attendees_raw(unmodified) |
paul@1375 | 158 | print |
paul@1375 | 159 | print "Removed:" |
paul@1375 | 160 | show_attendees_raw(removed) |
paul@1375 | 161 | |
paul@1375 | 162 | def show_period_classification(new, replaced, retained, cancelled, obsolete): |
paul@1375 | 163 | |
paul@1375 | 164 | "Show 'new', 'replaced', 'retained', 'cancelled' and 'obsolete' periods." |
paul@1375 | 165 | |
paul@1375 | 166 | print |
paul@1375 | 167 | print_title("Period classification") |
paul@1375 | 168 | print |
paul@1375 | 169 | print "New:" |
paul@1375 | 170 | show_periods_raw(new) |
paul@1375 | 171 | print |
paul@1375 | 172 | print "Replaced:" |
paul@1375 | 173 | show_periods_raw(replaced) |
paul@1375 | 174 | print |
paul@1375 | 175 | print "Retained:" |
paul@1375 | 176 | show_periods_raw(retained) |
paul@1375 | 177 | print |
paul@1375 | 178 | print "Cancelled:" |
paul@1375 | 179 | show_periods_raw(cancelled) |
paul@1375 | 180 | print |
paul@1375 | 181 | print "Obsolete:" |
paul@1375 | 182 | show_periods_raw(obsolete) |
paul@1375 | 183 | |
paul@1375 | 184 | def show_changes(modified, unmodified, removed): |
paul@1375 | 185 | |
paul@1375 | 186 | "Show 'modified', 'unmodified' and 'removed' periods." |
paul@1375 | 187 | |
paul@1375 | 188 | print |
paul@1375 | 189 | print_title("Changes to periods") |
paul@1375 | 190 | print |
paul@1375 | 191 | print "Modified:" |
paul@1375 | 192 | show_periods_raw(modified) |
paul@1375 | 193 | print |
paul@1375 | 194 | print "Unmodified:" |
paul@1375 | 195 | show_periods_raw(unmodified) |
paul@1375 | 196 | print |
paul@1375 | 197 | print "Removed:" |
paul@1375 | 198 | show_periods_raw(removed) |
paul@1375 | 199 | |
paul@1375 | 200 | def show_attendee_operations(to_invite, to_cancel, to_modify): |
paul@1375 | 201 | |
paul@1375 | 202 | "Show attendees 'to_invite', 'to_cancel' and 'to_modify'." |
paul@1375 | 203 | |
paul@1375 | 204 | print |
paul@1375 | 205 | print_title("Attendee update operations") |
paul@1375 | 206 | print |
paul@1375 | 207 | print "To invite:" |
paul@1375 | 208 | show_attendees_raw(to_invite) |
paul@1375 | 209 | print |
paul@1375 | 210 | print "To cancel:" |
paul@1375 | 211 | show_attendees_raw(to_cancel) |
paul@1375 | 212 | print |
paul@1375 | 213 | print "To modify:" |
paul@1375 | 214 | show_attendees_raw(to_modify) |
paul@1375 | 215 | |
paul@1375 | 216 | def show_period_operations(to_unschedule, to_reschedule, to_add, to_exclude, to_set, |
paul@1375 | 217 | all_unscheduled, all_rescheduled): |
paul@1375 | 218 | |
paul@1375 | 219 | """ |
paul@1375 | 220 | Show operations for periods 'to_unschedule', 'to_reschedule', 'to_add', |
paul@1375 | 221 | 'to_exclude' and 'to_set' (for updating other calendar participants), and |
paul@1375 | 222 | for periods 'all_unscheduled' and 'all_rescheduled' (for publishing event |
paul@1375 | 223 | state). |
paul@1375 | 224 | """ |
paul@1375 | 225 | |
paul@1375 | 226 | print |
paul@1375 | 227 | print_title("Period update and publishing operations") |
paul@1375 | 228 | print |
paul@1375 | 229 | print "Unschedule:" |
paul@1375 | 230 | show_periods_raw(to_unschedule) |
paul@1375 | 231 | print |
paul@1375 | 232 | print "Reschedule:" |
paul@1375 | 233 | show_periods_raw(to_reschedule) |
paul@1375 | 234 | print |
paul@1375 | 235 | print "Added:" |
paul@1375 | 236 | show_periods_raw(to_add) |
paul@1375 | 237 | print |
paul@1375 | 238 | print "Excluded:" |
paul@1375 | 239 | show_periods_raw(to_exclude) |
paul@1375 | 240 | print |
paul@1375 | 241 | print "Set in object:" |
paul@1375 | 242 | show_periods_raw(to_set) |
paul@1375 | 243 | print |
paul@1375 | 244 | print "All unscheduled:" |
paul@1375 | 245 | show_periods_raw(all_unscheduled) |
paul@1375 | 246 | print |
paul@1375 | 247 | print "All rescheduled:" |
paul@1375 | 248 | show_periods_raw(all_rescheduled) |
paul@1375 | 249 | |
paul@1375 | 250 | class TextClient(EditingClient): |
paul@1375 | 251 | |
paul@1375 | 252 | "Simple client with textual output." |
paul@1375 | 253 | |
paul@1375 | 254 | def new_object(self): |
paul@1375 | 255 | |
paul@1375 | 256 | "Create a new object with the current time." |
paul@1375 | 257 | |
paul@1375 | 258 | utcnow = get_time() |
paul@1375 | 259 | now = to_timezone(utcnow, self.get_tzid()) |
paul@1375 | 260 | obj = EditingClient.new_object(self, "VEVENT") |
paul@1375 | 261 | obj.set_value("SUMMARY", "New event") |
paul@1375 | 262 | obj["DTSTART"] = [get_datetime_item(now)] |
paul@1375 | 263 | obj["DTEND"] = [get_datetime_item(now)] |
paul@1375 | 264 | return obj |
paul@1375 | 265 | |
paul@1375 | 266 | # Editing methods involving interaction. |
paul@1375 | 267 | |
paul@1375 | 268 | def edit_attendee(self, index): |
paul@1375 | 269 | |
paul@1375 | 270 | "Edit the attendee at 'index'." |
paul@1375 | 271 | |
paul@1375 | 272 | t = self.can_edit_attendee(index) |
paul@1375 | 273 | if t: |
paul@1375 | 274 | attendees = self.state.get("attendees") |
paul@1375 | 275 | attendee, attr = t |
paul@1375 | 276 | del attendees[attendee] |
paul@1375 | 277 | attendee = input_with_default("Attendee (%s)? ", attendee) |
paul@1375 | 278 | attendees[attendee] = attr |
paul@1375 | 279 | |
paul@1375 | 280 | def edit_period(self, index, args=None): |
paul@1375 | 281 | period = self.can_edit_period(index) |
paul@1375 | 282 | if period: |
paul@1375 | 283 | edit_period(period, args) |
paul@1375 | 284 | period.cancelled = False |
paul@1375 | 285 | |
paul@1375 | 286 | # Sort the periods after this change. |
paul@1375 | 287 | |
paul@1375 | 288 | periods = self.state.get("periods") |
paul@1375 | 289 | periods.sort() |
paul@1375 | 290 | |
paul@1375 | 291 | def edit_summary(self, summary=None): |
paul@1375 | 292 | if self.can_edit_properties(): |
paul@1375 | 293 | if not summary: |
paul@1375 | 294 | summary = input_with_default("Summary (%s)? ", self.state.get("summary")) |
paul@1375 | 295 | self.state.set("summary", summary) |
paul@1375 | 296 | |
paul@1375 | 297 | def finish(self): |
paul@1375 | 298 | try: |
paul@1375 | 299 | EditingClient.finish(self) |
paul@1375 | 300 | except PeriodError: |
paul@1375 | 301 | print "Errors exist in the periods." |
paul@1375 | 302 | return |
paul@1375 | 303 | |
paul@1375 | 304 | # Diagnostic methods. |
paul@1375 | 305 | |
paul@1375 | 306 | def show_period_classification(self): |
paul@1375 | 307 | try: |
paul@1375 | 308 | new, replaced, retained, cancelled, obsolete = self.classify_periods() |
paul@1375 | 309 | show_period_classification(new, replaced, retained, cancelled, obsolete) |
paul@1375 | 310 | except PeriodError: |
paul@1375 | 311 | print |
paul@1375 | 312 | print "Errors exist in the periods." |
paul@1375 | 313 | |
paul@1375 | 314 | def show_changes(self): |
paul@1375 | 315 | try: |
paul@1375 | 316 | modified, unmodified, removed = self.classify_period_changes() |
paul@1375 | 317 | show_changes(modified, unmodified, removed) |
paul@1375 | 318 | except PeriodError: |
paul@1375 | 319 | print "Errors exist in the periods." |
paul@1375 | 320 | |
paul@1375 | 321 | is_changed = self.properties_changed() |
paul@1375 | 322 | if is_changed: |
paul@1375 | 323 | print |
paul@1375 | 324 | print "Properties changed:", ", ".join(is_changed) |
paul@1375 | 325 | new, modified, unmodified, removed = self.classify_attendee_changes() |
paul@1375 | 326 | show_attendee_changes(new, modified, unmodified, removed) |
paul@1375 | 327 | |
paul@1375 | 328 | def show_operations(self): |
paul@1375 | 329 | is_changed = self.properties_changed() |
paul@1375 | 330 | |
paul@1375 | 331 | try: |
paul@1375 | 332 | to_unschedule, to_reschedule, to_add, to_exclude, to_set, \ |
paul@1375 | 333 | all_unscheduled, all_rescheduled = self.classify_period_operations() |
paul@1375 | 334 | show_period_operations(to_unschedule, to_reschedule, to_add, |
paul@1375 | 335 | to_exclude, to_set, |
paul@1375 | 336 | all_unscheduled, all_rescheduled) |
paul@1375 | 337 | except PeriodError: |
paul@1375 | 338 | print "Errors exist in the periods." |
paul@1375 | 339 | |
paul@1375 | 340 | to_invite, to_cancel, to_modify = self.classify_attendee_operations() |
paul@1375 | 341 | show_attendee_operations(to_invite, to_cancel, to_modify) |
paul@1375 | 342 | |
paul@1375 | 343 | # Output methods. |
paul@1375 | 344 | |
paul@1375 | 345 | def show_message(self, message, plain=False, filename=None): |
paul@1375 | 346 | if plain: |
paul@1375 | 347 | decode_part(message) |
paul@1375 | 348 | write(message_as_string(message), filename) |
paul@1375 | 349 | |
paul@1375 | 350 | def show_cancel_message(self, plain=False, filename=None): |
paul@1375 | 351 | |
paul@1375 | 352 | "Show the cancel message for uninvited attendees." |
paul@1375 | 353 | |
paul@1375 | 354 | message = self.prepare_cancel_message() |
paul@1375 | 355 | if message: |
paul@1375 | 356 | self.show_message(message, plain, filename) |
paul@1375 | 357 | |
paul@1375 | 358 | def show_publish_message(self, plain=False, filename=None): |
paul@1375 | 359 | |
paul@1375 | 360 | "Show the publishing message for the updated event." |
paul@1375 | 361 | |
paul@1375 | 362 | message = self.prepare_publish_message() |
paul@1375 | 363 | self.show_message(message, plain, filename) |
paul@1375 | 364 | |
paul@1375 | 365 | def show_update_message(self, plain=False, filename=None): |
paul@1375 | 366 | |
paul@1375 | 367 | "Show the update message for the updated event." |
paul@1375 | 368 | |
paul@1375 | 369 | message = self.prepare_update_message() |
paul@1375 | 370 | if message: |
paul@1375 | 371 | self.show_message(message, plain, filename) |
paul@1375 | 372 | |
paul@1375 | 373 | # General display methods. |
paul@1375 | 374 | |
paul@1375 | 375 | def show_object(self): |
paul@1375 | 376 | print |
paul@1375 | 377 | print "Summary:", self.state.get("summary") |
paul@1375 | 378 | print |
paul@1375 | 379 | print "Organiser:", self.state.get("organiser") |
paul@1375 | 380 | self.show_attendees() |
paul@1375 | 381 | self.show_periods() |
paul@1375 | 382 | self.show_suggested_attendees() |
paul@1375 | 383 | self.show_suggested_periods() |
paul@1375 | 384 | self.show_conflicting_periods() |
paul@1375 | 385 | |
paul@1375 | 386 | def show_attendees(self): |
paul@1375 | 387 | print |
paul@1375 | 388 | print_title("Attendees") |
paul@1375 | 389 | attendees = self.state.get("attendees") |
paul@1375 | 390 | for index, attendee_item in enumerate(attendees.items()): |
paul@1375 | 391 | show_attendee(attendee_item, index) |
paul@1375 | 392 | |
paul@1375 | 393 | def show_periods(self): |
paul@1375 | 394 | print |
paul@1375 | 395 | print_title("Periods") |
paul@1375 | 396 | show_periods(self.state.get("periods"), self.state.get("period_errors")) |
paul@1375 | 397 | |
paul@1375 | 398 | def show_suggested_attendees(self): |
paul@1375 | 399 | current_attendee = None |
paul@1375 | 400 | for index, (attendee, suggested_item) in enumerate(self.state.get("suggested_attendees")): |
paul@1375 | 401 | if attendee != current_attendee: |
paul@1375 | 402 | print |
paul@1375 | 403 | print_title("Attendees suggested by %s" % attendee) |
paul@1375 | 404 | current_attendee = attendee |
paul@1375 | 405 | show_attendee(suggested_item, index) |
paul@1375 | 406 | |
paul@1375 | 407 | def show_suggested_periods(self): |
paul@1375 | 408 | periods = self.state.get("suggested_periods") |
paul@1375 | 409 | current_attendee = None |
paul@1375 | 410 | index = 0 |
paul@1375 | 411 | for attendee, period, operation in periods: |
paul@1375 | 412 | if attendee != current_attendee: |
paul@1375 | 413 | print |
paul@1375 | 414 | print_title("Periods suggested by %s" % attendee) |
paul@1375 | 415 | current_attendee = attendee |
paul@1375 | 416 | show_period(period, index) |
paul@1375 | 417 | print " %s" % (operation == "add" and "Add this period" or "Remove this period") |
paul@1375 | 418 | index += 1 |
paul@1375 | 419 | |
paul@1375 | 420 | def show_conflicting_periods(self): |
paul@1375 | 421 | conflicts = self.get_conflicting_periods() |
paul@1375 | 422 | if not conflicts: |
paul@1375 | 423 | return |
paul@1375 | 424 | print |
paul@1375 | 425 | print_title("Conflicting periods") |
paul@1375 | 426 | |
paul@1375 | 427 | conflicts = list(conflicts) |
paul@1375 | 428 | conflicts.sort() |
paul@1375 | 429 | |
paul@1375 | 430 | for p in conflicts: |
paul@1375 | 431 | print p.summary, p.uid, p.get_start(), p.get_end() |
paul@1375 | 432 | |
paul@1375 | 433 | # Interaction functions. |
paul@1375 | 434 | |
paul@1375 | 435 | def expand_arg(args): |
paul@1375 | 436 | if args[0] and args[0][1:].isdigit(): |
paul@1375 | 437 | args[:1] = [args[0][0], args[0][1:]] |
paul@1375 | 438 | |
paul@1375 | 439 | def get_filename_arg(cmd): |
paul@1375 | 440 | return (cmd.split()[1:] or [None])[0] |
paul@1375 | 441 | |
paul@1375 | 442 | def next_arg(args): |
paul@1375 | 443 | if args: |
paul@1375 | 444 | arg = args[0] |
paul@1375 | 445 | del args[0] |
paul@1375 | 446 | return arg |
paul@1375 | 447 | return None |
paul@1375 | 448 | |
paul@1375 | 449 | def edit_period(period, args=None): |
paul@1375 | 450 | |
paul@1375 | 451 | "Edit the given 'period'." |
paul@1375 | 452 | |
paul@1375 | 453 | print "Editing start (%s)" % period.get_start() |
paul@1375 | 454 | edit_date(period.start, args) |
paul@1375 | 455 | print "Editing end (%s)" % period.get_end() |
paul@1375 | 456 | edit_date(period.end, args) |
paul@1375 | 457 | |
paul@1375 | 458 | def edit_date(date, args=None): |
paul@1375 | 459 | |
paul@1375 | 460 | "Edit the given 'date' object attributes." |
paul@1375 | 461 | |
paul@1375 | 462 | date.date = next_arg(args) or input_with_default("Date (%s)? ", date.date) |
paul@1375 | 463 | date.hour = next_arg(args) or input_with_default("Hour (%s)? ", date.hour) |
paul@1375 | 464 | date.minute = next_arg(args) or input_with_default("Minute (%s)? ", date.minute) |
paul@1375 | 465 | date.second = next_arg(args) or input_with_default("Second (%s)? ", date.second) |
paul@1375 | 466 | date.tzid = next_arg(args) or input_with_default("Time zone (%s)? ", date.tzid) |
paul@1375 | 467 | date.reset() |
paul@1375 | 468 | |
paul@1375 | 469 | def select_object(cl, objects): |
paul@1375 | 470 | print |
paul@1375 | 471 | while True: |
paul@1375 | 472 | try: |
paul@1375 | 473 | cmd = read_input("Object or (n)ew object or (q)uit> ") |
paul@1375 | 474 | except EOFError: |
paul@1375 | 475 | return None |
paul@1375 | 476 | |
paul@1375 | 477 | if cmd.isdigit(): |
paul@1375 | 478 | index = int(cmd) |
paul@1375 | 479 | if 0 <= index < len(objects): |
paul@1375 | 480 | obj = objects[index] |
paul@1375 | 481 | return cl.load_object(obj.get_uid(), obj.get_recurrenceid()) |
paul@1375 | 482 | elif cmd in ("n", "new"): |
paul@1375 | 483 | return cl.new_object() |
paul@1375 | 484 | elif cmd in ("q", "quit", "exit"): |
paul@1375 | 485 | return None |
paul@1375 | 486 | |
paul@1375 | 487 | def show_help(): |
paul@1375 | 488 | print |
paul@1375 | 489 | print_title("Editing commands") |
paul@1375 | 490 | print |
paul@1375 | 491 | print """\ |
paul@1375 | 492 | a [ <uri> ] |
paul@1375 | 493 | attendee [ <uri> ] |
paul@1375 | 494 | Add attendee |
paul@1375 | 495 | |
paul@1375 | 496 | A, attend, attendance |
paul@1375 | 497 | Change attendance/participation |
paul@1375 | 498 | |
paul@1375 | 499 | a<digit> |
paul@1375 | 500 | attendee <digit> |
paul@1375 | 501 | Select attendee from list |
paul@1375 | 502 | |
paul@1375 | 503 | as<digit> |
paul@1375 | 504 | Add suggested attendee from list |
paul@1375 | 505 | |
paul@1375 | 506 | f, finish |
paul@1375 | 507 | Finish editing, confirming changes, proceeding to messaging |
paul@1375 | 508 | |
paul@1375 | 509 | h, help, ? |
paul@1375 | 510 | Show this help message |
paul@1375 | 511 | |
paul@1375 | 512 | l, list, show |
paul@1375 | 513 | List/show all event details |
paul@1375 | 514 | |
paul@1375 | 515 | p, period |
paul@1375 | 516 | Add new period |
paul@1375 | 517 | |
paul@1375 | 518 | p<digit> |
paul@1375 | 519 | period <digit> |
paul@1375 | 520 | Select period from list |
paul@1375 | 521 | |
paul@1375 | 522 | ps<digit> |
paul@1375 | 523 | Add or remove suggested period from list |
paul@1375 | 524 | |
paul@1375 | 525 | q, quit, exit |
paul@1375 | 526 | Exit/quit this program |
paul@1375 | 527 | |
paul@1375 | 528 | r, reload, reset, restart |
paul@1375 | 529 | Reset event periods (return to editing mode, if already finished) |
paul@1375 | 530 | |
paul@1375 | 531 | s, summary |
paul@1375 | 532 | Set event summary |
paul@1375 | 533 | """ |
paul@1375 | 534 | |
paul@1375 | 535 | print_title("Diagnostic commands") |
paul@1375 | 536 | print |
paul@1375 | 537 | print """\ |
paul@1375 | 538 | c, class, classification |
paul@1375 | 539 | Show period classification |
paul@1375 | 540 | |
paul@1375 | 541 | C, changes |
paul@1375 | 542 | Show changes made by editing |
paul@1375 | 543 | |
paul@1375 | 544 | o, ops, operations |
paul@1375 | 545 | Show update operations |
paul@1375 | 546 | |
paul@1375 | 547 | RECURRENCE-ID [ <filename> ] |
paul@1375 | 548 | Show event recurrence identifier, writing to <filename> if specified |
paul@1375 | 549 | |
paul@1375 | 550 | UID [ <filename> ] |
paul@1375 | 551 | Show event unique identifier, writing to <filename> if specified |
paul@1375 | 552 | """ |
paul@1375 | 553 | |
paul@1375 | 554 | print_title("Messaging commands") |
paul@1375 | 555 | print |
paul@1375 | 556 | print """\ |
paul@1375 | 557 | P [ <filename> ] |
paul@1375 | 558 | publish [ <filename> ] |
paul@1375 | 559 | Show publishing message, writing to <filename> if specified |
paul@1375 | 560 | |
paul@1375 | 561 | R [ <filename> ] |
paul@1375 | 562 | remove [ <filename> ] |
paul@1375 | 563 | cancel [ <filename> ] |
paul@1375 | 564 | Show cancellation message sent to uninvited/removed recipients, writing to |
paul@1375 | 565 | <filename> if specified |
paul@1375 | 566 | |
paul@1375 | 567 | U [ <filename> ] |
paul@1375 | 568 | update [ <filename> ] |
paul@1375 | 569 | Show update message, writing to <filename> if specified |
paul@1375 | 570 | """ |
paul@1375 | 571 | |
paul@1375 | 572 | def edit_object(cl, obj): |
paul@1375 | 573 | cl.show_object() |
paul@1375 | 574 | print |
paul@1375 | 575 | |
paul@1375 | 576 | try: |
paul@1375 | 577 | while True: |
paul@1375 | 578 | role = cl.is_organiser() and "Organiser" or "Attendee" |
paul@1375 | 579 | status = cl.state.get("finished") and " (editing complete)" or "" |
paul@1375 | 580 | |
paul@1375 | 581 | cmd = read_input("%s%s> " % (role, status)) |
paul@1375 | 582 | |
paul@1375 | 583 | args = cmd.split() |
paul@1375 | 584 | |
paul@1375 | 585 | if not args or not args[0]: |
paul@1375 | 586 | continue |
paul@1375 | 587 | |
paul@1375 | 588 | # Check the status of the periods. |
paul@1375 | 589 | |
paul@1375 | 590 | if cmd in ("c", "class", "classification"): |
paul@1375 | 591 | cl.show_period_classification() |
paul@1375 | 592 | print |
paul@1375 | 593 | |
paul@1375 | 594 | elif cmd in ("C", "changes"): |
paul@1375 | 595 | cl.show_changes() |
paul@1375 | 596 | print |
paul@1375 | 597 | |
paul@1375 | 598 | # Finish editing. |
paul@1375 | 599 | |
paul@1375 | 600 | elif cmd in ("f", "finish"): |
paul@1375 | 601 | cl.finish() |
paul@1375 | 602 | |
paul@1375 | 603 | # Help. |
paul@1375 | 604 | |
paul@1375 | 605 | elif cmd in ("h", "?", "help"): |
paul@1375 | 606 | show_help() |
paul@1375 | 607 | |
paul@1375 | 608 | # Show object details. |
paul@1375 | 609 | |
paul@1375 | 610 | elif cmd in ("l", "list", "show"): |
paul@1375 | 611 | cl.show_object() |
paul@1375 | 612 | print |
paul@1375 | 613 | |
paul@1375 | 614 | # Show the operations. |
paul@1375 | 615 | |
paul@1375 | 616 | elif cmd in ("o", "ops", "operations"): |
paul@1375 | 617 | cl.show_operations() |
paul@1375 | 618 | print |
paul@1375 | 619 | |
paul@1375 | 620 | # Quit or exit. |
paul@1375 | 621 | |
paul@1375 | 622 | elif cmd in ("q", "quit", "exit"): |
paul@1375 | 623 | break |
paul@1375 | 624 | |
paul@1375 | 625 | # Restart editing. |
paul@1375 | 626 | |
paul@1375 | 627 | elif cmd in ("r", "reload", "reset", "restart"): |
paul@1375 | 628 | obj = cl.load_object(obj.get_uid(), obj.get_recurrenceid()) |
paul@1375 | 629 | if not obj: |
paul@1375 | 630 | obj = cl.new_object() |
paul@1375 | 631 | cl.reset() |
paul@1375 | 632 | cl.show_object() |
paul@1375 | 633 | print |
paul@1375 | 634 | |
paul@1375 | 635 | # Show UID details. |
paul@1375 | 636 | |
paul@1375 | 637 | elif args[0] == "UID": |
paul@1375 | 638 | filename = get_filename_arg(cmd) |
paul@1375 | 639 | write(obj.get_uid(), filename) |
paul@1375 | 640 | |
paul@1375 | 641 | elif args[0] == "RECURRENCE-ID": |
paul@1375 | 642 | filename = get_filename_arg(cmd) |
paul@1375 | 643 | write(obj.get_recurrenceid() or "", filename) |
paul@1375 | 644 | |
paul@1375 | 645 | # Post-editing operations. |
paul@1375 | 646 | |
paul@1375 | 647 | elif cl.state.get("finished"): |
paul@1375 | 648 | |
paul@1375 | 649 | # Show messages. |
paul@1375 | 650 | |
paul@1375 | 651 | if args[0] in ("P", "publish"): |
paul@1375 | 652 | filename = get_filename_arg(cmd) |
paul@1375 | 653 | cl.show_publish_message(plain=not filename, filename=filename) |
paul@1375 | 654 | |
paul@1375 | 655 | elif args[0] in ("R", "remove", "cancel"): |
paul@1375 | 656 | filename = get_filename_arg(cmd) |
paul@1375 | 657 | cl.show_cancel_message(plain=not filename, filename=filename) |
paul@1375 | 658 | |
paul@1375 | 659 | elif args[0] in ("U", "update"): |
paul@1375 | 660 | filename = get_filename_arg(cmd) |
paul@1375 | 661 | cl.show_update_message(plain=not filename, filename=filename) |
paul@1375 | 662 | |
paul@1375 | 663 | # Editing operations. |
paul@1375 | 664 | |
paul@1375 | 665 | elif not cl.state.get("finished"): |
paul@1375 | 666 | |
paul@1375 | 667 | # Expand short-form arguments. |
paul@1375 | 668 | |
paul@1375 | 669 | expand_arg(args) |
paul@1375 | 670 | |
paul@1375 | 671 | # Add or edit attendee. |
paul@1375 | 672 | |
paul@1375 | 673 | if args[0] in ("a", "attendee"): |
paul@1375 | 674 | |
paul@1375 | 675 | args = args[1:] |
paul@1375 | 676 | value = next_arg(args) |
paul@1375 | 677 | |
paul@1375 | 678 | if value and value.isdigit(): |
paul@1375 | 679 | index = int(value) |
paul@1375 | 680 | else: |
paul@1375 | 681 | try: |
paul@1375 | 682 | index = cl.find_attendee(value) |
paul@1375 | 683 | except ValueError: |
paul@1375 | 684 | index = None |
paul@1375 | 685 | |
paul@1375 | 686 | # Add an attendee. |
paul@1375 | 687 | |
paul@1375 | 688 | if index is None: |
paul@1375 | 689 | cl.add_attendee(value) |
paul@1375 | 690 | if not value: |
paul@1375 | 691 | cl.edit_attendee(-1) |
paul@1375 | 692 | |
paul@1375 | 693 | # Edit attendee (using index). |
paul@1375 | 694 | |
paul@1375 | 695 | else: |
paul@1375 | 696 | attendee_item = cl.can_remove_attendee(index) |
paul@1375 | 697 | if attendee_item: |
paul@1375 | 698 | while True: |
paul@1375 | 699 | show_attendee(attendee_item, index) |
paul@1375 | 700 | |
paul@1375 | 701 | # Obtain a command from any arguments. |
paul@1375 | 702 | |
paul@1375 | 703 | cmd = next_arg(args) |
paul@1375 | 704 | if not cmd: |
paul@1375 | 705 | cmd = read_input(" (e)dit, (r)emove (or return)> ") |
paul@1375 | 706 | if cmd in ("e", "edit"): |
paul@1375 | 707 | cl.edit_attendee(index) |
paul@1375 | 708 | elif cmd in ("r", "remove"): |
paul@1375 | 709 | cl.remove_attendees([index]) |
paul@1375 | 710 | elif not cmd: |
paul@1375 | 711 | pass |
paul@1375 | 712 | else: |
paul@1375 | 713 | continue |
paul@1375 | 714 | break |
paul@1375 | 715 | |
paul@1375 | 716 | cl.show_attendees() |
paul@1375 | 717 | print |
paul@1375 | 718 | |
paul@1375 | 719 | # Add suggested attendee (using index). |
paul@1375 | 720 | |
paul@1375 | 721 | elif args[0] in ("as", "attendee-suggested", "suggested-attendee"): |
paul@1375 | 722 | try: |
paul@1375 | 723 | index = int(args[1]) |
paul@1375 | 724 | cl.add_suggested_attendee(index) |
paul@1375 | 725 | except ValueError: |
paul@1375 | 726 | pass |
paul@1375 | 727 | cl.show_attendees() |
paul@1375 | 728 | print |
paul@1375 | 729 | |
paul@1375 | 730 | # Edit attendance. |
paul@1375 | 731 | |
paul@1375 | 732 | elif args[0] in ("A", "attend", "attendance"): |
paul@1375 | 733 | |
paul@1375 | 734 | args = args[1:] |
paul@1375 | 735 | |
paul@1375 | 736 | if not cl.is_attendee() and cl.is_organiser(): |
paul@1375 | 737 | cl.add_attendee(cl.user) |
paul@1375 | 738 | |
paul@1375 | 739 | # NOTE: Support delegation. |
paul@1375 | 740 | |
paul@1375 | 741 | if cl.can_edit_attendance(): |
paul@1375 | 742 | while True: |
paul@1375 | 743 | |
paul@1375 | 744 | # Obtain a command from any arguments. |
paul@1375 | 745 | |
paul@1375 | 746 | cmd = next_arg(args) |
paul@1375 | 747 | if not cmd: |
paul@1375 | 748 | cmd = read_input(" (a)ccept, (d)ecline, (t)entative (or return)> ") |
paul@1375 | 749 | if cmd in ("a", "accept", "accepted", "attend"): |
paul@1375 | 750 | cl.edit_attendance("ACCEPTED") |
paul@1375 | 751 | elif cmd in ("d", "decline", "declined"): |
paul@1375 | 752 | cl.edit_attendance("DECLINED") |
paul@1375 | 753 | elif cmd in ("t", "tentative"): |
paul@1375 | 754 | cl.edit_attendance("TENTATIVE") |
paul@1375 | 755 | elif not cmd: |
paul@1375 | 756 | pass |
paul@1375 | 757 | else: |
paul@1375 | 758 | continue |
paul@1375 | 759 | break |
paul@1375 | 760 | |
paul@1375 | 761 | cl.show_attendees() |
paul@1375 | 762 | print |
paul@1375 | 763 | |
paul@1375 | 764 | # Add or edit period. |
paul@1375 | 765 | |
paul@1375 | 766 | elif args[0] in ("p", "period"): |
paul@1375 | 767 | |
paul@1375 | 768 | args = args[1:] |
paul@1375 | 769 | value = next_arg(args) |
paul@1375 | 770 | |
paul@1375 | 771 | if value and value.isdigit(): |
paul@1375 | 772 | index = int(value) |
paul@1375 | 773 | else: |
paul@1375 | 774 | index = None |
paul@1375 | 775 | |
paul@1375 | 776 | # Add a new period. |
paul@1375 | 777 | |
paul@1375 | 778 | if index is None: |
paul@1375 | 779 | cl.add_period() |
paul@1375 | 780 | cl.edit_period(-1) |
paul@1375 | 781 | |
paul@1375 | 782 | # Edit period (using index). |
paul@1375 | 783 | |
paul@1375 | 784 | else: |
paul@1375 | 785 | period = cl.can_edit_period(index) |
paul@1375 | 786 | if period: |
paul@1375 | 787 | while True: |
paul@1375 | 788 | show_period_raw(period) |
paul@1375 | 789 | |
paul@1375 | 790 | # Obtain a command from any arguments. |
paul@1375 | 791 | |
paul@1375 | 792 | cmd = next_arg(args) |
paul@1375 | 793 | if not cmd: |
paul@1375 | 794 | cmd = read_input(" (e)dit, (c)ancel, (u)ncancel (or return)> ") |
paul@1375 | 795 | if cmd in ("e", "edit"): |
paul@1375 | 796 | cl.edit_period(index, args) |
paul@1375 | 797 | elif cmd in ("c", "cancel"): |
paul@1375 | 798 | cl.cancel_periods([index]) |
paul@1375 | 799 | elif cmd in ("u", "uncancel", "restore"): |
paul@1375 | 800 | cl.cancel_periods([index], False) |
paul@1375 | 801 | elif not cmd: |
paul@1375 | 802 | pass |
paul@1375 | 803 | else: |
paul@1375 | 804 | continue |
paul@1375 | 805 | break |
paul@1375 | 806 | |
paul@1375 | 807 | cl.show_periods() |
paul@1375 | 808 | print |
paul@1375 | 809 | |
paul@1375 | 810 | # Apply suggested period (using index). |
paul@1375 | 811 | |
paul@1375 | 812 | elif args[0] in ("ps", "period-suggested", "suggested-period"): |
paul@1375 | 813 | try: |
paul@1375 | 814 | index = int(args[1]) |
paul@1375 | 815 | cl.apply_suggested_period(index) |
paul@1375 | 816 | except ValueError: |
paul@1375 | 817 | pass |
paul@1375 | 818 | cl.show_periods() |
paul@1375 | 819 | print |
paul@1375 | 820 | |
paul@1375 | 821 | # Set the summary. |
paul@1375 | 822 | |
paul@1375 | 823 | elif args[0] in ("s", "summary"): |
paul@1375 | 824 | cl.edit_summary(cmd.split(None, 1)[1]) |
paul@1375 | 825 | cl.show_object() |
paul@1375 | 826 | print |
paul@1375 | 827 | |
paul@1375 | 828 | except EOFError: |
paul@1375 | 829 | return |
paul@1375 | 830 | |
paul@1375 | 831 | def main(args): |
paul@1375 | 832 | global echo |
paul@1375 | 833 | |
paul@1375 | 834 | # Parse command line arguments using the standard options plus some extra |
paul@1375 | 835 | # options. |
paul@1375 | 836 | |
paul@1375 | 837 | args = parse_args(args, { |
paul@1375 | 838 | "--echo" : ("echo", False), |
paul@1375 | 839 | "-f" : ("filename", None), |
paul@1375 | 840 | "--suppress-bcc" : ("suppress_bcc", False), |
paul@1375 | 841 | "-u" : ("user", None), |
paul@1375 | 842 | }) |
paul@1375 | 843 | |
paul@1375 | 844 | echo = args["echo"] |
paul@1375 | 845 | filename = args["filename"] |
paul@1375 | 846 | sender = (args["senders"] or [None])[0] |
paul@1375 | 847 | suppress_bcc = args["suppress_bcc"] |
paul@1375 | 848 | user = args["user"] |
paul@1375 | 849 | |
paul@1375 | 850 | # Determine the user and sender identities. |
paul@1375 | 851 | |
paul@1375 | 852 | if sender and not user: |
paul@1375 | 853 | user = get_uri(sender) |
paul@1375 | 854 | elif user and not sender: |
paul@1375 | 855 | sender = get_address(user) |
paul@1375 | 856 | elif not sender and not user: |
paul@1375 | 857 | print >>sys.stderr, "A sender or a user must be specified." |
paul@1375 | 858 | sys.exit(1) |
paul@1375 | 859 | |
paul@1375 | 860 | # Open a store. |
paul@1375 | 861 | |
paul@1375 | 862 | store_type = args.get("store_type") |
paul@1375 | 863 | store_dir = args.get("store_dir") |
paul@1375 | 864 | preferences_dir = args.get("preferences_dir") |
paul@1375 | 865 | |
paul@1375 | 866 | store = get_store(store_type, store_dir) |
paul@1375 | 867 | journal = None |
paul@1375 | 868 | |
paul@1375 | 869 | # Open a messenger for the user. |
paul@1375 | 870 | |
paul@1375 | 871 | messenger = Messenger(sender=sender, suppress_bcc=suppress_bcc) |
paul@1375 | 872 | |
paul@1375 | 873 | # Open a client for the user. |
paul@1375 | 874 | |
paul@1375 | 875 | cl = TextClient(user, messenger, store, journal, preferences_dir) |
paul@1375 | 876 | |
paul@1375 | 877 | # Read any input resource. |
paul@1375 | 878 | |
paul@1375 | 879 | if filename: |
paul@1375 | 880 | objects = get_objects(filename) |
paul@1375 | 881 | |
paul@1375 | 882 | # Choose an object to edit. |
paul@1375 | 883 | |
paul@1375 | 884 | show_objects(objects, user, store) |
paul@1375 | 885 | obj = select_object(cl, objects) |
paul@1375 | 886 | |
paul@1375 | 887 | # Exit without any object. |
paul@1375 | 888 | |
paul@1375 | 889 | if not obj: |
paul@1375 | 890 | print >>sys.stderr, "Object not loaded." |
paul@1375 | 891 | sys.exit(1) |
paul@1375 | 892 | |
paul@1375 | 893 | # Or create a new object. |
paul@1375 | 894 | |
paul@1375 | 895 | else: |
paul@1375 | 896 | obj = cl.new_object() |
paul@1375 | 897 | |
paul@1375 | 898 | # Edit the object. |
paul@1375 | 899 | |
paul@1375 | 900 | edit_object(cl, obj) |
paul@1375 | 901 | |
paul@1375 | 902 | if __name__ == "__main__": |
paul@1375 | 903 | main(sys.argv[1:]) |
paul@1375 | 904 | |
paul@1375 | 905 | # vim: tabstop=4 expandtab shiftwidth=4 |