mirror of
https://github.com/Stone-Red-Code/dvc.git
synced 2026-11-30 23:10:41 +01:00
* dvc: refactor config
Advantages:
- read like a dict
- modify as a dict for testing or manipulation purposes
- wrap modifications into context manager to save them:
with config.edit("local") as conf:
conf["core"]["remote"] = "new-default"
Design decisions:
- dropped string constants
- remote sections are parsed into a simple subdict upon load
- remote config schema is now defined by url scheme
- relative paths are handled transparently upon load/save
- user config and remote manipulations are moved to commands
- same dev manipulations are done by simply modifying a config dict
* test: do not set cache.type for remotes that doesn't support it
* dvc: fix windows absolute paths validation in config
* test: fix windows separate drive test
* dvc: stylistic fixes in config
97 lines
2.8 KiB
Python
97 lines
2.8 KiB
Python
import argparse
|
|
import logging
|
|
|
|
from dvc.command.base import append_doc_link, CmdBaseNoRepo
|
|
from dvc.config import Config, ConfigError
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class CmdConfig(CmdBaseNoRepo):
|
|
def __init__(self, args):
|
|
super().__init__(args)
|
|
|
|
self.config = Config(validate=False)
|
|
|
|
def run(self):
|
|
section, opt = self.args.name.lower().strip().split(".", 1)
|
|
|
|
if self.args.value is None and not self.args.unset:
|
|
conf = self.config.load_one(self.args.level)
|
|
self._check(conf, section, opt)
|
|
logger.info(conf[section][opt])
|
|
return 0
|
|
|
|
with self.config.edit(self.args.level) as conf:
|
|
if self.args.unset:
|
|
self._check(conf, section, opt)
|
|
del conf[section][opt]
|
|
else:
|
|
self._check(conf, section)
|
|
conf[section][opt] = self.args.value
|
|
|
|
if self.args.name == "cache.type":
|
|
logger.warning(
|
|
"You have changed the 'cache.type' option. This doesn't update"
|
|
" any existing workspace file links, but it can be done with:"
|
|
"\n dvc checkout --relink"
|
|
)
|
|
|
|
return 0
|
|
|
|
def _check(self, conf, section, opt=None):
|
|
if section not in conf:
|
|
msg = "section {} doesn't exist"
|
|
raise ConfigError(msg.format(self.args.name))
|
|
|
|
if opt and opt not in conf[section]:
|
|
msg = "option {} doesn't exist"
|
|
raise ConfigError(msg.format(self.args.name))
|
|
|
|
|
|
parent_config_parser = argparse.ArgumentParser(add_help=False)
|
|
parent_config_parser.add_argument(
|
|
"--global",
|
|
dest="level",
|
|
action="store_const",
|
|
const="global",
|
|
help="Use global config.",
|
|
)
|
|
parent_config_parser.add_argument(
|
|
"--system",
|
|
dest="level",
|
|
action="store_const",
|
|
const="system",
|
|
help="Use system config.",
|
|
)
|
|
parent_config_parser.add_argument(
|
|
"--local",
|
|
dest="level",
|
|
action="store_const",
|
|
const="local",
|
|
help="Use local config.",
|
|
)
|
|
parent_config_parser.set_defaults(level="repo")
|
|
|
|
|
|
def add_parser(subparsers, parent_parser):
|
|
CONFIG_HELP = "Get or set config options."
|
|
|
|
config_parser = subparsers.add_parser(
|
|
"config",
|
|
parents=[parent_config_parser, parent_parser],
|
|
description=append_doc_link(CONFIG_HELP, "config"),
|
|
help=CONFIG_HELP,
|
|
formatter_class=argparse.RawDescriptionHelpFormatter,
|
|
)
|
|
config_parser.add_argument(
|
|
"-u",
|
|
"--unset",
|
|
default=False,
|
|
action="store_true",
|
|
help="Unset option.",
|
|
)
|
|
config_parser.add_argument("name", help="Option name.")
|
|
config_parser.add_argument("value", nargs="?", help="Option value.")
|
|
config_parser.set_defaults(func=CmdConfig)
|