Home | History | Annotate | Download | only in utils
      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
     71     try:
     72         fvalue = float(value)
     73     except ValueError:
     74         raise ValueError('Not numeric: {}'.format(value))
     75     if not math.isnan(fvalue) and not math.isinf(fvalue):
     76         ivalue = int(fvalue)
     77         if ivalue == fvalue:  # yeah, yeah, I know. Whatever. This is best-effort.
     78             return ivalue
     79     return fvalue
     80 
     81 
     82 class caseless_string(str):
     83     """
     84     Just like built-in Python string except case-insensitive on comparisons. However, the
     85     case is preserved otherwise.
     86 
     87     """
     88 
     89     def __eq__(self, other):
     90         if isinstance(other, basestring):
     91             other = other.lower()
     92         return self.lower() == other
     93 
     94     def __ne__(self, other):
     95         return not self.__eq__(other)
     96 
     97     def __cmp__(self, other):
     98         if isinstance(basestring, other):
     99             other = other.lower()
    100         return cmp(self.lower(), other)
    101 
    102     def format(self, *args, **kwargs):
    103         return caseless_string(super(caseless_string, self).format(*args, **kwargs))
    104 
    105 
    106 def bitmask(value):
    107     if isinstance(value, basestring):
    108         value = ranges_to_list(value)
    109     if isiterable(value):
    110         value = list_to_mask(value)
    111     if not isinstance(value, int):
    112         raise ValueError(value)
    113     return value
    114