summaryrefslogtreecommitdiffstats
path: root/meta/lib/oe/types.py
diff options
context:
space:
mode:
Diffstat (limited to 'meta/lib/oe/types.py')
-rw-r--r--meta/lib/oe/types.py104
1 files changed, 104 insertions, 0 deletions
diff --git a/meta/lib/oe/types.py b/meta/lib/oe/types.py
new file mode 100644
index 0000000000..ea31cf4219
--- /dev/null
+++ b/meta/lib/oe/types.py
@@ -0,0 +1,104 @@
1import re
2
3class OEList(list):
4 """OpenEmbedded 'list' type
5
6 Acts as an ordinary list, but is constructed from a string value and a
7 separator (optional), and re-joins itself when converted to a string with
8 str(). Set the variable type flag to 'list' to use this type, and the
9 'separator' flag may be specified (defaulting to whitespace)."""
10
11 name = "list"
12
13 def __init__(self, value, separator = None):
14 if value is not None:
15 list.__init__(self, value.split(separator))
16 else:
17 list.__init__(self)
18
19 if separator is None:
20 self.separator = " "
21 else:
22 self.separator = separator
23
24 def __str__(self):
25 return self.separator.join(self)
26
27def choice(value, choices):
28 """OpenEmbedded 'choice' type
29
30 Acts as a multiple choice for the user. To use this, set the variable
31 type flag to 'choice', and set the 'choices' flag to a space separated
32 list of valid values."""
33 if not isinstance(value, basestring):
34 raise TypeError("choice accepts a string, not '%s'" % type(value))
35
36 value = value.lower()
37 choices = choices.lower()
38 if value not in choices.split():
39 raise ValueError("Invalid choice '%s'. Valid choices: %s" %
40 (value, choices))
41 return value
42
43def regex(value, regexflags=None):
44 """OpenEmbedded 'regex' type
45
46 Acts as a regular expression, returning the pre-compiled regular
47 expression pattern object. To use this type, set the variable type flag
48 to 'regex', and optionally, set the 'regexflags' type to a space separated
49 list of the flags to control the regular expression matching (e.g.
50 FOO[regexflags] += 'ignorecase'). See the python documentation on the
51 're' module for a list of valid flags."""
52
53 flagval = 0
54 if regexflags:
55 for flag in regexflags.split():
56 flag = flag.upper()
57 try:
58 flagval |= getattr(re, flag)
59 except AttributeError:
60 raise ValueError("Invalid regex flag '%s'" % flag)
61
62 try:
63 return re.compile(value, flagval)
64 except re.error, exc:
65 raise ValueError("Invalid regex value '%s': %s" %
66 (value, exc.args[0]))
67
68def boolean(value):
69 """OpenEmbedded 'boolean' type
70
71 Valid values for true: 'yes', 'y', 'true', 't', '1'
72 Valid values for false: 'no', 'n', 'false', 'f', '0'
73 """
74
75 if not isinstance(value, basestring):
76 raise TypeError("boolean accepts a string, not '%s'" % type(value))
77
78 value = value.lower()
79 if value in ('yes', 'y', 'true', 't', '1'):
80 return True
81 elif value in ('no', 'n', 'false', 'f', '0'):
82 return False
83 raise ValueError("Invalid boolean value '%s'" % value)
84
85def integer(value, numberbase=10):
86 """OpenEmbedded 'integer' type
87
88 Defaults to base 10, but this can be specified using the optional
89 'numberbase' flag."""
90
91 return int(value, int(numberbase))
92
93_float = float
94def float(value, fromhex='false'):
95 """OpenEmbedded floating point type
96
97 To use this type, set the type flag to 'float', and optionally set the
98 'fromhex' flag to a true value (obeying the same rules as for the
99 'boolean' type) if the value is in base 16 rather than base 10."""
100
101 if boolean(fromhex):
102 return _float.fromhex(value)
103 else:
104 return _float(value)