From: Rob D. <rd...@gm...> - 2015-01-10 13:27:05
|
Hello Darren, I'm one of the current maintainers along with Eli, I'm modifying a slightly tweaked example from here: https://configobj.readthedocs.org/en/latest/validate.html#the-standard-functions I'm seeing the behavior I expect for handcrafting the options validator: >>> from validate import Validator >>> vtor = Validator() >>> checked_val = vtor.check("option('option 1', 'option 2', 'option 3')", 'option 1') >>> checked_val 'option 1' >>> checked_val = vtor.check("option('option 1', 'option 2', 'option 3')", 'option 4') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "validate.py", line 625, in check return self._check_value(value, fun_name, fun_args, fun_kwargs) File "validate.py", line 657, in _check_value return fun(value, *fun_args, **fun_kwargs) File "validate.py", line 1332, in is_option raise VdtValueError(value) validate.VdtValueError: the value "option 4" is unacceptable. which if we tweak a bit more shows: >>> my_categories = 'option 1', 'option 2', 'option 3' >>> checked_val = vtor.check("option({})".format(', '.join(repr(cat) for cat in my_categories)), 'option 1') >>> checked_val 'option 1' note the example still does use join, but is also joining the repr of each element (to get the quotes around strings, mostly) >>> "option({})".format(', '.join(repr(cat) for cat in my_categories)) "option('option 1', 'option 2', 'option 3')" Is this on the right track for what you need? I'm out of contact for the next ~6 hours, but could try and respond after On Sat, Jan 10, 2015 at 12:57 AM, Darren Spruell <pha...@gm...> wrote: > I have a case in a script where I have a data structure defined in an > external module, which is also a library we use in the script. I'd > like to use that data in a DRY fashion in this script (which uses > ConfigObj) by passing the structure (a list) as the argument to the > option() validator. The reason for this is that the data in the target > configuration field will ultimately be passed to the external module > and will be validated against it, so the external module is > essentially "authoritative" for the application. > > In the external module (mylib.py): > > CATEGORIES = ['WHITE', 'GREEN', 'AMBER', 'RED'] > > > In the script: > > from mylib import CATEGORIES as allowed_categories > > CONFIGSPEC = '''\ > logfile = string > [foo] > user = string > pass = string > [bar] > [[__many__]] > category = string > ''' > > > As shown I currently validate the 'category' option with string(), but > I'd really like to use option() instead with the set of choices taken > from the list imported from mylib; something like the following: > > category = option(allowed_categories) > > That syntax doesn't work and various attempts to coerce the list into > a string that resembles a list of allowed values with join() hasn't > done the trick. Is there currently a way to get this to function as I > want? Any way to allow option() to accept a tuple or list as the > arguments to ease working with structured data for that validator? > > -- > Darren Spruell > pha...@gm... > > > ------------------------------------------------------------------------------ > Dive into the World of Parallel Programming! The Go Parallel Website, > sponsored by Intel and developed in partnership with Slashdot Media, is > your > hub for all things parallel software development, from weekly thought > leadership blogs to news, videos, case studies, tutorials and more. Take a > look and join the conversation now. http://goparallel.sourceforge.net > _______________________________________________ > Configobj-develop mailing list > Con...@li... > https://lists.sourceforge.net/lists/listinfo/configobj-develop > |