Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #316 from RussTheAerialist/master
Merge in v0.7 from development.
- Loading branch information
Showing
41 changed files
with
885 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import argparse | ||
import getpass | ||
import logging | ||
|
||
import tableauserverclient as TSC | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser(description='Export a view as an image, pdf, or csv') | ||
parser.add_argument('--server', '-s', required=True, help='server address') | ||
parser.add_argument('--username', '-u', required=True, help='username to sign into server') | ||
parser.add_argument('--site', '-S', default=None) | ||
parser.add_argument('-p', default=None) | ||
|
||
parser.add_argument('--logging-level', '-l', choices=['debug', 'info', 'error'], default='error', | ||
help='desired logging level (set to error by default)') | ||
group = parser.add_mutually_exclusive_group(required=True) | ||
group.add_argument('--pdf', dest='type', action='store_const', const=('populate_pdf', 'PDFRequestOptions', 'pdf', | ||
'pdf')) | ||
group.add_argument('--png', dest='type', action='store_const', const=('populate_image', 'ImageRequestOptions', | ||
'image', 'png')) | ||
group.add_argument('--csv', dest='type', action='store_const', const=('populate_csv', 'CSVRequestOptions', 'csv', | ||
'csv')) | ||
|
||
parser.add_argument('--file', '-f', help='filename to store the exported data') | ||
parser.add_argument('--filter', '-vf', metavar='COLUMN:VALUE', | ||
help='View filter to apply to the view') | ||
parser.add_argument('resource_id', help='LUID for the view') | ||
|
||
args = parser.parse_args() | ||
|
||
if args.p is None: | ||
password = getpass.getpass("Password: ") | ||
else: | ||
password = args.p | ||
|
||
# Set logging level based on user input, or error by default | ||
logging_level = getattr(logging, args.logging_level.upper()) | ||
logging.basicConfig(level=logging_level) | ||
|
||
tableau_auth = TSC.TableauAuth(args.username, password, args.site) | ||
server = TSC.Server(args.server, use_server_version=True) | ||
with server.auth.sign_in(tableau_auth): | ||
views = filter(lambda x: x.id == args.resource_id, | ||
TSC.Pager(server.views.get)) | ||
view = views.pop() | ||
|
||
# We have a number of different types and functions for each different export type. | ||
# We encode that information above in the const=(...) parameter to the add_argument function to make | ||
# the code automatically adapt for the type of export the user is doing. | ||
# We unroll that information into methods we can call, or objects we can create by using getattr() | ||
(populate_func_name, option_factory_name, member_name, extension) = args.type | ||
populate = getattr(server.views, populate_func_name) | ||
option_factory = getattr(TSC, option_factory_name) | ||
|
||
if args.filter: | ||
options = option_factory().vf(*args.filter.split(':')) | ||
else: | ||
options = None | ||
if args.file: | ||
filename = args.file | ||
else: | ||
filename = 'out.{}'.format(extension) | ||
|
||
populate(view, options) | ||
with file(filename, 'wb') as f: | ||
if member_name == 'csv': | ||
f.writelines(getattr(view, member_name)) | ||
else: | ||
f.write(getattr(view, member_name)) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# | ||
# This sample uses the PyPDF2 library for combining pdfs together to get the full pdf for all the views in a | ||
# workbook. | ||
# | ||
# You will need to do `pip install PyPDF2` to use this sample. | ||
# | ||
|
||
import argparse | ||
import getpass | ||
import logging | ||
import tempfile | ||
import shutil | ||
import functools | ||
import os.path | ||
|
||
import tableauserverclient as TSC | ||
try: | ||
import PyPDF2 | ||
except ImportError: | ||
print('Please `pip install PyPDF2` to use this sample') | ||
import sys | ||
sys.exit(1) | ||
|
||
|
||
def get_views_for_workbook(server, workbook_id): # -> Iterable of views | ||
workbook = server.workbooks.get_by_id(workbook_id) | ||
server.workbooks.populate_views(workbook) | ||
return workbook.views | ||
|
||
|
||
def download_pdf(server, tempdir, view): # -> Filename to downloaded pdf | ||
logging.info("Exporting {}".format(view.id)) | ||
destination_filename = os.path.join(tempdir, view.id) | ||
server.views.populate_pdf(view) | ||
with file(destination_filename, 'wb') as f: | ||
f.write(view.pdf) | ||
|
||
return destination_filename | ||
|
||
|
||
def combine_into(dest_pdf, filename): # -> None | ||
dest_pdf.append(filename) | ||
return dest_pdf | ||
|
||
|
||
def cleanup(tempdir): | ||
shutil.rmtree(tempdir) | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser(description='Export to PDF all of the views in a workbook') | ||
parser.add_argument('--server', '-s', required=True, help='server address') | ||
parser.add_argument('--site', '-S', default=None, help='Site to log into, do not specify for default site') | ||
parser.add_argument('--username', '-u', required=True, help='username to sign into server') | ||
parser.add_argument('--password', '-p', default=None, help='password for the user') | ||
|
||
parser.add_argument('--logging-level', '-l', choices=['debug', 'info', 'error'], default='error', | ||
help='desired logging level (set to error by default)') | ||
parser.add_argument('--file', '-f', default='out.pdf', help='filename to store the exported data') | ||
parser.add_argument('resource_id', help='LUID for the workbook') | ||
|
||
args = parser.parse_args() | ||
|
||
if args.password is None: | ||
password = getpass.getpass("Password: ") | ||
else: | ||
password = args.password | ||
|
||
# Set logging level based on user input, or error by default | ||
logging_level = getattr(logging, args.logging_level.upper()) | ||
logging.basicConfig(level=logging_level) | ||
|
||
tempdir = tempfile.mkdtemp('tsc') | ||
logging.debug("Saving to tempdir: %s", tempdir) | ||
|
||
tableau_auth = TSC.TableauAuth(args.username, password, args.site) | ||
server = TSC.Server(args.server, use_server_version=True) | ||
try: | ||
with server.auth.sign_in(tableau_auth): | ||
get_list = functools.partial(get_views_for_workbook, server) | ||
download = functools.partial(download_pdf, server, tempdir) | ||
|
||
downloaded = (download(x) for x in get_list(args.resource_id)) | ||
output = reduce(combine_into, downloaded, PyPDF2.PdfFileMerger()) | ||
with file(args.file, 'wb') as f: | ||
output.write(f) | ||
finally: | ||
cleanup(tempdir) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#### | ||
# This script demonstrates how to kill all of the running jobs | ||
# | ||
# To run the script, you must have installed Python 2.7.X or 3.3 and later. | ||
#### | ||
|
||
import argparse | ||
import getpass | ||
import logging | ||
|
||
import tableauserverclient as TSC | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser(description='Cancel all of the running background jobs') | ||
parser.add_argument('--server', '-s', required=True, help='server address') | ||
parser.add_argument('--site', '-S', default=None, help='site to log into, do not specify for default site') | ||
parser.add_argument('--username', '-u', required=True, help='username to sign into server') | ||
parser.add_argument('--password', '-p', default=None, help='password for the user') | ||
|
||
parser.add_argument('--logging-level', '-l', choices=['debug', 'info', 'error'], default='error', | ||
help='desired logging level (set to error by default)') | ||
|
||
args = parser.parse_args() | ||
|
||
if args.password is None: | ||
password = getpass.getpass("Password: ") | ||
else: | ||
password = args.password | ||
|
||
# Set logging level based on user input, or error by default | ||
logging_level = getattr(logging, args.logging_level.upper()) | ||
logging.basicConfig(level=logging_level) | ||
|
||
# SIGN IN | ||
tableau_auth = TSC.TableauAuth(args.username, password, args.site) | ||
server = TSC.Server(args.server, use_server_version=True) | ||
with server.auth.sign_in(tableau_auth): | ||
req = TSC.RequestOptions() | ||
|
||
req.filter.add(TSC.Filter("progress", TSC.RequestOptions.Operator.LessThanOrEqual, 0)) | ||
for job in TSC.Pager(server.jobs, request_opts=req): | ||
print(server.jobs.cancel(job.id), job.id, job.status, job.type) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.