1 #!/usr/bin/env python 2 3 """ 4 Show the contents of a table. 5 6 Copyright (C) 2016, 2017 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 if data: 28 for row in data: 29 print row or "" 30 31 def show_object(obj): 32 if obj: 33 print obj.to_string(line_length=1000) 34 35 def show_periods(data): 36 if data: 37 for row in data: 38 print "\t".join(row.as_tuple(strings_only=True)) 39 40 def show_tuples(data): 41 if data: 42 for row in data: 43 print "\t".join([(column or "") for column in row]) 44 45 if __name__ == "__main__": 46 try: 47 store_type, store_dir, journal_dir, user, table = sys.argv[1:6] 48 args = sys.argv[6:] 49 except ValueError: 50 print >>sys.stderr, """\ 51 Need a store type, a store directory, a journal directory, a user URI, and a 52 table to show. Other arguments may be needed for certain tables. 53 """ 54 sys.exit(1) 55 56 store = get_store(store_type, store_dir) 57 journal = get_journal(store_type, journal_dir) 58 59 # Periods. 60 61 if table == "entries": 62 group = args[0] 63 data = journal.get_entries(user, group) 64 show_periods(data) 65 66 elif table == "freebusy": 67 data = store.get_freebusy(user) 68 show_periods(data) 69 70 elif table == "freebusy_offers": 71 data = store.get_freebusy_offers(user) 72 show_periods(data) 73 74 elif table == "freebusy_other": 75 other = args[0] 76 data = store.get_freebusy_for_other(user, other) 77 show_periods(data) 78 79 # Tuples. 80 81 elif table == "requests": 82 data = store.get_requests(user) 83 show_tuples(data) 84 85 elif table == "freebusy_providers": 86 data = store.get_freebusy_providers(user) 87 show_tuples(data) 88 89 elif table == "journal_freebusy_providers": 90 data = journal.get_freebusy_providers(user) 91 show_tuples(data) 92 93 # Objects. 94 95 elif table == "countered_object": 96 uid = args[0] 97 other = args[1] 98 obj = store.get_counter(user, other, uid) 99 show_object(obj) 100 101 elif table == "object": 102 uid = args[0] 103 obj = store.get_event(user, uid) 104 show_object(obj) 105 106 elif table == "journal_object": 107 uid = args[0] 108 obj = journal.get_event(user, uid) 109 show_object(obj) 110 111 elif table == "recurrence": 112 uid = args[0] 113 recurrenceid = args[1] 114 obj = store.get_event(user, uid, recurrenceid) 115 show_object(obj) 116 117 elif table == "cancelled_recurrences": 118 uid = args[0] 119 data = store.get_cancelled_recurrences(user, uid) 120 show_list(data) 121 122 # vim: tabstop=4 expandtab shiftwidth=4