| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
"""Custom parser to override tuples in the config dict.""" |
| 16 |
import ast |
| 17 |
import collections.abc |
| 18 |
|
| 19 |
from absl import flags |
| 20 |
|
| 21 |
|
| 22 |
class TupleParser(flags.ArgumentParser): |
| 23 |
"""Parser for tuple arguments. |
| 24 |
|
| 25 |
Custom flag parser for Tuple objects that is based on the existing parsers |
| 26 |
in `absl.flags`. This parser can be used to read in and override tuple |
| 27 |
arguments. It outputs a `tuple` object from an existing `tuple` or `str`. |
| 28 |
The ony requirement is that the overriding parameter should be a `tuple` |
| 29 |
as well. The overriding parameter can have a different number of elements |
| 30 |
of different type than the original. For a detailed list of what `str` |
| 31 |
arguments are supported for overriding, look at `ast.literal_eval` from the |
| 32 |
Python Standard Library. |
| 33 |
""" |
| 34 |
|
| 35 |
def parse(self, argument): |
| 36 |
"""Returns a `tuple` representing the input `argument`. |
| 37 |
|
| 38 |
Args: |
| 39 |
argument: The argument to be parsed. Valid types are `tuple` and |
| 40 |
`str` or a single object. `str` arguments are parsed and converted to a |
| 41 |
tuple, a single object is converted to a tuple of length 1, and an empty |
| 42 |
`tuple` is returned for arguments `NoneType`. |
| 43 |
|
| 44 |
Returns: |
| 45 |
A `TupleType` representing the input argument as a `tuple`. |
| 46 |
""" |
| 47 |
if isinstance(argument, tuple): |
| 48 |
return argument |
| 49 |
elif isinstance(argument, str): |
| 50 |
return _convert_str_to_tuple(argument) |
| 51 |
elif argument is None: |
| 52 |
return () |
| 53 |
else: |
| 54 |
return (argument,) |
| 55 |
|
| 56 |
def flag_type(self): |
| 57 |
return 'tuple' |
| 58 |
|
| 59 |
|
| 60 |
def _convert_str_to_tuple(string): |
| 61 |
"""Function to convert a Python `str` object to a `tuple`. |
| 62 |
|
| 63 |
Args: |
| 64 |
string: The `str` to be converted. |
| 65 |
|
| 66 |
Returns: |
| 67 |
A `tuple` version of the string. |
| 68 |
|
| 69 |
Raises: |
| 70 |
ValueError: If the string is not a well formed `tuple`. |
| 71 |
""" |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
try: |
| 76 |
value = ast.literal_eval(string) |
| 77 |
except ValueError: |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
value = string |
| 84 |
except SyntaxError as exc: |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
if ',' not in string and ' ' not in string: |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
value = string |
| 96 |
else: |
| 97 |
msg = ( |
| 98 |
f'Error while parsing string: {string} as tuple. If you intended to' |
| 99 |
' pass the argument as a single element, use quotes such as `--flag' |
| 100 |
f' {repr(repr(string))}, otherwise insert quotes around each element.' |
| 101 |
) |
| 102 |
if ' ' in string: |
| 103 |
msg += ( |
| 104 |
' Use commas instead of whitespace as the separator between' |
| 105 |
' elements.' |
| 106 |
) |
| 107 |
raise ValueError(msg) from exc |
| 108 |
|
| 109 |
|
| 110 |
if isinstance(value, tuple): |
| 111 |
return value |
| 112 |
elif (isinstance(value, collections.abc.Iterable) and |
| 113 |
not isinstance(value, str)): |
| 114 |
return tuple(value) |
| 115 |
else: |
| 116 |
return (value,) |