diff options
| author | Ulf Magnusson <ulfalizer@gmail.com> | 2019-06-23 02:29:14 +0200 |
|---|---|---|
| committer | Ulf Magnusson <ulfalizer@gmail.com> | 2019-06-23 02:52:26 +0200 |
| commit | 7b97da6821a45ce34e731df68980dea0983cf18c (patch) | |
| tree | 93c77c748a91b46e09d57d02975bc20153aa5fe1 | |
| parent | 3b45f4bfdb253085062e86749f9ed449b85c3c86 (diff) | |
Shorten __repr__() functions a bit
Do 'add = fields.append'. Pointless as an optimization, but shaves some
lines, and obvious in context.
Also add test coverage for __repr__() for string symbols with user
values.
| -rw-r--r-- | kconfiglib.py | 81 | ||||
| -rw-r--r-- | tests/Krepr | 3 | ||||
| -rw-r--r-- | testsuite.py | 45 |
3 files changed, 67 insertions, 62 deletions
diff --git a/kconfiglib.py b/kconfiglib.py index 9b52905..e901d15 100644 --- a/kconfiglib.py +++ b/kconfiglib.py @@ -4524,52 +4524,49 @@ class Symbol(object): interactive Python prompt. """ fields = ["symbol " + self.name, TYPE_TO_STR[self.type]] + add = fields.append for node in self.nodes: if node.prompt: - fields.append('"{}"'.format(node.prompt[0])) + add('"{}"'.format(node.prompt[0])) # Only add quotes for non-bool/tristate symbols - fields.append("value " + - (self.str_value - if self.orig_type in _BOOL_TRISTATE else - '"{}"'.format(self.str_value))) + add("value " + (self.str_value if self.orig_type in _BOOL_TRISTATE + else '"{}"'.format(self.str_value))) if not self.is_constant: # These aren't helpful to show for constant symbols if self.user_value is not None: # Only add quotes for non-bool/tristate symbols - fields.append("user value " + - (TRI_TO_STR[self.user_value] - if self.orig_type in _BOOL_TRISTATE else - '"{}"'.format(self.user_value))) + add("user value " + (TRI_TO_STR[self.user_value] + if self.orig_type in _BOOL_TRISTATE + else '"{}"'.format(self.user_value))) - fields.append("visibility " + TRI_TO_STR[self.visibility]) + add("visibility " + TRI_TO_STR[self.visibility]) if self.choice: - fields.append("choice symbol") + add("choice symbol") if self.is_allnoconfig_y: - fields.append("allnoconfig_y") + add("allnoconfig_y") if self is self.kconfig.defconfig_list: - fields.append("is the defconfig_list symbol") + add("is the defconfig_list symbol") if self.env_var is not None: - fields.append("from environment variable " + self.env_var) + add("from environment variable " + self.env_var) if self is self.kconfig.modules: - fields.append("is the modules symbol") + add("is the modules symbol") - fields.append("direct deps " + - TRI_TO_STR[expr_value(self.direct_dep)]) + add("direct deps " + TRI_TO_STR[expr_value(self.direct_dep)]) if self.nodes: for node in self.nodes: - fields.append("{}:{}".format(node.filename, node.linenr)) + add("{}:{}".format(node.filename, node.linenr)) else: - fields.append("constant" if self.is_constant else "undefined") + add("constant" if self.is_constant else "undefined") return "<{}>".format(", ".join(fields)) @@ -5117,18 +5114,19 @@ class Choice(object): """ fields = ["choice " + self.name if self.name else "choice", TYPE_TO_STR[self.type]] + add = fields.append for node in self.nodes: if node.prompt: - fields.append('"{}"'.format(node.prompt[0])) + add('"{}"'.format(node.prompt[0])) - fields.append("mode " + self.str_value) + add("mode " + self.str_value) if self.user_value is not None: - fields.append('user mode {}'.format(TRI_TO_STR[self.user_value])) + add('user mode {}'.format(TRI_TO_STR[self.user_value])) if self.selection: - fields.append("{} selected".format(self.selection.name)) + add("{} selected".format(self.selection.name)) if self.user_selection: user_sel_str = "{} selected by user" \ @@ -5137,15 +5135,15 @@ class Choice(object): if self.selection is not self.user_selection: user_sel_str += " (overridden)" - fields.append(user_sel_str) + add(user_sel_str) - fields.append("visibility " + TRI_TO_STR[self.visibility]) + add("visibility " + TRI_TO_STR[self.visibility]) if self.is_optional: - fields.append("optional") + add("optional") for node in self.nodes: - fields.append("{}:{}".format(node.filename, node.linenr)) + add("{}:{}".format(node.filename, node.linenr)) return "<{}>".format(", ".join(fields)) @@ -5524,46 +5522,45 @@ class MenuNode(object): evaluated on e.g. the interactive Python prompt. """ fields = [] + add = fields.append if self.item.__class__ is Symbol: - fields.append("menu node for symbol " + self.item.name) + add("menu node for symbol " + self.item.name) elif self.item.__class__ is Choice: s = "menu node for choice" if self.item.name is not None: s += " " + self.item.name - fields.append(s) + add(s) elif self.item is MENU: - fields.append("menu node for menu") + add("menu node for menu") else: # self.item is COMMENT - fields.append("menu node for comment") + add("menu node for comment") if self.prompt: - fields.append('prompt "{}" (visibility {})' - .format(self.prompt[0], - TRI_TO_STR[expr_value(self.prompt[1])])) + add('prompt "{}" (visibility {})'.format( + self.prompt[0], TRI_TO_STR[expr_value(self.prompt[1])])) if self.item.__class__ is Symbol and self.is_menuconfig: - fields.append("is menuconfig") + add("is menuconfig") - fields.append("deps " + TRI_TO_STR[expr_value(self.dep)]) + add("deps " + TRI_TO_STR[expr_value(self.dep)]) if self.item is MENU: - fields.append("'visible if' deps " + - TRI_TO_STR[expr_value(self.visibility)]) + add("'visible if' deps " + TRI_TO_STR[expr_value(self.visibility)]) if self.item.__class__ in _SYMBOL_CHOICE and self.help is not None: - fields.append("has help") + add("has help") if self.list: - fields.append("has child") + add("has child") if self.next: - fields.append("has next") + add("has next") - fields.append("{}:{}".format(self.filename, self.linenr)) + add("{}:{}".format(self.filename, self.linenr)) return "<{}>".format(", ".join(fields)) diff --git a/tests/Krepr b/tests/Krepr index 72b401d..fe6d8f0 100644 --- a/tests/Krepr +++ b/tests/Krepr @@ -14,6 +14,9 @@ config BASIC config VISIBLE bool "visible" +config STRING + string "visible" + config DIR_DEP_N depends on n diff --git a/testsuite.py b/testsuite.py index 8e17d84..4bb1991 100644 --- a/testsuite.py +++ b/testsuite.py @@ -856,25 +856,30 @@ config DEP_REM_CORNER_CASES """) c.syms["VISIBLE"].set_value(2) + c.syms["STRING"].set_value("foo") verify_repr(c.syms["VISIBLE"], """ <symbol VISIBLE, bool, "visible", value y, user value y, visibility y, direct deps y, Kconfiglib/tests/Krepr:14> """) + verify_repr(c.syms["STRING"], """ +<symbol STRING, string, "visible", value "foo", user value "foo", visibility y, direct deps y, Kconfiglib/tests/Krepr:17> +""") + verify_repr(c.syms["DIR_DEP_N"], """ -<symbol DIR_DEP_N, unknown, value "DIR_DEP_N", visibility n, direct deps n, Kconfiglib/tests/Krepr:17> +<symbol DIR_DEP_N, unknown, value "DIR_DEP_N", visibility n, direct deps n, Kconfiglib/tests/Krepr:20> """) verify_repr(c.syms["OPTIONS"], """ -<symbol OPTIONS, unknown, value "OPTIONS", visibility n, allnoconfig_y, is the defconfig_list symbol, from environment variable ENV, direct deps y, Kconfiglib/tests/Krepr:20> +<symbol OPTIONS, unknown, value "OPTIONS", visibility n, allnoconfig_y, is the defconfig_list symbol, from environment variable ENV, direct deps y, Kconfiglib/tests/Krepr:23> """) verify_repr(c.syms["MULTI_DEF"], """ -<symbol MULTI_DEF, unknown, value "MULTI_DEF", visibility n, direct deps y, Kconfiglib/tests/Krepr:25, Kconfiglib/tests/Krepr:26> +<symbol MULTI_DEF, unknown, value "MULTI_DEF", visibility n, direct deps y, Kconfiglib/tests/Krepr:28, Kconfiglib/tests/Krepr:29> """) verify_repr(c.syms["CHOICE_1"], """ -<symbol CHOICE_1, tristate, "choice sym", value n, visibility m, choice symbol, direct deps m, Kconfiglib/tests/Krepr:33> +<symbol CHOICE_1, tristate, "choice sym", value n, visibility m, choice symbol, direct deps m, Kconfiglib/tests/Krepr:36> """) verify_repr(c.modules, """ @@ -885,29 +890,29 @@ config DEP_REM_CORNER_CASES print("Testing Choice.__repr__()") verify_repr(c.named_choices["CHOICE"], """ -<choice CHOICE, tristate, "choice", mode m, visibility y, Kconfiglib/tests/Krepr:30> +<choice CHOICE, tristate, "choice", mode m, visibility y, Kconfiglib/tests/Krepr:33> """) c.named_choices["CHOICE"].set_value(2) verify_repr(c.named_choices["CHOICE"], """ -<choice CHOICE, tristate, "choice", mode y, user mode y, CHOICE_1 selected, visibility y, Kconfiglib/tests/Krepr:30> +<choice CHOICE, tristate, "choice", mode y, user mode y, CHOICE_1 selected, visibility y, Kconfiglib/tests/Krepr:33> """) c.syms["CHOICE_2"].set_value(2) verify_repr(c.named_choices["CHOICE"], """ -<choice CHOICE, tristate, "choice", mode y, user mode y, CHOICE_2 selected, CHOICE_2 selected by user, visibility y, Kconfiglib/tests/Krepr:30> +<choice CHOICE, tristate, "choice", mode y, user mode y, CHOICE_2 selected, CHOICE_2 selected by user, visibility y, Kconfiglib/tests/Krepr:33> """) c.named_choices["CHOICE"].set_value(1) verify_repr(c.named_choices["CHOICE"], """ -<choice CHOICE, tristate, "choice", mode m, user mode m, CHOICE_2 selected by user (overridden), visibility y, Kconfiglib/tests/Krepr:30> +<choice CHOICE, tristate, "choice", mode m, user mode m, CHOICE_2 selected by user (overridden), visibility y, Kconfiglib/tests/Krepr:33> """) verify_repr(c.syms["CHOICE_HOOK"].nodes[0].next.item, """ -<choice, tristate, "optional choice", mode n, visibility n, optional, Kconfiglib/tests/Krepr:43> +<choice, tristate, "optional choice", mode n, visibility n, optional, Kconfiglib/tests/Krepr:46> """) @@ -918,46 +923,46 @@ config DEP_REM_CORNER_CASES """) verify_repr(c.syms["DIR_DEP_N"].nodes[0], """ -<menu node for symbol DIR_DEP_N, deps n, has next, Kconfiglib/tests/Krepr:17> +<menu node for symbol DIR_DEP_N, deps n, has next, Kconfiglib/tests/Krepr:20> """) verify_repr(c.syms["MULTI_DEF"].nodes[0], """ -<menu node for symbol MULTI_DEF, deps y, has next, Kconfiglib/tests/Krepr:25> +<menu node for symbol MULTI_DEF, deps y, has next, Kconfiglib/tests/Krepr:28> """) verify_repr(c.syms["MULTI_DEF"].nodes[1], """ -<menu node for symbol MULTI_DEF, deps y, has next, Kconfiglib/tests/Krepr:26> +<menu node for symbol MULTI_DEF, deps y, has next, Kconfiglib/tests/Krepr:29> """) verify_repr(c.syms["MENUCONFIG"].nodes[0], """ -<menu node for symbol MENUCONFIG, is menuconfig, deps y, has next, Kconfiglib/tests/Krepr:28> +<menu node for symbol MENUCONFIG, is menuconfig, deps y, has next, Kconfiglib/tests/Krepr:31> """) verify_repr(c.named_choices["CHOICE"].nodes[0], """ -<menu node for choice CHOICE, prompt "choice" (visibility y), deps y, has child, has next, Kconfiglib/tests/Krepr:30> +<menu node for choice CHOICE, prompt "choice" (visibility y), deps y, has child, has next, Kconfiglib/tests/Krepr:33> """) verify_repr(c.syms["CHOICE_HOOK"].nodes[0].next, """ -<menu node for choice, prompt "optional choice" (visibility n), deps y, has next, Kconfiglib/tests/Krepr:43> +<menu node for choice, prompt "optional choice" (visibility n), deps y, has next, Kconfiglib/tests/Krepr:46> """) verify_repr(c.syms["NO_VISIBLE_IF_HOOK"].nodes[0].next, """ -<menu node for menu, prompt "no visible if" (visibility y), deps y, 'visible if' deps y, has next, Kconfiglib/tests/Krepr:50> +<menu node for menu, prompt "no visible if" (visibility y), deps y, 'visible if' deps y, has next, Kconfiglib/tests/Krepr:53> """) verify_repr(c.syms["VISIBLE_IF_HOOK"].nodes[0].next, """ -<menu node for menu, prompt "visible if" (visibility y), deps y, 'visible if' deps m, has next, Kconfiglib/tests/Krepr:55> +<menu node for menu, prompt "visible if" (visibility y), deps y, 'visible if' deps m, has next, Kconfiglib/tests/Krepr:58> """) verify_repr(c.syms["COMMENT_HOOK"].nodes[0].next, """ -<menu node for comment, prompt "comment" (visibility y), deps y, Kconfiglib/tests/Krepr:61> +<menu node for comment, prompt "comment" (visibility y), deps y, Kconfiglib/tests/Krepr:64> """) print("Testing Kconfig.__repr__()") verify_repr(c, """ -<configuration with 14 symbols, main menu prompt "Main menu", srctree is current directory, config symbol prefix "CONFIG_", warnings disabled, printing of warnings to stderr enabled, undef. symbol assignment warnings disabled, overriding symbol assignment warnings enabled, redundant symbol assignment warnings enabled> +<configuration with 15 symbols, main menu prompt "Main menu", srctree is current directory, config symbol prefix "CONFIG_", warnings disabled, printing of warnings to stderr enabled, undef. symbol assignment warnings disabled, overriding symbol assignment warnings enabled, redundant symbol assignment warnings enabled> """) os.environ["srctree"] = "Kconfiglib" @@ -971,7 +976,7 @@ config DEP_REM_CORNER_CASES c.warn_assign_undef = True verify_repr(c, """ -<configuration with 14 symbols, main menu prompt "Main menu", srctree "Kconfiglib", config symbol prefix "CONFIG_ value", warnings enabled, printing of warnings to stderr disabled, undef. symbol assignment warnings enabled, overriding symbol assignment warnings disabled, redundant symbol assignment warnings disabled> +<configuration with 15 symbols, main menu prompt "Main menu", srctree "Kconfiglib", config symbol prefix "CONFIG_ value", warnings enabled, printing of warnings to stderr disabled, undef. symbol assignment warnings enabled, overriding symbol assignment warnings disabled, redundant symbol assignment warnings disabled> """) os.environ.pop("srctree", None) |
