Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
webctrl/wc_getReport.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
134 lines (114 sloc)
5.16 KB
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
import suds | |
import sys | |
import numpy | |
import xml | |
import argparse | |
import getpass | |
def main(args): | |
# Check all arguments | |
try: | |
if args['username'] is None: | |
print 'No username specified. Login to WebCTRL needs a username and a password. Check all options for this command via -h' | |
sys.exit(1) | |
else: | |
username = args['username'] | |
except KeyError: | |
print 'No "username" key specified. Please provide the key-value pair: \'username\':\'myUsername\'' | |
sys.exit(1) | |
try: | |
if args['password'] is None: | |
print 'No password specified. Login to WebCTRL needs a username and a password. Check all options for this command via -h' | |
sys.exit(1) | |
else: | |
password = args['password'] | |
except KeyError: | |
print 'No "password" key specified. Please provide the key-value pair: \'password\':\'myPassword\'' | |
sys.exit(1) | |
try: | |
if args['node'] is None: | |
print 'No path to a node specified. Check all options for this command via -h' | |
sys.exit(1) | |
except KeyError: | |
print 'No "node" key specified. Please provide the key-value pair: \'node\':\'/my/node\'' | |
sys.exit(1) | |
try: | |
if args['url'] is None: | |
print 'No URL given. Specify the URL to the WebCTRL server analogous to http://google.de' | |
sys.exit(1) | |
else: | |
wsdlFile = args['url'] + '/_common/webservices/Report?wsdl' | |
except KeyError: | |
print 'No "url" key specified. Please provide the key-value pair: \'url\':\'http://myURL.de\'' | |
sys.exit(1) | |
# Connect to the webCTRL server | |
try: | |
client = suds.client.Client(wsdlFile, username=username, password=password) | |
except AttributeError: | |
print 'Error: Incorrect username and/or password' | |
except xml.sax._exceptions.SAXParseException: | |
print 'Error: Incorrect/Misspelled WSDL file. It should be: http(s)://URL?wsdl' | |
sys.exit(1) | |
except: | |
print("Unexpected error:", sys.exc_info()[0]) | |
print('Perhaps your URL to the WSDL file is not correct.') | |
sys.exit(1) | |
# Get the report | |
try: | |
report = client.service.runReport(args['node'], '~point-list-report', 'csv') | |
except suds.WebFault as fault: | |
print fault | |
sys.exit(1) | |
# Split the report into separate lines and calculate the number of columns | |
lines = report.split("\n") | |
nColumns = len(lines[0].split(",")) | |
# Declare arrays | |
maxColumnWidth = [0 for i in range(nColumns)] | |
emptyColumns = numpy.full(nColumns, True, dtype=bool) | |
# Initialize the maximum column widths by the width of the column headers | |
column = lines[0].replace('"', '').split(",") | |
for i, col in enumerate(column): | |
maxColumnWidth[i] = len(col) | |
# Calculate the maximum column width taking into account all entries of each column and check whether a column is completely empty. | |
# If only one entry of a specific column is not empty the whole column will be marked as not empty. | |
for l, line in enumerate(lines): | |
if l > 0 and len(line) > 0: | |
column = line.replace('"', '').split(",") | |
for i, col in enumerate(column): | |
if len(col) > maxColumnWidth[i]: | |
maxColumnWidth[i] = len(col) | |
if col.strip() != "": | |
emptyColumns[i] = False | |
# Print the report in column format skipping those columns that are marked as completely empty. | |
for l, line in enumerate(lines): | |
if len(line) > 0: | |
column = line.replace('"', '').split(",") | |
c = '|' | |
for i, col in enumerate(column): | |
if not emptyColumns[i]: | |
formatStr = ' {:<'+str(maxColumnWidth[i])+'}' | |
c = c + formatStr.format(col) + ' |' | |
print c | |
if l == 0: | |
c = '|' | |
for i, n in enumerate(maxColumnWidth): | |
if not emptyColumns[i]: | |
c = c + n * '-' + '---' | |
print c | |
if __name__=='__main__': | |
if len(sys.argv) < 2: | |
print "You haven't specified any arguments. Use -h to get more details on how to use this command." | |
sys.exit(1) | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--username', '-u', type=str, default=None, help='Username for the login to the WebCTRL server') | |
parser.add_argument('--password', '-p', type=str, default=None, help='Password for the login to the WebCTRL server') | |
parser.add_argument('--node', '-n', type=str, default=None, | |
help='Path to the point or node whose children you want to retrieve. Start querying at the lowest level with "-n /trees/geographic"') | |
parser.add_argument('-url', type=str, default='https://webctrl.rz-berlin.mpg.de', | |
help="URL of the WebCTRL server as e.g. http://google.de") | |
args = parser.parse_args() | |
# Get the password if it hasn't been passed as argument | |
if args.password is None: | |
args.password = getpass.getpass('No password specified via -p. Please enter your WebCTRL login password: ') | |
# Convert the argparse.Namespace to a dictionary via vars(args) | |
main(vars(args)) | |
sys.exit(0) |