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 data = journal.get_entries(user) 60 show_periods(data) 61 62 elif table == "freebusy": 63 data = store.get_freebusy(user) 64 show_periods(data) 65 66 elif table == "freebusy_offers": 67 data = store.get_freebusy_offers(user) 68 show_periods(data) 69 70 elif table == "freebusy_other": 71 other = args[0] 72 data = store.get_freebusy_for_other(user, other) 73 show_periods(data) 74 75 # Tuples. 76 77 elif table == "requests": 78 data = store.get_requests(user) 79 show_tuples(data) 80 81 # Objects. 82 83 elif table == "countered_object": 84 uid = args[0] 85 other = args[1] 86 data = store.get_counter(user, other, uid) 87 show_object(data) 88 89 elif table == "object": 90 uid = args[0] 91 data = store.get_event(user, uid) 92 show_object(data) 93 94 elif table == "recurrence": 95 uid = args[0] 96 recurrenceid = args[1] 97 data = store.get_event(user, uid, recurrenceid) 98 show_object(data) 99 100 elif table == "cancelled_recurrences": 101 uid = args[0] 102 data = store.get_cancelled_recurrences(user, uid) 103 show_list(data) 104 105 # vim: tabstop=4 expandtab shiftwidth=4