Skip to content

Commit dd4cfe7

Browse files
koubaaMohamed Koubaapyansys-ci-bot
authored
feat: add include name property to keyword (#768)
Co-authored-by: Mohamed Koubaa <[email protected]> Co-authored-by: pyansys-ci-bot <[email protected]>
1 parent c3a7bb6 commit dd4cfe7

File tree

4 files changed

+46
-25
lines changed

4 files changed

+46
-25
lines changed

doc/changelog/768.miscellaneous.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
feat: add include name property to keyword

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

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -218,33 +218,32 @@ def _expand_helper(self, search_paths: typing.List[str], recurse: bool) -> typin
218218
search_paths.append(keyword.filename)
219219
keywords.append(keyword)
220220
continue
221-
success = False
221+
expand_include_file = None
222222
for search_path in search_paths:
223223
include_file = os.path.join(search_path, keyword.filename)
224-
if not os.path.isfile(include_file):
225-
continue
226-
xform = None
227-
if keyword.subkeyword == "TRANSFORM":
228-
xform = keyword
229-
context = ImportContext(xform, self, include_file)
230-
try:
231-
include_deck = self._prepare_deck_for_expand(keyword)
232-
include_deck._import_file(include_file, "utf-8", context)
233-
except UnicodeDecodeError as e:
234-
encoding = self._detect_encoding(include_file)
235-
include_deck = self._prepare_deck_for_expand(keyword)
236-
include_deck._import_file(include_file, encoding, context)
237-
success = True
238-
break
239-
if success:
240-
if recurse:
241-
# TODO: merge the parameters if the "LOCAL" option is not used!
242-
expanded = include_deck._expand_helper(search_paths, True)
243-
keywords.extend(expanded)
244-
else:
245-
keywords.extend(include_deck.all_keywords)
246-
else:
224+
if os.path.isfile(include_file):
225+
expand_include_file = include_file
226+
break
227+
if expand_include_file is None:
247228
keywords.append(keyword)
229+
continue
230+
xform = None
231+
if keyword.subkeyword == "TRANSFORM":
232+
xform = keyword
233+
context = ImportContext(xform, self, expand_include_file)
234+
try:
235+
include_deck = self._prepare_deck_for_expand(keyword)
236+
include_deck._import_file(expand_include_file, "utf-8", context)
237+
except UnicodeDecodeError as e:
238+
encoding = self._detect_encoding(expand_include_file)
239+
include_deck = self._prepare_deck_for_expand(keyword)
240+
include_deck._import_file(expand_include_file, encoding, context)
241+
if recurse:
242+
# TODO: merge the parameters if the "LOCAL" option is not used!
243+
expanded = include_deck._expand_helper(search_paths, True)
244+
keywords.extend(expanded)
245+
else:
246+
keywords.extend(include_deck.all_keywords)
248247
for keyword in keywords:
249248
if isinstance(keyword, KeywordBase):
250249
keyword.deck = None
@@ -505,7 +504,10 @@ def _import_file(self, path: str, encoding: str, context: ImportContext):
505504
from ansys.dyna.core.lib.deck_loader import load_deck_from_buffer
506505

507506
with open(path, encoding=encoding) as f:
508-
return load_deck_from_buffer(self, f, context, self._import_handlers)
507+
loader_result = load_deck_from_buffer(self, f, context, self._import_handlers)
508+
for keyword in self.keywords:
509+
keyword.included_from = path
510+
return loader_result
509511

510512
def import_file(
511513
self, path: str, encoding: str = "utf-8"

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ def __init__(self, **kwargs):
4848
self.user_comment = kwargs.get("user_comment", "")
4949
self._format_type: format_type = kwargs.get("format", format_type.default)
5050
self._deck = None
51+
self._included_from = None
5152

5253
@property
5354
def deck(self) -> typing.Optional["Deck"]:
@@ -109,6 +110,19 @@ def cards(self) -> typing.List[CardInterface]:
109110
"""Gets the cards of the keyword"""
110111
return self._get_all_cards()
111112

113+
@property
114+
def included_from(self) -> str:
115+
"""Get the filename this was included from.
116+
117+
If the keyword was not read from a file,
118+
return None.
119+
"""
120+
return self._included_from
121+
122+
@included_from.setter
123+
def included_from(self, value: str):
124+
self._included_from = value
125+
112126
def _get_user_comment_lines(self) -> typing.List[str]:
113127
user_comment = self.user_comment
114128
if user_comment == "":

tests/test_deck.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ def after_import(self, context, keyword):
100100
assert import_handler._num_keywords == 12
101101
assert len(deck.keywords) == 11
102102
assert len(deck.all_keywords) == 11
103+
assert deck.keywords[0].included_from == filepath
103104
assert deck.get(type="ALE")[0].nid1 == 1
104105

105106

@@ -178,6 +179,7 @@ def test_deck_copy():
178179
x.append(kwd.Mat001(mid=99))
179180
y = copy.deepcopy(x)
180181
y.keywords[0].mid = 100
182+
assert y.keywords[0].included_from is None
181183
assert x.keywords[0].mid == 99
182184
assert y.keywords[0].mid == 100
183185

@@ -396,10 +398,12 @@ def test_deck_expand(file_utils):
396398
"""Test that a long deck can read a standard deck."""
397399
# I think there are more corner cases related to this to iron out..
398400
deck = Deck(format=format_type.long)
401+
include_path = file_utils.get_asset_file_path("test.k")
399402
deck.append(kwd.Include(filename=file_utils.get_asset_file_path("test.k"), format=format_type.standard))
400403
expanded_deck = deck.expand()
401404
assert len(expanded_deck.keywords) == 12
402405
assert expanded_deck.keywords[1].format == format_type.standard
406+
assert expanded_deck.keywords[0].included_from == include_path
403407

404408

405409
@pytest.mark.keywords

0 commit comments

Comments
 (0)