@@ -129,14 +129,9 @@ def plugin_names(self) -> list[str]:
129129 return list (chain .from_iterable (self ._plugin_names .values ()))
130130
131131 def _reload_plugins (self ):
132- self .plugin_extra_files : list [ str ] = self .plugin .get ("$files" , []) # type: ignore
132+ self .plugin_extra_files = self .plugin .get ("$files" , []) # type: ignore
133133 self .prelude_plugin = self .plugin .get ("$prelude" , []) # type: ignore
134- plugin_prefixes = {item ["key" ]: item ["plugins" ] for item in self .plugin .get ("$prefix" , [])} # type: ignore
135- inverted = defaultdict (list )
136- for key , plugs in plugin_prefixes .items ():
137- for plug in plugs :
138- inverted [plug ].append (key )
139- self .plugin_prefixes = dict (inverted )
134+
140135 for key in list (self .plugin .keys ()):
141136 if key .startswith ("$" ):
142137 continue
@@ -149,17 +144,36 @@ def _reload_plugins(self):
149144 key = key [1 :]
150145 value ["$optional" ] = True
151146 self .plugin [key ] = value
147+ extra_plugins : dict [str , list [str ]] = {}
152148 for file in self .plugin_extra_files :
153149 path = Path (file )
154150 if not path .exists ():
155151 raise FileNotFoundError (file )
152+ _plugins = extra_plugins .setdefault (file , [])
156153 if path .is_dir ():
157154 for _path in path .iterdir ():
158155 if not _path .is_file () or _path .name .endswith (".schema.json" ):
159156 continue
160157 self .plugin [_path .stem ] = self .loader (_path )
158+ _plugins .append (_path .stem )
161159 elif path .name .endswith (".schema.json" ):
162160 self .plugin [path .stem ] = self .loader (path )
161+ _plugins .append (path .stem )
162+
163+ plugin_prefixes = {}
164+ for item in self .plugin .get ("$prefix" , []): # type: ignore
165+ if "key" not in item or "plugins" not in item :
166+ continue
167+ _prefixes = plugin_prefixes .setdefault (item ["key" ], [])
168+ if isinstance (item ["plugins" ], list ):
169+ _prefixes .extend (item ["plugins" ])
170+ elif isinstance (item ["plugins" ], str ) and item ["plugins" ] in extra_plugins :
171+ _prefixes .extend (extra_plugins [item ["plugins" ]])
172+ inverted = defaultdict (list )
173+ for key , plugs in plugin_prefixes .items ():
174+ for plug in plugs :
175+ inverted [plug ].append (key )
176+ self .plugin_prefixes = dict (inverted )
163177
164178 slots = [
165179 (name , self .plugin [name ].get ("$priority" , 16 ))
@@ -302,7 +316,7 @@ def generate_schema(self, plugins: list["Plugin"]):
302316 else :
303317 plugins_properties [plug ._config_key ] = {"type" : "object" , "description" : "No configuration required" , "additionalProperties" : True , "properties" : plugin_meta_properties } # noqa: E501
304318 schemas = {
305- "basic" : config_model_schema (BasicConfig , ref_root = "/properties/basic/" ), "plugins" : {"type" : "object" , "description" : "Plugin configurations" , "properties" : {"$prefix" : {"description" : "List of prefix config" , "items" : {"properties" : {"key" : {"description" : "Prefix key" , "title" : "Key" , "type" : "string" }, "plugins" : {"description" : "List of plugins under the prefix" , "items" : {"type" : "string" , "description" : "Plugin name" }, "title" : "Plugins" , "type" : "array" , "uniqueItems" : True }}, "required" : ["key" ], "title" : "Prefix Config" , "type" : "object" }, "type" : "array" }, "$prelude" : {"type" : "array" , "items" : {"type" : "string" , "description" : "Plugin name" }, "description" : "List of prelude plugins to load" , "default" : [], "uniqueItems" : True }, "$files" : {"type" : "array" , "items" : {"type" : "string" , "description" : "File path" }, "description" : "List of configuration files to load" , "default" : [], "uniqueItems" : True }, ** plugins_properties }}, "adapters" : {"type" : "array" , "description" : "Adapter configurations" , "items" : {"type" : "object" , "description" : "Adapter configuration" , "properties" : {"$path" : {"type" : "string" , "description" : "Adapter Module Path" }}, "required" : ["$path" ], "additionalProperties" : True }} # noqa: E501
319+ "basic" : config_model_schema (BasicConfig , ref_root = "/properties/basic/" ), "plugins" : {"type" : "object" , "description" : "Plugin configurations" , "properties" : {"$prefix" : {"description" : "List of prefix config" , "items" : {"properties" : {"key" : {"description" : "Prefix key" , "title" : "Key" , "type" : "string" }, "plugins" : {"anyOf" : [{"type" : "string" }, {"items" : {"type" : "string" , "description" : "Plugin name" }, "type" : "array" , "uniqueItems" : True }], "description" : "List of plugins under the prefix, or select an item of $files to apply plugins" , "title" : "Plugins" }}, "required" : ["key" ], "title" : "Prefix Config" , "type" : "object" }, "type" : "array" }, "$prelude" : {"type" : "array" , "items" : {"type" : "string" , "description" : "Plugin name" }, "description" : "List of prelude plugins to load" , "default" : [], "uniqueItems" : True }, "$files" : {"type" : "array" , "items" : {"type" : "string" , "description" : "File path" }, "description" : "List of configuration files to load" , "default" : [], "uniqueItems" : True }, ** plugins_properties }}, "adapters" : {"type" : "array" , "description" : "Adapter configurations" , "items" : {"type" : "object" , "description" : "Adapter configuration" , "properties" : {"$path" : {"type" : "string" , "description" : "Adapter Module Path" }}, "required" : ["$path" ], "additionalProperties" : True }} # noqa: E501
306320 }
307321 with open (f"{ self .path .stem } .schema.json" , "w" , encoding = "utf-8" ) as f :
308322 json .dump ({"$schema" : "https://json-schema.org/draft/2020-12/schema" , "type" : "object" , "properties" : schemas , "additionalProperties" : False , "required" : ["basic" ]}, f , indent = 2 , ensure_ascii = False ) # noqa: E501
0 commit comments