Skip to content

Commit 5eb11e4

Browse files
committed
fixed regression in Documents.open() Documents.read() and Documents.new_from()
where strings are passed in lieu of Path objects. It was the intention for both strings and paths to work. Tests updated to test for both Path and str objects.
1 parent 0769ae7 commit 5eb11e4

File tree

3 files changed

+55
-29
lines changed

3 files changed

+55
-29
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## 0.7.2
44
* addded missing type hints to `Document()` methods.
5+
* fixed regression in Documents.open() Documents.read() and Documents.new_from()
6+
where strings are passed in lieu of Path objects. It was the intention for both
7+
strings and paths to work. Tests updated to test for both Path and str objects.
58

69
## 0.7.1
710

pycatia/in_interfaces/documents.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,12 @@ def new_from(self, file_name: Path) -> Document:
150150
:rtype: Document
151151
"""
152152

153-
# legacy support for < 0.7.1 where file_name could be a string.
154-
if isinstance(file_name, str):
155-
if not os.path.isfile(file_name):
156-
raise FileNotFoundError(f'Could not find file {file_name}.')
157-
else:
158-
if not file_name.is_file():
159-
raise FileNotFoundError(f'Could not find file {file_name}.')
153+
# legacy support for strings.
154+
if type(file_name) is str:
155+
file_name = Path(file_name)
160156

157+
if not file_name.is_file:
158+
raise FileNotFoundError(f'Could not find file {file_name}.')
161159

162160
return Document(self.documents.NewFrom(file_name))
163161

@@ -256,13 +254,13 @@ def open(self, file_name: Path) -> Document:
256254
:param Path file_name:
257255
:rtype: Document
258256
"""
259-
# legacy support for < 0.7.1 where file_name could be a string.
260-
if isinstance(file_name, str):
261-
if not os.path.isfile(file_name):
262-
raise FileNotFoundError(f'Could not find file {file_name}.')
263-
else:
264-
if not file_name.is_file:
265-
raise FileNotFoundError(f'Could not find file {file_name}.')
257+
258+
# legacy support for strings.
259+
if type(file_name) is str:
260+
file_name = Path(file_name)
261+
262+
if not file_name.is_file:
263+
raise FileNotFoundError(f'Could not find file {file_name}.')
266264

267265
try:
268266
self.logger.info(f'Opening document "{file_name}".')
@@ -313,6 +311,14 @@ def read(self, file_name: Path) -> Document:
313311
"""
314312
# return Document(self.documents.Read(file_name))
315313

314+
# legacy support for strings.
315+
if type(file_name) is str:
316+
file_name = Path(file_name)
317+
318+
if not file_name.is_file:
319+
raise FileNotFoundError(f'Could not find file {file_name}.')
320+
321+
316322
read_doc_com = self.documents.Read(file_name)
317323
extension = file_name.suffix[1:]
318324
types = [document_types[k]['type'] for k in (document_types) if document_types[k]['extension'] == extension]

tests/in/test_document.py

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -166,14 +166,13 @@ def test_new_from():
166166

167167
document.close()
168168

169-
with pytest.raises(FileNotFoundError):
170-
documents.new_from("lala")
169+
def test_new_from_str():
170+
documents = caa.documents
171+
document = documents.new_from(str(cat_part_measurable))
171172

173+
assert document.name is not os.path.basename(cat_part_measurable)
172174

173-
def test_no_such_file():
174-
with pytest.raises(FileNotFoundError):
175-
documents = caa.documents
176-
documents.open("lala")
175+
document.close()
177176

178177

179178
def test_num_open():
@@ -185,22 +184,40 @@ def test_num_open():
185184

186185

187186
def test_open_document():
188-
# This assertion has been removed as my version of CATIA keeps an open link to ABQMaterialPropertiesCatalog.CATfct
189-
# once the document is closed. I don't know if this is a CATIA bug or `feature` to keep the linked item loaded.
190-
# assert documents.documents.Count == 0
187+
188+
documents = caa.documents
189+
document = documents.open(cat_part_measurable)
190+
191+
assert type(document) is PartDocument
192+
193+
document.close
194+
195+
def test_open_document_str():
196+
197+
documents = caa.documents
198+
document = documents.open(str(cat_part_measurable))
199+
200+
assert type(document) is PartDocument
201+
202+
document.close
191203

192-
with CATIADocHandler(cat_part_measurable) as caa:
193-
document = caa.document
194-
assert document is not None
195-
assert document.name == cat_part_measurable.name
196-
assert f'PartDocument(name="{document.name}")' == document.__repr__()
197204

198205
def test_read_document():
199206

200207
documents = caa.documents
201208
document = documents.read(cat_part_measurable)
202209

203-
assert type(document) == PartDocument
210+
assert type(document) is PartDocument
211+
212+
document.close
213+
214+
215+
def test_read_document_str():
216+
217+
documents = caa.documents
218+
document = documents.read(str(cat_part_measurable))
219+
220+
assert type(document) is PartDocument
204221

205222
document.close
206223

0 commit comments

Comments
 (0)