diff --git a/src/west/app/config.py b/src/west/app/config.py index d6c848a3..2740f867 100644 --- a/src/west/app/config.py +++ b/src/west/app/config.py @@ -5,11 +5,9 @@ '''West config commands''' import argparse -import configparser from west import log -from west.configuration import read_config, update_config, delete_config, \ - ConfigFile +from west.configuration import ConfigFile from west.commands import WestCommand, CommandError CONFIG_DESCRIPTION = '''\ @@ -154,35 +152,33 @@ def do_run(self, args, user_args): self.write(args) def list(self, args): - cfg = configparser.ConfigParser() what = args.configfile or ALL - read_config(configfile=what, config=cfg) - for s in cfg.sections(): - for k, v in cfg[s].items(): - log.inf(f'{s}.{k}={v}') + for option, value in self.config.items(configfile=what): + log.inf(f'{option}={value}') def delete(self, args): - section, key = self._sk(args) if args.delete_all: - what = ALL + configfiles = [ALL] elif args.configfile: - what = args.configfile + configfiles = [args.configfile] else: - what = None # local or global, whichever comes first - - try: - delete_config(section, key, configfile=what) - except KeyError: - log.dbg(f'{args.name} was not set in requested location(s)') - raise CommandError(returncode=1) - except PermissionError as pe: - self._perm_error(pe, what, section, key) + # local or global, whichever comes first + configfiles = [LOCAL, GLOBAL] + + for i, configfile in enumerate(configfiles): + try: + self.config.delete(args.name, configfile=configfile) + return + except KeyError: + if i == len(configfiles) - 1: + log.dbg( + f'{args.name} was not set in requested location(s)') + raise CommandError(returncode=1) + except PermissionError as pe: + self._perm_error(pe, configfile, args.name) def read(self, args): - section, key = self._sk(args) - cfg = configparser.ConfigParser() - read_config(configfile=args.configfile or ALL, config=cfg) - value = cfg.get(section, key, fallback=None) + value = self.config.get(args.name, configfile=args.configfile or ALL) if value is not None: log.inf(value) else: @@ -190,22 +186,14 @@ def read(self, args): raise CommandError(returncode=1) def write(self, args): - section, key = self._sk(args) what = args.configfile or LOCAL try: - update_config(section, key, args.value, configfile=what) + self.config.set(args.name, args.value, configfile=what) except PermissionError as pe: - self._perm_error(pe, what, section, key) - - def _sk(self, args): - name_list = args.name.split(".", 1) - if len(name_list) != 2: - self.parser.error(f"name '{args.name}' should be in the form " - "
.") - return name_list[0], name_list[1] + self._perm_error(pe, what, args.name) - def _perm_error(self, pe, what, section, key): + def _perm_error(self, pe, what, name): rootp = ('; are you root/administrator?' if what in [SYSTEM, ALL] else '') - log.die(f"can't update {section}.{key}: " + log.die(f"can't update {name}: " f"permission denied when writing {pe.filename}{rootp}") diff --git a/src/west/app/project.py b/src/west/app/project.py index 32c301b0..bc0a5812 100644 --- a/src/west/app/project.py +++ b/src/west/app/project.py @@ -19,7 +19,7 @@ from time import perf_counter from urllib.parse import urlparse -from west.configuration import config, update_config +from west.configuration import Configuration from west import log from west import util from west.commands import WestCommand, CommandError @@ -227,8 +227,9 @@ def local(self, args) -> Path: log.small_banner(f'Creating {west_dir} and local configuration file') self.create(west_dir) os.chdir(topdir) - update_config('manifest', 'path', os.fspath(rel_manifest)) - update_config('manifest', 'file', manifest_filename, topdir=topdir) + self.config = Configuration(topdir=topdir) + self.config.set('manifest.path', os.fspath(rel_manifest)) + self.config.set('manifest.file', manifest_filename) return topdir @@ -306,9 +307,9 @@ def bootstrap(self, args) -> Path: except shutil.Error as e: log.die(e) log.small_banner('setting manifest.path to', manifest_path) - update_config('manifest', 'path', manifest_path, topdir=topdir) - update_config('manifest', 'file', temp_manifest_filename, - topdir=topdir) + self.config = Configuration(topdir=topdir) + self.config.set('manifest.path', manifest_path) + self.config.set('manifest.file', temp_manifest_filename) return topdir @@ -425,7 +426,7 @@ def delay(func, project): # Special-case the manifest repository while it's # still showing up in the 'projects' list. Yet # more evidence we should tackle #327. - path = config.get('manifest', 'path') + path = self.config.get('manifest.path') apath = abspath(os.path.join(self.topdir, path)) ppath = Path(apath).as_posix() else: @@ -811,14 +812,12 @@ def init_state(self, args): if args.exclude_west: log.wrn('ignoring --exclude-west') - self.narrow = args.narrow or config.getboolean('update', 'narrow', - fallback=False) - self.path_cache = args.path_cache or config.get('update', 'path-cache', - fallback=None) - self.name_cache = args.name_cache or config.get('update', 'name-cache', - fallback=None) - self.sync_submodules = config.getboolean('update', 'sync-submodules', - fallback=True) + config = self.config + self.narrow = args.narrow or config.getboolean('update.narrow') + self.path_cache = args.path_cache or config.get('update.path-cache') + self.name_cache = args.name_cache or config.get('update.name-cache') + self.sync_submodules = config.getboolean('update.sync-submodules', + default=True) self.group_filter: List[str] = [] @@ -975,7 +974,7 @@ def toplevel_projects(self): ' Only plain "west update" can currently update them.') def fetch_strategy(self): - cfg = config.get('update', 'fetch', fallback=None) + cfg = self.config.get('update.fetch') if cfg is not None and cfg not in ('always', 'smart'): log.wrn(f'ignoring invalid config update.fetch={cfg}; ' 'choices: always, smart')