forked from pantsbuild/pants
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds custom list and dict option types.
Values (in cmd-line args, env vars and config files) must be JSON lists/objects. Currently our config reader 'parses' list and dict values using python 'eval', so we change all of those in our pants.ini to be valid JSON. Fortunately they remain valid eval-able python too. Eventually we'll get rid of that eval-ing code, but for now we need to support both, during the transition. Adds checks in migrate_config.py for list/dict values that aren't valid JSON. Note that the list type is different from action='append'. The latter appends cmd-line arg values to the default. The former replaces the default. Testing Done: CI passes. Reviewed at https://rbcommons.com/s/twitter/r/1389/
- Loading branch information
Showing
10 changed files
with
253 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# coding=utf-8 | ||
# Copyright 2014 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
from __future__ import (nested_scopes, generators, division, absolute_import, with_statement, | ||
print_function, unicode_literals) | ||
|
||
import json | ||
|
||
from pants.option.errors import ParseError | ||
|
||
|
||
def _parse_error(s, msg): | ||
"""Return a ParseError with a usefully formatted message, for the caller to throw. | ||
:param s: The option value we're parsing. | ||
:param msg: An extra message to add to the ParseError. | ||
""" | ||
return ParseError('Error while parsing option value {0}: {1}'.format(s, msg)) | ||
|
||
|
||
def dict_type(s): | ||
"""An option of type 'dict'. | ||
The value (on the command-line, in an env var or in the config file) must be a JSON object. | ||
""" | ||
if isinstance(s, dict): | ||
return s | ||
try: | ||
ret = json.loads(s) | ||
except ValueError as e: | ||
raise _parse_error(s, e.message) | ||
if not isinstance(ret, dict): | ||
raise _parse_error(s, 'Value is not dict') | ||
return ret | ||
|
||
|
||
def list_type(s): | ||
"""An option of type 'list'. | ||
The value (on the command-line, in an env var or in the config file) must be a JSON list. | ||
""" | ||
if isinstance(s, (list, tuple)): | ||
return s | ||
try: | ||
ret = json.loads(s) | ||
except ValueError as e: | ||
raise _parse_error(s, e.message) | ||
if not isinstance(ret, list): | ||
raise _parse_error(s, 'Value is not list') | ||
return ret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# coding=utf-8 | ||
# Copyright 2014 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
from __future__ import (nested_scopes, generators, division, absolute_import, with_statement, | ||
print_function, unicode_literals) | ||
|
||
|
||
class RegistrationError(Exception): | ||
"""An error at option registration time.""" | ||
pass | ||
|
||
|
||
class ParseError(Exception): | ||
"""An error at flag parsing time.""" | ||
pass |
Oops, something went wrong.