Refactored option classes to mesonlib.

pull/275/head
Jussi Pakkanen 9 years ago
parent c02009a698
commit 4dff3f9fb3
  1. 12
      mesonconf.py
  2. 10
      mesongui.py
  3. 52
      mesonlib.py
  4. 85
      optinterpreter.py

@ -1,6 +1,6 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# Copyright 2014 The Meson development team # Copyright 2014-2015 The Meson development team
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.
@ -17,7 +17,7 @@
import sys, os import sys, os
import pickle import pickle
import argparse import argparse
import coredata, optinterpreter import coredata, mesonlib
from meson import build_types, layouts, warning_levels from meson import build_types, layouts, warning_levels
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
@ -127,15 +127,15 @@ class Conf:
self.coredata.localedir = v self.coredata.localedir = v
elif k in self.coredata.user_options: elif k in self.coredata.user_options:
tgt = self.coredata.user_options[k] tgt = self.coredata.user_options[k]
if isinstance(tgt, optinterpreter.UserBooleanOption): if isinstance(tgt, mesonlib.UserBooleanOption):
tgt.set_value(self.tobool(v)) tgt.set_value(self.tobool(v))
elif isinstance(tgt, optinterpreter.UserComboOption): elif isinstance(tgt, mesonlib.UserComboOption):
try: try:
tgt.set_value(v) tgt.set_value(v)
except optinterpreter.OptionException: except coredata.MesonException:
raise ConfException('Value of %s must be one of %s.' % raise ConfException('Value of %s must be one of %s.' %
(k, tgt.choices)) (k, tgt.choices))
elif isinstance(tgt, optinterpreter.UserStringOption): elif isinstance(tgt, mesonlib.UserStringOption):
tgt.set_value(v) tgt.set_value(v)
else: else:
raise ConfException('Internal error, unknown option type.') raise ConfException('Internal error, unknown option type.')

@ -1,6 +1,6 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# Copyright 2013 The Meson development team # Copyright 2013-2015 The Meson development team
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.
@ -15,7 +15,7 @@
# limitations under the License. # limitations under the License.
import sys, os, pickle, time, shutil import sys, os, pickle, time, shutil
import build, coredata, environment, optinterpreter import build, coredata, environment, mesonlib
from PyQt5 import uic from PyQt5 import uic
from PyQt5.QtWidgets import QApplication, QMainWindow, QHeaderView from PyQt5.QtWidgets import QApplication, QMainWindow, QHeaderView
from PyQt5.QtWidgets import QComboBox, QCheckBox from PyQt5.QtWidgets import QComboBox, QCheckBox
@ -272,14 +272,14 @@ class OptionForm:
self.opt_widgets = [] self.opt_widgets = []
for key in keys: for key in keys:
opt = options[key] opt = options[key]
if isinstance(opt, optinterpreter.UserStringOption): if isinstance(opt, mesonlib.UserStringOption):
w = PyQt5.QtWidgets.QLineEdit(opt.value) w = PyQt5.QtWidgets.QLineEdit(opt.value)
w.textChanged.connect(self.user_option_changed) w.textChanged.connect(self.user_option_changed)
elif isinstance(opt, optinterpreter.UserBooleanOption): elif isinstance(opt, mesonlib.UserBooleanOption):
w = QCheckBox('') w = QCheckBox('')
w.setChecked(opt.value) w.setChecked(opt.value)
w.stateChanged.connect(self.user_option_changed) w.stateChanged.connect(self.user_option_changed)
elif isinstance(opt, optinterpreter.UserComboOption): elif isinstance(opt, mesonlib.UserComboOption):
w = QComboBox() w = QComboBox()
for i in opt.choices: for i in opt.choices:
w.addItem(i) w.addItem(i)

@ -255,3 +255,55 @@ def replace_if_different(dst, dst_tmp):
pass pass
os.replace(dst_tmp, dst) os.replace(dst_tmp, dst)
class UserOption:
def __init__(self, name, description):
super().__init__()
self.name = name
self.description = description
def parse_string(self, valuestring):
return valuestring
class UserStringOption(UserOption):
def __init__(self, name, description, value):
super().__init__(name, description)
self.set_value(value)
def set_value(self, newvalue):
if not isinstance(newvalue, str):
raise MesonException('Value "%s" for string option "%s" is not a string.' % (str(newvalue), self.name))
self.value = newvalue
class UserBooleanOption(UserOption):
def __init__(self, name, description, value):
super().__init__(name, description)
self.set_value(value)
def set_value(self, newvalue):
if not isinstance(newvalue, bool):
raise MesonException('Value "%s" for boolean option "%s" is not a boolean.' % (str(newvalue), self.name))
self.value = newvalue
def parse_string(self, valuestring):
if valuestring == 'false':
return False
if valuestring == 'true':
return True
raise MesonException('Value "%s" for boolean option "%s" is not a boolean.' % (valuestring, self.name))
class UserComboOption(UserOption):
def __init__(self, name, description, choices, value):
super().__init__(name, description)
self.choices = choices
if not isinstance(self.choices, list):
raise MesonException('Combo choices must be an array.')
for i in self.choices:
if not isinstance(i, str):
raise MesonException('Combo choice elements must be strings.')
self.set_value(value)
def set_value(self, newvalue):
if newvalue not in self.choices:
optionsstring = ', '.join(['"%s"' % (item,) for item in self.choices])
raise MesonException('Value "%s" for combo option "%s" is not one of the choices. Possible choices are: %s.' % (newvalue, self.name, optionsstring))
self.value = newvalue

@ -1,4 +1,4 @@
# Copyright 2013-2014 The Meson development team # Copyright 2013-2015 The Meson development team
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.
@ -13,7 +13,7 @@
# limitations under the License. # limitations under the License.
import mparser import mparser
import coredata import coredata, mesonlib
import os, re import os, re
forbidden_option_names = coredata.builtin_options forbidden_option_names = coredata.builtin_options
@ -23,64 +23,27 @@ class OptionException(coredata.MesonException):
optname_regex = re.compile('[^a-zA-Z0-9_-]') optname_regex = re.compile('[^a-zA-Z0-9_-]')
class UserOption: def StringParser(name, description, kwargs):
def __init__(self, name, kwargs): return mesonlib.UserStringOption(name, description,
super().__init__() kwargs.get('value', ''))
self.description = kwargs.get('description', '')
self.name = name def BooleanParser(name, description, kwargs):
return mesonlib.UserBooleanOption(name, description, kwargs.get('value', True))
def parse_string(self, valuestring):
return valuestring def ComboParser(name, description, kwargs):
if 'choices' not in kwargs:
class UserStringOption(UserOption): raise OptionException('Combo option missing "choices" keyword.')
def __init__(self, name, kwargs): choices = kwargs['choices']
super().__init__(name, kwargs) if not isinstance(choices, list):
self.set_value(kwargs.get('value', '')) raise OptionException('Combo choices must be an array.')
for i in choices:
def set_value(self, newvalue): if not isinstance(i, str):
if not isinstance(newvalue, str): raise OptionException('Combo choice elements must be strings.')
raise OptionException('Value "%s" for string option "%s" is not a string.' % (str(newvalue), self.name)) return mesonlib.UserComboOption(name, description, choices, kwargs.get('value', choices[0]))
self.value = newvalue
option_types = {'string' : StringParser,
class UserBooleanOption(UserOption): 'boolean' : BooleanParser,
def __init__(self, name, kwargs): 'combo' : ComboParser,
super().__init__(name, kwargs)
self.set_value(kwargs.get('value', 'true'))
def set_value(self, newvalue):
if not isinstance(newvalue, bool):
raise OptionException('Value "%s" for boolean option "%s" is not a boolean.' % (str(newvalue), self.name))
self.value = newvalue
def parse_string(self, valuestring):
if valuestring == 'false':
return False
if valuestring == 'true':
return True
raise OptionException('Value "%s" for boolean option "%s" is not a boolean.' % (valuestring, self.name))
class UserComboOption(UserOption):
def __init__(self, name, kwargs):
super().__init__(name, kwargs)
if 'choices' not in kwargs:
raise OptionException('Combo option missing "choices" keyword.')
self.choices = kwargs['choices']
if not isinstance(self.choices, list):
raise OptionException('Combo choices must be an array.')
for i in self.choices:
if not isinstance(i, str):
raise OptionException('Combo choice elements must be strings.')
self.value = kwargs.get('value', self.choices[0])
def set_value(self, newvalue):
if newvalue not in self.choices:
optionsstring = ', '.join(['"%s"' % (item,) for item in self.choices])
raise OptionException('Value "%s" for combo option "%s" is not one of the choices. Possible choices are: %s.' % (newvalue, self.name, optionsstring))
self.value = newvalue
option_types = {'string' : UserStringOption,
'boolean' : UserBooleanOption,
'combo' : UserComboOption,
} }
class OptionInterpreter: class OptionInterpreter:
@ -161,7 +124,7 @@ class OptionInterpreter:
raise OptionException('Option name %s is reserved.' % opt_name) raise OptionException('Option name %s is reserved.' % opt_name)
if self.subproject != '': if self.subproject != '':
opt_name = self.subproject + ':' + opt_name opt_name = self.subproject + ':' + opt_name
opt = option_types[opt_type](opt_name, kwargs) opt = option_types[opt_type](opt_name, kwargs.get('description', ''), kwargs)
if opt.description == '': if opt.description == '':
opt.description = opt_name opt.description = opt_name
if opt_name in self.cmd_line_options: if opt_name in self.cmd_line_options:

Loading…
Cancel
Save