1.1 --- a/tools/make_freebusy.py Mon Aug 31 18:09:31 2015 +0200
1.2 +++ b/tools/make_freebusy.py Mon Aug 31 23:29:43 2015 +0200
1.3 @@ -21,7 +21,6 @@
1.4 this program. If not, see <http://www.gnu.org/licenses/>.
1.5 """
1.6
1.7 -from bisect import bisect_left
1.8 from codecs import getwriter
1.9 from imiptools.data import get_window_end, Object
1.10 from imiptools.dates import get_default_timezone, to_utc_datetime
1.11 @@ -30,26 +29,28 @@
1.12 from imip_store import FileStore, FilePublisher
1.13 import sys
1.14
1.15 -# Main program.
1.16 +def make_freebusy(user, participant, store_and_publish, include_needs_action, reset_updated_list, verbose):
1.17
1.18 -if __name__ == "__main__":
1.19 + """
1.20 + Make free/busy details for the records of the given 'user', generating
1.21 + details for 'participant' if not indicated as None; otherwise, generating
1.22 + free/busy details concerning the given user.
1.23
1.24 - # Interpret the command line arguments.
1.25 + If 'store_and_publish' is set, the stored details will be updated;
1.26 + otherwise, the details will be written to standard output.
1.27
1.28 - try:
1.29 - user = sys.argv[1]
1.30 - args = sys.argv[2:]
1.31 - participant = args and args[0] not in ("-n", "-s", "-v", "-r") and args[0] or user
1.32 - store_and_publish = "-s" in args
1.33 - include_needs_action = "-n" in args
1.34 - verbose = "-v" in args
1.35 - reset_updated_list = "-r" in args
1.36 - except IndexError:
1.37 - print >>sys.stderr, """\
1.38 - Need a user and an optional participant (if different from the user),
1.39 - along with the -s option if updating the store and the published details.
1.40 + If 'include_needs_action' is set, details of objects whose participation
1.41 + status is set to "NEEDS-ACTION" for the participant will be included in the
1.42 + details.
1.43 +
1.44 + If 'reset_updated_list' is set, all objects will be inspected for periods;
1.45 + otherwise, only those in the stored free/busy providers file will be
1.46 + inspected.
1.47 +
1.48 + If 'verbose' is set, messages will be written to standard error.
1.49 """
1.50 - sys.exit(1)
1.51 +
1.52 + participant = participant or user
1.53
1.54 preferences = Preferences(user)
1.55 tzid = preferences.get("TZID") or get_default_timezone()
1.56 @@ -110,12 +111,54 @@
1.57 if user == participant:
1.58 store.set_freebusy(user, fb)
1.59 publisher.set_freebusy(user, fb)
1.60 - store.set_freebusy_providers(user, to_utc_datetime(window_end, tzid), [obj for obj in objs if obj.possibly_active_from(window_end, tzid)])
1.61 +
1.62 + # Update the list of objects providing periods on future occasions.
1.63 +
1.64 + store.set_freebusy_providers(user, to_utc_datetime(window_end, tzid),
1.65 + [obj for obj in objs if obj.possibly_active_from(window_end, tzid)])
1.66 else:
1.67 store.set_freebusy_for_other(user, fb, participant)
1.68 +
1.69 + # Alternatively, just write the collection to standard output.
1.70 +
1.71 else:
1.72 f = getwriter("utf-8")(sys.stdout)
1.73 for item in fb:
1.74 print >>f, "\t".join(item.as_tuple(strings_only=True))
1.75
1.76 +# Main program.
1.77 +
1.78 +if __name__ == "__main__":
1.79 +
1.80 + # Interpret the command line arguments.
1.81 +
1.82 + try:
1.83 + user = sys.argv[1]
1.84 + args = sys.argv[2:]
1.85 + participant = args and args[0] not in ("-n", "-s", "-v", "-r") and args[0] or None
1.86 + store_and_publish = "-s" in args
1.87 + include_needs_action = "-n" in args
1.88 + reset_updated_list = "-r" in args
1.89 + verbose = "-v" in args
1.90 +
1.91 + except IndexError:
1.92 + print >>sys.stderr, """\
1.93 +Need a user and an optional participant (if different from the user),
1.94 +along with the -s option if updating the store and the published details.
1.95 +Specify -n to include objects with PARTSTAT of NEEDS-ACTION.
1.96 +Specify -r to inspect all objects, not just those expected to provide details.
1.97 +Specify -v for additional messages on standard error.
1.98 + """
1.99 + sys.exit(1)
1.100 +
1.101 + if user in ("*", "all"):
1.102 + users = FileStore().get_users()
1.103 + else:
1.104 + users = [user]
1.105 +
1.106 + for user in users:
1.107 + if verbose:
1.108 + print >>sys.stderr, user
1.109 + make_freebusy(user, participant, store_and_publish, include_needs_action, reset_updated_list, verbose)
1.110 +
1.111 # vim: tabstop=4 expandtab shiftwidth=4