1 #!/usr/bin/env python 2 3 """ 4 Show the contents of a table. 5 6 Copyright (C) 2016 Paul Boddie <paul@boddie.org.uk> 7 8 This program is free software; you can redistribute it and/or modify it under 9 the terms of the GNU General Public License as published by the Free Software 10 Foundation; either version 3 of the License, or (at your option) any later 11 version. 12 13 This program is distributed in the hope that it will be useful, but WITHOUT 14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 16 details. 17 18 You should have received a copy of the GNU General Public License along with 19 this program. If not, see <http://www.gnu.org/licenses/>. 20 """ 21 22 from imiptools.data import Object 23 from imiptools.stores import get_store, get_journal 24 import sys 25 26 def show_list(data): 27 for row in data: 28 print row or "" 29 30 def show_object(data): 31 if data: 32 print Object(data).to_string() 33 34 def show_periods(data): 35 for row in data: 36 print "\t".join(row.as_tuple(strings_only=True)) 37 38 def show_tuples(data): 39 for row in data: 40 print "\t".join([(column or "") for column in row]) 41 42 if __name__ == "__main__": 43 try: 44 store_type, store_dir, journal_dir, user, table = sys.argv[1:6] 45 args = sys.argv[6:] 46 except ValueError: 47 print >>sys.stderr, """\ 48 Need a store type, a store directory, a journal directory, a user URI, and a 49 table to show. Other arguments may be needed for certain tables. 50 """ 51 sys.exit(1) 52 53 store = get_store(store_type, store_dir) 54 journal = get_journal(store_type, journal_dir) 55 56 # Periods. 57 58 if table == "entries": 59 group = args[0] 60 data = journal.get_entries(user, group) 61 show_periods(data) 62 63 elif table == "freebusy": 64 data = store.get_freebusy(user) 65 show_periods(data) 66 67 elif table == "freebusy_offers": 68 data = store.get_freebusy_offers(user) 69 show_periods(data) 70 71 elif table == "freebusy_other": 72 other = args[0] 73 data = store.get_freebusy_for_other(user, other) 74 show_periods(data) 75 76 # Tuples. 77 78 elif table == "requests": 79 data = store.get_requests(user) 80 show_tuples(data) 81 82 elif table == "freebusy_providers": 83 data = store.get_freebusy_providers(user) 84 show_tuples(data) 85 86 # Objects. 87 88 elif table == "countered_object": 89 uid = args[0] 90 other = args[1] 91 data = store.get_counter(user, other, uid) 92 show_object(data) 93 94 elif table == "object": 95 uid = args[0] 96 data = store.get_event(user, uid) 97 show_object(data) 98 99 elif table == "recurrence": 100 uid = args[0] 101 recurrenceid = args[1] 102 data = store.get_event(user, uid, recurrenceid) 103 show_object(data) 104 105 elif table == "cancelled_recurrences": 106 uid = args[0] 107 data = store.get_cancelled_recurrences(user, uid) 108 show_list(data) 109 110 # vim: tabstop=4 expandtab shiftwidth=4