Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 1 | # Copyright 2014-2015 ARM Limited |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | # |
| 15 | |
| 16 | |
| 17 | """ |
| 18 | Routines for doing various type conversions. These usually embody some higher-level |
| 19 | semantics than are present in standard Python types (e.g. ``boolean`` will convert the |
| 20 | string ``"false"`` to ``False``, where as non-empty strings are usually considered to be |
| 21 | ``True``). |
| 22 | |
| 23 | A lot of these are intened to stpecify type conversions declaratively in place like |
| 24 | ``Parameter``'s ``kind`` argument. These are basically "hacks" around the fact that Python |
| 25 | is not the best language to use for configuration. |
| 26 | |
| 27 | """ |
| 28 | import math |
| 29 | |
| 30 | from devlib.utils.misc import isiterable, to_identifier, ranges_to_list, list_to_mask |
| 31 | |
| 32 | |
| 33 | def identifier(text): |
| 34 | """Converts text to a valid Python identifier by replacing all |
| 35 | whitespace and punctuation.""" |
| 36 | return to_identifier(text) |
| 37 | |
| 38 | |
| 39 | def boolean(value): |
| 40 | """ |
| 41 | Returns bool represented by the value. This is different from |
| 42 | calling the builtin bool() in that it will interpret string representations. |
| 43 | e.g. boolean('0') and boolean('false') will both yield False. |
| 44 | |
| 45 | """ |
| 46 | false_strings = ['', '0', 'n', 'no', 'off'] |
| 47 | if isinstance(value, basestring): |
| 48 | value = value.lower() |
| 49 | if value in false_strings or 'false'.startswith(value): |
| 50 | return False |
| 51 | return bool(value) |
| 52 | |
| 53 | |
| 54 | def integer(value): |
| 55 | """Handles conversions for string respresentations of binary, octal and hex.""" |
| 56 | if isinstance(value, basestring): |
| 57 | return int(value, 0) |
| 58 | else: |
| 59 | return int(value) |
| 60 | |
| 61 | |
| 62 | def numeric(value): |
| 63 | """ |
| 64 | Returns the value as number (int if possible, or float otherwise), or |
| 65 | raises ``ValueError`` if the specified ``value`` does not have a straight |
| 66 | forward numeric conversion. |
| 67 | |
| 68 | """ |
| 69 | if isinstance(value, int): |
| 70 | return value |
Sergei Trofimov | 3d10e3e | 2017-08-16 15:58:10 +0100 | [diff] [blame] | 71 | |
| 72 | if isinstance(value, basestring): |
| 73 | value = value.strip() |
| 74 | if value.endswith('%'): |
| 75 | try: |
| 76 | return float(value.rstrip('%')) / 100 |
| 77 | except ValueError: |
| 78 | raise ValueError('Not numeric: {}'.format(value)) |
| 79 | |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 80 | try: |
| 81 | fvalue = float(value) |
| 82 | except ValueError: |
| 83 | raise ValueError('Not numeric: {}'.format(value)) |
| 84 | if not math.isnan(fvalue) and not math.isinf(fvalue): |
| 85 | ivalue = int(fvalue) |
| 86 | if ivalue == fvalue: # yeah, yeah, I know. Whatever. This is best-effort. |
| 87 | return ivalue |
| 88 | return fvalue |
| 89 | |
| 90 | |
| 91 | class caseless_string(str): |
| 92 | """ |
| 93 | Just like built-in Python string except case-insensitive on comparisons. However, the |
| 94 | case is preserved otherwise. |
| 95 | |
| 96 | """ |
| 97 | |
| 98 | def __eq__(self, other): |
| 99 | if isinstance(other, basestring): |
| 100 | other = other.lower() |
| 101 | return self.lower() == other |
| 102 | |
| 103 | def __ne__(self, other): |
| 104 | return not self.__eq__(other) |
| 105 | |
| 106 | def __cmp__(self, other): |
| 107 | if isinstance(basestring, other): |
| 108 | other = other.lower() |
| 109 | return cmp(self.lower(), other) |
| 110 | |
| 111 | def format(self, *args, **kwargs): |
| 112 | return caseless_string(super(caseless_string, self).format(*args, **kwargs)) |
| 113 | |
| 114 | |
| 115 | def bitmask(value): |
| 116 | if isinstance(value, basestring): |
| 117 | value = ranges_to_list(value) |
| 118 | if isiterable(value): |
| 119 | value = list_to_mask(value) |
| 120 | if not isinstance(value, int): |
| 121 | raise ValueError(value) |
| 122 | return value |