Skip to content

Commit

Permalink
Add support for parsing config.toml from config dir
Browse files Browse the repository at this point in the history
Initially this just supports a single config key,
calendars.default-calendar.
  • Loading branch information
dbarnett committed Sep 12, 2024
1 parent ae30d65 commit 2bd4fc5
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ gcalcli.egg-info
.pytest_cache?
.tox

.python-version
*.pyc
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ include ChangeLog
include README.md
exclude .git*

graft data
graft docs
prune .github
19 changes: 19 additions & 0 deletions data/config-schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "gcalcli config",
"type": "object",
"description": "User configuration for gcalcli command-line tool. See https://pypi.org/project/gcalcli/.",
"properties": {
"calendars": {
"type": "object",
"description": "Settings about the set of calendars gcalcli operates on",
"properties": {
"default-calendar": {
"type": "array",
"items": { "type": "string" },
"description": "Calendar(s) to use as default for certain commands when no explicit target calendar is otherwise specified"
}
}
}
}
}
10 changes: 3 additions & 7 deletions gcalcli/argparsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@
import copy as _copy
import datetime
import locale
import os
import pathlib
import sys
from shutil import get_terminal_size

import argcomplete # type: ignore
import platformdirs

import gcalcli

from . import utils
from . import env, utils
from .deprecations import DeprecatedStoreTrue, parser_allow_deprecated
from .details import DETAILS
from .printer import valid_color_name
Expand All @@ -35,7 +33,7 @@
'is no longer supported.',
},
'--config-folder': {
'default': os.environ.get('GCALCLI_CONFIG'),
'default': env.default_config_dir(),
'type': pathlib.Path,
'help': 'Optional directory used to load config files. Deprecated: '
'prefer $GCALCLI_CONFIG.',
Expand Down Expand Up @@ -312,9 +310,7 @@ class RawDescArgDefaultsHelpFormatter(

@parser_allow_deprecated(name='program')
def get_argument_parser():
config_dir = (
os.environ.get('GCALCLI_CONFIG') or platformdirs.user_config_dir()
)
config_dir = env.default_config_dir()
rc_paths = [pathlib.Path(config_dir).joinpath('gcalclirc')]
legacy_rc_path = pathlib.Path.home().joinpath('.gcalclirc')
if legacy_rc_path.exists():
Expand Down
25 changes: 22 additions & 3 deletions gcalcli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,15 @@
import os
import signal
import sys
if sys.version_info[:2] < (3, 11):
import toml as tomllib
else:
import tomllib

import argparse
import truststore

from . import utils
from . import env, utils
from .argparsers import get_argument_parser, handle_unparsed
from .exceptions import GcalcliError
from .gcal import GoogleCalendarInterface
Expand Down Expand Up @@ -86,9 +92,10 @@ def run_add_prompt(parsed_args, printer):


def main():
parser = get_argument_parser()
# import trusted certificate store to enable SSL, e.g., behind firewalls
truststore.inject_into_ssl()

parser = get_argument_parser()
try:
argv = sys.argv[1:]
gcalclirc = os.path.expanduser('~/.gcalclirc')
Expand All @@ -106,6 +113,16 @@ def main():
parser.print_usage()
sys.exit(1)

config_dir = env.default_config_dir()
config_filepath = config_dir.joinpath('config.toml')
opts_from_config = argparse.Namespace()
if config_filepath.exists():
with config_filepath.open('rb') as config_file:
config = tomllib.load(config_file)
opts_from_config.defaultCalendar = config.get('calendars', {}).get(
'default-calendar', []
)

if parsed_args.config_folder:
parsed_args.config_folder = parsed_args.config_folder.expanduser()
gcalclirc_path = parsed_args.config_folder.joinpath('gcalclirc')
Expand All @@ -116,7 +133,9 @@ def main():
tmp_argv if parsed_args.includeRc else argv
)

(parsed_args, unparsed) = parser.parse_known_args(tmp_argv)
(parsed_args, unparsed) = parser.parse_known_args(
tmp_argv, namespace=opts_from_config
)
if parsed_args.config_folder:
parsed_args.config_folder = parsed_args.config_folder.expanduser()

Expand Down
16 changes: 16 additions & 0 deletions gcalcli/env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import os
import pathlib
import platformdirs

from . import __program__


def default_data_dir() -> pathlib.Path:
return platformdirs.user_data_path(__program__)


def default_config_dir() -> pathlib.Path:
return pathlib.Path(
os.environ.get('GCALCLI_CONFIG')
or platformdirs.user_config_dir(__program__)
)
9 changes: 2 additions & 7 deletions gcalcli/gcal.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@
from dateutil.tz import tzlocal
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
import platformdirs

from . import __program__, actions, auth, ics, utils
from . import actions, auth, env, ics, utils
from ._types import Cache, CalendarListEntry, Event
from .actions import ACTIONS
from .conflicts import ShowConflicts
Expand Down Expand Up @@ -134,13 +133,9 @@ def _retry_with_backoff(self, method: googleapiclient.http.HttpRequest):

return None

@staticmethod
def default_data_dir() -> pathlib.Path:
return platformdirs.user_data_path(__program__)

@functools.cache
def data_file_path(self, name: str) -> pathlib.Path:
path = self.default_data_dir().joinpath(name)
path = env.default_data_dir().joinpath(name)
explicit_config: pathlib.Path = self.options['config_folder']
if explicit_config:
legacy_path = explicit_config.joinpath(name)
Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ dependencies = [
"parsedatetime",
"platformdirs",
"python-dateutil",
"toml; python_version < '3.11'",
"truststore",
]

Expand All @@ -46,6 +47,7 @@ dev = [
"google-api-python-client-stubs",
"types-python-dateutil",
"types-requests",
"types-toml; python_version < '3.11'",
"types-vobject",
]
vobject = ["vobject"]
Expand Down

0 comments on commit 2bd4fc5

Please sign in to comment.