Skip to content

Commit

Permalink
!squash options split out dict/struct method
Browse files Browse the repository at this point in the history
  • Loading branch information
tony committed May 4, 2024
1 parent 403b276 commit 6a87f72
Showing 1 changed file with 59 additions and 6 deletions.
65 changes: 59 additions & 6 deletions src/libtmux/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,57 @@ def _show_options_raw(

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

def _show_options_dict(
self,
g: t.Optional[bool] = False,
_global: t.Optional[bool] = False,
scope: t.Optional[
t.Union[OptionScope, _DefaultOptionScope]
] = DEFAULT_OPTION_SCOPE,
include_hooks: t.Optional[bool] = None,
include_inherited: t.Optional[bool] = None,
) -> "UntypedOptionsDict":
"""Return dict of options for the target.
Parameters
----------
g : str, optional
Pass ``-g`` flag for global variable, default False.
Examples
--------
>>> import typing as t
>>> from libtmux.common import tmux_cmd
>>> from libtmux.constants import OptionScope
>>> class MyServer(OptionMixin):
... socket_name = server.socket_name
... def cmd(self, cmd: str, *args: object):
... cmd_args: t.List[t.Union[str, int]] = [cmd]
... if self.socket_name:
... cmd_args.insert(0, f"-L{self.socket_name}")
... cmd_args.insert(0, "-f/dev/null")
... return tmux_cmd(*cmd_args, *args)
...
... default_option_scope = OptionScope.Server
>>> MyServer()._show_options_dict()
{...}
>>> isinstance(MyServer()._show_options_dict(), dict)
True
"""
cmd = self._show_options_raw(
_global=_global,
scope=scope,
include_hooks=include_hooks,
include_inherited=include_inherited,
)

return parse_options_to_dict(
io.StringIO("\n".join(cmd.stdout)),
)

def _show_options(
self,
g: t.Optional[bool] = False,
Expand Down Expand Up @@ -844,7 +895,7 @@ def _show_options(
>>> MyServer()._show_options()
{...}
"""
cmd = self._show_options_raw(
dict_output = self._show_options_dict(
_global=_global,
scope=scope,
include_hooks=include_hooks,
Expand All @@ -853,11 +904,7 @@ def _show_options(

output_exploded = convert_values(
explode_complex(
explode_arrays(
parse_options_to_dict(
io.StringIO("\n".join(cmd.stdout)),
),
),
explode_arrays(dict_output),
),
)

Expand Down Expand Up @@ -913,6 +960,12 @@ def _show_option_raw(
>>> MyServer()._show_option_raw('exit-unattached', _global=True).stdout
['exit-unattached off']
>>> isinstance(MyServer()._show_option_raw('exit-unattached', _global=True).stdout, list)
True
>>> isinstance(MyServer()._show_option_raw('exit-unattached', _global=True).stdout[0], str)
True
"""
if scope is DEFAULT_OPTION_SCOPE:
scope = self.default_option_scope
Expand Down

0 comments on commit 6a87f72

Please sign in to comment.