Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch core's configuration to allow get from global scope #86

Merged
merged 1 commit into from
Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions changelog.d/20220718_122937_michael.hanke_enh_config.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<!-- Uncomment the section that is right (remove the HTML comment wrapper).-->
<!--
### 🐛 Bug Fixes

- Describe change, possibly reference closed/related issue/PR.
Fixes [#XXX](https://github.com/datalad/datalad-next/issues/XXX)
[#XXX](https://github.com/datalad/datalad-next/pull/XXX) (by @GITHUBHANDLE)
-->
### 💫 Enhancements and new features

- The `configuration` command no longer requires a datasets to be present
for a `get` operation to retrieve a configuration item from scope `global`.
Fixes [#6864](https://github.com/datalad/datalad/issues/6854) via
[#86](https://github.com/datalad/datalad-next/pull/86) (by @mih)
<!--
### 🪓 Deprecations and removals

- Describe change, possibly reference closed/related issue/PR.
Fixes [#XXX](https://github.com/datalad/datalad-next/issues/XXX)
[#XXX](https://github.com/datalad/datalad-next/pull/XXX) (by @GITHUBHANDLE)
-->
<!--
### 📝 Documentation

- Describe change, possibly reference closed/related issue/PR.
Fixes [#XXX](https://github.com/datalad/datalad-next/issues/XXX)
[#XXX](https://github.com/datalad/datalad-next/pull/XXX) (by @GITHUBHANDLE)
-->
<!--
### 🏠 Internal

- Describe change, possibly reference closed/related issue/PR.
Fixes [#XXX](https://github.com/datalad/datalad-next/issues/XXX)
[#XXX](https://github.com/datalad/datalad-next/pull/XXX) (by @GITHUBHANDLE)
-->
<!--
### 🛡 Tests

- Describe change, possibly reference closed/related issue/PR.
Fixes [#XXX](https://github.com/datalad/datalad-next/issues/XXX)
[#XXX](https://github.com/datalad/datalad-next/pull/XXX) (by @GITHUBHANDLE)
-->
1 change: 1 addition & 0 deletions datalad_next/patches/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from . import (
annexrepo,
configuration,
create_sibling_ghlike,
push_to_export_remote,
push_optimize,
Expand Down
199 changes: 199 additions & 0 deletions datalad_next/patches/configuration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
# emacs: -*- mode: python; py-indent-offset: 4; tab-width: 4; indent-tabs-mode: nil -*-
# ex: set sts=4 ts=4 sw=4 noet:
# ## ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
#
# See COPYING file distributed along with the datalad package for the
# copyright and license terms.
#
# ## ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
"""Frontend for the DataLad config"""

__docformat__ = 'restructuredtext'


import logging

from datalad import cfg as dlcfg
from datalad.distribution.dataset import (
Dataset,
datasetmethod,
require_dataset,
)
from datalad.interface.base import (
build_doc,
)
from datalad.interface.common_cfg import definitions as cfg_defs
from datalad.interface.results import get_status_dict
from datalad.interface.utils import (
eval_results,
)
from datalad.local import configuration as conf_mod
from datalad.local.configuration import (
config_actions,
_dump,
_get,
_set,
_unset,
)
from datalad.support.exceptions import (
NoDatasetFound,
)
from datalad.utils import (
ensure_list,
)

lgr = logging.getLogger('datalad.local.configuration')


@build_doc
class Configuration(conf_mod.Configuration):
""""""
@staticmethod
@datasetmethod(name='configuration')
@eval_results
def __call__(
action='dump',
spec=None,
*,
scope=None,
dataset=None,
recursive=False,
recursion_limit=None):

# check conditions
# - global and recursion makes no sense

if action == 'dump':
if scope:
raise ValueError(
'Scope selection is not supported for dumping')

# normalize variable specificatons
specs = []
for s in ensure_list(spec):
if isinstance(s, tuple):
specs.append((str(s[0]), str(s[1])))
elif '=' not in s:
specs.append((str(s),))
else:
specs.append(tuple(s.split('=', 1)))

if action == 'set':
missing_values = [s[0] for s in specs if len(s) < 2]
if missing_values:
raise ValueError(
'Values must be provided for all configuration '
'settings. Missing: {}'.format(missing_values))
invalid_names = [s[0] for s in specs if '.' not in s[0]]
if invalid_names:
raise ValueError(
'Name must contain a section (i.e. "section.name"). '
'Invalid: {}'.format(invalid_names))

ds = None
if scope != 'global' or recursive:
try:
ds = require_dataset(
dataset,
check_installed=True,
purpose='configure')
except NoDatasetFound:
if action not in ('dump', 'get') or dataset:
raise

res_kwargs = dict(
action='configuration',
logger=lgr,
)
if ds:
res_kwargs['refds'] = ds.path
yield from configuration(action, scope, specs, res_kwargs, ds)

if not recursive:
return

for subds in ds.subdatasets(
state='present',
recursive=True,
recursion_limit=recursion_limit,
on_failure='ignore',
return_type='generator',
result_renderer='disabled'):
yield from configuration(
action, scope, specs, res_kwargs, Dataset(subds['path']))


def configuration(action, scope, specs, res_kwargs, ds=None):
# go with the more specific dataset configmanager, if we are
# operating on a dataset
cfg = dlcfg if ds is None else ds.config

if action not in config_actions:
raise ValueError("Unsupported action '{}'".format(action))

if action == 'dump':
if not specs:
# dumping is querying for all known keys
specs = [
(n,) for n in sorted(
set(cfg_defs.keys()).union(cfg.keys()))
]
scope = None

for spec in specs:
if '.' not in spec[0]:
yield get_status_dict(
ds=ds,
status='error',
message=(
"Configuration key without a section: '%s'",
spec[0],
),
**res_kwargs)
continue
# TODO without get-all there is little sense in having add
#if action == 'add':
# res = _add(cfg, scope, spec)
if action == 'get':
res = _get(cfg, scope, spec[0])
elif action == 'dump':
res = _dump(cfg, spec[0])
# TODO this should be there, if we want to be comprehensive
# however, we turned this off by default in the config manager
# because we hardly use it, and the handling in ConfigManager
# is not really well done.
#elif action == 'get-all':
# res = _get_all(cfg, scope, spec)
elif action == 'set':
res = _set(cfg, scope, *spec)
elif action == 'unset':
res = _unset(cfg, scope, spec[0])

if ds:
res['path'] = ds.path

if 'status' not in res:
res['status'] = 'ok'

yield dict(res_kwargs, **res)

if action in ('add', 'set', 'unset'):
# we perform a single reload, rather than one for each modification
# TODO: can we detect a call from cmdline? We could skip the reload.
cfg.reload(force=True)


conf_mod.Configuration.__call__ = Configuration.__call__
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you derive from from conf_mod.Configuration, wouldn't it make sense to replace the class rather than just __call__? Or the other way around: derive from Interface as usual?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought so too, but just replacing the class is not enough. You would still need to replace the references to its __call__. When I did this, I ended up in a situation where replacing the class itself no longer has any impact.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I seem to remember I got that way of patching to work at some point. Need to dig. But okay - don't let it be an obstacle for merge. We can "fix" that later.

conf_mod.Configuration._params_['scope']._doc = """\
scope for getting or setting
configuration. If no scope is declared for a query, all
configuration sources (including overrides via environment
variables) are considered according to the normal
rules of precedence. A 'get' action can be constrained to
scope 'branch', otherwise 'global' is used when not operating
on a dataset, or 'local' (including 'global', when operating
on a dataset.
For action 'dump', a scope selection is ignored and all available
scopes are considered."""
conf_mod.Configuration.__call__.__doc__ = None
conf_mod.Configuration = build_doc(conf_mod.Configuration)
28 changes: 28 additions & 0 deletions datalad_next/patches/tests/test_configuration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from datalad.tests.utils_pytest import (
assert_in_results,
chpwd,
with_tempfile,
)
from datalad.api import (
Dataset,
configuration,
)

# run all -core tests
from datalad.local.tests.test_configuration import *


@with_tempfile(mkdir=True)
def test_config_get_global(path=None):
"""Make sure `get` does not require a dataset to be present"""
# enter a tempdir to be confident that there is no dataset around
with chpwd(path):
res = configuration('get', 'user.name', result_renderer='disabled')
assert_in_results(
res,
name='user.name',
status='ok',
)
# verify that the dataset method was replaced too
ds = Dataset(path).create()
assert "'get' action can be constrained" in ds.configuration.__doc__