1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
5 # http://www.apache.org/licenses/LICENSE-2.0
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
16 from toscaparser.common.exception import ExceptionCollector
17 from toscaparser.utils.gettextutils import _
18 from toscaparser.utils import validateutils
20 log = logging.getLogger('tosca')
23 class ScalarUnit(object):
24 '''Parent class for scalar-unit type.'''
27 SCALAR_UNIT_SIZE, SCALAR_UNIT_FREQUENCY, SCALAR_UNIT_TIME
29 'scalar-unit.size', 'scalar-unit.frequency', 'scalar-unit.time'
32 def __init__(self, value):
35 def _check_unit_in_scalar_standard_units(self, input_unit):
36 """Check whether the input unit is following specified standard
38 If unit is not following specified standard, convert it to standard
39 unit after displaying a warning message.
41 if input_unit in self.SCALAR_UNIT_DICT.keys():
44 for key in self.SCALAR_UNIT_DICT.keys():
45 if key.upper() == input_unit.upper():
46 log.warning(_('The unit "%(unit)s" does not follow '
47 'scalar unit standards; using "%(key)s" '
48 'instead.') % {'unit': input_unit,
51 msg = (_('The unit "%(unit)s" is not valid. Valid units are '
52 '"%(valid_units)s".') %
54 'valid_units': sorted(self.SCALAR_UNIT_DICT.keys())})
55 ExceptionCollector.appendException(ValueError(msg))
57 def validate_scalar_unit(self):
58 regex = re.compile('([0-9.]+)\s*(\w+)')
60 result = regex.match(str(self.value)).groups()
61 validateutils.str_to_num(result[0])
62 scalar_unit = self._check_unit_in_scalar_standard_units(result[1])
63 self.value = ' '.join([result[0], scalar_unit])
67 ExceptionCollector.appendException(
68 ValueError(_('"%s" is not a valid scalar-unit.')
71 def get_num_from_scalar_unit(self, unit=None):
73 unit = self._check_unit_in_scalar_standard_units(unit)
75 unit = self.SCALAR_UNIT_DEFAULT
76 self.validate_scalar_unit()
78 regex = re.compile('([0-9.]+)\s*(\w+)')
79 result = regex.match(str(self.value)).groups()
80 converted = (float(validateutils.str_to_num(result[0]))
81 * self.SCALAR_UNIT_DICT[result[1]]
82 / self.SCALAR_UNIT_DICT[unit])
83 if converted - int(converted) < 0.0000000000001:
84 converted = int(converted)
88 class ScalarUnit_Size(ScalarUnit):
90 SCALAR_UNIT_DEFAULT = 'B'
91 SCALAR_UNIT_DICT = {'B': 1, 'kB': 1000, 'KiB': 1024, 'MB': 1000000,
92 'MiB': 1048576, 'GB': 1000000000,
93 'GiB': 1073741824, 'TB': 1000000000000,
97 class ScalarUnit_Time(ScalarUnit):
99 SCALAR_UNIT_DEFAULT = 'ms'
100 SCALAR_UNIT_DICT = {'d': 86400, 'h': 3600, 'm': 60, 's': 1,
101 'ms': 0.001, 'us': 0.000001, 'ns': 0.000000001}
104 class ScalarUnit_Frequency(ScalarUnit):
106 SCALAR_UNIT_DEFAULT = 'GHz'
107 SCALAR_UNIT_DICT = {'Hz': 1, 'kHz': 1000,
108 'MHz': 1000000, 'GHz': 1000000000}
111 scalarunit_mapping = {
112 ScalarUnit.SCALAR_UNIT_FREQUENCY: ScalarUnit_Frequency,
113 ScalarUnit.SCALAR_UNIT_SIZE: ScalarUnit_Size,
114 ScalarUnit.SCALAR_UNIT_TIME: ScalarUnit_Time,
118 def get_scalarunit_class(type):
119 return scalarunit_mapping.get(type)
122 def get_scalarunit_value(type, value, unit=None):
123 if type in ScalarUnit.SCALAR_UNIT_TYPES:
124 ScalarUnit_Class = get_scalarunit_class(type)
125 return (ScalarUnit_Class(value).
126 get_num_from_scalar_unit(unit))
128 ExceptionCollector.appendException(
129 TypeError(_('"%s" is not a valid scalar-unit type.') % type))