1111logger = logging .getLogger (__name__ )
1212
1313
14- def _get_data_sources (session : Session ) -> list [DataSource ]:
14+ def _get_data_sources (
15+ session : Session ,
16+ project : str | None ,
17+ ) -> list [DataSource ]:
1518 """Get the list of available data sources.
1619
1720 Arguments
1821 ---------
1922 session:
2023 The configuration.
24+ project:
25+ If specified, only data sources for this project are returned.
2126
2227 Returns
2328 -------
2429 :obj:`list` of :obj:`DataSource`:
2530 A list of available data sources.
2631
32+ Raises
33+ ------
34+ TypeError:
35+ If a data source in the configuration is not of type `DataSource`.
36+ KeyError:
37+ If the project or its settings are not found in the configuration.
38+
2739 """
2840 data_sources : list [DataSource ] = []
29- for project , project_settings in session ["projects" ].items ():
41+ if project is not None and project not in session ["projects" ]:
42+ msg = f"Unknown project '{ project } ', please configure it under 'projects'."
43+ raise KeyError (msg )
44+ settings = (
45+ session ["projects" ]
46+ if project is None
47+ else {project : session ["projects" ][project ]}
48+ )
49+ for project_ , project_settings in settings .items ():
3050 if "data" not in project_settings :
31- logger .info ("Using legacy data sources for project '%s'" , project )
51+ logger .info ("Using legacy data sources for project '%s'" , project_ )
3252 # Use legacy data sources from config-user.yml.
33- legacy_local_sources = esmvalcore .local ._get_data_sources (project ) # noqa: SLF001
53+ legacy_local_sources = esmvalcore .local ._get_data_sources (project_ ) # noqa: SLF001
3454 data_sources .extend (legacy_local_sources )
3555 if (
3656 session ["search_esgf" ] != "never"
37- and project in esmvalcore .esgf .facets .FACETS
57+ and project_ in esmvalcore .esgf .facets .FACETS
3858 ):
3959 data_source = esmvalcore .esgf .ESGFDataSource (
40- name = "legacy" ,
41- project = project ,
60+ name = "legacy-esgf " ,
61+ project = project_ ,
4262 priority = 2 ,
4363 download_dir = session ["download_dir" ],
4464 )
@@ -56,16 +76,26 @@ def _get_data_sources(session: Session) -> list[DataSource]:
5676 priority = kwargs .pop ("priority" , 1 )
5777 data_source = cls (
5878 name = name ,
59- project = project ,
79+ project = project_ ,
6080 priority = priority ,
6181 ** kwargs ,
6282 )
6383 if not isinstance (data_source , DataSource ):
6484 msg = (
6585 "Expected a data source of type `esmvalcore.io.protocol.DataSource`, "
66- f"but your configuration for project '{ project } ' contains "
86+ f"but your configuration for project '{ project_ } ' contains "
6787 f"'{ data_source } ' of type '{ type (data_source )} '."
6888 )
6989 raise TypeError (msg )
7090 data_sources .append (data_source )
91+
92+ if not data_sources :
93+ if project is None :
94+ msg = "No data sources found. Check your configuration under 'projects'"
95+ else :
96+ msg = (
97+ f"No data sources found for project '{ project } '. "
98+ f"Check your configuration under 'projects: { project } : data'"
99+ )
100+ raise KeyError (msg )
71101 return data_sources
0 commit comments