Skip to content

Commit

Permalink
Merge pull request #746 from insanum/config-toml
Browse files Browse the repository at this point in the history
New config.toml: rename default-calendar->default-calendars, add note config.toml file into --help
  • Loading branch information
dbarnett authored Sep 12, 2024
2 parents 64c5c8e + 8bf8a10 commit 6b34d4f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
2 changes: 1 addition & 1 deletion data/config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"type": "object",
"description": "Settings about the set of calendars gcalcli operates on",
"properties": {
"default-calendar": {
"default-calendars": {
"type": "array",
"items": { "type": "string" },
"description": "Calendar(s) to use as default for certain commands when no explicit target calendar is otherwise specified"
Expand Down
31 changes: 26 additions & 5 deletions gcalcli/argparsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,12 +293,18 @@ class RawDescArgDefaultsHelpFormatter(
%(prog)s supports a few other configuration mechanisms in addition to
the command-line arguments listed below.
$GCALCLI_CONFIG={config_dir!r}
$GCALCLI_CONFIG={config_dir}
Path to user config directory.
Note: this path is also used to determine fallback paths to check
for cache/oauth files to be migrated into their proper data dir
paths.
{config_file}
A toml config file where some general-purpose settings can be
configured.
Schema:
https://raw.githubusercontent.com/insanum/gcalcli/HEAD/data/config-schema.json
gcalclirc @ {rc_paths}
A flag file listing additional command-line args to always pass,
one per line.
Expand All @@ -308,17 +314,32 @@ class RawDescArgDefaultsHelpFormatter(
"""


def shorten_path(path: pathlib.Path) -> pathlib.Path:
"""Try to shorten path using special characters like ~.
Returns original path unmodified if it can't be shortened.
"""
tilde_home = pathlib.Path('~')
expanduser_len = len(tilde_home.expanduser().parts)
if path.parts[:expanduser_len] == tilde_home.expanduser().parts:
return tilde_home.joinpath(*path.parts[expanduser_len:])
return path


@parser_allow_deprecated(name='program')
def get_argument_parser():
config_dir = env.default_config_dir()
rc_paths = [pathlib.Path(config_dir).joinpath('gcalclirc')]
config_dir = shorten_path(env.default_config_dir())
# Shorten path to ~/PATH if possible for easier readability.
rc_paths = [config_dir.joinpath('gcalclirc')]
legacy_rc_path = pathlib.Path.home().joinpath('.gcalclirc')
if legacy_rc_path.exists():
rc_paths.append(legacy_rc_path)
rc_paths.append(shorten_path(legacy_rc_path))

parser = argparse.ArgumentParser(
description=DESCRIPTION.format(
config_dir=config_dir, rc_paths=', '.join(str(p) for p in rc_paths)
config_dir=config_dir,
config_file=config_dir.joinpath('config.toml'),
rc_paths=', '.join(str(p) for p in rc_paths),
),
formatter_class=RawDescArgDefaultsHelpFormatter,
fromfile_prefix_chars='@',
Expand Down
2 changes: 1 addition & 1 deletion gcalcli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def main():
with config_filepath.open('rb') as config_file:
config = tomllib.load(config_file)
opts_from_config.defaultCalendar = config.get('calendars', {}).get(
'default-calendar', []
'default-calendars', []
)

if parsed_args.config_folder:
Expand Down

0 comments on commit 6b34d4f

Please sign in to comment.