mirror of
https://github.com/dkmstr/openuds.git
synced 2025-01-24 02:04:09 +03:00
latests fixes for uds.core.utils for 3.7
This commit is contained in:
parent
de24aaab93
commit
064423413b
@ -1,7 +1,7 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
#
|
||||
# Copyright (c) 2012 Virtual Cable S.L.
|
||||
# Copyright (c) 2012-2019 Virtual Cable S.L.
|
||||
# All rights reserved.
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without modification,
|
||||
@ -30,33 +30,35 @@
|
||||
"""
|
||||
@author: Adolfo Gómez, dkmaster at dkmon dot com
|
||||
"""
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import transaction
|
||||
from uds.models.Storage import Storage as dbStorage
|
||||
from uds.core.util import encoders
|
||||
import hashlib
|
||||
import logging
|
||||
import pickle
|
||||
import six
|
||||
import typing
|
||||
|
||||
from django.db import transaction
|
||||
from uds.models.Storage import Storage as DBStorage
|
||||
from uds.core.util import encoders
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Storage(object):
|
||||
class Storage:
|
||||
_owner: str
|
||||
_bownwer: bytes
|
||||
|
||||
def __init__(self, owner):
|
||||
self._owner = owner.encode('utf-8') if isinstance(owner, six.text_type) else owner
|
||||
def __init__(self, owner: typing.Union[str, bytes]):
|
||||
self._owner = owner.decode('utf-8') if isinstance(owner, bytes) else owner
|
||||
self._bowner = self._owner.encode('utf8')
|
||||
|
||||
def __getKey(self, key):
|
||||
def __getKey(self, key: typing.Union[str, bytes]) -> str:
|
||||
h = hashlib.md5()
|
||||
h.update(self._owner)
|
||||
h.update(key.encode('utf8') if isinstance(key, six.text_type) else key)
|
||||
h.update(self._bowner)
|
||||
h.update(key.encode('utf8') if isinstance(key, str) else key)
|
||||
return h.hexdigest()
|
||||
|
||||
def saveData(self, skey, data, attr1=None):
|
||||
def saveData(self, skey: typing.Union[str, bytes], data: typing.Any, attr1: typing.Optional[str] = None) -> None:
|
||||
# If None is to be saved, remove
|
||||
if data == None:
|
||||
if not data:
|
||||
self.remove(skey)
|
||||
return
|
||||
|
||||
@ -66,64 +68,64 @@ class Storage(object):
|
||||
data = encoders.encode(data, 'base64', asText=True)
|
||||
attr1 = '' if attr1 is None else attr1
|
||||
try:
|
||||
dbStorage.objects.create(owner=self._owner, key=key, data=data, attr1=attr1) # @UndefinedVariable
|
||||
DBStorage.objects.create(owner=self._owner, key=key, data=data, attr1=attr1) # @UndefinedVariable
|
||||
except Exception:
|
||||
dbStorage.objects.filter(key=key).update(owner=self._owner, data=data, attr1=attr1) # @UndefinedVariable
|
||||
DBStorage.objects.filter(key=key).update(owner=self._owner, data=data, attr1=attr1) # @UndefinedVariable
|
||||
# logger.debug('Key saved')
|
||||
|
||||
def put(self, skey, data):
|
||||
def put(self, skey: typing.Union[str, bytes], data: typing.Any) -> None:
|
||||
return self.saveData(skey, data)
|
||||
|
||||
def putPickle(self, skey, data, attr1=None):
|
||||
def putPickle(self, skey: typing.Union[str, bytes], data: typing.Any, attr1: typing.Optional[str] = None) -> None:
|
||||
return self.saveData(skey, pickle.dumps(data), attr1) # Protocol 2 is compatible with python 2.7. This will be unnecesary when fully migrated
|
||||
|
||||
def updateData(self, skey, data, attr1=None):
|
||||
def updateData(self, skey: typing.Union[str, bytes], data: typing.Any, attr1: typing.Optional[str] = None) -> None:
|
||||
self.saveData(skey, data, attr1)
|
||||
|
||||
def readData(self, skey, fromPickle=False):
|
||||
def readData(self, skey: typing.Union[str, bytes], fromPickle: bool = False) -> typing.Optional[typing.Union[str, bytes]]:
|
||||
try:
|
||||
key = self.__getKey(skey)
|
||||
logger.debug('Accesing to {0} {1}'.format(skey, key))
|
||||
c = dbStorage.objects.get(pk=key) # @UndefinedVariable
|
||||
val = encoders.decode(c.data, 'base64')
|
||||
logger.debug('Accesing to %s %s', skey, key)
|
||||
c: DBStorage = DBStorage.objects.get(pk=key) # @UndefinedVariable
|
||||
val: bytes = typing.cast(bytes, encoders.decode(c.data, 'base64'))
|
||||
|
||||
if fromPickle:
|
||||
return val
|
||||
|
||||
try:
|
||||
return val.decode('utf-8') # Tries to encode in utf-8
|
||||
except:
|
||||
except Exception:
|
||||
return val
|
||||
except dbStorage.DoesNotExist: # @UndefinedVariable
|
||||
except DBStorage.DoesNotExist: # @UndefinedVariable
|
||||
logger.debug('key not found')
|
||||
return None
|
||||
|
||||
def get(self, skey):
|
||||
def get(self, skey: typing.Union[str, bytes]) -> typing.Optional[typing.Union[str, bytes]]:
|
||||
return self.readData(skey)
|
||||
|
||||
def getPickle(self, skey):
|
||||
def getPickle(self, skey: typing.Union[str, bytes]) -> typing.Any:
|
||||
v = self.readData(skey, True)
|
||||
if v is not None:
|
||||
v = pickle.loads(v)
|
||||
return v
|
||||
if v:
|
||||
return pickle.loads(typing.cast(bytes, v))
|
||||
return None
|
||||
|
||||
def getPickleByAttr1(self, attr1, forUpdate=False):
|
||||
def getPickleByAttr1(self, attr1: str, forUpdate: bool = False):
|
||||
try:
|
||||
query = dbStorage.objects.filter(owner=self._owner, attr1=attr1)
|
||||
query = DBStorage.objects.filter(owner=self._owner, attr1=attr1)
|
||||
if forUpdate:
|
||||
query = filter.select_for_update()
|
||||
return pickle.loads(encoders.decode(query[0].data, 'base64')) # @UndefinedVariable
|
||||
query = query.select_for_update()
|
||||
return pickle.loads(typing.cast(bytes, encoders.decode(query[0].data, 'base64'))) # @UndefinedVariable
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
def remove(self, skey):
|
||||
def remove(self, skey: typing.Union[str, bytes]) -> None:
|
||||
try:
|
||||
if isinstance(skey, (list, tuple)):
|
||||
# Process several keys at once
|
||||
dbStorage.objects.filter(key__in=[self.__getKey(k) for k in skey])
|
||||
DBStorage.objects.filter(key__in=[self.__getKey(k) for k in skey])
|
||||
else:
|
||||
key = self.__getKey(skey)
|
||||
dbStorage.objects.filter(key=key).delete() # @UndefinedVariable
|
||||
DBStorage.objects.filter(key=key).delete() # @UndefinedVariable
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
@ -132,29 +134,27 @@ class Storage(object):
|
||||
Use with care. If locked, it must be unlocked before returning
|
||||
"""
|
||||
# dbStorage.objects.lock() # @UndefinedVariable
|
||||
pass
|
||||
|
||||
def unlock(self):
|
||||
"""
|
||||
Must be used to unlock table
|
||||
"""
|
||||
# dbStorage.objects.unlock() # @UndefinedVariable
|
||||
pass
|
||||
|
||||
def locateByAttr1(self, attr1):
|
||||
if isinstance(attr1, (list, tuple)):
|
||||
query = dbStorage.objects.filter(owner=self._owner, attr1_in=attr1) # @UndefinedVariable
|
||||
def locateByAttr1(self, attr1: typing.Union[typing.Iterable[str], str]) -> typing.Generator[bytes, None,None]:
|
||||
if isinstance(attr1, str):
|
||||
query = DBStorage.objects.filter(owner=self._owner, attr1=attr1) # @UndefinedVariable
|
||||
else:
|
||||
query = dbStorage.objects.filter(owner=self._owner, attr1=attr1) # @UndefinedVariable
|
||||
query = DBStorage.objects.filter(owner=self._owner, attr1_in=attr1) # @UndefinedVariable
|
||||
|
||||
for v in query:
|
||||
yield encoders.decode(v.data, 'base64')
|
||||
yield typing.cast(bytes, encoders.decode(v.data, 'base64'))
|
||||
|
||||
def filter(self, attr1, forUpdate=False):
|
||||
def filter(self, attr1: typing.Optional[str], forUpdate: bool = False):
|
||||
if attr1 is None:
|
||||
query = dbStorage.objects.filter(owner=self._owner) # @UndefinedVariable
|
||||
query = DBStorage.objects.filter(owner=self._owner) # @UndefinedVariable
|
||||
else:
|
||||
query = dbStorage.objects.filter(owner=self._owner, attr1=attr1) # @UndefinedVariable
|
||||
query = DBStorage.objects.filter(owner=self._owner, attr1=attr1) # @UndefinedVariable
|
||||
|
||||
if forUpdate:
|
||||
query = query.select_for_update()
|
||||
@ -162,14 +162,14 @@ class Storage(object):
|
||||
for v in query: # @UndefinedVariable
|
||||
yield (v.key, encoders.decode(v.data, 'base64'), v.attr1)
|
||||
|
||||
def filterPickle(self, attr1=None, forUpdate=False):
|
||||
def filterPickle(self, attr1: typing.Optional[str] = None, forUpdate: bool = False):
|
||||
for v in self.filter(attr1, forUpdate):
|
||||
yield (v[0], pickle.loads(v[1]), v[2])
|
||||
|
||||
@staticmethod
|
||||
def delete(owner=None):
|
||||
def delete(owner: typing.Optional[str] = None):
|
||||
if owner is None:
|
||||
objects = dbStorage.objects.all() # @UndefinedVariable
|
||||
objects = DBStorage.objects.all() # @UndefinedVariable
|
||||
else:
|
||||
objects = dbStorage.objects.filter(owner=owner) # @UndefinedVariable
|
||||
objects = DBStorage.objects.filter(owner=owner) # @UndefinedVariable
|
||||
objects.delete()
|
||||
|
@ -27,12 +27,9 @@
|
||||
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import six
|
||||
from threading import Thread
|
||||
|
||||
import logging
|
||||
import queue
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@ -53,7 +50,7 @@ class Worker(Thread):
|
||||
while self._stop is False:
|
||||
try:
|
||||
func, args, kargs = self._tasks.get(block=True, timeout=1)
|
||||
except six.moves.queue.Empty:
|
||||
except queue.Empty:
|
||||
continue
|
||||
|
||||
try:
|
||||
@ -66,7 +63,7 @@ class Worker(Thread):
|
||||
|
||||
class ThreadPool:
|
||||
def __init__(self, num_threads, queueSize=DEFAULT_QUEUE_SIZE):
|
||||
self._tasks = six.moves.queue.Queue(queueSize)
|
||||
self._tasks = queue.Queue(queueSize)
|
||||
self._numThreads = num_threads
|
||||
self._threads = []
|
||||
|
||||
@ -74,7 +71,7 @@ class ThreadPool:
|
||||
"""
|
||||
Add a task to the queue
|
||||
"""
|
||||
if len(self._threads) == 0:
|
||||
if not self._threads:
|
||||
for _ in range(self._numThreads):
|
||||
self._threads.append(Worker(self._tasks))
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
#
|
||||
# Copyright (c) 2012 Virtual Cable S.L.
|
||||
# Copyright (c) 2012-2019 Virtual Cable S.L.
|
||||
# All rights reserved.
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without modification,
|
||||
@ -30,10 +30,9 @@
|
||||
"""
|
||||
@author: Adolfo Gómez, dkmaster at dkmon dot com
|
||||
"""
|
||||
from __future__ import unicode_literals
|
||||
import logging
|
||||
|
||||
from .UniqueIDGenerator import UniqueIDGenerator, MAX_SEQ
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
#
|
||||
# Copyright (c) 2012 Virtual Cable S.L.
|
||||
# Copyright (c) 2012-2019 Virtual Cable S.L.
|
||||
# All rights reserved.
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without modification,
|
||||
@ -30,12 +30,11 @@
|
||||
"""
|
||||
@author: Adolfo Gómez, dkmaster at dkmon dot com
|
||||
"""
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from .UniqueIDGenerator import UniqueIDGenerator
|
||||
import logging
|
||||
import re
|
||||
|
||||
from .UniqueIDGenerator import UniqueIDGenerator
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@ -53,16 +52,16 @@ class UniqueMacGenerator(UniqueIDGenerator):
|
||||
return re.sub(r"(..)", r"\1:", "%0*X" % (12, seq))[:-1]
|
||||
|
||||
# noinspection PyMethodOverriding
|
||||
def get(self, macRange):
|
||||
def get(self, macRange): # pylint: disable=arguments-differ
|
||||
firstMac, lastMac = macRange.split('-')
|
||||
firstMac = self.__toInt(firstMac)
|
||||
lastMac = self.__toInt(lastMac)
|
||||
return self.__toMac(super(UniqueMacGenerator, self).get(firstMac, lastMac))
|
||||
|
||||
def transfer(self, mac, toUMgen):
|
||||
def transfer(self, mac, toUMgen): # pylint: disable=arguments-differ
|
||||
super(UniqueMacGenerator, self).transfer(self.__toInt(mac), toUMgen)
|
||||
|
||||
def free(self, mac):
|
||||
def free(self, mac): # pylint: disable=arguments-differ
|
||||
super(UniqueMacGenerator, self).free(self.__toInt(mac))
|
||||
|
||||
# Release is inherited, no mod needed
|
||||
|
@ -1,7 +1,7 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
#
|
||||
# Copyright (c) 2012 Virtual Cable S.L.
|
||||
# Copyright (c) 2012-2019 Virtual Cable S.L.
|
||||
# All rights reserved.
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without modification,
|
||||
@ -30,10 +30,9 @@
|
||||
"""
|
||||
@author: Adolfo Gómez, dkmaster at dkmon dot com
|
||||
"""
|
||||
from __future__ import unicode_literals
|
||||
import logging
|
||||
|
||||
from .UniqueIDGenerator import UniqueIDGenerator
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@ -49,16 +48,16 @@ class UniqueNameGenerator(UniqueIDGenerator):
|
||||
raise KeyError('No more names available. Please, increase service digits.')
|
||||
return "%s%0*d" % (self._baseName, length, seq)
|
||||
|
||||
def get(self, baseName, length=5):
|
||||
def get(self, baseName, length=5): # pylint: disable=arguments-differ
|
||||
self.setBaseName(baseName)
|
||||
minVal = 0
|
||||
maxVal = 10 ** length - 1
|
||||
return self.__toName(super(UniqueNameGenerator, self).get(minVal, maxVal), length)
|
||||
|
||||
def transfer(self, baseName, name, toUNGen):
|
||||
def transfer(self, baseName, name, toUNGen): # pylint: disable=arguments-differ
|
||||
self.setBaseName(baseName)
|
||||
super(UniqueNameGenerator, self).transfer(int(name[len(self._baseName):]), toUNGen)
|
||||
|
||||
def free(self, baseName, name):
|
||||
def free(self, baseName, name): # pylint: disable=arguments-differ
|
||||
self.setBaseName(baseName)
|
||||
super(UniqueNameGenerator, self).free(int(name[len(self._baseName):]))
|
||||
|
@ -30,10 +30,9 @@
|
||||
"""
|
||||
@author: Adolfo Gómez, dkmaster at dkmon dot com
|
||||
"""
|
||||
from __future__ import unicode_literals
|
||||
|
||||
# Helper to acommodate all unique generators in one place
|
||||
# pylint: disable=unused-import
|
||||
|
||||
from .UniqueGIDGenerator import UniqueGIDGenerator # @UnusedImport
|
||||
from .UniqueMacGenerator import UniqueMacGenerator # @UnusedImport
|
||||
from .UniqueNameGenerator import UniqueNameGenerator # @UnusedImport
|
||||
from .UniqueGIDGenerator import UniqueGIDGenerator
|
||||
from .UniqueMacGenerator import UniqueMacGenerator
|
||||
from .UniqueNameGenerator import UniqueNameGenerator
|
||||
|
@ -1,7 +1,7 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
#
|
||||
# Copyright (c) 2014 Virtual Cable S.L.
|
||||
# Copyright (c) 2014-2019 Virtual Cable S.L.
|
||||
# All rights reserved.
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without modification,
|
||||
@ -30,19 +30,24 @@
|
||||
"""
|
||||
@author: Adolfo Gómez, dkmaster at dkmon dot com
|
||||
"""
|
||||
from __future__ import unicode_literals
|
||||
import re
|
||||
import logging
|
||||
import typing
|
||||
|
||||
from uds.core.module import Module
|
||||
from django.utils.translation import gettext as _
|
||||
|
||||
import re
|
||||
import logging
|
||||
import six
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def validateNumeric(numericStr, minValue=None, maxValue=None, returnAsInteger=True, fieldName=None):
|
||||
def validateNumeric(
|
||||
numericStr: str,
|
||||
minValue: typing.Optional[int] = None,
|
||||
maxValue: typing.Optional[int] = None,
|
||||
returnAsInteger: bool = True,
|
||||
fieldName: typing.Optional[str] = None
|
||||
) -> typing.Union[str, int]:
|
||||
"""
|
||||
Validates that a numeric value is valid
|
||||
:param numericStr: Numeric value to check (as string)
|
||||
@ -53,7 +58,7 @@ def validateNumeric(numericStr, minValue=None, maxValue=None, returnAsInteger=Tr
|
||||
:return: Raises Module.Validation exception if is invalid, else return the value "fixed"
|
||||
"""
|
||||
numericStr = numericStr.replace(' ', '')
|
||||
fieldName = fieldName if fieldName is not None else _('Numeric')
|
||||
fieldName = fieldName or _('Numeric')
|
||||
|
||||
try:
|
||||
numeric = int(numericStr)
|
||||
@ -63,7 +68,7 @@ def validateNumeric(numericStr, minValue=None, maxValue=None, returnAsInteger=Tr
|
||||
if maxValue is not None and numeric > maxValue:
|
||||
raise Module.ValidationException(_('{0} must be lower than or equal to {1}'.format(fieldName, maxValue)))
|
||||
|
||||
numericStr = six.u(str(numeric))
|
||||
numericStr = str(numeric)
|
||||
|
||||
except ValueError:
|
||||
raise Module.ValidationException(_('{0} contains invalid characters').format(fieldName))
|
||||
@ -74,7 +79,7 @@ def validateNumeric(numericStr, minValue=None, maxValue=None, returnAsInteger=Tr
|
||||
return numericStr
|
||||
|
||||
|
||||
def validatePort(portStr, returnAsInteger=True):
|
||||
def validatePort(portStr: str, returnAsInteger: bool = True) -> typing.Union[str, int]:
|
||||
"""
|
||||
Validates that a port number is valid
|
||||
:param portStr: port to validate, as string
|
||||
@ -84,7 +89,7 @@ def validatePort(portStr, returnAsInteger=True):
|
||||
return validateNumeric(portStr, minValue=0, maxValue=65535, returnAsInteger=returnAsInteger, fieldName='Port')
|
||||
|
||||
|
||||
def validateTimeout(timeOutStr, returnAsInteger=True):
|
||||
def validateTimeout(timeOutStr, returnAsInteger: bool = True) -> typing.Union[str, int]:
|
||||
"""
|
||||
Validates that a timeout value is valid
|
||||
:param timeOutStr: timeout to validate
|
||||
@ -94,7 +99,7 @@ def validateTimeout(timeOutStr, returnAsInteger=True):
|
||||
return validateNumeric(timeOutStr, minValue=0, returnAsInteger=returnAsInteger, fieldName='Timeout')
|
||||
|
||||
|
||||
def validateMacRange(macRange):
|
||||
def validateMacRange(macRange: str) -> str:
|
||||
"""
|
||||
Corrects mac range (uppercase, without spaces), and checks that is range is valid
|
||||
:param macRange: Range to fix
|
||||
|
@ -6,7 +6,7 @@ Created on Jul 11, 2016
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
#
|
||||
# Copyright (c) 2013 Virtual Cable S.L.
|
||||
# Copyright (c) 2013-2019 Virtual Cable S.L.
|
||||
# All rights reserved.
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without modification,
|
||||
@ -31,8 +31,8 @@ Created on Jul 11, 2016
|
||||
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
import typing
|
||||
|
||||
from __future__ import unicode_literals
|
||||
from collections import defaultdict
|
||||
from xml.etree import cElementTree
|
||||
|
||||
@ -56,6 +56,5 @@ def etree_to_dict(t):
|
||||
d[t.tag] = text
|
||||
return d
|
||||
|
||||
def parse(xml_string):
|
||||
return etree_to_dict(cElementTree.XML(xml_string)) # @UndefinedVariable
|
||||
|
||||
def parse(xml_string: str) -> typing.Dict:
|
||||
return etree_to_dict(cElementTree.XML(xml_string))
|
||||
|
Loading…
x
Reference in New Issue
Block a user