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_getTrend.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
223 lines (200 sloc)
9.66 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
# -*- coding: utf-8 -*- | |
import suds | |
import sys | |
import xml | |
import argparse | |
import getpass | |
from Environment import readSettings | |
def main(args): | |
#################################################################################################################### | |
### Check all arguments | |
#################################################################################################################### | |
errorMessage = "No path to SETTINGS file specified. Provide one via '-f my_settingsFile' or {..., 'settingsFile':'my_settingsFile', ...}" | |
try: | |
if args['settingsFile'] is None or not args['settingsFile']: | |
print(errorMessage) | |
sys.exit(1) | |
else: | |
settingsFile = args['settingsFile'] | |
except KeyError: | |
print(errorMessage) | |
sys.exit(1) | |
settings = readSettings(settingsFile) | |
errorMessage = "No username specified. Provide one via '-u my_username' or {..., 'username':'my_username', ...} or WC_USERNAME=my_username" | |
try: | |
if args['username'] is None or not args['username']: | |
if not settings['WC_USERNAME']: | |
print(errorMessage) | |
sys.exit(1) | |
else: | |
username = settings['WC_USERNAME'] | |
else: | |
username = str(args['username']) | |
except KeyError: | |
print(errorMessage) | |
sys.exit(1) | |
# The password cannot be given via environment variable | |
errorMessage = "No password specified. Provide one via {..., 'password':'my_password', ...}" | |
try: | |
if args['password'] is None or not args['password']: | |
print(errorMessage) | |
sys.exit(1) | |
else: | |
password = args['password'] | |
except KeyError: | |
print(errorMessage) | |
sys.exit(1) | |
nodeDefault = '/trees/geographic' | |
errorMessage = "No node specified. Provide one via '-n my_node' or {..., 'node':'my_node', ...} or WC_NODE=my_node. " \ | |
"Using a default now: '" + nodeDefault + "'" | |
try: | |
if args['node'] is None or not args['node']: | |
if not settings['WC_NODE']: | |
print(errorMessage) | |
node = nodeDefault | |
else: | |
node = settings['WC_NODE'] | |
else: | |
node = str(args['node']) | |
except KeyError: | |
print(errorMessage) | |
node = nodeDefault | |
urlDefault = 'https://webctrl.rz-berlin.mpg.de' | |
errorMessage = "No URL specified. Provide one via '-url my_url' or {..., 'url':'my_url', ...} or WC_URL=my_url. " \ | |
"Using a default now: '" + urlDefault + "'" | |
try: | |
if args['url'] is None or not args['url']: | |
if not settings['WC_URL']: | |
print(errorMessage) | |
url = urlDefault | |
else: | |
url = settings['WC_URL'] | |
else: | |
url = str(args['url']) | |
except KeyError: | |
print(errorMessage) | |
url = urlDefault | |
wsdlFile = url + '/_common/webservices/Trend?wsdl' | |
sTimeDefault = '' | |
errorMessage = "No start time specified. Provide one via '-s my_stime' or {..., 'sTime':'my_sTime', ...} or WC_TREND_STIME=my_sTime. " \ | |
"sTime requires the following pattern: 'MM/DD/YYYY HH:MM:SS AM'. Using a default now: sTime = '" + sTimeDefault + "'" | |
try: | |
if args['sTime'] is None or not args['sTime']: | |
if not settings['WC_TREND_STIME']: | |
print(errorMessage) | |
sTime = sTimeDefault | |
else: | |
sTime = settings['WC_TREND_STIME'] | |
else: | |
sTime = str(args['sTime']) | |
except KeyError: | |
print(errorMessage) | |
sTime = sTimeDefault | |
eTimeDefault = '' | |
errorMessage = "No end time specified. Provide one via '-e my_eTime' or {..., 'eTime':'my_eTime', ...} or WC_TREND_ETIME=my_eTime. " \ | |
"eTime requires the following pattern: 'MM/DD/YYYY HH:MM:SS PM'. Using a default now: eTime = '" + eTimeDefault + "'" | |
try: | |
if args['eTime'] is None or not args['eTime']: | |
if not settings['WC_TREND_ETIME']: | |
print(errorMessage) | |
eTime = eTimeDefault | |
else: | |
eTime = settings['WC_TREND_ETIME'] | |
else: | |
eTime = str(args['eTime']) | |
except KeyError: | |
print(errorMessage) | |
eTime = eTimeDefault | |
limitDefault = False | |
errorMessage = "No parameter 'limit' specified. Provide one via '-l my_limit' or {..., 'limit':'my_limit', ...} or " \ | |
"WC_TREND_LIMIT=my_limit. limit=False: a maximum number of records, 'maxRecords', is retrieved from the start of the " \ | |
"specified time period. limit=True: 'maxRecords' are retrieved from the end of the time period. Now, " \ | |
"a default, limit = " + str(limitDefault) + ", is set." | |
try: | |
if args['limit'] is None or not args['limit']: | |
if not settings['WC_TREND_LIMIT']: | |
print(errorMessage) | |
# default value: | |
limit = limitDefault | |
else: | |
limit = settings['WC_TREND_LIMIT'] | |
else: | |
limit = str(args['limit']) | |
except KeyError: | |
print(errorMessage) | |
limit = limitDefault | |
maxRecDefault = 10 | |
errorMessage = "No parameter 'maxRec' specified. Provide one via '-m my_maxRec' or {..., 'maxRec':'my_maxRec', ...} or " \ | |
"WC_TREND_MAXREC=my_maxRec. If possible a maximum number of records, 'maxRec', is retrieved. Now, " \ | |
"a default, maxRec = " + str(maxRecDefault) + ", is set." | |
try: | |
#if args['maxRec'] is None or not args['maxRec']: | |
if args['maxRec'] is None: | |
if not settings['WC_TREND_MAXREC']: | |
print(errorMessage) | |
maxRec = maxRecDefault | |
else: | |
maxRec = settings['WC_TREND_MAXREC'] | |
else: | |
maxRec = str(args['maxRec']) | |
except KeyError: | |
print(errorMessage) | |
maxRec = maxRecDefault | |
#################################################################################################################### | |
### 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 answer from the server | |
#################################################################################################################### | |
try: | |
trendData = client.service.getTrendData(node, sTime, eTime, limit, maxRec) | |
except suds.WebFault as fault: | |
print fault | |
sys.exit(1) | |
if trendData: | |
for x in range(0, len(trendData) - 1, 2): | |
print trendData[x] + ' ' + trendData[x + 1] | |
else: | |
print 'No data caught. Check your -s and -e time specifications. Perhaps the end time is before the start time.' | |
sys.exit(1) | |
# The argument parser is only called if this script is called as a script/executable (via command line) but not when | |
# imported by another script | |
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._action_groups.pop() | |
required = parser.add_argument_group('required arguments') | |
optional = parser.add_argument_group('optional arguments (can also be specified via SETTINGS file)') | |
required.add_argument('--password', '-p', type=str, default=None, help='Password for the login to the WebCTRL server') | |
required.add_argument('--settingsFile', '-f', type=str, default=None, help='File name of settings containing WC_* variables') | |
required.add_argument('--username', '-u', type=str, default=None, help='Username for the login to the WebCTRL server') | |
optional.add_argument('--url', '-url', type=str, default=None, help="URL of the webctrl server like 'http(s)://webctrl.de'.") | |
optional.add_argument('--node', '-n', type=str, default=None, help='Path to the point or node for which you want to get the trend') | |
optional.add_argument('--sTime', '-s', type=str, default=None, | |
help="Start Time. Returns trend data values starting with this time. Like this: 'MM/DD/YYYY HH:MM:SS AM'.") | |
optional.add_argument('--eTime', '-e', type=str, default=None, | |
help="End Time. Returns trend data values until this time. Like this: 'MM/DD/YYYY HH:MM:SS PM'.") | |
optional.add_argument('--limit', '-l', action='store_true', default=None, | |
help='If specified maxRecords are retrieved from the start. If not specified maxRecords are retrieved from the end.') | |
optional.add_argument('--maxRec', '-m', type=int, default=None, | |
help="Maximum number of records desired. Use a number >0 to limit records; use '-m 0' to retrieve unlimited records.") | |
args = parser.parse_args() | |
# Get the password if it hasn't been passed as argument | |
if args.password is None or not args.password: | |
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) |