@@ -159,6 +159,97 @@ class Config:
159159 files.
160160 """
161161
162+ def __init__ (self ) -> None :
163+ from rich .console import Console
164+
165+ self .console = Console ()
166+
167+ def show (
168+ self ,
169+ filter : tuple [str ] | None = ("extra_facets" ,), # noqa: A002
170+ ) -> None :
171+ """Show the current configuration.
172+
173+ Parameters
174+ ----------
175+ filter:
176+ Filter this list of keys. By default, the `extra_facets`
177+ key is filtered out, as it can be very large.
178+
179+ """
180+ import yaml
181+ from nested_lookup import nested_delete
182+ from rich .syntax import Syntax
183+
184+ from esmvalcore .config import CFG
185+
186+ cfg = dict (CFG )
187+ if filter :
188+ for key in filter :
189+ cfg = nested_delete (cfg , key )
190+ exclude_msg = (
191+ ", excluding the keys " + ", " .join (f"'{ f } '" for f in filter )
192+ if filter
193+ else ""
194+ )
195+ self .console .print (f"# Current configuration{ exclude_msg } :" )
196+ self .console .print (
197+ Syntax (
198+ yaml .safe_dump (cfg ),
199+ "yaml" ,
200+ background_color = "default" ,
201+ ),
202+ )
203+
204+ def list (self ) -> None :
205+ """List all available example configuration files."""
206+ import importlib .resources
207+
208+ import esmvalcore .config
209+
210+ config_dir = (
211+ importlib .resources .files (esmvalcore .config ) / "configurations"
212+ )
213+ self .console .print ("Available configuration files:" )
214+ available_files = sorted (
215+ f .name
216+ for f in config_dir .iterdir ()
217+ if f .suffix == ".yml" # type: ignore[attr-defined]
218+ )
219+ self .console .print ("\n " .join (f"- { f } " for f in available_files ))
220+
221+ def copy (
222+ self ,
223+ source_file : str ,
224+ target_file : Path | None = None ,
225+ overwrite : bool = False ,
226+ ) -> None :
227+ """Copy one of the available example configuration files to the configuration directory."""
228+ import importlib .resources
229+
230+ import esmvalcore .config
231+
232+ target_dir = esmvalcore .config ._config_object ._get_user_config_dir () # noqa: SLF001
233+ target_file = target_dir / (
234+ source_file if target_file is None else target_file
235+ )
236+ config_dir = (
237+ importlib .resources .files (esmvalcore .config ) / "configurations"
238+ )
239+ available_files = sorted (
240+ f .name
241+ for f in config_dir .iterdir ()
242+ if f .suffix == ".yml" # type: ignore[attr-defined]
243+ )
244+ if source_file not in available_files :
245+ msg = (
246+ f"Configuration file { source_file } not found, choose from "
247+ f"{ ', ' .join (available_files )} "
248+ )
249+ raise FileNotFoundError (msg )
250+ with importlib .resources .as_file (config_dir / source_file ) as file :
251+ self ._copy_config_file (file , target_file , overwrite = overwrite )
252+
162253 @staticmethod
163254 def _copy_config_file (
164255 in_file : Path ,
@@ -184,7 +275,7 @@ def _copy_config_file(
184275 logger .info ("Creating folder %s" , target_folder )
185276 target_folder .mkdir (parents = True , exist_ok = True )
186277
187- logger .info ("Copying file %s to path %s. " , in_file , out_file )
278+ logger .info ("Copying file %s to path %s" , in_file , out_file )
188279 shutil .copy2 (in_file , out_file )
189280 logger .info ("Copy finished." )
190281
0 commit comments