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_setValue.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
183 lines (158 sloc)
7.29 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 | |
errorMessage = "No value to be set was specified. Provide one via '-v my_value' or {..., 'value':'my_value', ...} or " \ | |
"WC_NEWVALUE=my_value. NO DEFAULT value will be used." | |
try: | |
if args['value'] is None or not args['value']: | |
if not settings['WC_NEWVALUE']: | |
print(errorMessage) | |
sys.exit(1) | |
else: | |
newValue = settings['WC_NEWVALUE'] | |
else: | |
newValue = str(args['value']) | |
except KeyError: | |
print(errorMessage) | |
sys.exit(1) | |
reasonDefault = '' | |
errorMessage = "No reason for changing the value was given. Provide one via '-r my_reason' or {..., 'reason':'my_reason', ...} or " \ | |
"WC_REASON=my_reason. A default, an empty string, will be used." | |
try: | |
if args['reason'] is None or not args['reason']: | |
if not settings['WC_REASON']: | |
print(errorMessage) | |
reason = reasonDefault | |
else: | |
reason = settings['WC_REASON'] | |
else: | |
reason = str(args['reason']) | |
except KeyError: | |
print(errorMessage) | |
reason = reasonDefault | |
wsdlFile = url + '/_common/webservices/Eval?wsdl' | |
#################################################################################################################### | |
### 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 and print the output | |
#################################################################################################################### | |
try: | |
#value = client.service.getValue(node) | |
value = client.service.setValue(node, newValue, reason) | |
except suds.WebFault as fault: | |
print fault | |
sys.exit(1) | |
if value is None: | |
print "You haven't specified a particular controller. The command could look like this:" | |
print '$ wc_getValue "#controller/m001"' | |
else: | |
print "Present value of " + node + " is: " + value | |
# 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') | |
required.add_argument('--value', '-v', type=str, default=None, help='New value to be set') | |
required.add_argument('--reason', '-r', type=str, default=None, help='Reason for setting/changing the value') | |
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 whose value you want to retrieve') | |
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) | |