Fixed double-writes. (#874)

* Fixed file-overwrites on generation.

Signed-off-by: Alex Loyko <alex.loyko96@gmail.com>

* Fixed CHANGELOG

Signed-off-by: Alex Loyko <alex.loyko96@gmail.com>

* fixing test and lint

Signed-off-by: Alex Loyko <alex.loyko96@gmail.com>

* Addressed comments.

Signed-off-by: Alex Loyko <alex.loyko96@gmail.com>

---------

Signed-off-by: Alex Loyko <alex.loyko96@gmail.com>
This commit is contained in:
Oleksandr Loyko
2024-12-20 14:06:06 -05:00
committed by GitHub
parent 7815c6abe8
commit 9777ebe4f8
3 changed files with 43 additions and 44 deletions
+1
View File
@@ -8,6 +8,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
### Updated APIs ### Updated APIs
### Changed ### Changed
- Small refactor of AWS Signer classes for both sync and async clients ([866](https://github.com/opensearch-project/opensearch-py/pull/866)) - Small refactor of AWS Signer classes for both sync and async clients ([866](https://github.com/opensearch-project/opensearch-py/pull/866))
- Small refactor to fix overwriting the module files when generating apis ([874](https://github.com/opensearch-project/opensearch-py/pull/874))
### Deprecated ### Deprecated
### Removed ### Removed
### Fixed ### Fixed
@@ -233,10 +233,8 @@ class TestAIOHttpConnection:
assert w == [] assert w == []
else: else:
assert len(w) == 1 assert len(w) == 1
assert ( assert "https://github.com/python/cpython/pull/118960" in str(
str(w[0].message) == "enable_cleanup_closed ignored because " w[0].message
"https://github.com/python/cpython/pull/118960 is fixed in "
"Python version sys.version_info(major=3, minor=12, micro=7, releaselevel='final', serial=0)"
) )
assert isinstance(con.session, aiohttp.ClientSession) assert isinstance(con.session, aiohttp.ClientSession)
+10 -10
View File
@@ -272,9 +272,9 @@ class Module:
if "from " + utils + " import" not in line if "from " + utils + " import" not in line
) )
with open(self.filepath, "w", encoding="utf-8") as file: module_content = ""
if update_header is True: if update_header is True:
file.write( module_content += (
self.header[:license_position] self.header[:license_position]
+ "\n" + "\n"
+ header_content + header_content
@@ -283,20 +283,18 @@ class Module:
+ self.header[license_position:] + self.header[license_position:]
) )
else: else:
file.write( module_content += (
self.header[:header_position] self.header[:header_position]
+ "\n" + "\n"
+ "#replace_token#\n" + "#replace_token#\n"
+ self.header[header_position:] + self.header[header_position:]
) )
for api in self._apis: for api in self._apis:
file.write(api.to_python()) module_content += api.to_python()
# Generating imports for each module # Generating imports for each module
utils_imports = "" utils_imports = ""
file_content = ""
with open(self.filepath, encoding="utf-8") as file:
content = file.read()
keywords = [ keywords = [
"SKIP_IN_PATH", "SKIP_IN_PATH",
"_normalize_hosts", "_normalize_hosts",
@@ -308,16 +306,18 @@ class Module:
"NamespacedClient", "NamespacedClient",
"AddonClient", "AddonClient",
] ]
present_keywords = [keyword for keyword in keywords if keyword in content] present_keywords = [
keyword for keyword in keywords if keyword in module_content
]
if present_keywords: if present_keywords:
utils_imports = "from " + utils + " import" utils_imports = "from " + utils + " import"
result = f"{utils_imports} {', '.join(present_keywords)}" result = f"{utils_imports} {', '.join(present_keywords)}"
utils_imports = result utils_imports = result
file_content = content.replace("#replace_token#", utils_imports) module_content = module_content.replace("#replace_token#", utils_imports)
with open(self.filepath, "w", encoding="utf-8") as file: with open(self.filepath, "w", encoding="utf-8") as file:
file.write(file_content) file.write(module_content)
@property @property
def filepath(self) -> Any: def filepath(self) -> Any: