we have to access to option without restriction

This commit is contained in:
Emmanuel Garette 2019-07-06 08:36:13 +02:00
parent d3eef77c03
commit e73f3d1e47
3 changed files with 252 additions and 235 deletions

View File

@ -4,6 +4,7 @@ from os import listdir
from os.path import dirname, abspath, join, normpath, splitext, isfile from os.path import dirname, abspath, join, normpath, splitext, isfile
import sys import sys
import warnings import warnings
import pytest
from tiramisu import OptionDescription, Config from tiramisu import OptionDescription, Config
from tiramisu.error import ValueWarning from tiramisu.error import ValueWarning
@ -123,7 +124,31 @@ def add_property(expected, prop, prop_value):
return ordered_form return ordered_form
def test_jsons():
LISTDATA = list_data()
LISTDATA_MOD = []
idx = 0
while True:
idx += 1
list_files = list_data('.mod{}'.format(idx))
if not list_files:
break
LISTDATA_MOD.extend(list_files)
@pytest.fixture(params=LISTDATA)
def filename(request):
return request.param
@pytest.fixture(params=LISTDATA_MOD)
def filename_mod(request):
return request.param
def test_jsons(filename):
debug = False debug = False
# debug = True # debug = True
datadir = datapath() datadir = datapath()
@ -137,252 +162,242 @@ def test_jsons():
for remote in ['minimum', 'none', 'all']: for remote in ['minimum', 'none', 'all']:
if debug: if debug:
print(' ==> remotable', remote) print(' ==> remotable', remote)
filenames = list_data() modulepath = splitext(filename)[0]
for filename in filenames: if debug:
modulepath = splitext(filename)[0] print(" {} (remote: {}, clearable: {})".format(filename, remote, clearable))
if debug: values = load_config(filename,
print(" {} (remote: {}, clearable: {})".format(filename, remote, clearable)) remote=remote,
values = load_config(filename, clearable=clearable)
remote=remote, #
clearable=clearable) if not isfile(join(datadir, modulepath + '.json')) and \
# clearable == 'minimum' and \
if not isfile(join(datadir, modulepath + '.json')) and \ remote == 'minimum':
clearable == 'minimum' and \ with open(join(datadir, modulepath + '.json'), 'w') as fh:
remote == 'minimum': dump(values, fh, indent=2)
with open(join(datadir, modulepath + '.json'), 'w') as fh: with open(join(datadir, modulepath + '.json'), 'r') as fh:
dump(values, fh, indent=2) expected = loads(fh.read())
with open(join(datadir, modulepath + '.json'), 'r') as fh: if clearable == 'none':
expected = loads(fh.read()) expected['form'] = del_property(expected, 'clearable')
if clearable == 'none': if remote == 'all':
expected['form'] = del_property(expected, 'clearable') if 'tiramisu' in expected:
if remote == 'all': del expected['tiramisu']
if 'tiramisu' in expected: expected['form'] = del_property(expected, 'pattern')
del expected['tiramisu'] if clearable == 'all':
expected['form'] = del_property(expected, 'pattern') expected['form'] = add_property(expected, 'clearable', True)
if clearable == 'all': if remote == 'all':
expected['form'] = add_property(expected, 'clearable', True) expected['form'] = add_property(expected, 'remote', True)
if remote == 'all': new_expected = {}
expected['form'] = add_property(expected, 'remote', True) for key, form in expected['form'].items():
new_expected = {} if key != 'null' and form.get('remote', False):
for key, form in expected['form'].items(): if 'dependencies' in form:
if key != 'null' and form.get('remote', False): del form['dependencies']
if 'dependencies' in form: if 'copy' in form:
del form['dependencies'] del form['copy']
if 'copy' in form: if 'not_equal' in form:
del form['copy'] del form['not_equal']
if 'not_equal' in form: new_expected[key] = form
del form['not_equal'] expected['form'] = new_expected
new_expected[key] = form
expected['form'] = new_expected
# properties are unordered # properties are unordered
for model in expected['model']: for model in expected['model']:
if 'properties' in model: if 'properties' in model:
model['properties'] = set(model['properties']) model['properties'] = set(model['properties'])
for model in values['model']: for model in values['model']:
if 'properties' in model: if 'properties' in model:
model['properties'] = set(model['properties']) model['properties'] = set(model['properties'])
del_value_property(expected['schema'], expected['form']) del_value_property(expected['schema'], expected['form'])
if debug: if debug:
from pprint import pprint from pprint import pprint
pprint(values) pprint(values)
print('----------------') print('----------------')
pprint(expected) pprint(expected)
assert values == expected, "error in file {}".format(filename) assert values == expected, "error in file {}".format(filename)
def test_jsons_subconfig(): def test_jsons_subconfig(filename):
debug = False debug = False
# debug = True # debug = True
datadir = datapath() datadir = datapath()
if debug: if debug:
print() print()
filenames = list_data() modulepath = splitext(filename)[0]
for filename in filenames: if debug:
modulepath = splitext(filename)[0] print(" ", filename)
if debug: values = load_config(filename, add_extra_od=True, root=modulepath)
print(" ", filename) #
values = load_config(filename, add_extra_od=True, root=modulepath) with open(join(datadir, modulepath + '.json'), 'r') as fh:
# expected = loads(fh.read())
with open(join(datadir, modulepath + '.json'), 'r') as fh: # properties are unordered
expected = loads(fh.read()) for model in expected['model'].values():
# properties are unordered if 'properties' in model:
for model in expected['model'].values(): model['properties'] = set(model['properties'])
if 'properties' in model: for model in values['model'].values():
model['properties'] = set(model['properties']) if 'properties' in model:
for model in values['model'].values(): model['properties'] = set(model['properties'])
if 'properties' in model: # add root
model['properties'] = set(model['properties'])
# add root
def change_key(schema): def change_key(schema):
new_schema = {} new_schema = {}
for key_schema, val_schema in schema.items(): for key_schema, val_schema in schema.items():
key = modulepath + '.' + key_schema key = modulepath + '.' + key_schema
# val_schema['name'] = key # val_schema['name'] = key
if 'opt_path' in val_schema: if 'opt_path' in val_schema:
val_schema['opt_path'] = modulepath + '.' + val_schema['opt_path'] val_schema['opt_path'] = modulepath + '.' + val_schema['opt_path']
if 'properties' in val_schema: if 'properties' in val_schema:
val_schema['properties'] = change_key(val_schema['properties']) val_schema['properties'] = change_key(val_schema['properties'])
new_schema[key] = val_schema new_schema[key] = val_schema
return new_schema return new_schema
expected['schema'] = change_key(expected['schema']) expected['schema'] = change_key(expected['schema'])
new_form_all = {} new_form_all = {}
for key, form in expected['form'].items(): for key, form in expected['form'].items():
if key != 'null': if key != 'null':
key = modulepath + '.' + key key = modulepath + '.' + key
new_form_all[key] = form new_form_all[key] = form
if 'copy' in form: if 'copy' in form:
for idx, noteq in enumerate(form['copy']): for idx, noteq in enumerate(form['copy']):
form['copy'][idx] = modulepath + '.' + noteq form['copy'][idx] = modulepath + '.' + noteq
if 'not_equal' in form: if 'not_equal' in form:
new_form = [] new_form = []
for idx, not_equal in enumerate(form['not_equal']): for idx, not_equal in enumerate(form['not_equal']):
for noteq in not_equal['options']: for noteq in not_equal['options']:
new_form.append(modulepath + '.' + noteq) new_form.append(modulepath + '.' + noteq)
form['not_equal'][idx]['options'] = new_form form['not_equal'][idx]['options'] = new_form
if 'dependencies' in form: if 'dependencies' in form:
for dependency in form['dependencies'].values(): for dependency in form['dependencies'].values():
for val1 in dependency.values(): for val1 in dependency.values():
if isinstance(val1, list): if isinstance(val1, list):
for idx, lst in enumerate(val1): for idx, lst in enumerate(val1):
val1[idx] = modulepath + '.' + lst val1[idx] = modulepath + '.' + lst
else: else:
for val2 in val1.values(): for val2 in val1.values():
if isinstance(val2, list): if isinstance(val2, list):
for idx, lst in enumerate(val2): for idx, lst in enumerate(val2):
val2[idx] = modulepath + '.' + lst val2[idx] = modulepath + '.' + lst
expected['form'] = new_form_all expected['form'] = new_form_all
new_model = {} new_model = {}
for key, model in expected['model'].items(): for key, model in expected['model'].items():
new_model[modulepath + '.' + key] = model new_model[modulepath + '.' + key] = model
expected['model'] = new_model expected['model'] = new_model
if debug: if debug:
from pprint import pprint from pprint import pprint
pprint(values) pprint(values)
print('----------------') print('----------------')
pprint(expected) pprint(expected)
assert values == expected, "error in file {}".format(filename) assert values == expected, "error in file {}".format(filename)
def test_updates(): def test_updates(filename_mod):
debug = False debug = False
# debug = True # debug = True
datadir = datapath() datadir = datapath()
idx = 0 if debug:
while True: print("test/data/" + filename_mod)
idx += 1 for issub in [False, True]:
list_files = list_data('.mod{}'.format(idx)) idx = int(filename_mod[-1])
if not list_files: modulepath = splitext(filename_mod)[0]
break mod = __import__(modulepath)
for filename in list_files: descr = mod.get_description()
if debug: if issub:
print("test/data/" + filename) descr = OptionDescription('root', '', [descr])
for issub in [False, True]: root = modulepath
modulepath = splitext(filename)[0] else:
mod = __import__(modulepath) root = None
descr = mod.get_description() # dict before modification
if not isfile(join(datadir, modulepath + '.dict')):
dico_ori = None
else:
with open(join(datadir, modulepath + '.dict'), 'r') as fh:
dico_ori = loads(fh.read())
if issub: if issub:
descr = OptionDescription('root', '', [descr]) new_dico_ori = {}
root = modulepath for key, value in dico_ori.items():
else: key = modulepath + '.' + key
root = None new_dico_ori[key] = value
# dict before modification dico_ori = new_dico_ori
if not isfile(join(datadir, modulepath + '.dict')): # modify config
dico_ori = None with open(join(datadir, modulepath + '.mod{}'.format(idx)), 'r') as fh:
else: body = loads(fh.read())['body']
with open(join(datadir, modulepath + '.dict'), 'r') as fh: if issub:
dico_ori = loads(fh.read()) for value in body['updates']:
if issub: value['name'] = modulepath + '.' + value['name']
new_dico_ori = {} # returns of set_updates
for key, value in dico_ori.items(): if not isfile(join(datadir, modulepath + '.updates{}'.format(idx))):
key = modulepath + '.' + key values = None
new_dico_ori[key] = value else:
dico_ori = new_dico_ori with open(join(datadir, modulepath + '.updates{}'.format(idx)), 'r') as fh:
# modify config values = loads(fh.read())
with open(join(datadir, modulepath + '.mod{}'.format(idx)), 'r') as fh: if issub:
body = loads(fh.read())['body'] for lidx, key in enumerate(values['updates']):
if issub: values['updates'][lidx] = modulepath + '.' + key
for value in body['updates']: if 'model' in values:
value['name'] = modulepath + '.' + value['name'] new_model = {}
# returns of set_updates for key, value in values['model'].items():
if not isfile(join(datadir, modulepath + '.updates{}'.format(idx))): new_model[modulepath + '.' + key] = value
values = None values['model'] = new_model
else: # dict after modification
with open(join(datadir, modulepath + '.updates{}'.format(idx)), 'r') as fh: if not isfile(join(datadir, modulepath + '.dict{}'.format(idx))):
values = loads(fh.read()) dico_mod = None
if issub: else:
for lidx, key in enumerate(values['updates']): with open(join(datadir, modulepath + '.dict{}'.format(idx)), 'r') as fh:
values['updates'][lidx] = modulepath + '.' + key dico_mod = loads(fh.read())
if 'model' in values: if issub:
new_model = {} new_dico = {}
for key, value in values['model'].items(): for key, value in dico_mod.items():
new_model[modulepath + '.' + key] = value key = modulepath + '.' + key
values['model'] = new_model new_dico[key] = value
# dict after modification dico_mod = new_dico
if not isfile(join(datadir, modulepath + '.dict{}'.format(idx))): if root is None:
dico_mod = None root_path = ''
else: else:
with open(join(datadir, modulepath + '.dict{}'.format(idx)), 'r') as fh: root_path = '{}.'.format(root)
dico_mod = loads(fh.read()) for clearable in ['none', 'minimum', 'all']:
if issub: for remote in ['none', 'minimum', 'all']:
new_dico = {} if debug:
for key, value in dico_mod.items(): print(" (remote: {}, clearable: {}, issub {}, root {}, root_path {})".format(remote, clearable, issub, root, root_path))
key = modulepath + '.' + key for with_model in [False, True]:
new_dico[key] = value config = Config(descr)
dico_mod = new_dico config.property.add('demoting_error_warning')
if root is None: if 'get_values' in dir(mod):
root_path = '' mod.get_values(config, issub)
else: if isfile(join(datadir, modulepath + '.mod')):
root_path = '{}.'.format(root) with open(join(datadir, modulepath + '.mod'), 'r') as fh:
for clearable in ['none', 'minimum', 'all']: eval(fh.read())
for remote in ['none', 'minimum', 'all']: if dico_ori is None:
if debug: if clearable == 'minimum' and remote == 'minimum':
print(" (remote: {}, clearable: {}, issub {}, root {}, root_path {})".format(remote, clearable, issub, root, root_path)) with open(join(datadir, modulepath + '.dict'), 'w') as fh:
for with_model in [False, True]: dump(config.value.dict(), fh, indent=2)
config = Config(descr) else:
config.property.add('demoting_error_warning') assert config.value.dict() == dico_ori, "clearable {}, remote: {}, filename: {}".format(clearable, remote, filename_mod)
if 'get_values' in dir(mod): if root is None:
mod.get_values(config, issub) suboption = config.option
if isfile(join(datadir, modulepath + '.mod')): else:
with open(join(datadir, modulepath + '.mod'), 'r') as fh: suboption = config.option(root)
eval(fh.read()) if with_model:
if dico_ori is None: bodym = body.copy()
if clearable == 'minimum' and remote == 'minimum': bodym['model'] = loads(dumps(suboption.dict(remotable=remote, clearable=clearable)))['model']
with open(join(datadir, modulepath + '.dict'), 'w') as fh: else:
dump(config.value.dict(), fh, indent=2) suboption.dict(remotable=remote, clearable=clearable)
else: bodym = body
assert config.value.dict() == dico_ori, "clearable {}, remote: {}, filename: {}".format(clearable, remote, filename) if with_model:
if root is None: cal_values = suboption.updates(bodym)
suboption = config.option if values is None:
else: if clearable == 'minimum' and remote == 'minimum':
suboption = config.option(root) with open(join(datadir, modulepath + '.updates{}'.format(idx)), 'w') as fh:
if with_model: dump(cal_values, fh, indent=2)
bodym = body.copy() else:
bodym['model'] = loads(dumps(suboption.dict(remotable=remote, clearable=clearable)))['model'] if debug:
else: from pprint import pprint
suboption.dict(remotable=remote, clearable=clearable) pprint(cal_values)
bodym = body print('------------')
if with_model: pprint(values)
cal_values = suboption.updates(bodym) assert cal_values == values
if values is None: else:
if clearable == 'minimum' and remote == 'minimum': assert suboption.updates(bodym) is None
with open(join(datadir, modulepath + '.updates{}'.format(idx)), 'w') as fh: if dico_mod is None:
dump(cal_values, fh, indent=2) if clearable == 'minimum' and remote == 'minimum':
else: with open(join(datadir, modulepath + '.dict{}'.format(idx)), 'w') as fh:
if debug: dump(config.value.dict(), fh, indent=2)
from pprint import pprint else:
pprint(cal_values) assert config.value.dict() == dico_mod
print('------------')
pprint(values)
assert cal_values == values
else:
assert suboption.updates(bodym) is None
if dico_mod is None:
if clearable == 'minimum' and remote == 'minimum':
with open(join(datadir, modulepath + '.dict{}'.format(idx)), 'w') as fh:
dump(config.value.dict(), fh, indent=2)
else:
assert config.value.dict() == dico_mod

View File

@ -144,6 +144,7 @@ class _TiramisuOptionOptionDescription(CommonTiramisuOption):
"""Manage option""" """Manage option"""
_allow_optiondescription = True _allow_optiondescription = True
_follower_need_index = False _follower_need_index = False
_validate_properties = False
def __init__(self, def __init__(self,
name: str, name: str,
@ -652,6 +653,7 @@ def _registers(_registers: Dict[str, type],
class _TiramisuOption(CommonTiramisu): class _TiramisuOption(CommonTiramisu):
"""Manage selected option""" """Manage selected option"""
_validate_properties = False
_registers = {} _registers = {}
def __init__(self, def __init__(self,
name: Optional[str], name: Optional[str],

View File

@ -470,8 +470,8 @@ class TiramisuDict:
self.add_help(schema[path], self.add_help(schema[path],
childapi) childapi)
except Exception as err: except Exception as err:
import traceback # import traceback
traceback.print_exc() # traceback.print_exc()
if not init: if not init:
raise err raise err
error = err error = err