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 if not journal: 64 print >>sys.stderr, "Journal not present." 65 sys.exit(1) 66 data = journal.get_entries(user, group) 67 show_periods(data) 68 69 elif table == "freebusy": 70 data = store.get_freebusy(user) 71 show_periods(data) 72 73 elif table == "freebusy_offers": 74 data = store.get_freebusy_offers(user) 75 show_periods(data) 76 77 elif table == "freebusy_other": 78 other = args[0] 79 data = store.get_freebusy_for_other(user, other) 80 show_periods(data) 81 82 # Tuples. 83 84 elif table == "requests": 85 data = store.get_requests(user) 86 show_tuples(data) 87 88 elif table == "freebusy_providers": 89 data = store.get_freebusy_providers(user) 90 show_tuples(data) 91 92 elif table == "journal_freebusy_providers": 93 if not journal: 94 print >>sys.stderr, "Journal not present." 95 sys.exit(1) 96 data = journal.get_freebusy_providers(user) 97 show_tuples(data) 98 99 # Objects. 100 101 elif table == "countered_object": 102 uid = args[0] 103 other = args[1] 104 obj = store.get_counter(user, other, uid) 105 show_object(obj) 106 107 elif table == "object": 108 uid = args[0] 109 obj = store.get_event(user, uid) 110 show_object(obj) 111 112 elif table == "journal_object": 113 uid = args[0] 114 if not journal: 115 print >>sys.stderr, "Journal not present." 116 sys.exit(1) 117 obj = journal.get_event(user, uid) 118 show_object(obj) 119 120 elif table == "recurrence": 121 uid = args[0] 122 recurrenceid = args[1] 123 obj = store.get_event(user, uid, recurrenceid) 124 show_object(obj) 125 126 elif table == "cancelled_recurrences": 127 uid = args[0] 128 data = store.get_cancelled_recurrences(user, uid) 129 show_list(data) 130 131 # vim: tabstop=4 expandtab shiftwidth=4