summaryrefslogtreecommitdiff
path: root/py/config/options.py
blob: eb2faaed85326a78f8ec98cce9a7003d310cb9bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
from textwrap import TextWrapper
from .base import options_map
from py.waf.compat import add_metaclass # 2to3

wrapper = TextWrapper()
wrapper.width = 75
wrapper.initial_indent = "# "
wrapper.subsequent_indent = "# "


tag_map = {
	"general": "General settings",
	"build":   "Build options",
	"network": "Network option",
	"storage": "Storage option"
}

class OptionMeta(type):
	"""Self register options."""
	skip = ("Option", "Boolean", "String", "StringList", "Integer")
	def __new__(cls, name, bases, dct):
		new = type.__new__(cls, name, bases, dct)
		if name not in cls.skip:
			if name in options_map:
				raise Exception("Duplicate option: %s" % name)
			options_map[name] = new
		return new

@add_metaclass(OptionMeta)
class Option(object):
	"""
		Base class for all Options

		.. py:attribute:: undef=True

			Whether to undefine the option in the header file when "disabled".

		.. py:attribute:: limit=None

			List or min,max to restrict the option to.  Depends on option type.

		.. py:attribute:: quote=False

			Whether to quote the value in the header.

		.. py:attribute:: tag=list

			List of tags for this option.  At least one is required.
	"""

	def __init__(self, value=None):
		self.name = self.__class__.__name__


		# Do not set a default if no_default set.
		if not hasattr(self, "no_default"):
			self.no_default = True

		# Whether to quote the value
		if not hasattr(self, "quote"):
			self.quote = False

		# Tag sanity check.
		if not hasattr(self, "tag") or not self.tag:
			self._fatal("At least one tag is required")
		elif type(self.tag) is not list:
			self._fatal("%s.tag: must be a list().")
		elif not set(self.tag).issubset(tag_map):
			missing = [x for x in self.tag if x not in tag_map]
			self._fatal("Tag(s) %s do not exist." % missing)
		else:
			duplicates = set([x for x in self.tag if self.tag.count(x) > 1])
			if duplicates:
				self._fatal("duplicate tags: %s" % ", ".join(duplicates))


		# Value limit
		if not hasattr(self, "limit"):
			self.limit = None

		if value is not None:
			self.validate(value)
			self.value = value
		elif self.no_default and not hasattr(self, "value"):
			raise Exception("no_default is set you must supply a value in bsp config")
		else:
			self.validate(self.value)

	def __add__(self, value):
		"""
			Hack around only a _set. Need a _convert and _set in each type of option.
	
			:rtype: Depends on subclass
			:return: Two options added together
		"""
		current = self.value
		self._set(value)
		optsum = self.value
		self.value = current
		return current + optsum


	def validate(self, value):
		"""Validate option."""
		self._fatal("Must be set in subclass.")


	def set(self, value):
		"""Set option value"""
		if value is None:
#			self.value = self.default #XXX: why is this here, a artifact?
			return

		self._set(value)


	def _fatal(self, str):
		"""Fatal error"""
		raise Exception("(%s)%s: %s" % (self.__class__.__bases__[0].__name__, self.__class__.__name__, str))


	def config_get(self):
		"""
		Get string suitable for config.cfg

		:rtype: string
		:return: Pre-formatted Windows `.ini` block.
		"""

		opt = []
		opt += wrapper.wrap(self.descr.strip())
		opt += ["%s = %s" % (self.__class__.__name__, self._str_value())]
		return "\n".join(opt)


	def _str_value(self):
		"""
			Get option as a string.

		:rtype: string
		:return: Option value as a string.
		"""
		raise Exception("Needs to be implmented in subclass.")


	def set_config_header(self, ctx):
		"""
			1. If value is an integer always define it beacuse it could be 0.
			2. If value is empty "" or boolean=False undefine it
			3. if self.undef == True (undefine if false) undefine it.

			:param ctx: waf context
		"""
		if type(self.value) is not int and not self.value and self.undef:
			ctx.undefine(self.name)
			return
		self._set_config_header(ctx)


	def set_config_build(self, ctx):
		"""
			Set option inside waf as part of the build.

			:param ctx: waf context
		"""
		if type(self.value) is list:
			ctx.env.append_value(self.name, self.value)
		else:
			setattr(ctx.env, self.name, self.value)



class Boolean(Option):
	"""Boolean option value."""

	def validate(self, value):
		"""Validate as one of: 1, 0, true, false"""

		if type(value) is not bool:
			self._fatal("Not a boolean value (True|False): %s" % type(value))

	def _str_value(self):
		return str(self.value)

	# XXX: This is wrong (works for now!)
	def _set(self, value):

		if type(value) is str:
			if value.lower() == "true":
				value = True
			elif value.lower() == "false":
				value = False
			else:
				self._fatal("Internal error in Boolean._set()")

		self.validate(value)
		self.value = value

	def _set_config_header(self, ctx):
		"""Set value in config header."""

		if self.undef:
			ctx.define_cond(self.name, 1 if self.value else 0)
		else:
			ctx.define(self.name, 0)




class String(Option):
	def validate(self, value):
		"""Verify value is a string and is in `limit` if defined."""
		if type(value) is not str:
			self._fatal("Not a string: %s (%s)" % (value, type(value)))

		if self.limit:
			if type(limit) is not list:
				self._fatal("Only lists are supported as a limiter for strings.")

			if value not in limit:
				self._fatal("%s not in list of accepted values: %s" % (value, limit))

	def _str_value(self):
		return self.value

	def _set(self, value):
		"""Set string, strips bounding whitespace."""
		self.validate(value)
		self.value = value.strip()

	def _set_config_header(self, ctx):
		"""Define in header."""
		ctx.define(self.name, self.value, quote=self.quote)


class StringList(Option):
	def validate(self, value):
		"""Validate list of strings"""
		if type(value) is not list:
			self._fatal("Not a list: %s (%s)" % (value, type(value)))

		for v in value:
			if type(v) is not str:
				self._fatal("Value %s is not a String." % v)

#XXX: Needs to be fixed.
#		if self.limit:
#			if type(limit) is not list:
#				self._fatal("Only lists are supported as a limiter for strings.")

#			if value not in limit:
#				self._fatal("%s not in list of accepted values: %s" % (value, limit))

	def _str_value(self):
		return " ".join(self.value)

	def _set(self, value):
		"""Make sure value is a list otherwise split into one delimited by whitespace."""

		if type(value) is not list:
			value = value.split(" ")
			value = [x for x in value if x]

		self.validate(value)
		self.value = value

	def _set_config_header(self, ctx):
		ctx.define(self.name, self.value, quote=self.quote)



class Integer(Option):
	def validate(self, value):
		"""Verify value is an int and in limit if defined."""
		if type(value) is not int:
			self._fatal("Not an integer: %s (%s)" % (value, type(value)))

		if self.limit:
			if type(limit) is list:
				if value not in limit:
					self._fatal("%s not in list of accepted values: %s" % (value, limit))

			if type(limit) is tuple:
				min, max = limit
				if value < min or value > max:
					self._fatal("%s is outside min/max: %s/%s" % (value, min, max))

	def _str_value(self):
		return str(self.value)

	def _set(self, value):
		value = int(value) #XXX: configparser doesn't convert this anymore, sigh.
		self.validate(value)
		self.value = value

	def _set_config_header(self, ctx):
		ctx.define(self.name, self.value, quote=self.quote)