Achieve Python 2/3 compatibility.

This commit is contained in:
Michael Hierweck 2016-05-29 10:45:53 +02:00
parent fcd3c33ed8
commit a1a0b79670

View File

@ -11,6 +11,11 @@ from .exceptions import MissingArgumentError, InvalidArgumentError
from .exceptions import APIInitializationError, APIInvokationError from .exceptions import APIInitializationError, APIInvokationError
from .exceptions import AmbiguousResultsError from .exceptions import AmbiguousResultsError
try:
string = (str, unicode,)
except:
string = (str,)
class Module(object): class Module(object):
@ -22,7 +27,7 @@ class Module(object):
def parse_argsfile(argsfile): def parse_argsfile(argsfile):
""" Returns parsed params provided by argsfile """ """ Returns parsed params provided by argsfile """
args = split(file(argsfile).read()) args = split(open(argsfile).read())
params = dict() params = dict()
for arg in args: for arg in args:
@ -43,37 +48,37 @@ class Module(object):
if not isinstance(hsadmin, dict): if not isinstance(hsadmin, dict):
return False return False
if not (hsadmin.has_key('cas') and if not (('cas' in hsadmin) and
isinstance(hsadmin['cas'], dict)): isinstance(hsadmin['cas'], dict)):
return False return False
else: else:
cas = hsadmin['cas'] cas = hsadmin['cas']
if not (isinstance(cas, dict) and if not (isinstance(cas, dict) and
cas.has_key('uri') and ('uri' in cas) and
isinstance(cas['uri'], basestring) and isinstance(cas['uri'], string) and
cas.has_key('service') and ('service' and cas) and
isinstance(cas['service'], basestring)): isinstance(cas['service'], string)):
return False return False
if not hsadmin.has_key('credentials'): if 'credentials' not in hsadmin:
return False return False
else: else:
credentials = hsadmin['credentials'] credentials = hsadmin['credentials']
if not (isinstance(credentials, dict) and if not (isinstance(credentials, dict) and
credentials.has_key('username') and ('username' in credentials) and
isinstance(credentials['username'], basestring) and isinstance(credentials['username'], string) and
credentials.has_key('password') and ('password' in credentials) and
isinstance(credentials['password'], basestring)): isinstance(credentials['password'], string)):
return False return False
if not hsadmin.has_key('backends'): if 'backends' not in hsadmin:
return False return False
else: else:
backends = hsadmin['backends'] backends = hsadmin['backends']
if not isinstance(backends, list) or (len(backends) == 0): if not isinstance(backends, list) or (len(backends) == 0):
return False return False
for backend in backends: for backend in backends:
if not isinstance(backend, basestring): if not isinstance(backend, string):
return False return False
return True return True
@ -81,7 +86,7 @@ class Module(object):
params = parse_argsfile(argsfile) params = parse_argsfile(argsfile)
if not params.has_key('hsadmin'): if 'hsadmin' not in params:
raise MissingArgumentError('Module argument "hsadmin" is missing.') raise MissingArgumentError('Module argument "hsadmin" is missing.')
hsadmin = params['hsadmin'] hsadmin = params['hsadmin']
@ -95,10 +100,10 @@ class Module(object):
except Exception as exception: except Exception as exception:
raise APIInitializationError('HSAdmin API initialization failed: ' + exception.message) raise APIInitializationError('HSAdmin API initialization failed: ' + exception.message)
if not api.modules.has_key(module): if module not in api.modules:
raise NotImplementedError('HSAdmin API does not implement the module: ' + module) raise NotImplementedError('HSAdmin API does not implement the module: ' + module)
if params.has_key('state'): if 'state' in params:
state = params['state'] state = params['state']
del params['state'] del params['state']
if not state in ('present', 'absent'): if not state in ('present', 'absent'):
@ -109,8 +114,9 @@ class Module(object):
# Patch module properties to provide information # Patch module properties to provide information
# about the property being part of the unique identifier # about the property being part of the unique identifier
# until that information is provided by the backends theirselves # until that information is provided by the backends theirselves
for key, value in api.modules[module].properties.iteritems(): properties = api.modules[module].properties
value['id'] = (key in ids) for key in properties.keys():
properties[key]['id'] = (key in ids)
self.module = api.modules[module] self.module = api.modules[module]
self.params = params self.params = params
@ -123,13 +129,14 @@ class Module(object):
""" Returns where data structure """ """ Returns where data structure """
ids = list() ids = list()
for key, value in module.properties.iteritems(): properties = module.properties
if value.has_key('id') and value['id']: for key in module.properties.keys():
if ('id' in properties[key]) and properties[key]['id']:
ids.append(key) ids.append(key)
where_params = dict() where_params = dict()
for key in ids: for key in ids:
if not params.has_key(key): if key not in params:
raise MissingArgumentError('Module argument "%s" is missing.' % key) raise MissingArgumentError('Module argument "%s" is missing.' % key)
where_params[key] = self.params[key] where_params[key] = self.params[key]
@ -140,11 +147,11 @@ class Module(object):
""" Returns where data structure """ """ Returns where data structure """
set_params = dict() set_params = dict()
for key, value in params.iteritems(): for key in params.keys():
if not module.properties.has_key(key): if key not in module.properties:
raise InvalidArgumentError('Module argument "%s" is invalid.' % key) raise InvalidArgumentError('Module argument "%s" is invalid.' % key)
if not current.has_key(key) or (current[key] != value): if (key not in current) or (current[key] != params[key]):
set_params[key] = value set_params[key] = params[key]
return set_params return set_params
@ -226,4 +233,4 @@ class Module(object):
if (state == 'absent') and retrieved: if (state == 'absent') and retrieved:
changed = delete(module, params) changed = delete(module, params)
print dumps({'changed': changed}) print(dumps({'changed': changed}))