Skip to content

Commit b32c5b5

Browse files
committed
!squash options split out dict/struct method
1 parent c5b09dd commit b32c5b5

File tree

1 file changed

+59
-6
lines changed

1 file changed

+59
-6
lines changed

src/libtmux/options.py

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,57 @@ def _show_options_raw(
807807

808808
return self.cmd("show-options", *flags)
809809

810+
def _show_options_dict(
811+
self,
812+
g: t.Optional[bool] = False,
813+
_global: t.Optional[bool] = False,
814+
scope: t.Optional[
815+
t.Union[OptionScope, _DefaultOptionScope]
816+
] = DEFAULT_OPTION_SCOPE,
817+
include_hooks: t.Optional[bool] = None,
818+
include_inherited: t.Optional[bool] = None,
819+
) -> "UntypedOptionsDict":
820+
"""Return dict of options for the target.
821+
822+
Parameters
823+
----------
824+
g : str, optional
825+
Pass ``-g`` flag for global variable, default False.
826+
827+
Examples
828+
--------
829+
>>> import typing as t
830+
>>> from libtmux.common import tmux_cmd
831+
>>> from libtmux.constants import OptionScope
832+
833+
>>> class MyServer(OptionMixin):
834+
... socket_name = server.socket_name
835+
... def cmd(self, cmd: str, *args: object):
836+
... cmd_args: t.List[t.Union[str, int]] = [cmd]
837+
... if self.socket_name:
838+
... cmd_args.insert(0, f"-L{self.socket_name}")
839+
... cmd_args.insert(0, "-f/dev/null")
840+
... return tmux_cmd(*cmd_args, *args)
841+
...
842+
... default_option_scope = OptionScope.Server
843+
844+
>>> MyServer()._show_options_dict()
845+
{...}
846+
847+
>>> isinstance(MyServer()._show_options_dict(), dict)
848+
True
849+
"""
850+
cmd = self._show_options_raw(
851+
_global=_global,
852+
scope=scope,
853+
include_hooks=include_hooks,
854+
include_inherited=include_inherited,
855+
)
856+
857+
return parse_options_to_dict(
858+
io.StringIO("\n".join(cmd.stdout)),
859+
)
860+
810861
def _show_options(
811862
self,
812863
g: t.Optional[bool] = False,
@@ -844,7 +895,7 @@ def _show_options(
844895
>>> MyServer()._show_options()
845896
{...}
846897
"""
847-
cmd = self._show_options_raw(
898+
dict_output = self._show_options_dict(
848899
_global=_global,
849900
scope=scope,
850901
include_hooks=include_hooks,
@@ -853,11 +904,7 @@ def _show_options(
853904

854905
output_exploded = convert_values(
855906
explode_complex(
856-
explode_arrays(
857-
parse_options_to_dict(
858-
io.StringIO("\n".join(cmd.stdout)),
859-
),
860-
),
907+
explode_arrays(dict_output),
861908
),
862909
)
863910

@@ -913,6 +960,12 @@ def _show_option_raw(
913960
914961
>>> MyServer()._show_option_raw('exit-unattached', _global=True).stdout
915962
['exit-unattached off']
963+
964+
>>> isinstance(MyServer()._show_option_raw('exit-unattached', _global=True).stdout, list)
965+
True
966+
967+
>>> isinstance(MyServer()._show_option_raw('exit-unattached', _global=True).stdout[0], str)
968+
True
916969
"""
917970
if scope is DEFAULT_OPTION_SCOPE:
918971
scope = self.default_option_scope

0 commit comments

Comments
 (0)