Merge "Add verigraph code base"
[parser.git] / tosca2heat / tosca-parser / toscaparser / elements / scalarunit.py
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
4 #
5 #         http://www.apache.org/licenses/LICENSE-2.0
6 #
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
11 #    under the License.
12
13 import logging
14 import re
15
16 from toscaparser.common.exception import ExceptionCollector
17 from toscaparser.utils.gettextutils import _
18 from toscaparser.utils import validateutils
19
20 log = logging.getLogger('tosca')
21
22
23 class ScalarUnit(object):
24     '''Parent class for scalar-unit type.'''
25
26     SCALAR_UNIT_TYPES = (
27         SCALAR_UNIT_SIZE, SCALAR_UNIT_FREQUENCY, SCALAR_UNIT_TIME
28     ) = (
29         'scalar-unit.size', 'scalar-unit.frequency', 'scalar-unit.time'
30     )
31
32     def __init__(self, value):
33         self.value = value
34
35     def _check_unit_in_scalar_standard_units(self, input_unit):
36         """Check whether the input unit is following specified standard
37
38         If unit is not following specified standard, convert it to standard
39         unit after displaying a warning message.
40         """
41         if input_unit in self.SCALAR_UNIT_DICT.keys():
42             return input_unit
43         else:
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,
49                                                  'key': key})
50                     return key
51             msg = (_('The unit "%(unit)s" is not valid. Valid units are '
52                      '"%(valid_units)s".') %
53                    {'unit': input_unit,
54                     'valid_units': sorted(self.SCALAR_UNIT_DICT.keys())})
55             ExceptionCollector.appendException(ValueError(msg))
56
57     def validate_scalar_unit(self):
58         regex = re.compile('([0-9.]+)\s*(\w+)')
59         try:
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])
64             return self.value
65
66         except Exception:
67             ExceptionCollector.appendException(
68                 ValueError(_('"%s" is not a valid scalar-unit.')
69                            % self.value))
70
71     def get_num_from_scalar_unit(self, unit=None):
72         if unit:
73             unit = self._check_unit_in_scalar_standard_units(unit)
74         else:
75             unit = self.SCALAR_UNIT_DEFAULT
76         self.validate_scalar_unit()
77
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)
85         return converted
86
87
88 class ScalarUnit_Size(ScalarUnit):
89
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,
94                         'TiB': 1099511627776}
95
96
97 class ScalarUnit_Time(ScalarUnit):
98
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}
102
103
104 class ScalarUnit_Frequency(ScalarUnit):
105
106     SCALAR_UNIT_DEFAULT = 'GHz'
107     SCALAR_UNIT_DICT = {'Hz': 1, 'kHz': 1000,
108                         'MHz': 1000000, 'GHz': 1000000000}
109
110
111 scalarunit_mapping = {
112     ScalarUnit.SCALAR_UNIT_FREQUENCY: ScalarUnit_Frequency,
113     ScalarUnit.SCALAR_UNIT_SIZE: ScalarUnit_Size,
114     ScalarUnit.SCALAR_UNIT_TIME: ScalarUnit_Time,
115     }
116
117
118 def get_scalarunit_class(type):
119     return scalarunit_mapping.get(type)
120
121
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))
127     else:
128         ExceptionCollector.appendException(
129             TypeError(_('"%s" is not a valid scalar-unit type.') % type))