Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | import errno |
| 2 | import re |
| 3 | import os |
| 4 | |
| 5 | |
| 6 | class OEList(list): |
| 7 | """OpenEmbedded 'list' type |
| 8 | |
| 9 | Acts as an ordinary list, but is constructed from a string value and a |
| 10 | separator (optional), and re-joins itself when converted to a string with |
| 11 | str(). Set the variable type flag to 'list' to use this type, and the |
| 12 | 'separator' flag may be specified (defaulting to whitespace).""" |
| 13 | |
| 14 | name = "list" |
| 15 | |
| 16 | def __init__(self, value, separator = None): |
| 17 | if value is not None: |
| 18 | list.__init__(self, value.split(separator)) |
| 19 | else: |
| 20 | list.__init__(self) |
| 21 | |
| 22 | if separator is None: |
| 23 | self.separator = " " |
| 24 | else: |
| 25 | self.separator = separator |
| 26 | |
| 27 | def __str__(self): |
| 28 | return self.separator.join(self) |
| 29 | |
| 30 | def choice(value, choices): |
| 31 | """OpenEmbedded 'choice' type |
| 32 | |
| 33 | Acts as a multiple choice for the user. To use this, set the variable |
| 34 | type flag to 'choice', and set the 'choices' flag to a space separated |
| 35 | list of valid values.""" |
| 36 | if not isinstance(value, basestring): |
| 37 | raise TypeError("choice accepts a string, not '%s'" % type(value)) |
| 38 | |
| 39 | value = value.lower() |
| 40 | choices = choices.lower() |
| 41 | if value not in choices.split(): |
| 42 | raise ValueError("Invalid choice '%s'. Valid choices: %s" % |
| 43 | (value, choices)) |
| 44 | return value |
| 45 | |
| 46 | class NoMatch(object): |
| 47 | """Stub python regex pattern object which never matches anything""" |
| 48 | def findall(self, string, flags=0): |
| 49 | return None |
| 50 | |
| 51 | def finditer(self, string, flags=0): |
| 52 | return None |
| 53 | |
| 54 | def match(self, flags=0): |
| 55 | return None |
| 56 | |
| 57 | def search(self, string, flags=0): |
| 58 | return None |
| 59 | |
| 60 | def split(self, string, maxsplit=0): |
| 61 | return None |
| 62 | |
| 63 | def sub(pattern, repl, string, count=0): |
| 64 | return None |
| 65 | |
| 66 | def subn(pattern, repl, string, count=0): |
| 67 | return None |
| 68 | |
| 69 | NoMatch = NoMatch() |
| 70 | |
| 71 | def regex(value, regexflags=None): |
| 72 | """OpenEmbedded 'regex' type |
| 73 | |
| 74 | Acts as a regular expression, returning the pre-compiled regular |
| 75 | expression pattern object. To use this type, set the variable type flag |
| 76 | to 'regex', and optionally, set the 'regexflags' type to a space separated |
| 77 | list of the flags to control the regular expression matching (e.g. |
| 78 | FOO[regexflags] += 'ignorecase'). See the python documentation on the |
| 79 | 're' module for a list of valid flags.""" |
| 80 | |
| 81 | flagval = 0 |
| 82 | if regexflags: |
| 83 | for flag in regexflags.split(): |
| 84 | flag = flag.upper() |
| 85 | try: |
| 86 | flagval |= getattr(re, flag) |
| 87 | except AttributeError: |
| 88 | raise ValueError("Invalid regex flag '%s'" % flag) |
| 89 | |
| 90 | if not value: |
| 91 | # Let's ensure that the default behavior for an undefined or empty |
| 92 | # variable is to match nothing. If the user explicitly wants to match |
| 93 | # anything, they can match '.*' instead. |
| 94 | return NoMatch |
| 95 | |
| 96 | try: |
| 97 | return re.compile(value, flagval) |
| 98 | except re.error as exc: |
| 99 | raise ValueError("Invalid regex value '%s': %s" % |
| 100 | (value, exc.args[0])) |
| 101 | |
| 102 | def boolean(value): |
| 103 | """OpenEmbedded 'boolean' type |
| 104 | |
| 105 | Valid values for true: 'yes', 'y', 'true', 't', '1' |
| 106 | Valid values for false: 'no', 'n', 'false', 'f', '0' |
| 107 | """ |
| 108 | |
| 109 | if not isinstance(value, basestring): |
| 110 | raise TypeError("boolean accepts a string, not '%s'" % type(value)) |
| 111 | |
| 112 | value = value.lower() |
| 113 | if value in ('yes', 'y', 'true', 't', '1'): |
| 114 | return True |
| 115 | elif value in ('no', 'n', 'false', 'f', '0'): |
| 116 | return False |
| 117 | raise ValueError("Invalid boolean value '%s'" % value) |
| 118 | |
| 119 | def integer(value, numberbase=10): |
| 120 | """OpenEmbedded 'integer' type |
| 121 | |
| 122 | Defaults to base 10, but this can be specified using the optional |
| 123 | 'numberbase' flag.""" |
| 124 | |
| 125 | return int(value, int(numberbase)) |
| 126 | |
| 127 | _float = float |
| 128 | def float(value, fromhex='false'): |
| 129 | """OpenEmbedded floating point type |
| 130 | |
| 131 | To use this type, set the type flag to 'float', and optionally set the |
| 132 | 'fromhex' flag to a true value (obeying the same rules as for the |
| 133 | 'boolean' type) if the value is in base 16 rather than base 10.""" |
| 134 | |
| 135 | if boolean(fromhex): |
| 136 | return _float.fromhex(value) |
| 137 | else: |
| 138 | return _float(value) |
| 139 | |
| 140 | def path(value, relativeto='', normalize='true', mustexist='false'): |
| 141 | value = os.path.join(relativeto, value) |
| 142 | |
| 143 | if boolean(normalize): |
| 144 | value = os.path.normpath(value) |
| 145 | |
| 146 | if boolean(mustexist): |
| 147 | try: |
| 148 | open(value, 'r') |
| 149 | except IOError as exc: |
| 150 | if exc.errno == errno.ENOENT: |
| 151 | raise ValueError("{0}: {1}".format(value, os.strerror(errno.ENOENT))) |
| 152 | |
| 153 | return value |