Big refactoring towards future 3.x version

This commit is contained in:
Adolfo Gómez García 2017-02-17 20:22:52 +01:00
parent c80535fabb
commit 240be81f26
46 changed files with 192 additions and 167 deletions

View File

@ -140,7 +140,7 @@ class Dispatcher(View):
start = time.time()
response = processor.getResponse(response)
logger.debug('Execution time for encoding: {0}'.format(time.time() - start))
for k, val in handler.headers().iteritems():
for k, val in six.iteritems(handler.headers()):
response[k] = val
return response
except RequestError as e:

View File

@ -35,6 +35,7 @@ from __future__ import unicode_literals
from uds.core.util.Config import Config as cfgConfig
from uds.REST import Handler, AccessDenied
import six
import logging
@ -67,7 +68,7 @@ class Config(Handler):
return res
def put(self):
for section, secDict in self._params.iteritems():
for key, vals in secDict.iteritems():
for section, secDict in six.iteritems(self._params):
for key, vals in six.iteritems(secDict):
cfgConfig.update(section, key, vals['value'])
return 'done'

View File

@ -34,13 +34,13 @@ from __future__ import unicode_literals
from uds.core.util.Config import GlobalConfig
from uds.core.util.model import processUuid
from uds.core.util import OsDetector
from uds.models import Authenticator
from uds.core.auths.auth import authenticate
from uds.REST import RequestError
from uds.REST import Handler
import six
import logging
logger = logging.getLogger(__name__)
@ -128,7 +128,7 @@ class Login(Handler):
raise Exception('Invalid Credentials')
except Exception as e:
logger.exception('exception')
return {'result': 'error', 'error': unicode(e)}
return {'result': 'error', 'error': six.text_type(e)}
class Logout(Handler):

View File

@ -40,6 +40,7 @@ from uds.core.ui.UserInterface import gui
from uds.REST.model import ModelHandler, SaveException
import six
import logging
logger = logging.getLogger(__name__)
@ -70,7 +71,7 @@ class Networks(ModelHandler):
fields['net_start'] = nr[0]
fields['net_end'] = nr[1]
except Exception as e:
raise SaveException(ugettext('Invalid network: ') + unicode(e))
raise SaveException(ugettext('Invalid network: ') + six.text_type(e))
logger.debug('Processed {0}'.format(fields))
def getGui(self, type_):

View File

@ -48,6 +48,7 @@ from django.db import IntegrityError
from uds.core.ui.images import DEFAULT_THUMB_BASE64
from uds.core.util.State import State
import six
import logging
logger = logging.getLogger(__name__)
@ -67,7 +68,7 @@ class Services(DetailHandler): # pylint: disable=too-many-public-methods
'icon': info.icon().replace('\n', ''),
'needs_publication': info.publicationType is not None,
'max_deployed': info.maxDeployed,
'uses_cache': info.usesCache and info.cacheConstains == None,
'uses_cache': info.usesCache and info.cacheConstrains == None,
'uses_cache_l2': info.usesCache_L2,
'cache_tooltip': _(info.cacheTooltip),
'cache_tooltip_l2': _(info.cacheTooltip_L2),
@ -156,7 +157,7 @@ class Services(DetailHandler): # pylint: disable=too-many-public-methods
except coreService.ValidationException as e:
if item is None: # Only remove partially saved element if creating new (if editing, ignore this)
self._deleteIncompleteService(service)
raise RequestError(_('Input error: {0}'.format(unicode(e))))
raise RequestError(_('Input error: {0}'.format(six.text_type(e))))
except Exception as e:
self._deleteIncompleteService(service)
logger.exception('Saving Service')
@ -222,7 +223,7 @@ class Services(DetailHandler): # pylint: disable=too-many-public-methods
return self.addDefaultFields(service.guiDescription(service), ['name', 'comments', 'tags'])
except Exception as e:
logger.exception('getGui')
raise ResponseError(unicode(e))
raise ResponseError(six.text_type(e))
def getLogs(self, parent, item):
try:

View File

@ -48,6 +48,7 @@ from .services_pool_calendars import AccessCalendars, ActionsCalendars
from .services import Services
from uds.core.managers import userServiceManager
import six
import logging
logger = logging.getLogger(__name__)
@ -288,8 +289,8 @@ class ServicesPools(ModelHandler):
else:
del fields['osmanager_id']
if serviceType.cacheConstains is not None:
for k, v in serviceType.cacheConstains.iteritems():
if serviceType.cacheConstrains is not None:
for k, v in six.iteritems(serviceType.cacheConstrains):
fields[k] = v
if serviceType.maxDeployed != -1:

View File

@ -45,8 +45,7 @@ from uds.core.util import log
from uds.REST.model import DetailHandler
from uds.REST import ResponseError
from uds.core.util import permissions
import six
import logging
@ -306,7 +305,7 @@ class Publications(DetailHandler):
ds = DeployedServicePublication.objects.get(uuid=processUuid(uuid))
ds.cancel()
except Exception as e:
raise ResponseError(unicode(e))
raise ResponseError(six.text_type(e))
return self.success()

View File

@ -50,6 +50,7 @@ from uds.core.ui.images import DEFAULT_THUMB_BASE64
from .user_services import AssignedService
from uds.REST.model import DetailHandler
import six
import logging
@ -165,9 +166,9 @@ class Users(DetailHandler):
except IntegrityError: # Duplicate key probably
raise RequestError(_('User already exists (duplicate key error)'))
except AuthenticatorException as e:
raise RequestError(unicode(e))
raise RequestError(six.text_type(e))
except ValidationError as e:
raise RequestError(unicode(e.message))
raise RequestError(six.text_type(e.message))
except Exception:
logger.exception('Saving user')
self.invalidRequestException()
@ -320,7 +321,7 @@ class Groups(DetailHandler):
except IntegrityError: # Duplicate key probably
raise RequestError(_('User already exists (duplicate key error)'))
except AuthenticatorException as e:
raise RequestError(unicode(e))
raise RequestError(six.text_type(e))
except Exception:
logger.exception('Saving group')
self.invalidRequestException()

View File

@ -44,7 +44,7 @@ from uds.core.util import permissions
from uds.core.util.model import processUuid
from uds.models import Tag
import six
import fnmatch
import re
import itertools
@ -199,7 +199,7 @@ class BaseModelHandler(Handler):
Returns a dict containing the table fields description
'''
return {
'title': unicode(title),
'title': six.text_type(title),
'fields': fields,
'row-style': row_style
}
@ -216,7 +216,7 @@ class BaseModelHandler(Handler):
args[key] = self._params[key]
del self._params[key]
except KeyError as e:
raise RequestError('needed parameter not found in data {0}'.format(unicode(e)))
raise RequestError('needed parameter not found in data {0}'.format(six.text_type(e)))
return args
@ -230,7 +230,7 @@ class BaseModelHandler(Handler):
if hasattr(item, 'getInstance'):
i = item.getInstance()
i.initGui() # Defaults & stuff
for key, value in i.valuesDict().iteritems():
for key, value in six.iteritems(i.valuesDict()):
if type(value) in (unicode, str):
value = {"true": True, "false": False}.get(value, value) # Translate "true" & "false" to True & False (booleans)
logger.debug('{0} = {1}'.format(key, value))
@ -577,7 +577,7 @@ class ModelHandler(BaseModelHandler):
Must be overriden by descendants.
Expects the return of an item as a dictionary
'''
pass
return None
def item_as_dict_overview(self, item):
'''
@ -620,6 +620,7 @@ class ModelHandler(BaseModelHandler):
# gui related
def getGui(self, type_):
self.invalidRequestException()
return None # Will never reach this because previous raises an exception
# Delete related, checks if the item can be deleted
# If it can't be so, raises an exception
@ -880,7 +881,7 @@ class ModelHandler(BaseModelHandler):
except IntegrityError: # Duplicate key probably
raise RequestError('Element already exists (duplicate key error)')
except SaveException as e:
raise RequestError(unicode(e))
raise RequestError(six.text_type(e))
except (RequestError, ResponseError):
raise
except Exception:

View File

@ -103,7 +103,7 @@ class ContentProcessor(object):
return int(obj)
elif isinstance(obj, dict):
res = {}
for k, v in obj.iteritems():
for k, v in six.iteritems(obj):
res[k] = ContentProcessor.procesForRender(v)
return res
elif isinstance(obj, (list, tuple, types.GeneratorType)):

View File

@ -51,6 +51,7 @@ __updated__ = '2017-01-30'
# Default ssl context is unverified, as MOST servers that we will connect will be with self signed certificates...
try:
# noinspection PyProtectedMember
_create_unverified_https_context = ssl._create_unverified_context
ssl._create_default_https_context = _create_unverified_https_context
except AttributeError:

View File

@ -40,6 +40,7 @@ from uds.core.auths.GroupsManager import GroupsManager
from uds.core.util import net
from uds.core.util.request import getRequest
from uds.core.ui.UserInterface import gui
import six
import logging
@ -80,7 +81,7 @@ class IPAuth(Authenticator):
if net.ipInNetwork(ip, g):
groupsManager.validate(g)
except Exception as e:
logger.error('Invalid network for IP auth: {0}'.format(unicode(e)))
logger.error('Invalid network for IP auth: {0}'.format(six.text_type(e)))
def authenticate(self, username, credentials, groupsManager):
# If credentials is a dict, that can't be sent directly from web interface, we allow entering

View File

@ -39,6 +39,7 @@ from uds.core.ui.UserInterface import gui
from uds.core import auths
from uds.core.auths.Exceptions import AuthenticatorException
import six
import ldap
import ldap.filter
import re
@ -234,7 +235,7 @@ class RegexLdap(auths.Authenticator):
password = self._password
l.simple_bind_s(who=username, cred=password)
except ldap.LDAPError, e:
except ldap.LDAPError as e:
str_ = _('Ldap connection error: ')
if type(e.message) == dict:
str_ += 'info' in e.message and e.message['info'] + ',' or ''
@ -258,7 +259,7 @@ class RegexLdap(auths.Authenticator):
res = con.search_ext_s(base=self._ldapBase, scope=ldap.SCOPE_SUBTREE,
filterstr=filter_, attrlist=attrlist, sizelimit=LDAP_RESULT_LIMIT)[0]
usr = dict((k, '') for k in attrlist)
dct = {k.lower(): v for k, v in res[1].iteritems()}
dct = {k.lower(): v for k, v in six.iteritems(res[1])}
usr.update(dct)
usr.update({'dn': res[0], '_id': username})
logger.debug('Usr: {0}'.format(usr))
@ -363,7 +364,7 @@ class RegexLdap(auths.Authenticator):
res = []
for r in con.search_ext_s(base=self._ldapBase, scope=ldap.SCOPE_SUBTREE, filterstr='(&(objectClass=%s)(%s=%s*))' % (self._userClass, self._userIdAttr, pattern), sizelimit=LDAP_RESULT_LIMIT):
if r[0] is not None: # Must have a dn, we do not accept references to other
dct = {k.lower(): v for k, v in r[1].iteritems()}
dct = {k.lower(): v for k, v in six.iteritems(r[1])}
logger.debug('R: {0}'.format(dct))
usrId = dct.get(self._userIdAttr.lower(), '')
usrId = type(usrId) == list and usrId[0] or usrId
@ -382,14 +383,14 @@ class RegexLdap(auths.Authenticator):
try:
auth = RegexLdap(None, env, data)
return auth.testConnection()
except Exception, e:
except Exception as e:
logger.error("Exception found testing Simple LDAP auth {0}: {1}".format(e.__class__, e))
return [False, "Error testing connection"]
def testConnection(self):
try:
con = self.__connection()
except Exception, e:
except Exception as e:
return [False, str(e)]
try:
@ -401,7 +402,7 @@ class RegexLdap(auths.Authenticator):
if len(con.search_ext_s(base=self._ldapBase, scope=ldap.SCOPE_SUBTREE, filterstr='(objectClass=%s)' % self._userClass, sizelimit=1)) == 1:
raise Exception()
return [False, _('Ldap user class seems to be incorrect (no user found by that class)')]
except Exception, e:
except Exception as e:
# If found 1 or more, all right
pass

View File

@ -167,7 +167,7 @@ class SimpleLDAPAuthenticator(Authenticator):
password = self._password
l.simple_bind_s(who=username, cred=password)
except ldap.LDAPError, e:
except ldap.LDAPError as e:
str_ = _('Ldap connection error: ')
if type(e.message) == dict:
str_ += 'info' in e.message and e.message['info'] + ',' or ''
@ -361,14 +361,14 @@ class SimpleLDAPAuthenticator(Authenticator):
try:
auth = SimpleLDAPAuthenticator(None, env, data)
return auth.testConnection()
except Exception, e:
except Exception as e:
logger.error("Exception found testing Simple LDAP auth {0}: {1}".format(e.__class__, e))
return [False, "Error testing connection"]
def testConnection(self):
try:
con = self.__connection()
except Exception, e:
except Exception as e:
return [False, str(e)]
try:
@ -380,7 +380,7 @@ class SimpleLDAPAuthenticator(Authenticator):
if len(con.search_ext_s(base=self._ldapBase, scope=ldap.SCOPE_SUBTREE, filterstr='(objectClass=%s)' % self._userClass, sizelimit=1)) == 1:
raise Exception()
return [False, _('Ldap user class seems to be incorrect (no user found by that class)')]
except Exception, e:
except Exception as e:
# If found 1 or more, all right
pass
@ -388,7 +388,7 @@ class SimpleLDAPAuthenticator(Authenticator):
if len(con.search_ext_s(base=self._ldapBase, scope=ldap.SCOPE_SUBTREE, filterstr='(objectClass=%s)' % self._groupClass, sizelimit=1)) == 1:
raise Exception()
return [False, _('Ldap group class seems to be incorrect (no group found by that class)')]
except Exception, e:
except Exception as e:
# If found 1 or more, all right
pass
@ -396,7 +396,7 @@ class SimpleLDAPAuthenticator(Authenticator):
if len(con.search_ext_s(base=self._ldapBase, scope=ldap.SCOPE_SUBTREE, filterstr='(%s=*)' % self._userIdAttr, sizelimit=1)) == 1:
raise Exception()
return [False, _('Ldap user id attribute seems to be incorrect (no user found by that attribute)')]
except Exception, e:
except Exception as e:
# If found 1 or more, all right
pass
@ -404,7 +404,7 @@ class SimpleLDAPAuthenticator(Authenticator):
if len(con.search_ext_s(base=self._ldapBase, scope=ldap.SCOPE_SUBTREE, filterstr='(%s=*)' % self._groupIdAttr, sizelimit=1)) == 1:
raise Exception()
return [False, _('Ldap group id attribute seems to be incorrect (no group found by that attribute)')]
except Exception, e:
except Exception as e:
# If found 1 or more, all right
pass
@ -413,7 +413,7 @@ class SimpleLDAPAuthenticator(Authenticator):
if len(con.search_ext_s(base=self._ldapBase, scope=ldap.SCOPE_SUBTREE, filterstr='(&(objectClass=%s)(%s=*))' % (self._userClass, self._userIdAttr), sizelimit=1)) == 1:
raise Exception()
return [False, _('Ldap user class or user id attr is probably wrong (can\'t find any user with both conditions)')]
except Exception, e:
except Exception as e:
# If found 1 or more, all right
pass
@ -429,7 +429,7 @@ class SimpleLDAPAuthenticator(Authenticator):
break
if ok is False:
raise Exception(_('Can\'t locate any group with the membership attribute specified'))
except Exception, e:
return [False, str(e)]
except Exception as e:
return [False, six.text_type(e)]
return [True, _("Connection params seem correct, test was succesfully executed")]

View File

@ -108,13 +108,13 @@ class Module(UserInterface, Environmentable, Serializable):
iconFile = 'base.png' # This is expected to be png, use this format always
class ValidationException(Exception):
'''
"""
Exception used to indicate that the params assigned are invalid
'''
"""
@classmethod
def name(cls):
'''
"""
Returns "translated" typeName, using ugettext for transforming
cls.typeName
@ -123,12 +123,12 @@ class Module(UserInterface, Environmentable, Serializable):
Returns:
Translated type name (using ugettext)
'''
"""
return _(cls.typeName)
@classmethod
def type(cls):
'''
"""
Returns typeType
Args:
@ -136,12 +136,12 @@ class Module(UserInterface, Environmentable, Serializable):
Returns:
the typeType of this class (or derived class)
'''
"""
return cls.typeType
@classmethod
def description(cls):
'''
"""
This method returns the "translated" description, that is, using
ugettext for transforming cls.typeDescription.
@ -151,12 +151,12 @@ class Module(UserInterface, Environmentable, Serializable):
Returns:
Translated description (using ugettext)
'''
"""
return _(cls.typeDescription)
@classmethod
def icon(cls, inBase64=True):
'''
"""
Reads the file specified by iconFile at module folder, and returns it content.
This is used to obtain an icon so administration can represent it.
@ -168,7 +168,7 @@ class Module(UserInterface, Environmentable, Serializable):
Returns:
Base 64 encoded or raw image, obtained from the specified file at
'iconFile' class attribute
'''
"""
logger.debug('Loading icon for class {0} ({1})'.format(cls, cls.iconFile))
file_ = open(os.path.dirname(sys.modules[cls.__module__].__file__) + '/' + cls.iconFile, 'rb')
data = file_.read()
@ -180,7 +180,7 @@ class Module(UserInterface, Environmentable, Serializable):
@staticmethod
def test(env, data):
'''
"""
Test if the connection data is ok.
Returns an array, first value indicates "Ok" if true, "Bad" or "Error"
@ -196,7 +196,7 @@ class Module(UserInterface, Environmentable, Serializable):
Array of two elements, first is True of False, depending on test
(True is all right, false is error),
second is an String with error, preferably internacionalizated..
'''
"""
return [True, _("No connection checking method is implemented.")]
def __init__(self, environment, values=None):
@ -242,22 +242,22 @@ class Module(UserInterface, Environmentable, Serializable):
return True
def marshal(self):
'''
"""
By default and if not overriden by descendants, this method, overridden
from Serializable, and returns the serialization of
form field stored values.
'''
"""
return self.serializeForm()
def unmarshal(self, str_):
'''
"""
By default and if not overriden by descendants, this method recovers
data serialized using serializeForm
'''
"""
self.unserializeForm(str_)
def check(self):
'''
"""
Method that will provide the "check" capability for the module.
The return value that this method must provide is simply an string,
@ -265,11 +265,11 @@ class Module(UserInterface, Environmentable, Serializable):
Returns:
Internacionalized (using ugettext) string of result of the check.
'''
"""
return _("No check method provided.")
def destroy(self):
'''
"""
Invoked before deleting an module from database.
Do whatever needed here, as deleting associated data if needed
@ -277,5 +277,5 @@ class Module(UserInterface, Environmentable, Serializable):
Returns:
Nothing
'''
"""
pass

View File

@ -32,6 +32,10 @@
'''
from __future__ import unicode_literals
from uds.core.util.Cache import Cache
from uds.core.util.Storage import Storage
import six
TEMP_ENV = 'temporary'
GLOBAL_ENV = 'global'
@ -44,16 +48,16 @@ class Environment(object):
The environment is composed of a "cache" and a "storage". First are volatile data, while second are persistent data.
'''
def __init__(self, uniqueKey, idGenerators={}):
'''
def __init__(self, uniqueKey, idGenerators=None):
"""
Initialized the Environment for the specified id
@param uniqueId: Key for this environment
@param uniqueKey: Key for this environment
@param idGenerators: Hash of generators of ids for this environment. This "generators of ids" feature
is used basically at User Services to auto-create ids for macs or names, using
{'mac' : UniqueMacGenerator, 'name' : UniqueNameGenerator } as argument.
'''
from uds.core.util.Cache import Cache
from uds.core.util.Storage import Storage
"""
if idGenerators is None:
idGenerators = dict()
self._key = uniqueKey
self._cache = Cache(uniqueKey)
self._storage = Storage(uniqueKey)
@ -61,51 +65,51 @@ class Environment(object):
@property
def cache(self):
'''
"""
Method to acces the cache of the environment.
@return: a referente to a Cache instance
'''
"""
return self._cache
@property
def storage(self):
'''
"""
Method to acces the cache of the environment.
@return: a referente to an Storage Instance
'''
"""
return self._storage
def idGenerators(self, generatorId):
'''
"""
The idea of generator of id is to obtain at some moment Ids with a proper generator.
If the environment do not contains generators of id, this method will return None.
The id generator feature is used by User Services to obtain different auto-id generators, as macs or names
@param generatorId: Id of the generator to obtain
@return: Generator for that id, or None if no generator for that id is found
'''
"""
return self._idGenerators.get(generatorId, None)
@property
def key(self):
'''
"""
@return: the key used for this environment
'''
"""
return self._key
def clearRelatedData(self):
'''
"""
Removes all related information from database for this environment.
'''
"""
from uds.core.util.Cache import Cache
from uds.core.util.Storage import Storage
Cache.delete(self._key)
Storage.delete(self._key)
for __, v in self._idGenerators.iteritems():
for __, v in six.iteritems(self._idGenerators):
v.release()
@staticmethod
def getEnvForTableElement(tblName, id_, idGeneratorsTypes={}):
'''
def getEnvForTableElement(tblName, id_, idGeneratorsTypes=None):
"""
From a table name, and a id, tries to load the associated environment or creates a new
one if no environment exists at database. The table name and the id are used to obtain the key
for the environment, so each element at database can have its own environment.
@ -113,35 +117,37 @@ class Environment(object):
@param id_: Id of the element (normally primary key of the record for which we want an environment)
@param idGeneratorsTypes: Associated Generators. Defaults to none
@return: Obtained associated environment (may be empty if none exists at database, but it will be valid)
'''
"""
if idGeneratorsTypes is None:
idGeneratorsTypes = {}
name = 't-' + tblName + '-' + str(id_)
idGenerators = {}
for k, v in idGeneratorsTypes.iteritems():
for k, v in six.iteritems(idGeneratorsTypes):
idGenerators[k] = v(name)
return Environment(name, idGenerators)
@staticmethod
def getEnvForType(type_):
'''
"""
Obtains an environment associated with a type instead of a record
@param type_: Type
@return Associated Environment
'''
"""
return Environment('type-' + str(type_))
@staticmethod
def getTempEnv():
'''
"""
Provides a temporary environment needed in some calls (test provider, for example)
It will not make environment persistent
'''
"""
return Environment(TEMP_ENV) # TODO: In fact, we should provide a "null" cache and a "null" storage, but for now this is right
@staticmethod
def getGlobalEnv():
'''
"""
Provides global environment
'''
"""
return Environment(GLOBAL_ENV) # This environment is a global environment for general utility.
@ -151,57 +157,57 @@ class Environmentable(object):
'''
def __init__(self, environment):
'''
"""
Initialized the element
Args:
environment: Environment to associate with
'''
"""
self._env = environment
@property
def env(self):
'''
"""
Utility method to access the envionment contained by this object
Returns:
Environmnet for the object
'''
"""
return self._env
@env.setter
def env(self, environment):
'''
"""
Assigns a new environment
Args:
environment: Environment to assign
'''
"""
self._env = environment
@property
def cache(self):
'''
"""
Utility method to access the cache of the environment containe by this object
Returns:
Cache for the object
'''
"""
return self._env.cache
@property
def storage(self):
'''
"""
Utility method to access the storage of the environment containe by this object
Returns:
Storage for the object
'''
"""
return self._env.storage
def idGenerators(self, generatorId):
'''
"""
Utility method to access the id generator of the environment containe by this object
Args:
@ -209,5 +215,5 @@ class Environmentable(object):
Returns:
Generator for the object and the id specified
'''
"""
return self._env.idGenerators(generatorId)

View File

@ -36,6 +36,7 @@ from uds.core.util.State import State
from uds.models import Group as dbGroup
from uds.core.auths.Group import Group
import six
import re
import inspect
import logging
@ -84,7 +85,7 @@ class GroupsManager(object):
'''
name = groupName.lower()
res = []
for gName, grp in self._groups.iteritems():
for gName, grp in six.iteritems(self._groups):
if grp['pattern'] is True:
logger.debug('Group is a pattern: {}'.format(grp))
try:
@ -104,7 +105,7 @@ class GroupsManager(object):
Return all groups names managed by this groups manager. The names are returned
as where inserted inside Database (most probably using administration interface)
'''
for g in self._groups.itervalues():
for g in six.itervalues(self._groups):
yield g['group'].dbGroup().name
def getValidGroups(self):
@ -112,7 +113,7 @@ class GroupsManager(object):
returns the list of valid groups (:py:class:uds.core.auths.Group.Group)
'''
lst = ()
for g in self._groups.itervalues():
for g in six.itervalues(self._groups):
if g['valid'] is True:
lst += (g['group'].dbGroup().id,)
yield g['group']
@ -130,7 +131,7 @@ class GroupsManager(object):
Checks if this groups manager has at least one group that has been
validated (using :py:meth:.validate)
'''
for g in self._groups.itervalues():
for g in six.itervalues(self._groups):
if g['valid'] is True:
return True
return False

View File

@ -138,7 +138,7 @@ class DelayedTaskRunner(object):
try:
self.__insert(instance, delay, tag)
break
except Exception, e:
except Exception as e:
logger.info('Exception inserting a delayed task {0}: {1}'.format(str(e.__class__), e))
try:
connection.close()

View File

@ -32,6 +32,7 @@
'''
from __future__ import unicode_literals
import six
import datetime
import logging
@ -70,7 +71,7 @@ class JobsFactory(object):
try:
logger.debug('Ensuring that jobs are registered inside database')
workers.initialize()
for name, type_ in self._jobs.iteritems():
for name, type_ in six.iteritems(self._jobs):
try:
type_.setup()
# We use database server datetime

View File

@ -93,7 +93,7 @@ class DownloadsManager(object):
memory at once. The FileWrapper will turn the file object into an
iterator for chunks of 8KB.
"""
wrapper = FileWrapper(file(filename))
wrapper = FileWrapper(open(filename, 'rb'))
response = HttpResponse(wrapper, content_type=mime)
response['Content-Length'] = os.path.getsize(filename)
response['Content-Disposition'] = 'attachment; filename=' + name

View File

@ -174,7 +174,7 @@ class PublicationFinishChecker(DelayedTask):
logger.debug("publication instance class: {0}".format(pi.__class__))
state = pi.checkState()
PublicationFinishChecker.checkAndUpdateState(servicePoolPub, pi, state)
except Exception, e:
except Exception as e:
logger.debug('Deployed service not found (erased from database) {0} : {1}'.format(e.__class__, e))
@ -251,7 +251,7 @@ class PublicationManager(object):
servicePoolPub.setState(State.CANCELING)
PublicationFinishChecker.checkAndUpdateState(servicePoolPub, pubInstance, state)
return servicePoolPub
except Exception, e:
except Exception as e:
raise PublishException(str(e))
def unpublish(self, servicePoolPub): # pylint: disable=no-self-use
@ -269,5 +269,5 @@ class PublicationManager(object):
state = pubInstance.destroy()
servicePoolPub.setState(State.REMOVING)
PublicationFinishChecker.checkAndUpdateState(servicePoolPub, pubInstance, state)
except Exception, e:
except Exception as e:
raise PublishException(str(e))

View File

@ -36,6 +36,7 @@ from django import forms
from django.utils.translation import ugettext as _, ugettext_lazy
from uds.core.ui.UserInterface import gui
import logging
import six
logger = logging.getLogger(__name__)
@ -87,7 +88,7 @@ class UserPrefsManager(object):
for up in user.preferences.all().order_by('module'):
data[self.__nameFor(up.module, up.name)] = up.value
res = ''
for mod, v in sorted(self._prefs.iteritems()):
for mod, v in sorted(six.iteritems(self._prefs)):
form = forms.Form()
for p in v['prefs']:
name = self.__nameFor(mod, p.getName())
@ -102,7 +103,7 @@ class UserPrefsManager(object):
for up in user.preferences.all():
data[self.__nameFor(up.module, up.name)] = up.value
res = []
for mod, v in self._prefs.iteritems():
for mod, v in six.iteritems(self._prefs):
grp = []
for p in v['prefs']:
name = self.__nameFor(mod, p.getName())
@ -118,7 +119,7 @@ class UserPrefsManager(object):
# First, read fields form every single "section"
logger.debug('Processing {0}'.format(self._prefs))
prefs = []
for mod, v in self._prefs.iteritems():
for mod, v in six.iteritems(self._prefs):
logger.debug(mod)
form = forms.Form(data)
for p in v['prefs']:
@ -144,7 +145,7 @@ class UserPrefsManager(object):
'''
logger.debug('Processing data {0}'.format(data))
prefs = []
for mod, v in self._prefs.iteritems():
for mod, v in six.iteritems(self._prefs):
logger.debug(mod)
for p in v['prefs']:
name = self.__nameFor(mod, p.getName())

View File

@ -167,7 +167,7 @@ class UserServiceManager(object):
state = deployed.deployForUser(user)
try:
UserServiceOpChecker.makeUnique(assignable, deployed, state)
except Exception, e:
except Exception as e:
logger.exception("Exception {0}".format(e))
logger.debug("Assignable: {0}".format(assignable))
return assignable

View File

@ -221,9 +221,9 @@ class UserServiceOpChecker(DelayedTask):
logger.debug("uService instance class: {0}".format(ci.__class__))
state = ci.checkState()
UserServiceOpChecker.checkAndUpdateState(uService, ci, state)
except UserService.DoesNotExist, e:
except UserService.DoesNotExist as e:
logger.error('User service not found (erased from database?) {0} : {1}'.format(e.__class__, e))
except Exception, e:
except Exception as e:
# Exception caught, mark service as errored
logger.exception("Error {0}, {1} :".format(e.__class__, e))
if uService is not None:

View File

@ -113,7 +113,7 @@ class Service(Module):
maxDeployed = UNLIMITED # : If the service provides more than 1 "provided service" (-1 = no limit, 0 = ???? (do not use it!!!), N = max number to deploy
# : If this item "has constains", on deployed service edition, defined keys will overwrite defined ones
cacheConstains = None
cacheConstrains = None
# : If this class uses cache or not. If uses cache is true, means that the
# : service can "prepare" some user deployments to allow quicker user access

View File

@ -107,7 +107,7 @@ class gui(object):
@staticmethod
def convertToList(vals):
if vals is not None:
return [unicode(v) for v in vals]
return [six.text_type(v) for v in vals]
return []
@staticmethod
@ -800,11 +800,11 @@ class UserInterface(object):
# Generate a deep copy of inherited Gui, so each User Interface instance has its own "field" set, and do not share the "fielset" with others, what can be really dangerous
# Till now, nothing bad happened cause there where being used "serialized", but this do not have to be this way
self._gui = copy.deepcopy(self._gui) # Ensure "gui" is our own instance, deep copied from base
for key, val in self._gui.iteritems(): # And refresh references to them
for key, val in six.iteritems(self._gui): # And refresh references to them
setattr(self, key, val)
if values is not None:
for k, v in self._gui.iteritems():
for k, v in six.iteritems(self._gui):
if k in values:
v.value = values[k]
else:
@ -859,7 +859,7 @@ class UserInterface(object):
'''
dic = {}
for k, v in self._gui.iteritems():
for k, v in six.iteritems(self._gui):
if v.isType(gui.InputField.EDITABLE_LIST):
dic[k] = gui.convertToList(v.value)
elif v.isType(gui.InputField.MULTI_CHOICE_TYPE):
@ -889,7 +889,7 @@ class UserInterface(object):
# logger.debug('Caller is : {}'.format(inspect.stack()))
arr = []
for k, v in self._gui.iteritems():
for k, v in six.iteritems(self._gui):
logger.debug('serializing Key: {0}/{1}'.format(k, v.value))
if v.isType(gui.InputField.HIDDEN_TYPE) and v.isSerializable() is False:
# logger.debug('Field {0} is not serializable'.format(k))
@ -922,7 +922,7 @@ class UserInterface(object):
try:
# Set all values to defaults ones
for k in self._gui.iterkeys():
for k in six.iterkeys(self._gui):
if self._gui[k].isType(gui.InputField.HIDDEN_TYPE) and self._gui[k].isSerializable() is False:
# logger.debug('Field {0} is not unserializable'.format(k))
continue
@ -967,7 +967,7 @@ class UserInterface(object):
res = []
# pylint: disable=protected-access,maybe-no-member
for key, val in theGui._gui.iteritems():
for key, val in six.iteritems(theGui._gui):
logger.debug('{0} ### {1}'.format(key, val))
res.append({'name': key, 'gui': val.guiDescription(), 'value': ''})

View File

@ -31,9 +31,12 @@
@author: Adolfo Gómez, dkmaster at dkmon dot com
'''
from __future__ import unicode_literals
from uds.core.Serializable import Serializable
import pickle
import timeit
import six
class Attribute(object):
@ -88,12 +91,12 @@ class AutoAttributes(Serializable):
def declare(self, **kwargs):
d = {}
for key, typ in kwargs.iteritems():
for key, typ in six.iteritems(kwargs):
d[key] = Attribute(typ)
self.dict = d
def marshal(self):
return '\2'.join(['%s\1%s' % (k, pickle.dumps(v)) for k, v in self.dict.iteritems()]).encode(AutoAttributes.ACODEC)
return '\2'.join(['%s\1%s' % (k, pickle.dumps(v)) for k, v in six.iteritems(self.dict)]).encode(AutoAttributes.ACODEC)
def unmarshal(self, data):
if data == '': # Can be empty
@ -105,6 +108,6 @@ class AutoAttributes(Serializable):
def __str__(self):
str_ = '<AutoAttribute '
for k, v in self.dict.iteritems():
for k, v in six.iteitems(self.dict):
str_ += "%s (%s) = %s" % (k, v.getType(), v.getStrValue())
return str_ + '>'

View File

@ -33,7 +33,7 @@
from __future__ import unicode_literals
from django.db import transaction
import uds.models.Cache
from uds.models import getSqlDatetime
from uds.models.Util import getSqlDatetime
from datetime import datetime, timedelta
import hashlib
import logging

View File

@ -35,6 +35,7 @@ from django.conf import settings
from django.apps import apps
import uds.models.Config
from uds.core.managers.CryptoManager import CryptoManager
import six
import logging
logger = logging.getLogger(__name__)
@ -340,7 +341,7 @@ class GlobalConfig(object):
try:
# Tries to initialize database data for global config so it is stored asap and get cached for use
GlobalConfig.initDone = True
for v in GlobalConfig.__dict__.itervalues():
for v in six.itervalues(GlobalConfig.__dict__):
if type(v) is Config._Value:
v.get()

View File

@ -33,6 +33,7 @@
from __future__ import unicode_literals
import re
import six
import logging
from .tools import DictAsObj
@ -109,7 +110,7 @@ def getOsFromUA(ua):
match = None
for ruleKey, ruleValue in browserRules.iteritems():
for ruleKey, ruleValue in six.iteritems(browserRules):
must, mustNot = ruleValue
for mustRe in browsersREs[must]:

View File

@ -33,6 +33,7 @@
from __future__ import unicode_literals
from django.utils.translation import ugettext_noop as _, ugettext_lazy
import six
# States for different objects. Not all objects supports all States
@ -158,6 +159,6 @@ class State(object):
Returns a dictionary with current active locale translation of States to States String
'''
res = {}
for k, v in State.string.iteritems():
for k, v in six.iteritems(State.string):
res[k] = ugettext_lazy(v)
return res

View File

@ -29,7 +29,7 @@
from __future__ import unicode_literals
import Queue
import six
from threading import Thread
import logging
@ -53,7 +53,7 @@ class Worker(Thread):
while self._stop is False:
try:
func, args, kargs = self._tasks.get(block=True, timeout=1)
except Queue.Empty:
except six.moves.queue.Empty:
continue
try:
@ -66,7 +66,7 @@ class Worker(Thread):
class ThreadPool:
def __init__(self, num_threads, queueSize=DEFAULT_QUEUE_SIZE):
self._tasks = Queue.Queue(queueSize)
self._tasks = six.moves.queue.Queue(queueSize)
self._numThreads = num_threads
self._threads = []

View File

@ -36,6 +36,7 @@ from django.utils.translation import get_language
from uds.core.util import OsDetector
from django.utils import formats
import six
import logging
__updated__ = '2015-05-03'
@ -109,7 +110,7 @@ def checkBrowser(request, browser):
needs_version = 0
needs = ''
for b, requires in _browsers.iteritems():
for b, requires in six.iteritems(_browsers):
if browser.startswith(b):
if request.os.Browser not in requires:
return False

View File

@ -58,7 +58,7 @@ def loadModulesUrls():
patterns += mod.urlpatterns
except:
logger.exception('Loading patterns')
except Exception, e:
except Exception as e:
logger.exception('Processing dispatchers loading')
pass

View File

@ -48,8 +48,8 @@ def ipToLong(ip):
convert decimal dotted quad string to long integer
'''
try:
hexn = ''.join(["%02X" % long(i) for i in ip.split('.')])
return long(hexn, 16)
hexn = ''.join(["%02X" % int(i) for i in ip.split('.')])
return int(hexn, 16)
except:
return 0 # Invalid values will map to "0.0.0.0" --> 0

View File

@ -34,6 +34,7 @@ from __future__ import unicode_literals
from django.utils.translation import ugettext_noop as _, ugettext_lazy
import six
import logging
__updated__ = '2016-02-19'
@ -138,4 +139,4 @@ def dictionary():
'''
Returns a dictionary with current active locale translation of States to States String
'''
return dict([(k, ugettext_lazy(v)) for k, v in string.iteritems()])
return dict([(k, ugettext_lazy(v)) for k, v in six.iteritems(string)])

View File

@ -34,7 +34,6 @@ from __future__ import unicode_literals
import datetime
import cairo
import pycha.line
import StringIO
import time
import six
@ -116,7 +115,7 @@ def make(obj, counterType, **kwargs):
chart.addDataset(dataset)
chart.render()
output = StringIO.StringIO()
output = six.StringIO()
surface.write_to_png(output)

View File

@ -37,7 +37,7 @@ from uds.core.ui.UserInterface import gui
from uds.core.reports import stock
from uds.models import Authenticator
import StringIO
import six
import csv
from .base import ListReport
@ -131,7 +131,7 @@ class ListReportUsers(ListReport):
auth = Authenticator.objects.get(uuid=self.authenticator.value)
users = auth.users.order_by('name')
output = StringIO.StringIO()
output = six.StringIO()
report = UsersReport(queryset=users)
report.title = _('Users List for {}').format(auth.name)

View File

@ -39,7 +39,6 @@ from uds.core.ui.UserInterface import gui
from uds.core.reports.tools import UDSImage, UDSGeraldoReport
from uds.core.util.stats import events
import StringIO
import csv
import cairo
@ -56,6 +55,7 @@ from reportlab.lib.enums import TA_RIGHT, TA_CENTER
from PIL import Image as PILImage
import datetime
import six
import logging
logger = logging.getLogger(__name__)
@ -333,7 +333,7 @@ class StatsReportLogin(StatsReport):
img3 = PILImage.frombuffer("RGBA", (surface.get_width(), surface.get_height()), surface.get_data(), "raw", "BGRA", 0, 1)
output = StringIO.StringIO()
output = six.StringIO()
queryset = [
{'image': img, 'image2': img2, 'image3': img3, 'data': reportData}

View File

@ -40,8 +40,8 @@ from uds.core.ui.UserInterface import gui
from uds.core.reports.tools import UDSImage, UDSGeraldoReport
from uds.core.util.stats import events
import StringIO
import csv
import six
import cairo
import pycha.line
@ -335,7 +335,7 @@ class PoolPerformanceReport(StatsReport):
logger.debug(queryset)
output = StringIO.StringIO()
output = six.StringIO()
try:
report = AccessReport(queryset=queryset)

View File

@ -39,13 +39,12 @@ from uds.core.reports.tools import UDSGeraldoReport
from uds.core.util.stats import events
import StringIO
import six
import csv
from .base import StatsReport
from uds.core.util import tools
from uds.models import ServicePool
from geraldo.generators.pdf import PDFGenerator
from geraldo import ReportBand, ObjectValue, Label
@ -161,7 +160,7 @@ class UsageByPool(StatsReport):
def generate(self):
items, poolName = self.getData()
output = StringIO.StringIO()
output = six.StringIO()
report = UsersReport(queryset=items)
report.title = _('Users usage list for {}').format(poolName)

View File

@ -40,7 +40,7 @@ import re
import logging
import six
import xmlrpclib
import six
from uds.core.util import xml2dict
__updated__ = '2016-11-10'
@ -111,7 +111,7 @@ class OpenNebulaClient(object):
if self.connection is not None:
return
self.connection = xmlrpclib.ServerProxy(self.endpoint)
self.connection = six.moves.xmlrpc_client.ServerProxy(self.endpoint)
@ensureConnected
def enumStorage(self, storageType=0):

View File

@ -30,8 +30,8 @@ from __future__ import unicode_literals
import six
import XenAPI
import xmlrpclib
import ssl
import six
import logging
@ -101,7 +101,7 @@ class XenPowerState(object):
class XenServer(object):
def __init__(self, host, port, username, password, useSSL=False, verifySSL=False):
self._originalHost = self._host = host
self._port = unicode(port)
self._port = six.text_type(port)
self._useSSL = useSSL and True or False
self._verifySSL = verifySSL and True or False
self._protocol = 'http' + (self._useSSL and 's' or '') + '://'
@ -155,7 +155,7 @@ class XenServer(object):
if self._useSSL and self._verifySSL is False:
context = ssl.SSLContext(ssl.PROTOCOL_SSLv23) # @UndefinedVariable
context.verify_mode = ssl.CERT_NONE
transport = xmlrpclib.SafeTransport(context=context)
transport = six.moves.xmlrpc_client.SafeTransport(context=context)
else:
transport = None
@ -163,7 +163,7 @@ class XenServer(object):
self._session.xenapi.login_with_password(self._username, self._password)
self._loggedIn = True
self._apiVersion = self._session.API_version
self._poolName = unicode(self.getPoolName())
self._poolName = six.text_type(self.getPoolName())
except XenAPI.Failure as e: # XenAPI.Failure: ['HOST_IS_SLAVE', '172.27.0.29'] indicates that this host is an slave of 172.27.0.29, connect to it...
if switchToMaster and e.details[0] == 'HOST_IS_SLAVE':
logger.info('{0} is an Slave, connecting to master at {1} cause switchToMaster is True'.format(self._host, e.details[1]))
@ -216,7 +216,7 @@ class XenServer(object):
status = 'failure'
except Exception as e:
logger.exception('Unexpected exception!')
result = unicode(e)
result = six.text_type(e)
status = 'failure'
# Removes <value></value> if present
@ -227,9 +227,9 @@ class XenServer(object):
try:
self.task.destroy(task)
except Exception as e:
logger.info('Task {0} returned error {1}'.format(task, unicode(e)))
logger.info('Task {0} returned error {1}'.format(task, six.text_type(e)))
return {'result': result, 'progress': progress, 'status': unicode(status)}
return {'result': result, 'progress': progress, 'status': six.text_type(status)}
def getSRs(self):
for srId in self.SR.get_all():
@ -293,7 +293,7 @@ class XenServer(object):
except XenAPI.Failure as e:
raise XenFailure(e.details)
except Exception as e:
raise XenException(unicode(e))
raise XenException(six.text_type(e))
def getVMPowerState(self, vmId):
try:
@ -425,7 +425,7 @@ class XenServer(object):
if memory is not None:
logger.debug('Setting up memory to {0} MB'.format(memory))
# Convert memory to MB
memory = unicode(int(memory) * 1024 * 1024)
memory = six.text_type(int(memory) * 1024 * 1024)
self.VM.set_memory_limits(vmId, memory, memory, memory, memory)
except XenAPI.Failure as e:
raise XenFailure(e.details)

View File

@ -142,7 +142,7 @@ def error(request, idError):
'''
idError = int(idError)
code = idError >> 8
idError = idError & 0xFF
idError &= 0xFF
errStr = errorString(idError)
if code != 0:

View File

@ -35,8 +35,9 @@ from __future__ import unicode_literals
from django.utils.translation import ugettext_lazy as _, ugettext
from django import forms
from django.utils.safestring import mark_safe
from django.forms.forms import NON_FIELD_ERRORS
from uds.models import Authenticator
import six
import logging
logger = logging.getLogger(__name__)
@ -57,7 +58,7 @@ class CustomSelect(forms.Select):
for choice in self.choices:
res += '<option value="{0}">{1}</option>'.format(choice[0], choice[1])
res += '</select>'
return mark_safe('<div class="form-group"{0}><label>'.format(visible) + unicode(_('authenticator')) + '</label>' + res + '</div>')
return mark_safe('<div class="form-group"{0}><label>'.format(visible) + six.text_type(_('authenticator')) + '</label>' + res + '</div>')
class LoginForm(forms.Form):
user = forms.CharField(label=_('Username'), max_length=64, widget=forms.TextInput())

View File

@ -36,6 +36,7 @@ from functools import wraps
import base64
import random
import string
import six
import logging
@ -69,7 +70,7 @@ def transformId(view_func):
def scrambleId(request, id_):
if request.session.get(SCRAMBLE_SES) is None:
request.session[SCRAMBLE_SES] = ''.join(random.choice(string.letters) for _ in range(SCRAMBLE_LEN))
return base64.b64encode(unicode(id_) + request.session.get(SCRAMBLE_SES)).encode('hex')
return base64.b64encode(six.text_type(id_) + request.session.get(SCRAMBLE_SES)).encode('hex')
def unscrambleId(request, id_):