|
| 1 | +import argparse |
| 2 | +import getpass |
| 3 | +import logging |
| 4 | + |
| 5 | +import tableauserverclient as TSC |
| 6 | + |
| 7 | + |
| 8 | +def main(): |
| 9 | + parser = argparse.ArgumentParser(description='Export a view as an image, pdf, or csv') |
| 10 | + parser.add_argument('--server', '-s', required=True, help='server address') |
| 11 | + parser.add_argument('--username', '-u', required=True, help='username to sign into server') |
| 12 | + parser.add_argument('--site', '-S', default=None) |
| 13 | + parser.add_argument('-p', default=None) |
| 14 | + |
| 15 | + parser.add_argument('--logging-level', '-l', choices=['debug', 'info', 'error'], default='error', |
| 16 | + help='desired logging level (set to error by default)') |
| 17 | + group = parser.add_mutually_exclusive_group(required=True) |
| 18 | + group.add_argument('--pdf', dest='type', action='store_const', const=('populate_pdf', 'PDFRequestOptions', 'pdf', |
| 19 | + 'pdf')) |
| 20 | + group.add_argument('--png', dest='type', action='store_const', const=('populate_image', 'ImageRequestOptions', |
| 21 | + 'image', 'png')) |
| 22 | + group.add_argument('--csv', dest='type', action='store_const', const=('populate_csv', 'CSVRequestOptions', 'csv', |
| 23 | + 'csv')) |
| 24 | + |
| 25 | + parser.add_argument('--file', '-f', help='filename to store the exported data') |
| 26 | + parser.add_argument('--filter', '-vf', metavar='COLUMN:VALUE', |
| 27 | + help='View filter to apply to the view') |
| 28 | + parser.add_argument('resource_id', help='LUID for the view') |
| 29 | + |
| 30 | + args = parser.parse_args() |
| 31 | + |
| 32 | + if args.p is None: |
| 33 | + password = getpass.getpass("Password: ") |
| 34 | + else: |
| 35 | + password = args.p |
| 36 | + |
| 37 | + # Set logging level based on user input, or error by default |
| 38 | + logging_level = getattr(logging, args.logging_level.upper()) |
| 39 | + logging.basicConfig(level=logging_level) |
| 40 | + |
| 41 | + tableau_auth = TSC.TableauAuth(args.username, password, args.site) |
| 42 | + server = TSC.Server(args.server, use_server_version=True) |
| 43 | + with server.auth.sign_in(tableau_auth): |
| 44 | + views = filter(lambda x: x.id == args.resource_id, |
| 45 | + TSC.Pager(server.views.get)) |
| 46 | + view = views.pop() |
| 47 | + |
| 48 | + # We have a number of different types and functions for each different export type. |
| 49 | + # We encode that information above in the const=(...) parameter to the add_argument function to make |
| 50 | + # the code automatically adapt for the type of export the user is doing. |
| 51 | + # We unroll that information into methods we can call, or objects we can create by using getattr() |
| 52 | + (populate_func_name, option_factory_name, member_name, extension) = args.type |
| 53 | + populate = getattr(server.views, populate_func_name) |
| 54 | + option_factory = getattr(TSC, option_factory_name) |
| 55 | + |
| 56 | + if args.filter: |
| 57 | + options = option_factory().vf(*args.filter.split(':')) |
| 58 | + else: |
| 59 | + options = None |
| 60 | + if args.file: |
| 61 | + filename = args.file |
| 62 | + else: |
| 63 | + filename = 'out.{}'.format(extension) |
| 64 | + |
| 65 | + populate(view, options) |
| 66 | + with file(filename, 'wb') as f: |
| 67 | + if member_name == 'csv': |
| 68 | + f.writelines(getattr(view, member_name)) |
| 69 | + else: |
| 70 | + f.write(getattr(view, member_name)) |
| 71 | + |
| 72 | + |
| 73 | +if __name__ == '__main__': |
| 74 | + main() |
0 commit comments