Skip to content

Commit

Permalink
Cleanup protected project archive file opener (#404)
Browse files Browse the repository at this point in the history
* ---
updated-dependencies:
- dependency-name: pylint
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

* Cleanup protected project archive file opener

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: farmio <farmio@alphart.net>
  • Loading branch information
dependabot[bot] and farmio authored May 20, 2024
1 parent 9638c5c commit a4fdd7f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
2 changes: 1 addition & 1 deletion requirements_testing.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-r requirements_production.txt
pre-commit==3.7.1
pylint==3.1.1
pylint==3.2.2
pytest==8.2.1
pytest-cov==5.0.0
pytest-icdiff==0.9
Expand Down
27 changes: 16 additions & 11 deletions xknxproject/zip/extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,19 +125,22 @@ def _extract_protected_project_file(

try:
project_archive: ZipFile
pwd: bytes
# Password protection is different for ETS4/5 and ETS6
project_archive_file = archive_zip.open(info, mode="r")
if schema_version < ETS_6_SCHEMA_VERSION:
project_archive = ZipFile(archive_zip.open(info, mode="r"), mode="r")
project_archive.setpassword(password.encode("utf-8"))
yield project_archive
project_archive = ZipFile(project_archive_file, mode="r")
pwd = password.encode("utf-8")
else:
project_archive = pyzipper.AESZipFile(
archive_zip.open(info, mode="r"), mode="r"
)
project_archive.setpassword(_generate_ets6_zip_password(password))
yield project_archive
project_archive = pyzipper.AESZipFile(project_archive_file, mode="r")
pwd = _generate_ets6_zip_password(password)
project_archive.setpassword(pwd)
yield project_archive
except RuntimeError as exception:
raise InvalidPasswordException("Invalid password.") from exception
finally:
_LOGGER.debug("Closed protected project archive")
project_archive_file.close()


def _get_xml_namespace(project_zip: ZipFile) -> str:
Expand All @@ -151,14 +154,16 @@ def _get_xml_namespace(project_zip: ZipFile) -> str:
line.decode(),
)
namespace = namespace_match.group(1) # type: ignore[union-attr]
_LOGGER.debug("Namespace: %s", namespace)
return namespace
except (AttributeError, IndexError, UnicodeDecodeError):
_LOGGER.error("Could not parse XML namespace from %s", line)
raise UnexpectedFileContent(
"Could not parse XML namespace."
) from None

_LOGGER.debug("Namespace: %s", namespace)
return namespace
if line_number > 2:
break
raise UnexpectedFileContent("Could not find XML namespace.")


def _get_schema_version(namespace: str) -> int:
Expand Down

0 comments on commit a4fdd7f

Please sign in to comment.