From c271a8f5cb9d6a2bc8c02464302b7f83facf9f7d Mon Sep 17 00:00:00 2001 From: snomiao Date: Tue, 17 Feb 2026 11:13:16 +0000 Subject: [PATCH] refactor: use shopen instead of manual launch_editor (ported from insanum#767) Replace the hand-rolled launch_editor utility (which tried editor/EDITOR/ xdg-open/open in sequence) with the shopen library. Removes subprocess and os imports from utils.py. Conflict-resolved against main. Co-Authored-By: David Barnett Co-Authored-By: Claude Sonnet 4.5 --- gcalcli/cli.py | 4 +++- gcalcli/utils.py | 22 ---------------------- pyproject.toml | 1 + 3 files changed, 4 insertions(+), 23 deletions(-) diff --git a/gcalcli/cli.py b/gcalcli/cli.py index 5f7ad1da..1ef51c58 100755 --- a/gcalcli/cli.py +++ b/gcalcli/cli.py @@ -40,6 +40,8 @@ from argparse import ArgumentTypeError from collections import namedtuple +import shopen + from . import config, env, utils from .argparsers import get_argument_parser, handle_unparsed from .exceptions import GcalcliError @@ -347,7 +349,7 @@ def main(): config_filepath.parent.mkdir(parents=True, exist_ok=True) with open(config_filepath, 'w') as f: f.write(EMPTY_CONFIG_TOML) - utils.launch_editor(config_filepath) + shopen.open(config_filepath, 'edit') elif parsed_args.command == 'util': if parsed_args.subcommand == 'config-schema': diff --git a/gcalcli/utils.py b/gcalcli/utils.py index 2d83c567..1959b6fb 100644 --- a/gcalcli/utils.py +++ b/gcalcli/utils.py @@ -2,11 +2,9 @@ from collections import OrderedDict import json import locale -import os import pathlib import pickle import re -import subprocess import time from datetime import datetime, timedelta from typing import Any, Tuple @@ -207,26 +205,6 @@ def localize_datetime(dt): return dt.astimezone(tzlocal()) -def launch_editor(path: str | os.PathLike): - if hasattr(os, 'startfile'): - os.startfile(path, 'edit') - return - for editor in ( - 'editor', - os.environ.get('EDITOR', None), - 'xdg-open', - 'open', - ): - if not editor: - continue - try: - subprocess.call((editor, path)) - return - except OSError: - pass - raise OSError(f'No editor/launcher detected on your system to edit {path}') - - def shorten_path(path: pathlib.Path) -> pathlib.Path: """Try to shorten path using special characters like ~. diff --git a/pyproject.toml b/pyproject.toml index f70a63a6..83e8219a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -35,6 +35,7 @@ dependencies = [ "platformdirs", "pydantic", "python-dateutil", + "shopen >= 0.2.2", "tomli; python_version < '3.11'", "truststore", ]