Coverage for gws-app/gws/config/util.py: 86%
80 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-05 13:35 +0200
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-05 13:35 +0200
1"""Helpers for ``configure`` methods: create common children and find providers."""
3from typing import Optional, cast
5import gws
6import gws.gis.source
9def configure_templates_for(obj: gws.Node, extra: Optional[list] = None) -> bool:
10 """Create the templates of an object.
12 Templates are created from the ``templates`` config and from ``extra``,
13 using the object's ``create_template`` method if it has one.
14 The result is stored in ``obj.templates``.
16 Args:
17 obj: Object to configure.
18 extra: Additional template configs, appended after the configured ones.
20 Returns:
21 ``True`` if at least one template was created.
22 """
23 fn = _create_fn(obj, 'create_template', gws.ext.object.template)
24 obj.templates = []
26 p = obj.cfg('templates')
27 if p:
28 obj.templates.extend(gws.u.compact(fn(c) for c in p))
30 if extra:
31 obj.templates.extend(gws.u.compact(fn(c) for c in extra))
33 return len(obj.templates) > 0
36def configure_models_for(obj: gws.Node, with_default=False) -> bool:
37 """Create the models of an object.
39 Models are created from the ``models`` config, using the object's
40 ``create_model`` method if it has one. The result is stored in ``obj.models``.
42 Args:
43 obj: Object to configure.
44 with_default: If no models are configured, create one model with an empty config.
46 Returns:
47 ``True`` if models are configured or a default model was created.
48 """
49 fn = _create_fn(obj, 'create_model', gws.ext.object.model)
50 obj.models = []
52 p = obj.cfg('models')
53 if p:
54 obj.models = gws.u.compact(fn(c) for c in p)
55 return True
57 if with_default:
58 obj.models = [fn(None)]
59 return True
61 return False
64def configure_finders_for(obj: gws.Node, with_default=False) -> bool:
65 """Create the finders of an object.
67 Finders are created from the ``finders`` config, using the object's
68 ``create_finder`` method if it has one. The result is stored in ``obj.finders``.
70 Args:
71 obj: Object to configure.
72 with_default: If no finders are configured, create one finder with an empty config.
74 Returns:
75 ``True`` if finders are configured or a default finder was created.
76 """
77 fn = _create_fn(obj, 'create_finder', gws.ext.object.finder)
78 obj.finders = []
80 p = obj.cfg('finders')
81 if p:
82 obj.finders = gws.u.compact(fn(c) for c in p)
83 return True
85 if with_default:
86 obj.finders = [fn(None)]
87 return True
89 return False
92def _create_fn(obj, name: str, cls: type):
93 """Return the object's create method with the given name, or a function that creates a child of ``cls``."""
94 fn = getattr(obj, name, None)
95 if fn:
96 return fn
97 return lambda c: obj.create_child(cls, c)
100def configure_source_layers_for(
101 obj: gws.Node,
102 layers: list[gws.SourceLayer],
103 is_group: bool = None,
104 is_image: bool = None,
105 is_queryable: bool = None,
106 is_visible: bool = None,
107) -> bool:
108 """Select the source layers of an object.
110 If ``sourceLayers`` is configured, it is used as a filter on ``layers``.
111 Otherwise, the internal ``_defaultSourceLayers`` config is used as is, if set.
112 Otherwise, ``layers`` are filtered by the given flags.
113 The result is stored in ``obj.sourceLayers``.
115 Args:
116 obj: Object to configure.
117 layers: Source layers to select from.
118 is_group: Filter by the group flag.
119 is_image: Filter by the image flag.
120 is_queryable: Filter by the queryable flag.
121 is_visible: Filter by the visible flag.
123 Returns:
124 Always ``True``.
125 """
126 p = obj.cfg('sourceLayers')
127 if p:
128 obj.sourceLayers = gws.gis.source.filter_layers(layers, p)
129 return True
131 p = obj.cfg('_defaultSourceLayers')
132 if p:
133 obj.sourceLayers = p
134 return True
136 obj.sourceLayers = gws.gis.source.filter_layers(
137 layers,
138 is_group=is_group,
139 is_image=is_image,
140 is_queryable=is_queryable,
141 is_visible=is_visible,
142 )
143 return True
146def configure_provider_for(obj: gws.Node, cls: type) -> bool:
147 """Set the provider of an object.
149 If ``provider`` is configured, a shared provider object is created from it.
150 Otherwise, the internal ``_defaultProvider`` config is used, if it is an instance of ``cls``.
151 The result is stored in ``obj.provider``.
153 Args:
154 obj: Object to configure.
155 cls: Provider class.
157 Returns:
158 ``True`` if a provider was set.
160 Raises:
161 ``gws.Error``: If no provider is found.
162 """
163 p = obj.cfg('provider')
164 if p:
165 obj.provider = obj.root.create_shared(cls, p)
166 return True
168 p = obj.cfg('_defaultProvider')
169 if p and isinstance(p, cls):
170 obj.provider = p
171 return True
173 raise gws.Error(f'no provider {cls!r} found for {obj!r}')
176def configure_database_provider_for(obj: gws.Node, ext_type: Optional[str] = None) -> bool:
177 """Set the database provider of an object.
179 The provider is looked up by the ``dbUid`` config, then taken from the
180 internal ``_defaultDb`` config, then found by the extension type.
181 The result is stored in ``obj.db``.
183 Args:
184 obj: Object to configure.
185 ext_type: Extension type of the provider, e.g. ``postgres``. Defaults to the object's ``extType``.
187 Returns:
188 ``True`` if a provider was set.
190 Raises:
191 ``gws.Error``: If ``dbUid`` is configured but not found, or if no provider is found.
192 """
193 mgr = obj.root.app.databaseMgr
195 uid = obj.cfg('dbUid')
196 if uid:
197 p = mgr.find_provider(uid=uid)
198 if p:
199 obj.db = p
200 return True
201 raise gws.Error(f'database provider {uid!r} not found')
203 p = obj.cfg('_defaultDb')
204 if p:
205 obj.db = p
206 return True
208 ext_type = ext_type or obj.extType
209 if ext_type:
210 p = mgr.find_provider(ext_type=ext_type)
211 if p:
212 obj.db = p
213 return True
215 raise gws.Error(f'no database providers of type {ext_type!r} configured')