Skip to content

Commit 01ec331

Browse files
committed
chore(ruff): Manual fixes for cli/convert.py
src/tmuxp/cli/convert.py:64:15: TRY002 Create your own exception src/tmuxp/cli/convert.py:64:15: TRY003 Avoid specifying long messages outside the exception class src/tmuxp/cli/convert.py:75:5: SIM102 Use a single `if` statement instead of nested `if` statements src/tmuxp/cli/convert.py:76:9: SIM102 Use a single `if` statement instead of nested `if` statements src/tmuxp/cli/convert.py:81:15: SIM115 Use context handler for opening files
1 parent 1927410 commit 01ec331

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

src/tmuxp/cli/convert.py

+17-8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from tmuxp.config_reader import ConfigReader
77
from tmuxp.workspace.finders import find_workspace_file, get_workspace_dir
88

9+
from .. import exc
910
from .utils import prompt_yes_no
1011

1112
if t.TYPE_CHECKING:
@@ -40,6 +41,13 @@ def create_convert_subparser(
4041
return parser
4142

4243

44+
class ConvertUnknownFileType(exc.TmuxpException):
45+
def __init__(self, ext: str, *args: object, **kwargs: object) -> None:
46+
return super().__init__(
47+
f"Unknown filetype: {ext} (valid: [.json, .yaml, .yml])"
48+
)
49+
50+
4351
def command_convert(
4452
workspace_file: t.Union[str, pathlib.Path],
4553
answer_yes: bool,
@@ -61,7 +69,7 @@ def command_convert(
6169
elif ext in [".yaml", ".yml"]:
6270
to_filetype = "json"
6371
else:
64-
raise Exception(f"Unknown filetype: {ext} (valid: [.json, .yaml, .yml])")
72+
raise ConvertUnknownFileType(ext)
6573

6674
configparser = ConfigReader.from_file(workspace_file)
6775
newfile = workspace_file.parent / (str(workspace_file.stem) + f".{to_filetype}")
@@ -72,13 +80,14 @@ def command_convert(
7280
**{"default_flow_style": False} if to_filetype == "yaml" else {},
7381
)
7482

75-
if not answer_yes:
76-
if prompt_yes_no(f"Convert to <{workspace_file}> to {to_filetype}?"):
77-
if prompt_yes_no("Save workspace to %s?" % newfile):
78-
answer_yes = True
83+
if (
84+
not answer_yes
85+
and prompt_yes_no(f"Convert to <{workspace_file}> to {to_filetype}?")
86+
and prompt_yes_no("Save workspace to %s?" % newfile)
87+
):
88+
answer_yes = True
7989

8090
if answer_yes:
81-
buf = open(newfile, "w")
82-
buf.write(new_workspace)
83-
buf.close()
91+
with open(newfile, "w") as buf:
92+
buf.write(new_workspace)
8493
print(f"New workspace file saved to <{newfile}>.")

0 commit comments

Comments
 (0)