@@ -101,9 +101,10 @@ def is_valid_url(url: str) -> bool:
101
101
102
102
103
103
class Module :
104
- def __init__ (self , namespace : str ) -> None :
104
+ def __init__ (self , namespace : str , is_plugin : bool ) -> None :
105
105
self .namespace : Any = namespace
106
106
self ._apis : Any = []
107
+ self .is_plugin : bool = is_plugin
107
108
self .parse_orig ()
108
109
109
110
def add (self , api : Any ) -> None :
@@ -118,10 +119,17 @@ def parse_orig(self) -> None:
118
119
reads the written module and updates with important code specific to this client
119
120
"""
120
121
self .orders = []
121
- self .header = "from typing import Any, Collection, Optional, Tuple, Union\n \n "
122
+ if self .is_plugin :
123
+ self .header = "from typing import Any\n \n "
124
+ else :
125
+ self .header = (
126
+ "from typing import Any, Collection, Optional, Tuple, Union\n \n "
127
+ )
122
128
123
- namespace_new = "" .join (word .capitalize () for word in self .namespace .split ("_" ))
124
- self .header += "class " + namespace_new + "Client(NamespacedClient):"
129
+ self .namespace_new = "" .join (
130
+ word .capitalize () for word in self .namespace .split ("_" )
131
+ )
132
+ self .header += "class " + self .namespace_new + "Client(NamespacedClient):"
125
133
if os .path .exists (self .filepath ):
126
134
with open (self .filepath , encoding = "utf-8" ) as file :
127
135
content = file .read ()
@@ -164,7 +172,45 @@ def dump(self) -> None:
164
172
writes the module out to disk
165
173
"""
166
174
self .sort ()
175
+ if not os .path .exists (self .filepath ):
176
+ # Imports added for new namespaces in appropriate files.
177
+ if self .is_plugin :
178
+ with open (
179
+ "opensearchpy/_async/client/plugins.py" , "r+" , encoding = "utf-8"
180
+ ) as file :
181
+ content = file .read ()
182
+ file_content = content .replace (
183
+ "super(PluginsClient, self).__init__(client)" ,
184
+ f"super(PluginsClient, self).__init__(client)\n self.{ self .namespace } = { self .namespace_new } Client(client)" , # pylint: disable=line-too-long
185
+ 1 ,
186
+ )
187
+ new_file_content = file_content .replace (
188
+ "from .client import Client" ,
189
+ f"from ..plugins.{ self .namespace } import { self .namespace_new } Client\n from .client import Client" , # pylint: disable=line-too-long
190
+ 1 ,
191
+ )
192
+ file .seek (0 )
193
+ file .write (new_file_content )
194
+ file .truncate ()
167
195
196
+ else :
197
+ with open (
198
+ "opensearchpy/_async/client/__init__.py" , "r+" , encoding = "utf-8"
199
+ ) as file :
200
+ content = file .read ()
201
+ file_content = content .replace (
202
+ "# namespaced clients for compatibility with API names" ,
203
+ f"# namespaced clients for compatibility with API names\n self.{ self .namespace } = { self .namespace_new } Client(client)" , # pylint: disable=line-too-long
204
+ 1 ,
205
+ )
206
+ new_file_content = file_content .replace (
207
+ "from .utils import" ,
208
+ f"from .{ self .namespace } import { self .namespace_new } Client\n from .utils import" , # pylint: disable=line-too-long
209
+ 1 ,
210
+ )
211
+ file .seek (0 )
212
+ file .write (new_file_content )
213
+ file .truncate ()
168
214
# This code snippet adds headers to each generated module indicating
169
215
# that the code is generated.The separator is the last line in the
170
216
# "THIS CODE IS AUTOMATICALLY GENERATED" header.
@@ -209,8 +255,14 @@ def dump(self) -> None:
209
255
210
256
# Imports are temporarily removed from the header and are regenerated
211
257
# later to ensure imports are updated after code generation.
258
+ utils = ".utils"
259
+ if self .is_plugin :
260
+ utils = "..client.utils"
261
+
212
262
self .header = "\n " .join (
213
- line for line in self .header .split ("\n " ) if "from .utils import" not in line
263
+ line
264
+ for line in self .header .split ("\n " )
265
+ if "from " + utils + " import" not in line
214
266
)
215
267
216
268
with open (self .filepath , "w" , encoding = "utf-8" ) as file :
@@ -252,7 +304,7 @@ def dump(self) -> None:
252
304
present_keywords = [keyword for keyword in keywords if keyword in content ]
253
305
254
306
if present_keywords :
255
- utils_imports = "from . utils import"
307
+ utils_imports = "from " + utils + " import"
256
308
result = f"{ utils_imports } { ', ' .join (present_keywords )} "
257
309
utils_imports = result
258
310
file_content = content .replace ("#replace_token#" , utils_imports )
@@ -265,7 +317,10 @@ def filepath(self) -> Any:
265
317
"""
266
318
:return: absolute path to the module
267
319
"""
268
- return CODE_ROOT / f"opensearchpy/_async/client/{ self .namespace } .py"
320
+ if self .is_plugin :
321
+ return CODE_ROOT / f"opensearchpy/_async/plugins/{ self .namespace } .py"
322
+ else :
323
+ return CODE_ROOT / f"opensearchpy/_async/client/{ self .namespace } .py"
269
324
270
325
271
326
class API :
@@ -704,8 +759,12 @@ def read_modules() -> Any:
704
759
705
760
api = apply_patch (namespace , name , api )
706
761
762
+ is_plugin = False
763
+ if "_plugins" in api ["url" ]["paths" ][0 ]["path" ] and namespace != "security" :
764
+ is_plugin = True
765
+
707
766
if namespace not in modules :
708
- modules [namespace ] = Module (namespace )
767
+ modules [namespace ] = Module (namespace , is_plugin )
709
768
710
769
modules [namespace ].add (API (namespace , name , api ))
711
770
@@ -752,13 +811,20 @@ def dump_modules(modules: Any) -> None:
752
811
todir = "/opensearchpy/client/" ,
753
812
additional_replacements = additional_replacements ,
754
813
),
814
+ unasync .Rule (
815
+ fromdir = "/opensearchpy/_async/plugins/" ,
816
+ todir = "/opensearchpy/plugins/" ,
817
+ additional_replacements = additional_replacements ,
818
+ ),
755
819
]
756
820
757
821
filepaths = []
758
822
for root , _ , filenames in os .walk (CODE_ROOT / "opensearchpy/_async" ):
759
823
for filename in filenames :
760
- if filename .rpartition ("." )[- 1 ] in ("py" ,) and not filename .startswith (
761
- "utils.py"
824
+ if filename .rpartition ("." )[- 1 ] in ("py" ,) and filename not in (
825
+ "utils.py" ,
826
+ "index_management.py" ,
827
+ "alerting.py" ,
762
828
):
763
829
filepaths .append (os .path .join (root , filename ))
764
830
0 commit comments