Skip to content

Commit bc3e536

Browse files
koubaaMohamed Koubaapyansys-ci-bot
authored
feat: Import encrypted file (#712)
Co-authored-by: Mohamed Koubaa <[email protected]> Co-authored-by: pyansys-ci-bot <[email protected]>
1 parent a9eb3a6 commit bc3e536

File tree

7 files changed

+531
-119
lines changed

7 files changed

+531
-119
lines changed

doc/changelog/712.miscellaneous.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
feat: Import encrypted file

src/ansys/dyna/core/lib/deck.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from typing import Union
2828
import warnings
2929

30+
from ansys.dyna.core.lib.encrypted_keyword import EncryptedKeyword
3031
from ansys.dyna.core.lib.format_type import format_type
3132
from ansys.dyna.core.lib.import_handler import ImportContext, ImportHandler
3233
from ansys.dyna.core.lib.io_utils import write_or_return
@@ -89,15 +90,15 @@ def append(self, keyword: Union[KeywordBase, str], check=False) -> None:
8990
9091
Parameters
9192
----------
92-
keyword : Union[KeywordBase, str]
93-
Keyword. The keyword can be either an implementation of the ``KeywordBase``
94-
instance or a string.
93+
keyword : Union[KeywordBase, EncryptedKeyword, str]
94+
Keyword. The keyword can be ``KeywordBase``, ``EncryptedKeyword``,
95+
or a string.
9596
check : bool, optional
9697
The default is ``False``.
9798
"""
98-
assert isinstance(keyword, KeywordBase) or isinstance(
99-
keyword, str
100-
), "Keywords or strings can only be appended to the deck."
99+
assert (
100+
isinstance(keyword, KeywordBase) or isinstance(keyword, str) or isinstance(keyword, EncryptedKeyword)
101+
), "Only keywords, encrypted keywords, or strings can be included in a deck."
101102
if isinstance(keyword, str):
102103
self._keywords.append(self._formatstring(keyword, check))
103104
else:
@@ -143,7 +144,7 @@ def _formatstring(self, string, check=False):
143144
return string
144145

145146
@property
146-
def all_keywords(self) -> typing.List[typing.Union[str, KeywordBase]]:
147+
def all_keywords(self) -> typing.List[typing.Union[str, KeywordBase, EncryptedKeyword]]:
147148
"""List of all keywords."""
148149
return self._keywords
149150

@@ -152,6 +153,11 @@ def string_keywords(self) -> typing.List[str]:
152153
"""List of keywords as a raw string."""
153154
return [kw for kw in self._keywords if isinstance(kw, str)]
154155

156+
@property
157+
def encrypted_keywords(self) -> typing.List[str]:
158+
"""List of keywords as a raw string."""
159+
return [kw for kw in self._keywords if isinstance(kw, EncryptedKeyword)]
160+
155161
@property
156162
def keywords(self):
157163
"""List of processed keywords."""
@@ -172,7 +178,8 @@ def _expand_helper(self, search_paths: typing.List[str], recurse: bool) -> typin
172178
"""Recursively outputs a list of keywords within Includes."""
173179
keywords = []
174180
for keyword in self.all_keywords:
175-
if isinstance(keyword, str):
181+
if not isinstance(keyword, KeywordBase):
182+
print(keyword)
176183
keywords.append(keyword)
177184
continue
178185
if keyword.keyword != "INCLUDE":
@@ -269,12 +276,18 @@ def dumps(self) -> str:
269276
warnings.warn("The 'dumps()' method is deprecated. Use the 'write()' method instead.")
270277
return self.write()
271278

272-
def _write_keyword(self, buf: typing.TextIO, kwd: typing.Union[str, KeywordBase], format: format_type) -> None:
279+
def _write_keyword(
280+
self, buf: typing.TextIO, kwd: typing.Union[str, KeywordBase, EncryptedKeyword], format: format_type
281+
) -> None:
273282
"""Write a keyword to the buffer."""
274283
if isinstance(kwd, KeywordBase):
275284
kwd.write(buf, None, format)
276285
elif isinstance(kwd, str):
277286
buf.write(kwd)
287+
elif isinstance(kwd, EncryptedKeyword):
288+
buf.write("-----BEGIN PGP MESSAGE-----\n")
289+
buf.write(kwd.data)
290+
buf.write("\n-----END PGP MESSAGE-----\n")
278291

279292
def _remove_trailing_newline(self, buf: typing.TextIO) -> None:
280293
"""If the last character is a newline, seek back so that it can be overwritten.
@@ -429,8 +442,10 @@ def get(self, **kwargs) -> typing.List[KeywordBase]:
429442
return kwds
430443

431444
def _import_file(self, path: str, encoding: str, context: ImportContext):
445+
from ansys.dyna.core.lib.deck_loader import load_deck_from_buffer
446+
432447
with open(path, encoding=encoding) as f:
433-
return self.loads(f.read(), context)
448+
return load_deck_from_buffer(self, f, context, self._import_handlers)
434449

435450
def import_file(
436451
self, path: str, encoding: str = "utf-8"
@@ -494,6 +509,8 @@ def __repr__(self) -> str:
494509
content_lines.append(f"kwd: {kw.get_title()}")
495510
elif isinstance(kw, str):
496511
content_lines.append("str: " + kw.split("\n")[0] + "...")
512+
elif isinstance(kw, EncryptedKeyword):
513+
content_lines.append("encrypted keyword...")
497514

498515
output = "\n".join(content_lines)
499516
return output

0 commit comments

Comments
 (0)