paul@1375 | 1 | #!/usr/bin/env python |
paul@1375 | 2 | |
paul@1396 | 3 | """ |
paul@1396 | 4 | A text interface to received and new events. |
paul@1396 | 5 | |
paul@1436 | 6 | Copyright (C) 2017, 2018 Paul Boddie <paul@boddie.org.uk> |
paul@1396 | 7 | |
paul@1396 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@1396 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@1396 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@1396 | 11 | version. |
paul@1396 | 12 | |
paul@1396 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@1396 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@1396 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@1396 | 16 | details. |
paul@1396 | 17 | |
paul@1396 | 18 | You should have received a copy of the GNU General Public License along with |
paul@1396 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@1396 | 20 | """ |
paul@1396 | 21 | |
paul@1375 | 22 | from email import message_from_file |
paul@1387 | 23 | from imiptools import get_handlers, parse_args |
paul@1375 | 24 | from imiptools.config import settings |
paul@1387 | 25 | from imiptools.content import get_objects_from_itip, handle_calendar_data, \ |
paul@1387 | 26 | handle_calendar_object, have_itip_part, \ |
paul@1423 | 27 | is_cancel_itip, parse_itip_part |
paul@1436 | 28 | from imiptools.data import get_address, get_periods_using_selector, \ |
paul@1436 | 29 | get_main_period, get_recurrence_periods, \ |
paul@1436 | 30 | get_value, parse_object |
paul@1375 | 31 | from imiptools.dates import get_datetime_item, get_time, to_timezone |
paul@1436 | 32 | from imiptools.editing import EditingClient, PeriodError, \ |
paul@1436 | 33 | form_periods_from_periods |
paul@1387 | 34 | from imiptools.handlers import person, person_outgoing |
paul@1375 | 35 | from imiptools.mail import Messenger |
paul@1375 | 36 | from imiptools.stores import get_journal, get_store |
paul@1375 | 37 | from imiptools.utils import decode_part, message_as_string |
paul@1396 | 38 | import vRecurrence |
paul@1387 | 39 | import sys, os |
paul@1375 | 40 | |
paul@1375 | 41 | # User interface functions. |
paul@1375 | 42 | |
paul@1375 | 43 | echo = False |
paul@1375 | 44 | |
paul@1375 | 45 | def read_input(label): |
paul@1396 | 46 | |
paul@1396 | 47 | """ |
paul@1396 | 48 | Read input, prompting using 'label', stripping leading and trailing |
paul@1396 | 49 | whitespace, echoing the input if the global 'echo' variable is set. |
paul@1396 | 50 | """ |
paul@1396 | 51 | |
paul@1375 | 52 | s = raw_input(label).strip() |
paul@1375 | 53 | if echo: |
paul@1375 | 54 | print s |
paul@1375 | 55 | return s |
paul@1375 | 56 | |
paul@1375 | 57 | def input_with_default(label, default): |
paul@1396 | 58 | |
paul@1396 | 59 | """ |
paul@1396 | 60 | Read input, prompting using 'label', parameterising the label with the given |
paul@1396 | 61 | 'default' and returning the default if no input is given. |
paul@1396 | 62 | """ |
paul@1396 | 63 | |
paul@1375 | 64 | return read_input(label % default) or default |
paul@1375 | 65 | |
paul@1436 | 66 | def to_int_or_none(value): |
paul@1436 | 67 | |
paul@1436 | 68 | "Return 'value' as an integer or None for other inputs." |
paul@1436 | 69 | |
paul@1436 | 70 | try: |
paul@1436 | 71 | return int(value) |
paul@1436 | 72 | except (TypeError, ValueError): |
paul@1436 | 73 | return None |
paul@1436 | 74 | |
paul@1436 | 75 | def format_value_ranges(ranges, null_value="-"): |
paul@1436 | 76 | |
paul@1436 | 77 | "Format 'ranges' as a single descriptive string." |
paul@1436 | 78 | |
paul@1436 | 79 | l = [] |
paul@1436 | 80 | |
paul@1436 | 81 | for value_range in ranges: |
paul@1436 | 82 | if isinstance(value_range, tuple): |
paul@1436 | 83 | start, end = value_range |
paul@1436 | 84 | l.append("%s...%s" % (start, end)) |
paul@1436 | 85 | elif isinstance(value_range, list): |
paul@1436 | 86 | l.append(", ".join(value_range)) |
paul@1436 | 87 | elif isinstance(value_range, dict): |
paul@1436 | 88 | l.append(", ".join(value_range.keys())) |
paul@1436 | 89 | else: |
paul@1436 | 90 | l.append(value_range or null_value) |
paul@1436 | 91 | |
paul@1436 | 92 | return ", ".join(l) |
paul@1436 | 93 | |
paul@1375 | 94 | def print_title(text): |
paul@1396 | 95 | |
paul@1396 | 96 | "Print 'text' with simple, fixed-width styling as a title." |
paul@1396 | 97 | |
paul@1375 | 98 | print text |
paul@1375 | 99 | print len(text) * "-" |
paul@1375 | 100 | |
paul@1396 | 101 | def print_table(rows, separator_index=0): |
paul@1396 | 102 | |
paul@1396 | 103 | """ |
paul@1396 | 104 | Print 'rows' as a simple, fixed-width table. If 'separator_index' is set to |
paul@1396 | 105 | a row index present in the rows, a table separator will be produced below |
paul@1396 | 106 | that row's data. Otherwise, a separator will appear below the first row. |
paul@1396 | 107 | """ |
paul@1396 | 108 | |
paul@1396 | 109 | widths = [] |
paul@1396 | 110 | for row in rows: |
paul@1396 | 111 | for i, col in enumerate(row): |
paul@1396 | 112 | if i >= len(widths): |
paul@1396 | 113 | widths.append(len(col)) |
paul@1396 | 114 | else: |
paul@1396 | 115 | widths[i] = max(widths[i], len(col)) |
paul@1396 | 116 | |
paul@1396 | 117 | for i, row in enumerate(rows): |
paul@1396 | 118 | for col, width in zip(row, widths): |
paul@1396 | 119 | print "%s%s" % (col, " " * (width - len(col))), |
paul@1396 | 120 | print |
paul@1396 | 121 | if i == separator_index: |
paul@1396 | 122 | for width in widths: |
paul@1396 | 123 | print "-" * width, |
paul@1396 | 124 | print |
paul@1396 | 125 | |
paul@1375 | 126 | def write(s, filename): |
paul@1396 | 127 | |
paul@1396 | 128 | "Write 's' to a file having the given 'filename'." |
paul@1396 | 129 | |
paul@1375 | 130 | f = filename and open(filename, "w") or None |
paul@1375 | 131 | try: |
paul@1375 | 132 | print >>(f or sys.stdout), s |
paul@1375 | 133 | finally: |
paul@1375 | 134 | if f: |
paul@1375 | 135 | f.close() |
paul@1375 | 136 | |
paul@1375 | 137 | # Interpret an input file containing a calendar resource. |
paul@1375 | 138 | |
paul@1387 | 139 | def get_itip_from_message(filename): |
paul@1375 | 140 | |
paul@1387 | 141 | "Return iTIP details provided by 'filename'." |
paul@1375 | 142 | |
paul@1375 | 143 | f = open(filename) |
paul@1375 | 144 | try: |
paul@1375 | 145 | msg = message_from_file(f) |
paul@1375 | 146 | finally: |
paul@1375 | 147 | f.close() |
paul@1375 | 148 | |
paul@1387 | 149 | all_itip = [] |
paul@1375 | 150 | |
paul@1375 | 151 | for part in msg.walk(): |
paul@1387 | 152 | if have_itip_part(part): |
paul@1387 | 153 | all_itip.append(parse_itip_part(part)) |
paul@1375 | 154 | |
paul@1387 | 155 | return all_itip |
paul@1387 | 156 | |
paul@1387 | 157 | def get_itip_from_data(filename, charset): |
paul@1375 | 158 | |
paul@1387 | 159 | "Return objects provided by 'filename'." |
paul@1375 | 160 | |
paul@1387 | 161 | f = open(filename) |
paul@1387 | 162 | try: |
paul@1387 | 163 | itip = parse_object(f, charset, "VCALENDAR") |
paul@1387 | 164 | finally: |
paul@1387 | 165 | f.close() |
paul@1387 | 166 | |
paul@1387 | 167 | return [itip] |
paul@1375 | 168 | |
paul@1436 | 169 | # Object and request display. |
paul@1436 | 170 | |
paul@1375 | 171 | def show_objects(objects, user, store): |
paul@1375 | 172 | |
paul@1375 | 173 | """ |
paul@1375 | 174 | Show details of 'objects', accessed by the given 'user' in the given |
paul@1375 | 175 | 'store'. |
paul@1375 | 176 | """ |
paul@1375 | 177 | |
paul@1387 | 178 | print |
paul@1387 | 179 | print_title("Objects") |
paul@1387 | 180 | print |
paul@1387 | 181 | |
paul@1375 | 182 | for index, obj in enumerate(objects): |
paul@1375 | 183 | recurrenceid = obj.get_recurrenceid() |
paul@1375 | 184 | recurrence_label = recurrenceid and " %s" % recurrenceid or "" |
paul@1375 | 185 | print "(%d) Summary: %s (%s%s)" % (index, obj.get_value("SUMMARY"), obj.get_uid(), recurrence_label) |
paul@1375 | 186 | |
paul@1387 | 187 | def show_requests(user, store): |
paul@1387 | 188 | |
paul@1387 | 189 | "Show requests available to the given 'user' in the given 'store'." |
paul@1387 | 190 | |
paul@1387 | 191 | requests = store.get_requests(user) |
paul@1387 | 192 | |
paul@1387 | 193 | print |
paul@1387 | 194 | print_title("Requests") |
paul@1387 | 195 | print |
paul@1387 | 196 | |
paul@1387 | 197 | if not requests: |
paul@1387 | 198 | print "No requests are pending." |
paul@1387 | 199 | return |
paul@1387 | 200 | |
paul@1387 | 201 | for index, (uid, recurrenceid) in enumerate(requests): |
paul@1387 | 202 | obj = store.get_event(user, uid, recurrenceid) |
paul@1387 | 203 | recurrence_label = recurrenceid and " %s" % recurrenceid or "" |
paul@1387 | 204 | print "(%d) Summary: %s (%s%s)" % (index, obj.get_value("SUMMARY"), obj.get_uid(), recurrence_label) |
paul@1387 | 205 | |
paul@1436 | 206 | # Object details display. |
paul@1436 | 207 | |
paul@1375 | 208 | def show_attendee(attendee_item, index): |
paul@1375 | 209 | |
paul@1375 | 210 | "Show the 'attendee_item' (value and attributes) at 'index'." |
paul@1375 | 211 | |
paul@1375 | 212 | attendee, attr = attendee_item |
paul@1375 | 213 | partstat = attr.get("PARTSTAT") |
paul@1375 | 214 | print "(%d) %s%s" % (index, attendee, partstat and " (%s)" % partstat or "") |
paul@1375 | 215 | |
paul@1375 | 216 | def show_attendees_raw(attendee_map): |
paul@1375 | 217 | |
paul@1375 | 218 | "Show the 'attendee_map' in a simple raw form." |
paul@1375 | 219 | |
paul@1375 | 220 | for attendee, attr in attendee_map.items(): |
paul@1375 | 221 | print attendee |
paul@1375 | 222 | |
paul@1375 | 223 | def show_periods(periods, errors=None): |
paul@1375 | 224 | |
paul@1375 | 225 | "Show 'periods' with any indicated 'errors'." |
paul@1375 | 226 | |
paul@1375 | 227 | main = get_main_period(periods) |
paul@1375 | 228 | if main: |
paul@1375 | 229 | show_period(main, 0, errors) |
paul@1375 | 230 | |
paul@1375 | 231 | recurrences = get_recurrence_periods(periods) |
paul@1375 | 232 | if recurrences: |
paul@1375 | 233 | print |
paul@1375 | 234 | print_title("Recurrences") |
paul@1375 | 235 | for index, p in enumerate(recurrences): |
paul@1375 | 236 | show_period(p, index + 1, errors) |
paul@1375 | 237 | |
paul@1375 | 238 | def show_period(p, index, errors=None): |
paul@1375 | 239 | |
paul@1375 | 240 | "Show period 'p' at 'index' with any indicated 'errors'." |
paul@1375 | 241 | |
paul@1375 | 242 | errors = errors and errors.get(index) |
paul@1422 | 243 | if p.cancelled: |
paul@1422 | 244 | label = "Cancelled" |
paul@1422 | 245 | elif p.replacement: |
paul@1422 | 246 | label = "Replaced" |
paul@1422 | 247 | elif p.new_replacement: |
paul@1422 | 248 | label = "To replace" |
paul@1422 | 249 | elif p.recurrenceid: |
paul@1422 | 250 | label = "Retained" |
paul@1375 | 251 | else: |
paul@1422 | 252 | label = "New" |
paul@1375 | 253 | |
paul@1375 | 254 | error_label = errors and " (errors: %s)" % ", ".join(errors) or "" |
paul@1375 | 255 | print "(%d) %s%s:" % (index, label, error_label), p.get_start(), p.get_end(), p.origin |
paul@1375 | 256 | |
paul@1375 | 257 | def show_periods_raw(periods): |
paul@1375 | 258 | |
paul@1375 | 259 | "Show 'periods' in a simple raw form." |
paul@1375 | 260 | |
paul@1375 | 261 | periods = periods[:] |
paul@1375 | 262 | periods.sort() |
paul@1375 | 263 | map(show_period_raw, periods) |
paul@1375 | 264 | |
paul@1375 | 265 | def show_period_raw(p): |
paul@1375 | 266 | |
paul@1375 | 267 | "Show period 'p' in a simple raw form." |
paul@1375 | 268 | |
paul@1375 | 269 | print p.get_start(), p.get_end(), p.origin |
paul@1375 | 270 | |
paul@1436 | 271 | def show_rule(selectors): |
paul@1396 | 272 | |
paul@1436 | 273 | "Show recurrence rule specification 'selectors'." |
paul@1396 | 274 | |
paul@1396 | 275 | # Collect limit, selection and frequency details. |
paul@1396 | 276 | |
paul@1436 | 277 | for i, selector in enumerate(selectors): |
paul@1436 | 278 | prefix = "(%d) " % i |
paul@1436 | 279 | show_rule_selector(selector, prefix) |
paul@1396 | 280 | |
paul@1436 | 281 | print |
paul@1436 | 282 | print vRecurrence.to_string(selectors) |
paul@1396 | 283 | |
paul@1436 | 284 | def show_rule_selector(selector, prefix=""): |
paul@1436 | 285 | |
paul@1436 | 286 | "Show the rule 'selector', employing any given 'prefix' for formatting." |
paul@1396 | 287 | |
paul@1436 | 288 | # COUNT |
paul@1396 | 289 | |
paul@1436 | 290 | if isinstance(selector, vRecurrence.LimitSelector): |
paul@1436 | 291 | print "%sAt most %d occurrences" % (prefix, selector.args["values"][0]) |
paul@1436 | 292 | |
paul@1436 | 293 | # BYSETPOS |
paul@1396 | 294 | |
paul@1436 | 295 | elif isinstance(selector, vRecurrence.PositionSelector): |
paul@1436 | 296 | for value in selector.get_positions(): |
paul@1436 | 297 | print "%sSelect occurrence #%d" % (prefix, value) |
paul@1436 | 298 | prefix = len(prefix) * " " |
paul@1396 | 299 | |
paul@1436 | 300 | # BYWEEKDAY |
paul@1396 | 301 | |
paul@1436 | 302 | elif isinstance(selector, vRecurrence.WeekDayFilter): |
paul@1436 | 303 | for value, index in selector.get_values(): |
paul@1436 | 304 | print "%sSelect occurrence #%d (from %s) of weekday %s" % ( |
paul@1436 | 305 | prefix, abs(index), index >= 0 and "start" or "end", |
paul@1436 | 306 | get_weekday(value)) |
paul@1436 | 307 | prefix = len(prefix) * " " |
paul@1396 | 308 | |
paul@1436 | 309 | # BY... |
paul@1396 | 310 | |
paul@1436 | 311 | elif isinstance(selector, vRecurrence.Enum): |
paul@1436 | 312 | for value in selector.get_values(): |
paul@1436 | 313 | print "%sSelect %s %r" % (prefix, get_resolution(selector.level), |
paul@1436 | 314 | value) |
paul@1436 | 315 | prefix = len(prefix) * " " |
paul@1436 | 316 | |
paul@1436 | 317 | # YEARLY, MONTHLY, WEEKLY, DAILY, HOURLY, MINUTELY, SECONDLY |
paul@1396 | 318 | |
paul@1436 | 319 | elif isinstance(selector, vRecurrence.Pattern): |
paul@1436 | 320 | print "%sEach %s with interval %d" % (prefix, |
paul@1436 | 321 | get_resolution(selector.level), selector.args.get("interval", 1)) |
paul@1396 | 322 | |
paul@1436 | 323 | def get_resolution(level): |
paul@1396 | 324 | |
paul@1436 | 325 | "Return a textual description of the given resolution 'level'." |
paul@1436 | 326 | |
paul@1436 | 327 | levels = ["year", "month", "week", "day in year", "day in month", "day", "hour", "minute", "second"] |
paul@1396 | 328 | return levels[level] |
paul@1396 | 329 | |
paul@1396 | 330 | def get_weekday(weekday): |
paul@1436 | 331 | |
paul@1436 | 332 | "Return the name of the given 1-based 'weekday' number." |
paul@1436 | 333 | |
paul@1396 | 334 | weekdays = [None, "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] |
paul@1396 | 335 | return weekdays[weekday] |
paul@1396 | 336 | |
paul@1375 | 337 | def show_attendee_changes(new, modified, unmodified, removed): |
paul@1375 | 338 | |
paul@1375 | 339 | "Show 'new', 'modified', 'unmodified' and 'removed' periods." |
paul@1375 | 340 | |
paul@1375 | 341 | print |
paul@1375 | 342 | print_title("Changes to attendees") |
paul@1375 | 343 | print |
paul@1375 | 344 | print "New:" |
paul@1375 | 345 | show_attendees_raw(new) |
paul@1375 | 346 | print |
paul@1375 | 347 | print "Modified:" |
paul@1375 | 348 | show_attendees_raw(modified) |
paul@1375 | 349 | print |
paul@1375 | 350 | print "Unmodified:" |
paul@1375 | 351 | show_attendees_raw(unmodified) |
paul@1375 | 352 | print |
paul@1375 | 353 | print "Removed:" |
paul@1375 | 354 | show_attendees_raw(removed) |
paul@1375 | 355 | |
paul@1375 | 356 | def show_period_classification(new, replaced, retained, cancelled, obsolete): |
paul@1375 | 357 | |
paul@1375 | 358 | "Show 'new', 'replaced', 'retained', 'cancelled' and 'obsolete' periods." |
paul@1375 | 359 | |
paul@1375 | 360 | print |
paul@1375 | 361 | print_title("Period classification") |
paul@1375 | 362 | print |
paul@1375 | 363 | print "New:" |
paul@1375 | 364 | show_periods_raw(new) |
paul@1375 | 365 | print |
paul@1375 | 366 | print "Replaced:" |
paul@1375 | 367 | show_periods_raw(replaced) |
paul@1375 | 368 | print |
paul@1375 | 369 | print "Retained:" |
paul@1375 | 370 | show_periods_raw(retained) |
paul@1375 | 371 | print |
paul@1375 | 372 | print "Cancelled:" |
paul@1375 | 373 | show_periods_raw(cancelled) |
paul@1375 | 374 | print |
paul@1375 | 375 | print "Obsolete:" |
paul@1375 | 376 | show_periods_raw(obsolete) |
paul@1375 | 377 | |
paul@1375 | 378 | def show_changes(modified, unmodified, removed): |
paul@1375 | 379 | |
paul@1375 | 380 | "Show 'modified', 'unmodified' and 'removed' periods." |
paul@1375 | 381 | |
paul@1375 | 382 | print |
paul@1375 | 383 | print_title("Changes to periods") |
paul@1375 | 384 | print |
paul@1375 | 385 | print "Modified:" |
paul@1375 | 386 | show_periods_raw(modified) |
paul@1375 | 387 | print |
paul@1375 | 388 | print "Unmodified:" |
paul@1375 | 389 | show_periods_raw(unmodified) |
paul@1375 | 390 | print |
paul@1375 | 391 | print "Removed:" |
paul@1375 | 392 | show_periods_raw(removed) |
paul@1375 | 393 | |
paul@1375 | 394 | def show_attendee_operations(to_invite, to_cancel, to_modify): |
paul@1375 | 395 | |
paul@1375 | 396 | "Show attendees 'to_invite', 'to_cancel' and 'to_modify'." |
paul@1375 | 397 | |
paul@1375 | 398 | print |
paul@1375 | 399 | print_title("Attendee update operations") |
paul@1375 | 400 | print |
paul@1375 | 401 | print "To invite:" |
paul@1375 | 402 | show_attendees_raw(to_invite) |
paul@1375 | 403 | print |
paul@1375 | 404 | print "To cancel:" |
paul@1375 | 405 | show_attendees_raw(to_cancel) |
paul@1375 | 406 | print |
paul@1375 | 407 | print "To modify:" |
paul@1375 | 408 | show_attendees_raw(to_modify) |
paul@1375 | 409 | |
paul@1375 | 410 | def show_period_operations(to_unschedule, to_reschedule, to_add, to_exclude, to_set, |
paul@1375 | 411 | all_unscheduled, all_rescheduled): |
paul@1375 | 412 | |
paul@1375 | 413 | """ |
paul@1375 | 414 | Show operations for periods 'to_unschedule', 'to_reschedule', 'to_add', |
paul@1375 | 415 | 'to_exclude' and 'to_set' (for updating other calendar participants), and |
paul@1375 | 416 | for periods 'all_unscheduled' and 'all_rescheduled' (for publishing event |
paul@1375 | 417 | state). |
paul@1375 | 418 | """ |
paul@1375 | 419 | |
paul@1375 | 420 | print |
paul@1375 | 421 | print_title("Period update and publishing operations") |
paul@1375 | 422 | print |
paul@1375 | 423 | print "Unschedule:" |
paul@1375 | 424 | show_periods_raw(to_unschedule) |
paul@1375 | 425 | print |
paul@1375 | 426 | print "Reschedule:" |
paul@1375 | 427 | show_periods_raw(to_reschedule) |
paul@1375 | 428 | print |
paul@1375 | 429 | print "Added:" |
paul@1375 | 430 | show_periods_raw(to_add) |
paul@1375 | 431 | print |
paul@1375 | 432 | print "Excluded:" |
paul@1375 | 433 | show_periods_raw(to_exclude) |
paul@1375 | 434 | print |
paul@1375 | 435 | print "Set in object:" |
paul@1375 | 436 | show_periods_raw(to_set) |
paul@1375 | 437 | print |
paul@1375 | 438 | print "All unscheduled:" |
paul@1375 | 439 | show_periods_raw(all_unscheduled) |
paul@1375 | 440 | print |
paul@1375 | 441 | print "All rescheduled:" |
paul@1375 | 442 | show_periods_raw(all_rescheduled) |
paul@1375 | 443 | |
paul@1375 | 444 | class TextClient(EditingClient): |
paul@1375 | 445 | |
paul@1375 | 446 | "Simple client with textual output." |
paul@1375 | 447 | |
paul@1375 | 448 | def new_object(self): |
paul@1375 | 449 | |
paul@1375 | 450 | "Create a new object with the current time." |
paul@1375 | 451 | |
paul@1375 | 452 | utcnow = get_time() |
paul@1375 | 453 | now = to_timezone(utcnow, self.get_tzid()) |
paul@1375 | 454 | obj = EditingClient.new_object(self, "VEVENT") |
paul@1375 | 455 | obj.set_value("SUMMARY", "New event") |
paul@1375 | 456 | obj["DTSTART"] = [get_datetime_item(now)] |
paul@1375 | 457 | obj["DTEND"] = [get_datetime_item(now)] |
paul@1375 | 458 | return obj |
paul@1375 | 459 | |
paul@1436 | 460 | def handle_outgoing_object(self): |
paul@1436 | 461 | |
paul@1436 | 462 | "Handle the current object using the outgoing handlers." |
paul@1436 | 463 | |
paul@1436 | 464 | unscheduled_objects, rescheduled_objects, added_objects = \ |
paul@1436 | 465 | self.get_publish_objects() |
paul@1436 | 466 | |
paul@1436 | 467 | handlers = get_handlers(self, person_outgoing.handlers, |
paul@1436 | 468 | [get_address(self.user)]) |
paul@1436 | 469 | |
paul@1436 | 470 | # Handle the parent object plus any altered periods. |
paul@1436 | 471 | |
paul@1436 | 472 | handle_calendar_object(self.obj, handlers, "PUBLISH") |
paul@1436 | 473 | |
paul@1436 | 474 | for o in unscheduled_objects: |
paul@1436 | 475 | handle_calendar_object(o, handlers, "CANCEL") |
paul@1436 | 476 | |
paul@1436 | 477 | for o in rescheduled_objects: |
paul@1436 | 478 | handle_calendar_object(o, handlers, "PUBLISH") |
paul@1436 | 479 | |
paul@1436 | 480 | for o in added_objects: |
paul@1436 | 481 | handle_calendar_object(o, handlers, "ADD") |
paul@1436 | 482 | |
paul@1436 | 483 | def update_periods_from_rule(self): |
paul@1436 | 484 | |
paul@1436 | 485 | "Update the periods from the rule." |
paul@1436 | 486 | |
paul@1436 | 487 | selectors = self.state.get("rule") |
paul@1436 | 488 | periods = self.state.get("periods") |
paul@1436 | 489 | |
paul@1436 | 490 | main_period = get_main_period(periods) |
paul@1436 | 491 | tzid = main_period.get_tzid() |
paul@1436 | 492 | |
paul@1436 | 493 | start = main_period.get_start() |
paul@1436 | 494 | end = self.get_window_end() or None |
paul@1436 | 495 | |
paul@1436 | 496 | selector = vRecurrence.get_selector(start, selectors) |
paul@1436 | 497 | until = None |
paul@1436 | 498 | inclusive = False |
paul@1436 | 499 | |
paul@1436 | 500 | # Generate the periods from the rule. |
paul@1436 | 501 | |
paul@1436 | 502 | rule_periods = form_periods_from_periods( |
paul@1436 | 503 | get_periods_using_selector(selector, main_period, tzid, |
paul@1436 | 504 | start, end, inclusive)) |
paul@1436 | 505 | |
paul@1436 | 506 | # Retain any applicable replacement periods that either modify or cancel |
paul@1436 | 507 | # rule periods. |
paul@1436 | 508 | # NOTE: To be done. |
paul@1436 | 509 | |
paul@1436 | 510 | self.state.set("periods", rule_periods) |
paul@1436 | 511 | |
paul@1375 | 512 | # Editing methods involving interaction. |
paul@1375 | 513 | |
paul@1436 | 514 | def add_rule_selectors(self): |
paul@1436 | 515 | |
paul@1436 | 516 | "Add rule selectors to the rule details." |
paul@1436 | 517 | |
paul@1436 | 518 | selectors = self.state.get("rule") |
paul@1436 | 519 | |
paul@1436 | 520 | while True: |
paul@1436 | 521 | |
paul@1436 | 522 | # Obtain a command from any arguments. |
paul@1436 | 523 | |
paul@1436 | 524 | s = read_input("Selector: (c)ount, (f)requency, (s)election (or return)> ") |
paul@1436 | 525 | args = s.split() |
paul@1436 | 526 | cmd = next_arg(args) |
paul@1436 | 527 | |
paul@1436 | 528 | if cmd in ("c", "count", "limit"): |
paul@1436 | 529 | add_rule_selector_count(selectors, args) |
paul@1436 | 530 | elif cmd in ("f", "freq", "frequency"): |
paul@1436 | 531 | add_rule_selector_frequency(selectors, args) |
paul@1436 | 532 | elif cmd in ("s", "select", "selection"): |
paul@1436 | 533 | add_rule_selector_selection(selectors, args) |
paul@1436 | 534 | |
paul@1436 | 535 | # Remain in the loop unless explicitly terminated. |
paul@1436 | 536 | |
paul@1436 | 537 | elif not cmd or cmd == "end": |
paul@1436 | 538 | break |
paul@1436 | 539 | |
paul@1375 | 540 | def edit_attendee(self, index): |
paul@1375 | 541 | |
paul@1375 | 542 | "Edit the attendee at 'index'." |
paul@1375 | 543 | |
paul@1375 | 544 | t = self.can_edit_attendee(index) |
paul@1375 | 545 | if t: |
paul@1375 | 546 | attendees = self.state.get("attendees") |
paul@1375 | 547 | attendee, attr = t |
paul@1375 | 548 | del attendees[attendee] |
paul@1375 | 549 | attendee = input_with_default("Attendee (%s)? ", attendee) |
paul@1375 | 550 | attendees[attendee] = attr |
paul@1375 | 551 | |
paul@1375 | 552 | def edit_period(self, index, args=None): |
paul@1436 | 553 | |
paul@1436 | 554 | "Edit the period at 'index'." |
paul@1436 | 555 | |
paul@1375 | 556 | period = self.can_edit_period(index) |
paul@1375 | 557 | if period: |
paul@1375 | 558 | edit_period(period, args) |
paul@1375 | 559 | period.cancelled = False |
paul@1422 | 560 | |
paul@1422 | 561 | # Change the origin of modified rule periods. |
paul@1422 | 562 | |
paul@1422 | 563 | if period.origin == "RRULE": |
paul@1422 | 564 | period.origin = "RDATE" |
paul@1375 | 565 | |
paul@1375 | 566 | # Sort the periods after this change. |
paul@1375 | 567 | |
paul@1375 | 568 | periods = self.state.get("periods") |
paul@1375 | 569 | periods.sort() |
paul@1375 | 570 | |
paul@1436 | 571 | def edit_rule_selector(self, index, args): |
paul@1436 | 572 | |
paul@1436 | 573 | "Edit the selector having the given 'index'." |
paul@1436 | 574 | |
paul@1436 | 575 | selectors = self.state.get("rule") |
paul@1436 | 576 | selector = self.can_edit_rule_selector(index) |
paul@1436 | 577 | |
paul@1436 | 578 | if not selector: |
paul@1436 | 579 | return |
paul@1436 | 580 | |
paul@1436 | 581 | while True: |
paul@1436 | 582 | show_rule_selector(selector) |
paul@1436 | 583 | |
paul@1436 | 584 | # Obtain a command from any arguments. |
paul@1436 | 585 | |
paul@1436 | 586 | cmd = next_arg(args) |
paul@1436 | 587 | if not cmd: |
paul@1436 | 588 | s = read_input("Selector: (e)dit, (r)emove (or return)> ") |
paul@1436 | 589 | args = s.split() |
paul@1436 | 590 | cmd = next_arg(args) |
paul@1436 | 591 | |
paul@1436 | 592 | # Edit an existing selector. |
paul@1436 | 593 | |
paul@1436 | 594 | if cmd in ("e", "edit"): |
paul@1436 | 595 | if isinstance(selector, vRecurrence.LimitSelector): |
paul@1436 | 596 | add_rule_selector_count(selectors, args, selector) |
paul@1436 | 597 | elif isinstance(selector, vRecurrence.Pattern): |
paul@1436 | 598 | add_rule_selector_frequency(selectors, args, selector) |
paul@1436 | 599 | else: |
paul@1436 | 600 | add_rule_selector_selection(selectors, args, selector) |
paul@1436 | 601 | |
paul@1436 | 602 | # Remove an existing selector. |
paul@1436 | 603 | |
paul@1436 | 604 | elif cmd in ("r", "remove"): |
paul@1436 | 605 | del selectors[index] |
paul@1436 | 606 | |
paul@1436 | 607 | # Exit if requested or after a successful |
paul@1436 | 608 | # operation. |
paul@1436 | 609 | |
paul@1436 | 610 | elif not cmd: |
paul@1436 | 611 | pass |
paul@1436 | 612 | else: |
paul@1436 | 613 | continue |
paul@1436 | 614 | break |
paul@1436 | 615 | |
paul@1375 | 616 | def edit_summary(self, summary=None): |
paul@1436 | 617 | |
paul@1436 | 618 | "Edit or set the 'summary'." |
paul@1436 | 619 | |
paul@1375 | 620 | if self.can_edit_properties(): |
paul@1375 | 621 | if not summary: |
paul@1375 | 622 | summary = input_with_default("Summary (%s)? ", self.state.get("summary")) |
paul@1375 | 623 | self.state.set("summary", summary) |
paul@1375 | 624 | |
paul@1375 | 625 | def finish(self): |
paul@1436 | 626 | |
paul@1436 | 627 | "Finish editing, warning of errors if any occur." |
paul@1436 | 628 | |
paul@1375 | 629 | try: |
paul@1375 | 630 | EditingClient.finish(self) |
paul@1375 | 631 | except PeriodError: |
paul@1375 | 632 | print "Errors exist in the periods." |
paul@1375 | 633 | return |
paul@1375 | 634 | |
paul@1375 | 635 | # Diagnostic methods. |
paul@1375 | 636 | |
paul@1375 | 637 | def show_period_classification(self): |
paul@1436 | 638 | |
paul@1436 | 639 | "Show the classification of the periods." |
paul@1436 | 640 | |
paul@1375 | 641 | try: |
paul@1375 | 642 | new, replaced, retained, cancelled, obsolete = self.classify_periods() |
paul@1375 | 643 | show_period_classification(new, replaced, retained, cancelled, obsolete) |
paul@1375 | 644 | except PeriodError: |
paul@1375 | 645 | print |
paul@1375 | 646 | print "Errors exist in the periods." |
paul@1375 | 647 | |
paul@1375 | 648 | def show_changes(self): |
paul@1436 | 649 | |
paul@1436 | 650 | "Show how the periods have changed." |
paul@1436 | 651 | |
paul@1375 | 652 | try: |
paul@1375 | 653 | modified, unmodified, removed = self.classify_period_changes() |
paul@1375 | 654 | show_changes(modified, unmodified, removed) |
paul@1375 | 655 | except PeriodError: |
paul@1375 | 656 | print "Errors exist in the periods." |
paul@1375 | 657 | |
paul@1375 | 658 | is_changed = self.properties_changed() |
paul@1375 | 659 | if is_changed: |
paul@1375 | 660 | print |
paul@1375 | 661 | print "Properties changed:", ", ".join(is_changed) |
paul@1375 | 662 | new, modified, unmodified, removed = self.classify_attendee_changes() |
paul@1375 | 663 | show_attendee_changes(new, modified, unmodified, removed) |
paul@1375 | 664 | |
paul@1375 | 665 | def show_operations(self): |
paul@1436 | 666 | |
paul@1436 | 667 | "Show the operations required to change the periods for recipients." |
paul@1436 | 668 | |
paul@1375 | 669 | is_changed = self.properties_changed() |
paul@1375 | 670 | |
paul@1375 | 671 | try: |
paul@1375 | 672 | to_unschedule, to_reschedule, to_add, to_exclude, to_set, \ |
paul@1375 | 673 | all_unscheduled, all_rescheduled = self.classify_period_operations() |
paul@1375 | 674 | show_period_operations(to_unschedule, to_reschedule, to_add, |
paul@1375 | 675 | to_exclude, to_set, |
paul@1375 | 676 | all_unscheduled, all_rescheduled) |
paul@1375 | 677 | except PeriodError: |
paul@1375 | 678 | print "Errors exist in the periods." |
paul@1375 | 679 | |
paul@1375 | 680 | to_invite, to_cancel, to_modify = self.classify_attendee_operations() |
paul@1375 | 681 | show_attendee_operations(to_invite, to_cancel, to_modify) |
paul@1375 | 682 | |
paul@1375 | 683 | # Output methods. |
paul@1375 | 684 | |
paul@1375 | 685 | def show_message(self, message, plain=False, filename=None): |
paul@1436 | 686 | |
paul@1436 | 687 | """ |
paul@1436 | 688 | Show the given mail 'message', decoding to plain text if 'plain' is set |
paul@1436 | 689 | to a true value, writing it to 'filename' if indicated. |
paul@1436 | 690 | """ |
paul@1436 | 691 | |
paul@1375 | 692 | if plain: |
paul@1375 | 693 | decode_part(message) |
paul@1375 | 694 | write(message_as_string(message), filename) |
paul@1375 | 695 | |
paul@1375 | 696 | def show_cancel_message(self, plain=False, filename=None): |
paul@1375 | 697 | |
paul@1375 | 698 | "Show the cancel message for uninvited attendees." |
paul@1375 | 699 | |
paul@1375 | 700 | message = self.prepare_cancel_message() |
paul@1375 | 701 | if message: |
paul@1375 | 702 | self.show_message(message, plain, filename) |
paul@1375 | 703 | |
paul@1375 | 704 | def show_publish_message(self, plain=False, filename=None): |
paul@1375 | 705 | |
paul@1375 | 706 | "Show the publishing message for the updated event." |
paul@1375 | 707 | |
paul@1375 | 708 | message = self.prepare_publish_message() |
paul@1375 | 709 | self.show_message(message, plain, filename) |
paul@1375 | 710 | |
paul@1375 | 711 | def show_update_message(self, plain=False, filename=None): |
paul@1375 | 712 | |
paul@1375 | 713 | "Show the update message for the updated event." |
paul@1375 | 714 | |
paul@1375 | 715 | message = self.prepare_update_message() |
paul@1375 | 716 | if message: |
paul@1375 | 717 | self.show_message(message, plain, filename) |
paul@1375 | 718 | |
paul@1375 | 719 | # General display methods. |
paul@1375 | 720 | |
paul@1375 | 721 | def show_object(self): |
paul@1375 | 722 | print |
paul@1387 | 723 | print_title("Object details") |
paul@1387 | 724 | print |
paul@1375 | 725 | print "Summary:", self.state.get("summary") |
paul@1375 | 726 | print |
paul@1375 | 727 | print "Organiser:", self.state.get("organiser") |
paul@1375 | 728 | self.show_attendees() |
paul@1375 | 729 | self.show_periods() |
paul@1436 | 730 | self.show_rule() |
paul@1375 | 731 | self.show_suggested_attendees() |
paul@1375 | 732 | self.show_suggested_periods() |
paul@1375 | 733 | self.show_conflicting_periods() |
paul@1436 | 734 | print |
paul@1436 | 735 | print "Object is", self.obj.is_shared() and "shared" or "not shared" |
paul@1375 | 736 | |
paul@1375 | 737 | def show_attendees(self): |
paul@1375 | 738 | print |
paul@1375 | 739 | print_title("Attendees") |
paul@1375 | 740 | attendees = self.state.get("attendees") |
paul@1375 | 741 | for index, attendee_item in enumerate(attendees.items()): |
paul@1375 | 742 | show_attendee(attendee_item, index) |
paul@1375 | 743 | |
paul@1375 | 744 | def show_periods(self): |
paul@1375 | 745 | print |
paul@1375 | 746 | print_title("Periods") |
paul@1375 | 747 | show_periods(self.state.get("periods"), self.state.get("period_errors")) |
paul@1436 | 748 | |
paul@1436 | 749 | def show_rule(self): |
paul@1436 | 750 | selectors = self.state.get("rule") |
paul@1436 | 751 | if selectors: |
paul@1436 | 752 | print |
paul@1436 | 753 | print_title("Period recurrence rule") |
paul@1436 | 754 | show_rule(selectors) |
paul@1375 | 755 | |
paul@1375 | 756 | def show_suggested_attendees(self): |
paul@1375 | 757 | current_attendee = None |
paul@1375 | 758 | for index, (attendee, suggested_item) in enumerate(self.state.get("suggested_attendees")): |
paul@1375 | 759 | if attendee != current_attendee: |
paul@1375 | 760 | print |
paul@1375 | 761 | print_title("Attendees suggested by %s" % attendee) |
paul@1375 | 762 | current_attendee = attendee |
paul@1375 | 763 | show_attendee(suggested_item, index) |
paul@1375 | 764 | |
paul@1375 | 765 | def show_suggested_periods(self): |
paul@1375 | 766 | periods = self.state.get("suggested_periods") |
paul@1375 | 767 | current_attendee = None |
paul@1375 | 768 | index = 0 |
paul@1375 | 769 | for attendee, period, operation in periods: |
paul@1375 | 770 | if attendee != current_attendee: |
paul@1375 | 771 | print |
paul@1375 | 772 | print_title("Periods suggested by %s" % attendee) |
paul@1375 | 773 | current_attendee = attendee |
paul@1375 | 774 | show_period(period, index) |
paul@1375 | 775 | print " %s" % (operation == "add" and "Add this period" or "Remove this period") |
paul@1375 | 776 | index += 1 |
paul@1375 | 777 | |
paul@1375 | 778 | def show_conflicting_periods(self): |
paul@1375 | 779 | conflicts = self.get_conflicting_periods() |
paul@1375 | 780 | if not conflicts: |
paul@1375 | 781 | return |
paul@1375 | 782 | print |
paul@1375 | 783 | print_title("Conflicting periods") |
paul@1375 | 784 | |
paul@1375 | 785 | conflicts = list(conflicts) |
paul@1375 | 786 | conflicts.sort() |
paul@1375 | 787 | |
paul@1375 | 788 | for p in conflicts: |
paul@1375 | 789 | print p.summary, p.uid, p.get_start(), p.get_end() |
paul@1375 | 790 | |
paul@1375 | 791 | # Interaction functions. |
paul@1375 | 792 | |
paul@1375 | 793 | def expand_arg(args): |
paul@1436 | 794 | |
paul@1436 | 795 | """ |
paul@1436 | 796 | Expand the first argument in 'args' to a pair of arguments if having the |
paul@1436 | 797 | form <char><digit>... |
paul@1436 | 798 | """ |
paul@1436 | 799 | |
paul@1375 | 800 | if args[0] and args[0][1:].isdigit(): |
paul@1375 | 801 | args[:1] = [args[0][0], args[0][1:]] |
paul@1375 | 802 | |
paul@1436 | 803 | def get_text_arg(s): |
paul@1436 | 804 | |
paul@1436 | 805 | """ |
paul@1436 | 806 | Split 's' after the first whitespace occurrence, returning the remaining |
paul@1436 | 807 | text or None if no such text exists. |
paul@1436 | 808 | """ |
paul@1436 | 809 | |
paul@1436 | 810 | return (s.split(None, 1)[1:] or [None])[0] |
paul@1375 | 811 | |
paul@1375 | 812 | def next_arg(args): |
paul@1436 | 813 | |
paul@1436 | 814 | """ |
paul@1436 | 815 | Return the first argument from 'args', removing it, or return None if no |
paul@1436 | 816 | arguments are left. |
paul@1436 | 817 | """ |
paul@1436 | 818 | |
paul@1375 | 819 | if args: |
paul@1375 | 820 | arg = args[0] |
paul@1375 | 821 | del args[0] |
paul@1375 | 822 | return arg |
paul@1375 | 823 | return None |
paul@1375 | 824 | |
paul@1436 | 825 | # Editing functions. |
paul@1436 | 826 | |
paul@1375 | 827 | def edit_period(period, args=None): |
paul@1375 | 828 | |
paul@1375 | 829 | "Edit the given 'period'." |
paul@1375 | 830 | |
paul@1375 | 831 | print "Editing start (%s)" % period.get_start() |
paul@1375 | 832 | edit_date(period.start, args) |
paul@1375 | 833 | print "Editing end (%s)" % period.get_end() |
paul@1375 | 834 | edit_date(period.end, args) |
paul@1375 | 835 | |
paul@1375 | 836 | def edit_date(date, args=None): |
paul@1375 | 837 | |
paul@1375 | 838 | "Edit the given 'date' object attributes." |
paul@1375 | 839 | |
paul@1375 | 840 | date.date = next_arg(args) or input_with_default("Date (%s)? ", date.date) |
paul@1375 | 841 | date.hour = next_arg(args) or input_with_default("Hour (%s)? ", date.hour) |
paul@1375 | 842 | date.minute = next_arg(args) or input_with_default("Minute (%s)? ", date.minute) |
paul@1375 | 843 | date.second = next_arg(args) or input_with_default("Second (%s)? ", date.second) |
paul@1375 | 844 | date.tzid = next_arg(args) or input_with_default("Time zone (%s)? ", date.tzid) |
paul@1375 | 845 | date.reset() |
paul@1375 | 846 | |
paul@1436 | 847 | def add_rule_selector_count(selectors, args, selector=None): |
paul@1436 | 848 | |
paul@1436 | 849 | "Add to 'selectors' a selector imposing a count restriction." |
paul@1436 | 850 | |
paul@1436 | 851 | while True: |
paul@1436 | 852 | arg = next_arg(args) |
paul@1436 | 853 | if not arg: |
paul@1436 | 854 | if selector: |
paul@1436 | 855 | arg = input_with_default("Number of occurrences (%d)? ", |
paul@1436 | 856 | selector.get_limit()) |
paul@1436 | 857 | else: |
paul@1436 | 858 | arg = read_input("Number of occurrences? ") |
paul@1436 | 859 | |
paul@1436 | 860 | count = to_int_or_none(arg) |
paul@1436 | 861 | |
paul@1436 | 862 | if count is None: |
paul@1436 | 863 | arg = None |
paul@1436 | 864 | continue |
paul@1436 | 865 | |
paul@1436 | 866 | # Change or add selector. |
paul@1436 | 867 | |
paul@1436 | 868 | selector = selector or selectors and \ |
paul@1436 | 869 | isinstance(selectors[0], vRecurrence.LimitSelector) and \ |
paul@1436 | 870 | selectors[0] or None |
paul@1436 | 871 | |
paul@1436 | 872 | if not selector: |
paul@1436 | 873 | selector = vRecurrence.new_selector("COUNT") |
paul@1436 | 874 | selectors.insert(0, selector) |
paul@1436 | 875 | |
paul@1436 | 876 | selector.set_limit(count) |
paul@1436 | 877 | break |
paul@1436 | 878 | |
paul@1436 | 879 | def add_rule_selector_frequency(selectors, args, selector=None): |
paul@1436 | 880 | |
paul@1436 | 881 | "Add to 'selectors' a selector for a frequency." |
paul@1436 | 882 | |
paul@1436 | 883 | while not selector: |
paul@1436 | 884 | arg = next_arg(args) |
paul@1436 | 885 | if not arg: |
paul@1436 | 886 | arg = read_input("Select (y)early, (M)onthly, (w)eekly, (d)aily, " |
paul@1436 | 887 | "(h)ourly, (m)inutely, (s)econdly (or return)? ") |
paul@1436 | 888 | |
paul@1436 | 889 | if not arg: |
paul@1436 | 890 | return |
paul@1436 | 891 | |
paul@1436 | 892 | arg_lower = arg.lower() |
paul@1436 | 893 | |
paul@1436 | 894 | if arg_lower in ("y", "year", "yearly"): |
paul@1436 | 895 | qualifier = "YEARLY" |
paul@1436 | 896 | elif arg == "M" or arg_lower in ("month", "monthly"): |
paul@1436 | 897 | qualifier = "MONTHLY" |
paul@1436 | 898 | elif arg_lower in ("w", "week", "weekly"): |
paul@1436 | 899 | qualifier = "WEEKLY" |
paul@1436 | 900 | elif arg_lower in ("d", "day", "daily"): |
paul@1436 | 901 | qualifier = "DAILY" |
paul@1436 | 902 | elif arg_lower in ("h", "hour", "hourly"): |
paul@1436 | 903 | qualifier = "HOURLY" |
paul@1436 | 904 | elif arg_lower in ("m", "minute", "minutely"): |
paul@1436 | 905 | qualifier = "MINUTELY" |
paul@1436 | 906 | elif arg_lower in ("s", "second", "secondly"): |
paul@1436 | 907 | qualifier = "SECONDLY" |
paul@1436 | 908 | else: |
paul@1436 | 909 | continue |
paul@1436 | 910 | |
paul@1436 | 911 | break |
paul@1436 | 912 | |
paul@1436 | 913 | while True: |
paul@1436 | 914 | arg = next_arg(args) |
paul@1436 | 915 | if not arg: |
paul@1436 | 916 | if selector: |
paul@1436 | 917 | arg = input_with_default("Interval (%d)? ", |
paul@1436 | 918 | selector.get_interval()) |
paul@1436 | 919 | else: |
paul@1436 | 920 | arg = input_with_default("Interval (%d)? ", 1) |
paul@1436 | 921 | |
paul@1436 | 922 | interval = to_int_or_none(arg) |
paul@1436 | 923 | |
paul@1436 | 924 | if interval is None: |
paul@1436 | 925 | arg = None |
paul@1436 | 926 | else: |
paul@1436 | 927 | break |
paul@1436 | 928 | |
paul@1436 | 929 | # Update an existing selector. |
paul@1436 | 930 | |
paul@1436 | 931 | if selector: |
paul@1436 | 932 | selector.set_interval(interval) |
paul@1436 | 933 | return |
paul@1436 | 934 | |
paul@1436 | 935 | # Create a new selector. |
paul@1436 | 936 | |
paul@1436 | 937 | selector = vRecurrence.new_selector(qualifier) |
paul@1436 | 938 | selector.set_interval(interval) |
paul@1436 | 939 | |
paul@1436 | 940 | # Remove any existing frequency selector. |
paul@1436 | 941 | |
paul@1436 | 942 | for index, _selector in enumerate(selectors): |
paul@1436 | 943 | if isinstance(_selector, vRecurrence.Pattern): |
paul@1436 | 944 | del selectors[index] |
paul@1436 | 945 | break |
paul@1436 | 946 | |
paul@1436 | 947 | # Add the new selector and keep the selectors in order. |
paul@1436 | 948 | |
paul@1436 | 949 | selectors.append(selector) |
paul@1436 | 950 | vRecurrence.sort_selectors(selectors) |
paul@1436 | 951 | |
paul@1436 | 952 | def add_rule_selector_selection(selectors, args, selector=None): |
paul@1436 | 953 | |
paul@1436 | 954 | "Add to 'selectors' a selector for a particular point in time." |
paul@1436 | 955 | |
paul@1436 | 956 | qualifier = selector and selector.qualifier or None |
paul@1436 | 957 | |
paul@1436 | 958 | while not selector: |
paul@1436 | 959 | arg = next_arg(args) |
paul@1436 | 960 | if not arg: |
paul@1436 | 961 | arg = read_input("Select (M)onths, (w)eeks, (y)eardays, " |
paul@1436 | 962 | "m(o)nthdays, week(d)ays, (h)ours, (m)inutes, " |
paul@1436 | 963 | "(s)econds (or return)? ") |
paul@1436 | 964 | |
paul@1436 | 965 | if not arg: |
paul@1436 | 966 | return |
paul@1436 | 967 | |
paul@1436 | 968 | arg_lower = arg.lower() |
paul@1436 | 969 | |
paul@1436 | 970 | if arg == "M" or arg_lower in ("month", "months"): |
paul@1436 | 971 | qualifier = "BYMONTH" |
paul@1436 | 972 | elif arg_lower in ("w", "week", "weeks"): |
paul@1436 | 973 | qualifier = "BYWEEKNO" |
paul@1436 | 974 | elif arg_lower in ("y", "yearday", "yeardays"): |
paul@1436 | 975 | qualifier = "BYYEARDAY" |
paul@1436 | 976 | elif arg_lower in ("o", "monthday", "monthdays"): |
paul@1436 | 977 | qualifier = "BYMONTHDAY" |
paul@1436 | 978 | elif arg_lower in ("d", "weekday", "weekdays"): |
paul@1436 | 979 | qualifier = "BYDAY" |
paul@1436 | 980 | elif arg_lower in ("h", "hour", "hours"): |
paul@1436 | 981 | qualifier = "BYHOUR" |
paul@1436 | 982 | elif arg_lower in ("m", "minute", "minutes"): |
paul@1436 | 983 | qualifier = "BYMINUTE" |
paul@1436 | 984 | elif arg_lower in ("s", "second", "seconds"): |
paul@1436 | 985 | qualifier = "BYSECOND" |
paul@1436 | 986 | else: |
paul@1436 | 987 | continue |
paul@1436 | 988 | |
paul@1436 | 989 | break |
paul@1436 | 990 | |
paul@1436 | 991 | if not qualifier: |
paul@1436 | 992 | return |
paul@1436 | 993 | |
paul@1436 | 994 | ranges = vRecurrence.get_value_ranges(qualifier) |
paul@1436 | 995 | ranges_str = format_value_ranges(ranges[0]) |
paul@1436 | 996 | |
paul@1436 | 997 | values = [] |
paul@1436 | 998 | |
paul@1436 | 999 | while True: |
paul@1436 | 1000 | arg = next_arg(args) |
paul@1436 | 1001 | if not arg: |
paul@1436 | 1002 | arg = read_input("Value (%s) (return to end)? " % ranges_str) |
paul@1436 | 1003 | |
paul@1436 | 1004 | # Stop if no more arguments. |
paul@1436 | 1005 | |
paul@1436 | 1006 | if not arg or arg == "end": |
paul@1436 | 1007 | break |
paul@1436 | 1008 | |
paul@1436 | 1009 | # Handle weekdays. |
paul@1436 | 1010 | |
paul@1436 | 1011 | if qualifier == "BYDAY": |
paul@1436 | 1012 | value = arg.upper() # help to match weekdays |
paul@1436 | 1013 | |
paul@1436 | 1014 | arg = next_arg(args) |
paul@1436 | 1015 | if not arg: |
paul@1436 | 1016 | arg = read_input("Occurrence within a month? ") |
paul@1436 | 1017 | |
paul@1436 | 1018 | index = to_int_or_none(arg) |
paul@1436 | 1019 | value = vRecurrence.check_values(qualifier, [value, index]) |
paul@1436 | 1020 | |
paul@1436 | 1021 | # Handle all other values. |
paul@1436 | 1022 | |
paul@1436 | 1023 | else: |
paul@1436 | 1024 | value = to_int_or_none(arg) |
paul@1436 | 1025 | l = vRecurrence.check_values(qualifier, [value]) |
paul@1436 | 1026 | value = l and l[0] |
paul@1436 | 1027 | |
paul@1436 | 1028 | # Append valid values. |
paul@1436 | 1029 | |
paul@1436 | 1030 | if value is not None: |
paul@1436 | 1031 | values.append(value) |
paul@1436 | 1032 | else: |
paul@1436 | 1033 | print "Value not recognised." |
paul@1436 | 1034 | |
paul@1436 | 1035 | if not values: |
paul@1436 | 1036 | return |
paul@1436 | 1037 | |
paul@1436 | 1038 | # Update an existing selector. |
paul@1436 | 1039 | |
paul@1436 | 1040 | if selector: |
paul@1436 | 1041 | selector.set_values(values) |
paul@1436 | 1042 | return |
paul@1436 | 1043 | |
paul@1436 | 1044 | # Create a new selector. |
paul@1436 | 1045 | |
paul@1436 | 1046 | selector = vRecurrence.new_selector(qualifier) |
paul@1436 | 1047 | selector.set_values(values) |
paul@1436 | 1048 | |
paul@1436 | 1049 | # Remove any existing selector. |
paul@1436 | 1050 | |
paul@1436 | 1051 | for index, _selector in enumerate(selectors): |
paul@1436 | 1052 | if _selector.qualifier == selector.qualifier: |
paul@1436 | 1053 | del selectors[index] |
paul@1436 | 1054 | break |
paul@1436 | 1055 | |
paul@1436 | 1056 | # Add the new selector and keep the selectors in order. |
paul@1436 | 1057 | |
paul@1436 | 1058 | selectors.append(selector) |
paul@1436 | 1059 | vRecurrence.sort_selectors(selectors) |
paul@1436 | 1060 | |
paul@1375 | 1061 | def select_object(cl, objects): |
paul@1436 | 1062 | |
paul@1436 | 1063 | "Select using 'cl' an object from the given 'objects'." |
paul@1436 | 1064 | |
paul@1375 | 1065 | print |
paul@1387 | 1066 | |
paul@1387 | 1067 | if objects: |
paul@1387 | 1068 | label = "Select object number or (n)ew object or (q)uit> " |
paul@1387 | 1069 | else: |
paul@1387 | 1070 | label = "Select (n)ew object or (q)uit> " |
paul@1387 | 1071 | |
paul@1375 | 1072 | while True: |
paul@1375 | 1073 | try: |
paul@1387 | 1074 | cmd = read_input(label) |
paul@1375 | 1075 | except EOFError: |
paul@1375 | 1076 | return None |
paul@1375 | 1077 | |
paul@1375 | 1078 | if cmd.isdigit(): |
paul@1436 | 1079 | index = to_int_or_none(cmd) |
paul@1436 | 1080 | |
paul@1436 | 1081 | if index is not None and 0 <= index < len(objects): |
paul@1375 | 1082 | obj = objects[index] |
paul@1375 | 1083 | return cl.load_object(obj.get_uid(), obj.get_recurrenceid()) |
paul@1387 | 1084 | |
paul@1375 | 1085 | elif cmd in ("n", "new"): |
paul@1375 | 1086 | return cl.new_object() |
paul@1375 | 1087 | elif cmd in ("q", "quit", "exit"): |
paul@1375 | 1088 | return None |
paul@1375 | 1089 | |
paul@1387 | 1090 | def show_commands(): |
paul@1436 | 1091 | |
paul@1436 | 1092 | "Show editing and inspection commands." |
paul@1436 | 1093 | |
paul@1375 | 1094 | print |
paul@1375 | 1095 | print_title("Editing commands") |
paul@1375 | 1096 | print |
paul@1375 | 1097 | print """\ |
paul@1375 | 1098 | a [ <uri> ] |
paul@1375 | 1099 | attendee [ <uri> ] |
paul@1375 | 1100 | Add attendee |
paul@1375 | 1101 | |
paul@1375 | 1102 | A, attend, attendance |
paul@1375 | 1103 | Change attendance/participation |
paul@1375 | 1104 | |
paul@1375 | 1105 | a<digit> |
paul@1375 | 1106 | attendee <digit> |
paul@1375 | 1107 | Select attendee from list |
paul@1375 | 1108 | |
paul@1375 | 1109 | as<digit> |
paul@1375 | 1110 | Add suggested attendee from list |
paul@1375 | 1111 | |
paul@1375 | 1112 | f, finish |
paul@1375 | 1113 | Finish editing, confirming changes, proceeding to messaging |
paul@1375 | 1114 | |
paul@1375 | 1115 | h, help, ? |
paul@1375 | 1116 | Show this help message |
paul@1375 | 1117 | |
paul@1375 | 1118 | l, list, show |
paul@1375 | 1119 | List/show all event details |
paul@1375 | 1120 | |
paul@1436 | 1121 | p, period [ new ] |
paul@1375 | 1122 | Add new period |
paul@1375 | 1123 | |
paul@1375 | 1124 | p<digit> |
paul@1375 | 1125 | period <digit> |
paul@1375 | 1126 | Select period from list |
paul@1375 | 1127 | |
paul@1375 | 1128 | ps<digit> |
paul@1375 | 1129 | Add or remove suggested period from list |
paul@1375 | 1130 | |
paul@1375 | 1131 | q, quit, exit |
paul@1375 | 1132 | Exit/quit this program |
paul@1375 | 1133 | |
paul@1375 | 1134 | r, reload, reset, restart |
paul@1375 | 1135 | Reset event periods (return to editing mode, if already finished) |
paul@1375 | 1136 | |
paul@1436 | 1137 | rule, rrule |
paul@1436 | 1138 | Add a period recurrence rule |
paul@1436 | 1139 | |
paul@1436 | 1140 | rule <digit> |
paul@1436 | 1141 | rrule <digit> |
paul@1436 | 1142 | Select period recurrence rule selector from list |
paul@1396 | 1143 | |
paul@1375 | 1144 | s, summary |
paul@1375 | 1145 | Set event summary |
paul@1375 | 1146 | """ |
paul@1375 | 1147 | |
paul@1387 | 1148 | print_title("Messaging commands") |
paul@1387 | 1149 | print |
paul@1387 | 1150 | print """\ |
paul@1387 | 1151 | S, send |
paul@1387 | 1152 | Send messages to recipients and to self, if appropriate |
paul@1387 | 1153 | """ |
paul@1387 | 1154 | |
paul@1375 | 1155 | print_title("Diagnostic commands") |
paul@1375 | 1156 | print |
paul@1375 | 1157 | print """\ |
paul@1375 | 1158 | c, class, classification |
paul@1375 | 1159 | Show period classification |
paul@1375 | 1160 | |
paul@1375 | 1161 | C, changes |
paul@1375 | 1162 | Show changes made by editing |
paul@1375 | 1163 | |
paul@1375 | 1164 | o, ops, operations |
paul@1375 | 1165 | Show update operations |
paul@1375 | 1166 | |
paul@1375 | 1167 | RECURRENCE-ID [ <filename> ] |
paul@1375 | 1168 | Show event recurrence identifier, writing to <filename> if specified |
paul@1375 | 1169 | |
paul@1375 | 1170 | UID [ <filename> ] |
paul@1375 | 1171 | Show event unique identifier, writing to <filename> if specified |
paul@1375 | 1172 | """ |
paul@1375 | 1173 | |
paul@1387 | 1174 | print_title("Message inspection commands") |
paul@1375 | 1175 | print |
paul@1375 | 1176 | print """\ |
paul@1375 | 1177 | P [ <filename> ] |
paul@1375 | 1178 | publish [ <filename> ] |
paul@1375 | 1179 | Show publishing message, writing to <filename> if specified |
paul@1375 | 1180 | |
paul@1375 | 1181 | R [ <filename> ] |
paul@1375 | 1182 | remove [ <filename> ] |
paul@1375 | 1183 | cancel [ <filename> ] |
paul@1375 | 1184 | Show cancellation message sent to uninvited/removed recipients, writing to |
paul@1375 | 1185 | <filename> if specified |
paul@1375 | 1186 | |
paul@1375 | 1187 | U [ <filename> ] |
paul@1375 | 1188 | update [ <filename> ] |
paul@1375 | 1189 | Show update message, writing to <filename> if specified |
paul@1375 | 1190 | """ |
paul@1375 | 1191 | |
paul@1387 | 1192 | def edit_object(cl, obj, handle_outgoing=False): |
paul@1436 | 1193 | |
paul@1436 | 1194 | """ |
paul@1436 | 1195 | Edit using 'cl' the given object 'obj'. If 'handle_outgoing' is specified |
paul@1436 | 1196 | and set to a true value, the details from outgoing messages are incorporated |
paul@1436 | 1197 | into the stored data. |
paul@1436 | 1198 | """ |
paul@1436 | 1199 | |
paul@1375 | 1200 | cl.show_object() |
paul@1375 | 1201 | print |
paul@1375 | 1202 | |
paul@1375 | 1203 | try: |
paul@1375 | 1204 | while True: |
paul@1375 | 1205 | role = cl.is_organiser() and "Organiser" or "Attendee" |
paul@1375 | 1206 | status = cl.state.get("finished") and " (editing complete)" or "" |
paul@1375 | 1207 | |
paul@1436 | 1208 | s = read_input("%s%s> " % (role, status)) |
paul@1436 | 1209 | args = s.split() |
paul@1375 | 1210 | |
paul@1375 | 1211 | if not args or not args[0]: |
paul@1375 | 1212 | continue |
paul@1375 | 1213 | |
paul@1436 | 1214 | # Expand short-form arguments. |
paul@1436 | 1215 | |
paul@1436 | 1216 | expand_arg(args) |
paul@1436 | 1217 | cmd = next_arg(args) |
paul@1436 | 1218 | |
paul@1375 | 1219 | # Check the status of the periods. |
paul@1375 | 1220 | |
paul@1375 | 1221 | if cmd in ("c", "class", "classification"): |
paul@1375 | 1222 | cl.show_period_classification() |
paul@1375 | 1223 | print |
paul@1375 | 1224 | |
paul@1375 | 1225 | elif cmd in ("C", "changes"): |
paul@1375 | 1226 | cl.show_changes() |
paul@1375 | 1227 | print |
paul@1375 | 1228 | |
paul@1375 | 1229 | # Finish editing. |
paul@1375 | 1230 | |
paul@1375 | 1231 | elif cmd in ("f", "finish"): |
paul@1375 | 1232 | cl.finish() |
paul@1375 | 1233 | |
paul@1375 | 1234 | # Help. |
paul@1375 | 1235 | |
paul@1375 | 1236 | elif cmd in ("h", "?", "help"): |
paul@1387 | 1237 | show_commands() |
paul@1375 | 1238 | |
paul@1375 | 1239 | # Show object details. |
paul@1375 | 1240 | |
paul@1375 | 1241 | elif cmd in ("l", "list", "show"): |
paul@1375 | 1242 | cl.show_object() |
paul@1375 | 1243 | print |
paul@1375 | 1244 | |
paul@1375 | 1245 | # Show the operations. |
paul@1375 | 1246 | |
paul@1375 | 1247 | elif cmd in ("o", "ops", "operations"): |
paul@1375 | 1248 | cl.show_operations() |
paul@1375 | 1249 | print |
paul@1375 | 1250 | |
paul@1375 | 1251 | # Quit or exit. |
paul@1375 | 1252 | |
paul@1375 | 1253 | elif cmd in ("q", "quit", "exit"): |
paul@1375 | 1254 | break |
paul@1375 | 1255 | |
paul@1375 | 1256 | # Restart editing. |
paul@1375 | 1257 | |
paul@1375 | 1258 | elif cmd in ("r", "reload", "reset", "restart"): |
paul@1375 | 1259 | obj = cl.load_object(obj.get_uid(), obj.get_recurrenceid()) |
paul@1375 | 1260 | if not obj: |
paul@1375 | 1261 | obj = cl.new_object() |
paul@1375 | 1262 | cl.reset() |
paul@1375 | 1263 | cl.show_object() |
paul@1375 | 1264 | print |
paul@1375 | 1265 | |
paul@1375 | 1266 | # Show UID details. |
paul@1375 | 1267 | |
paul@1436 | 1268 | elif cmd == "UID": |
paul@1436 | 1269 | filename = get_text_arg(s) |
paul@1375 | 1270 | write(obj.get_uid(), filename) |
paul@1375 | 1271 | |
paul@1436 | 1272 | elif cmd == "RECURRENCE-ID": |
paul@1436 | 1273 | filename = get_text_arg(s) |
paul@1375 | 1274 | write(obj.get_recurrenceid() or "", filename) |
paul@1375 | 1275 | |
paul@1375 | 1276 | # Post-editing operations. |
paul@1375 | 1277 | |
paul@1375 | 1278 | elif cl.state.get("finished"): |
paul@1375 | 1279 | |
paul@1375 | 1280 | # Show messages. |
paul@1375 | 1281 | |
paul@1436 | 1282 | if cmd in ("P", "publish"): |
paul@1436 | 1283 | filename = get_text_arg(s) |
paul@1375 | 1284 | cl.show_publish_message(plain=not filename, filename=filename) |
paul@1375 | 1285 | |
paul@1436 | 1286 | elif cmd in ("R", "remove", "cancel"): |
paul@1436 | 1287 | filename = get_text_arg(s) |
paul@1375 | 1288 | cl.show_cancel_message(plain=not filename, filename=filename) |
paul@1375 | 1289 | |
paul@1436 | 1290 | elif cmd in ("U", "update"): |
paul@1436 | 1291 | filename = get_text_arg(s) |
paul@1375 | 1292 | cl.show_update_message(plain=not filename, filename=filename) |
paul@1375 | 1293 | |
paul@1387 | 1294 | # Definitive finishing action. |
paul@1387 | 1295 | |
paul@1436 | 1296 | elif cmd in ("S", "send"): |
paul@1387 | 1297 | |
paul@1387 | 1298 | # Send update and cancellation messages. |
paul@1387 | 1299 | |
paul@1387 | 1300 | did_send = False |
paul@1387 | 1301 | |
paul@1387 | 1302 | message = cl.prepare_update_message() |
paul@1387 | 1303 | if message: |
paul@1387 | 1304 | cl.send_message(message, cl.get_recipients()) |
paul@1387 | 1305 | did_send = True |
paul@1387 | 1306 | |
paul@1387 | 1307 | to_cancel = cl.state.get("attendees_to_cancel") |
paul@1387 | 1308 | if to_cancel: |
paul@1387 | 1309 | message = cl.prepare_cancel_message() |
paul@1387 | 1310 | if message: |
paul@1387 | 1311 | cl.send_message(message, to_cancel) |
paul@1387 | 1312 | did_send = True |
paul@1387 | 1313 | |
paul@1387 | 1314 | # Process the object using the person outgoing handler. |
paul@1387 | 1315 | |
paul@1387 | 1316 | if handle_outgoing: |
paul@1436 | 1317 | cl.handle_outgoing_object() |
paul@1387 | 1318 | |
paul@1387 | 1319 | # Otherwise, send a message to self with the event details. |
paul@1387 | 1320 | |
paul@1387 | 1321 | else: |
paul@1387 | 1322 | message = cl.prepare_publish_message() |
paul@1387 | 1323 | if message: |
paul@1387 | 1324 | cl.send_message_to_self(message) |
paul@1387 | 1325 | did_send = True |
paul@1387 | 1326 | |
paul@1387 | 1327 | # Exit if sending occurred. |
paul@1387 | 1328 | |
paul@1387 | 1329 | if did_send: |
paul@1387 | 1330 | break |
paul@1387 | 1331 | else: |
paul@1387 | 1332 | print "No messages sent. Try making edits or exit manually." |
paul@1387 | 1333 | |
paul@1375 | 1334 | # Editing operations. |
paul@1375 | 1335 | |
paul@1375 | 1336 | elif not cl.state.get("finished"): |
paul@1375 | 1337 | |
paul@1375 | 1338 | # Add or edit attendee. |
paul@1375 | 1339 | |
paul@1436 | 1340 | if cmd in ("a", "attendee"): |
paul@1375 | 1341 | value = next_arg(args) |
paul@1436 | 1342 | index = to_int_or_none(value) |
paul@1375 | 1343 | |
paul@1436 | 1344 | if index is None: |
paul@1375 | 1345 | try: |
paul@1375 | 1346 | index = cl.find_attendee(value) |
paul@1375 | 1347 | except ValueError: |
paul@1375 | 1348 | index = None |
paul@1375 | 1349 | |
paul@1375 | 1350 | # Add an attendee. |
paul@1375 | 1351 | |
paul@1375 | 1352 | if index is None: |
paul@1375 | 1353 | cl.add_attendee(value) |
paul@1375 | 1354 | if not value: |
paul@1375 | 1355 | cl.edit_attendee(-1) |
paul@1375 | 1356 | |
paul@1375 | 1357 | # Edit attendee (using index). |
paul@1375 | 1358 | |
paul@1375 | 1359 | else: |
paul@1375 | 1360 | attendee_item = cl.can_remove_attendee(index) |
paul@1375 | 1361 | if attendee_item: |
paul@1375 | 1362 | while True: |
paul@1375 | 1363 | show_attendee(attendee_item, index) |
paul@1375 | 1364 | |
paul@1375 | 1365 | # Obtain a command from any arguments. |
paul@1375 | 1366 | |
paul@1375 | 1367 | cmd = next_arg(args) |
paul@1375 | 1368 | if not cmd: |
paul@1436 | 1369 | cmd = read_input("Attendee: (e)dit, (r)emove (or return)> ") |
paul@1375 | 1370 | if cmd in ("e", "edit"): |
paul@1375 | 1371 | cl.edit_attendee(index) |
paul@1375 | 1372 | elif cmd in ("r", "remove"): |
paul@1375 | 1373 | cl.remove_attendees([index]) |
paul@1436 | 1374 | |
paul@1436 | 1375 | # Exit if requested or after a successful |
paul@1436 | 1376 | # operation. |
paul@1436 | 1377 | |
paul@1375 | 1378 | elif not cmd: |
paul@1375 | 1379 | pass |
paul@1375 | 1380 | else: |
paul@1375 | 1381 | continue |
paul@1375 | 1382 | break |
paul@1375 | 1383 | |
paul@1375 | 1384 | cl.show_attendees() |
paul@1375 | 1385 | print |
paul@1375 | 1386 | |
paul@1375 | 1387 | # Add suggested attendee (using index). |
paul@1375 | 1388 | |
paul@1436 | 1389 | elif cmd in ("as", "attendee-suggested", "suggested-attendee"): |
paul@1436 | 1390 | value = next_arg(args) |
paul@1436 | 1391 | index = to_int_or_none(value) |
paul@1436 | 1392 | |
paul@1436 | 1393 | if index is not None: |
paul@1375 | 1394 | cl.add_suggested_attendee(index) |
paul@1436 | 1395 | |
paul@1375 | 1396 | cl.show_attendees() |
paul@1375 | 1397 | print |
paul@1375 | 1398 | |
paul@1375 | 1399 | # Edit attendance. |
paul@1375 | 1400 | |
paul@1436 | 1401 | elif cmd in ("A", "attend", "attendance"): |
paul@1375 | 1402 | |
paul@1375 | 1403 | if not cl.is_attendee() and cl.is_organiser(): |
paul@1375 | 1404 | cl.add_attendee(cl.user) |
paul@1375 | 1405 | |
paul@1375 | 1406 | # NOTE: Support delegation. |
paul@1375 | 1407 | |
paul@1375 | 1408 | if cl.can_edit_attendance(): |
paul@1375 | 1409 | while True: |
paul@1375 | 1410 | |
paul@1375 | 1411 | # Obtain a command from any arguments. |
paul@1375 | 1412 | |
paul@1375 | 1413 | cmd = next_arg(args) |
paul@1375 | 1414 | if not cmd: |
paul@1436 | 1415 | cmd = read_input("Attendance: (a)ccept, (d)ecline, (t)entative (or return)> ") |
paul@1375 | 1416 | if cmd in ("a", "accept", "accepted", "attend"): |
paul@1375 | 1417 | cl.edit_attendance("ACCEPTED") |
paul@1375 | 1418 | elif cmd in ("d", "decline", "declined"): |
paul@1375 | 1419 | cl.edit_attendance("DECLINED") |
paul@1375 | 1420 | elif cmd in ("t", "tentative"): |
paul@1375 | 1421 | cl.edit_attendance("TENTATIVE") |
paul@1436 | 1422 | |
paul@1436 | 1423 | # Exit if requested or after a successful operation. |
paul@1436 | 1424 | |
paul@1375 | 1425 | elif not cmd: |
paul@1375 | 1426 | pass |
paul@1375 | 1427 | else: |
paul@1375 | 1428 | continue |
paul@1375 | 1429 | break |
paul@1375 | 1430 | |
paul@1375 | 1431 | cl.show_attendees() |
paul@1375 | 1432 | print |
paul@1375 | 1433 | |
paul@1375 | 1434 | # Add or edit period. |
paul@1375 | 1435 | |
paul@1436 | 1436 | elif cmd in ("p", "period"): |
paul@1375 | 1437 | value = next_arg(args) |
paul@1436 | 1438 | index = to_int_or_none(value) |
paul@1375 | 1439 | |
paul@1375 | 1440 | # Add a new period. |
paul@1375 | 1441 | |
paul@1436 | 1442 | if index is None or value == "new": |
paul@1375 | 1443 | cl.add_period() |
paul@1436 | 1444 | cl.edit_period(-1, args) |
paul@1375 | 1445 | |
paul@1375 | 1446 | # Edit period (using index). |
paul@1375 | 1447 | |
paul@1375 | 1448 | else: |
paul@1375 | 1449 | period = cl.can_edit_period(index) |
paul@1375 | 1450 | if period: |
paul@1375 | 1451 | while True: |
paul@1375 | 1452 | show_period_raw(period) |
paul@1375 | 1453 | |
paul@1375 | 1454 | # Obtain a command from any arguments. |
paul@1375 | 1455 | |
paul@1375 | 1456 | cmd = next_arg(args) |
paul@1375 | 1457 | if not cmd: |
paul@1436 | 1458 | cmd = read_input("Period: (e)dit, (c)ancel, (u)ncancel (or return)> ") |
paul@1375 | 1459 | if cmd in ("e", "edit"): |
paul@1375 | 1460 | cl.edit_period(index, args) |
paul@1375 | 1461 | elif cmd in ("c", "cancel"): |
paul@1375 | 1462 | cl.cancel_periods([index]) |
paul@1375 | 1463 | elif cmd in ("u", "uncancel", "restore"): |
paul@1375 | 1464 | cl.cancel_periods([index], False) |
paul@1436 | 1465 | |
paul@1436 | 1466 | # Exit if requested or after a successful |
paul@1436 | 1467 | # operation. |
paul@1436 | 1468 | |
paul@1375 | 1469 | elif not cmd: |
paul@1375 | 1470 | pass |
paul@1375 | 1471 | else: |
paul@1375 | 1472 | continue |
paul@1375 | 1473 | break |
paul@1375 | 1474 | |
paul@1375 | 1475 | cl.show_periods() |
paul@1375 | 1476 | print |
paul@1375 | 1477 | |
paul@1375 | 1478 | # Apply suggested period (using index). |
paul@1375 | 1479 | |
paul@1436 | 1480 | elif cmd in ("ps", "period-suggested", "suggested-period"): |
paul@1436 | 1481 | value = next_arg(args) |
paul@1436 | 1482 | index = to_int_or_none(value) |
paul@1436 | 1483 | |
paul@1436 | 1484 | if index is not None: |
paul@1375 | 1485 | cl.apply_suggested_period(index) |
paul@1436 | 1486 | |
paul@1375 | 1487 | cl.show_periods() |
paul@1375 | 1488 | print |
paul@1375 | 1489 | |
paul@1396 | 1490 | # Specify a recurrence rule. |
paul@1396 | 1491 | |
paul@1436 | 1492 | elif cmd in ("rule", "rrule"): |
paul@1436 | 1493 | value = next_arg(args) |
paul@1436 | 1494 | index = to_int_or_none(value) |
paul@1436 | 1495 | |
paul@1436 | 1496 | # Add a new rule. |
paul@1436 | 1497 | |
paul@1436 | 1498 | if index is None: |
paul@1436 | 1499 | cl.add_rule_selectors() |
paul@1436 | 1500 | else: |
paul@1436 | 1501 | cl.edit_rule_selector(index, args) |
paul@1436 | 1502 | |
paul@1436 | 1503 | cl.show_rule() |
paul@1436 | 1504 | cl.update_periods_from_rule() |
paul@1436 | 1505 | print |
paul@1396 | 1506 | |
paul@1375 | 1507 | # Set the summary. |
paul@1375 | 1508 | |
paul@1436 | 1509 | elif cmd in ("s", "summary"): |
paul@1436 | 1510 | cl.edit_summary(get_text_arg(s)) |
paul@1375 | 1511 | cl.show_object() |
paul@1375 | 1512 | print |
paul@1375 | 1513 | |
paul@1375 | 1514 | except EOFError: |
paul@1375 | 1515 | return |
paul@1375 | 1516 | |
paul@1375 | 1517 | def main(args): |
paul@1436 | 1518 | |
paul@1436 | 1519 | """ |
paul@1436 | 1520 | The main program, employing command line 'args' to initialise the editing |
paul@1436 | 1521 | activity. |
paul@1436 | 1522 | """ |
paul@1436 | 1523 | |
paul@1375 | 1524 | global echo |
paul@1375 | 1525 | |
paul@1387 | 1526 | if "--help" in args: |
paul@1387 | 1527 | show_help(os.path.split(sys.argv[0])[-1]) |
paul@1436 | 1528 | return 0 |
paul@1387 | 1529 | |
paul@1375 | 1530 | # Parse command line arguments using the standard options plus some extra |
paul@1375 | 1531 | # options. |
paul@1375 | 1532 | |
paul@1375 | 1533 | args = parse_args(args, { |
paul@1387 | 1534 | "--calendar-data" : ("calendar_data", False), |
paul@1387 | 1535 | "--charset" : ("charset", "utf-8"), |
paul@1375 | 1536 | "--echo" : ("echo", False), |
paul@1375 | 1537 | "-f" : ("filename", None), |
paul@1387 | 1538 | "--handle-data" : ("handle_data", False), |
paul@1375 | 1539 | "--suppress-bcc" : ("suppress_bcc", False), |
paul@1375 | 1540 | "-u" : ("user", None), |
paul@1436 | 1541 | "--uid" : ("uid", None), |
paul@1436 | 1542 | "--recurrence-id" : ("recurrenceid", None), |
paul@1436 | 1543 | "--show-config" : ("show_config", False) |
paul@1375 | 1544 | }) |
paul@1375 | 1545 | |
paul@1387 | 1546 | charset = args["charset"] |
paul@1387 | 1547 | calendar_data = args["calendar_data"] |
paul@1375 | 1548 | echo = args["echo"] |
paul@1375 | 1549 | filename = args["filename"] |
paul@1387 | 1550 | handle_data = args["handle_data"] |
paul@1375 | 1551 | sender = (args["senders"] or [None])[0] |
paul@1375 | 1552 | suppress_bcc = args["suppress_bcc"] |
paul@1375 | 1553 | user = args["user"] |
paul@1436 | 1554 | uid = args["uid"] |
paul@1436 | 1555 | recurrenceid = args["recurrenceid"] |
paul@1436 | 1556 | |
paul@1436 | 1557 | # Open a store. |
paul@1436 | 1558 | |
paul@1436 | 1559 | store_type = args.get("store_type") |
paul@1436 | 1560 | store_dir = args.get("store_dir") |
paul@1436 | 1561 | preferences_dir = args.get("preferences_dir") |
paul@1436 | 1562 | |
paul@1436 | 1563 | # Show configuration and exit if requested. |
paul@1436 | 1564 | |
paul@1436 | 1565 | if args["show_config"]: |
paul@1436 | 1566 | print """\ |
paul@1436 | 1567 | Store type: %s (%s) |
paul@1436 | 1568 | Store directory: %s (%s) |
paul@1436 | 1569 | Preferences directory: %s |
paul@1436 | 1570 | """ % ( |
paul@1436 | 1571 | store_type, settings["STORE_TYPE"], |
paul@1436 | 1572 | store_dir, settings["STORE_DIR"], |
paul@1436 | 1573 | preferences_dir) |
paul@1436 | 1574 | return 0 |
paul@1375 | 1575 | |
paul@1375 | 1576 | # Determine the user and sender identities. |
paul@1375 | 1577 | |
paul@1375 | 1578 | if sender and not user: |
paul@1375 | 1579 | user = get_uri(sender) |
paul@1375 | 1580 | elif user and not sender: |
paul@1375 | 1581 | sender = get_address(user) |
paul@1375 | 1582 | elif not sender and not user: |
paul@1375 | 1583 | print >>sys.stderr, "A sender or a user must be specified." |
paul@1436 | 1584 | return 1 |
paul@1375 | 1585 | |
paul@1436 | 1586 | # Obtain a store but not a journal. |
paul@1375 | 1587 | |
paul@1375 | 1588 | store = get_store(store_type, store_dir) |
paul@1375 | 1589 | journal = None |
paul@1375 | 1590 | |
paul@1375 | 1591 | # Open a messenger for the user. |
paul@1375 | 1592 | |
paul@1375 | 1593 | messenger = Messenger(sender=sender, suppress_bcc=suppress_bcc) |
paul@1375 | 1594 | |
paul@1375 | 1595 | # Open a client for the user. |
paul@1375 | 1596 | |
paul@1375 | 1597 | cl = TextClient(user, messenger, store, journal, preferences_dir) |
paul@1375 | 1598 | |
paul@1436 | 1599 | # Read any input resource, using it to obtain identifier details. |
paul@1375 | 1600 | |
paul@1375 | 1601 | if filename: |
paul@1387 | 1602 | if calendar_data: |
paul@1387 | 1603 | all_itip = get_itip_from_data(filename, charset) |
paul@1387 | 1604 | else: |
paul@1387 | 1605 | all_itip = get_itip_from_message(filename) |
paul@1387 | 1606 | |
paul@1387 | 1607 | objects = [] |
paul@1387 | 1608 | |
paul@1387 | 1609 | # Process the objects using the person handler. |
paul@1387 | 1610 | |
paul@1387 | 1611 | if handle_data: |
paul@1387 | 1612 | for itip in all_itip: |
paul@1423 | 1613 | handled = handle_calendar_data(itip, get_handlers(cl, person.handlers, None)) |
paul@1423 | 1614 | if not is_cancel_itip(itip): |
paul@1423 | 1615 | objects += handled |
paul@1387 | 1616 | |
paul@1387 | 1617 | # Or just obtain objects from the data. |
paul@1387 | 1618 | |
paul@1387 | 1619 | else: |
paul@1387 | 1620 | for itip in all_itip: |
paul@1423 | 1621 | handled = get_objects_from_itip(itip, ["VEVENT"]) |
paul@1423 | 1622 | if not is_cancel_itip(itip): |
paul@1423 | 1623 | objects += handled |
paul@1375 | 1624 | |
paul@1375 | 1625 | # Choose an object to edit. |
paul@1375 | 1626 | |
paul@1375 | 1627 | show_objects(objects, user, store) |
paul@1375 | 1628 | obj = select_object(cl, objects) |
paul@1375 | 1629 | |
paul@1436 | 1630 | # Load any indicated object. |
paul@1375 | 1631 | |
paul@1436 | 1632 | elif uid: |
paul@1436 | 1633 | obj = cl.load_object(uid, recurrenceid) |
paul@1375 | 1634 | |
paul@1375 | 1635 | # Or create a new object. |
paul@1375 | 1636 | |
paul@1375 | 1637 | else: |
paul@1375 | 1638 | obj = cl.new_object() |
paul@1375 | 1639 | |
paul@1436 | 1640 | # Exit without any object. |
paul@1436 | 1641 | |
paul@1436 | 1642 | if not obj: |
paul@1436 | 1643 | print >>sys.stderr, "No object loaded." |
paul@1436 | 1644 | return 1 |
paul@1436 | 1645 | |
paul@1375 | 1646 | # Edit the object. |
paul@1375 | 1647 | |
paul@1387 | 1648 | edit_object(cl, obj, handle_outgoing=handle_data) |
paul@1387 | 1649 | |
paul@1387 | 1650 | def show_help(progname): |
paul@1387 | 1651 | print >>sys.stderr, help_text % progname |
paul@1387 | 1652 | |
paul@1387 | 1653 | help_text = """\ |
paul@1387 | 1654 | Usage: %s -s <sender> | -u <user> \\ |
paul@1436 | 1655 | [ -f <filename> | --uid <uid> [ --recurrence-id <recurrence-id> ] ] \\ |
paul@1387 | 1656 | [ --calendar-data --charset ] \\ |
paul@1387 | 1657 | [ --handle-data ] \\ |
paul@1387 | 1658 | [ -T <store type ] [ -S <store directory> ] \\ |
paul@1387 | 1659 | [ -p <preferences directory> ] \\ |
paul@1387 | 1660 | [ --echo ] |
paul@1387 | 1661 | |
paul@1387 | 1662 | Identity options: |
paul@1387 | 1663 | |
paul@1387 | 1664 | -s Indicate the user by specifying a sender address |
paul@1387 | 1665 | -u Indicate the user by specifying their URI |
paul@1387 | 1666 | |
paul@1387 | 1667 | Input options: |
paul@1387 | 1668 | |
paul@1436 | 1669 | -f Indicates a filename containing a MIME-encoded message or |
paul@1436 | 1670 | calendar object |
paul@1436 | 1671 | --uid Indicates the UID of a stored calendar object |
paul@1436 | 1672 | --recurrence-id Indicates a stored object with a specific RECURRENCE-ID |
paul@1387 | 1673 | |
paul@1387 | 1674 | --calendar-data Indicates that the specified file contains a calendar object |
paul@1387 | 1675 | as opposed to a mail message |
paul@1387 | 1676 | --charset Specifies the character encoding used by a calendar object |
paul@1387 | 1677 | description |
paul@1387 | 1678 | |
paul@1387 | 1679 | Processing options: |
paul@1387 | 1680 | |
paul@1387 | 1681 | --handle-data Cause the input to be handled and stored in the configured |
paul@1387 | 1682 | data store |
paul@1387 | 1683 | |
paul@1387 | 1684 | Configuration options (overriding configured defaults): |
paul@1387 | 1685 | |
paul@1387 | 1686 | -p Indicates the location of user preference directories |
paul@1387 | 1687 | -S Indicates the location of the calendar data store containing user storage |
paul@1387 | 1688 | directories |
paul@1387 | 1689 | -T Indicates the store type (the configured value if omitted) |
paul@1387 | 1690 | |
paul@1387 | 1691 | Output options: |
paul@1387 | 1692 | |
paul@1387 | 1693 | --echo Echo received input, useful if consuming input from the |
paul@1387 | 1694 | standard input stream and producing a log of the program's |
paul@1387 | 1695 | activity |
paul@1387 | 1696 | """ |
paul@1375 | 1697 | |
paul@1375 | 1698 | if __name__ == "__main__": |
paul@1436 | 1699 | sys.exit(main(sys.argv[1:])) |
paul@1375 | 1700 | |
paul@1375 | 1701 | # vim: tabstop=4 expandtab shiftwidth=4 |