diff options
author | Mike Frysinger <vapier@google.com> | 2020-08-25 20:16:18 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2020-08-25 20:16:18 +0000 |
commit | fd6d0e0b33455142503a39741c486ff26c6ba232 (patch) | |
tree | 791ba958fbe46162c22480b02609406efbd60a82 | |
parent | 03fa555537fceb77c35ad6b2a10c4dcc84d4de21 (diff) | |
parent | 0d0796a3ea13cecf458b99578c7681321e225d5d (diff) | |
download | repohooks-fd6d0e0b33455142503a39741c486ff26c6ba232.tar.gz |
config: fix handling of optional args am: 0d0796a3ea
Original change: https://android-review.googlesource.com/c/platform/tools/repohooks/+/1409410
Change-Id: Ie20fc9502b2683e80f3f172255a4275c2131cfbf
-rw-r--r-- | rh/config.py | 41 |
1 files changed, 17 insertions, 24 deletions
diff --git a/rh/config.py b/rh/config.py index c83dfed..00d4640 100644 --- a/rh/config.py +++ b/rh/config.py @@ -41,53 +41,46 @@ class ValidationError(Error): """Config file has unknown sections/keys or other values.""" +# Sentinel so we can handle None-vs-unspecified. +_UNSET = object() + + class RawConfigParser(configparser.RawConfigParser): """Like RawConfigParser but with some default helpers.""" - @staticmethod - def _check_args(name, cnt_min, cnt_max, args): - cnt = len(args) - if cnt not in (0, cnt_max - cnt_min): - raise TypeError('%s() takes %i or %i arguments (got %i)' % - (name, cnt_min, cnt_max, cnt,)) - return cnt - - # pylint can't seem to grok our use of *args here. + # pylint doesn't like it when we extend the API. # pylint: disable=arguments-differ - def options(self, section, *args): - """Return the options in |section| (with default |args|). + def options(self, section, default=_UNSET): + """Return the options in |section|. Args: section: The section to look up. - args: What to return if |section| does not exist. + default: What to return if |section| does not exist. """ - cnt = self._check_args('options', 2, 3, args) try: return configparser.RawConfigParser.options(self, section) except configparser.NoSectionError: - if cnt == 1: - return args[0] + if default is not _UNSET: + return default raise - def get(self, section, option, *args): - """Return the value for |option| in |section| (with default |args|).""" - cnt = self._check_args('get', 3, 4, args) + def get(self, section, option, default=_UNSET): + """Return the value for |option| in |section| (with |default|).""" try: return configparser.RawConfigParser.get(self, section, option) except (configparser.NoSectionError, configparser.NoOptionError): - if cnt == 1: - return args[0] + if default is not _UNSET: + return default raise - def items(self, section, *args): + def items(self, section, default=_UNSET): """Return a list of (key, value) tuples for the options in |section|.""" - cnt = self._check_args('items', 2, 3, args) try: return configparser.RawConfigParser.items(self, section) except configparser.NoSectionError: - if cnt == 1: - return args[0] + if default is not _UNSET: + return default raise |