Skip to content

Commit f4ff1a0

Browse files
committed
Merge branch 'fix-windows-tests'
* fix failing test for backslashed unicode paths on Windows * fix reading of CIFs from URL-like paths
2 parents df264a8 + 15e1374 commit f4ff1a0

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

src/diffpy/structure/parsers/p_cif.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ def parseFile(self, filename):
281281
"""
282282
self.ciffile = None
283283
self.filename = filename
284-
fileurl = _fixIfWindowsPath(filename)
284+
fileurl = _quoteLocalPath(filename)
285285
rv = self._parseCifDataSource(fileurl)
286286
# all good here
287287
return rv
@@ -706,17 +706,30 @@ def getSymOp(s):
706706
return rv
707707

708708

709-
def _fixIfWindowsPath(filename):
710-
"""Convert Windows-style path to valid local URL.
711-
CifFile loads files using urlopen, which fails for Windows-style paths.
709+
def _quoteLocalPath(filename):
710+
"""Quote local paths to file URL-s.
712711
713-
filename -- path to be fixed
712+
CifFile reads files with urlopen, which fails for Windows paths or
713+
for paths containing ":".
714714
715-
Return fixed URL when run on Windows, otherwise return filename.
715+
Parameters
716+
----------
717+
filename : str
718+
The path to be corrected.
719+
720+
Returns
721+
-------
722+
str
723+
The fixed URL when it contains ":" or `filename`.
724+
Return filename if it forms http or ftp URL.
716725
"""
717726
rv = filename
718-
if filename[:1].isalpha() and filename[1:3] == ':\\':
719-
from urllib.request import pathname2url
727+
cnvflag = False
728+
if ':' in filename:
729+
head = filename.split(':', 1)[0].lower()
730+
cnvflag = head.isalpha() and head not in ('http', 'https', 'ftp')
731+
if cnvflag:
732+
from six.moves.urllib.request import pathname2url
720733
rv = pathname2url(filename)
721734
return rv
722735

src/diffpy/structure/tests/testp_cif.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
from diffpy.structure.tests.testutils import datafile
2424
from diffpy.structure.parsers.p_cif import P_cif, leading_float, getSymOp
25+
from diffpy.structure.parsers.p_cif import _quoteLocalPath
2526
from diffpy.structure.parsers import getParser
2627
from diffpy.structure import Structure
2728
from diffpy.structure import StructureFormatError
@@ -60,6 +61,18 @@ def test_getSymOp(self):
6061
self.assertEqual(str(op1_std), str(op1))
6162
return
6263

64+
65+
def test__quoteLocalPath(self):
66+
"check _quoteLocalPath()"
67+
from six.moves.urllib.request import pathname2url as p2u
68+
self.assertEqual('/a/b/c.cif', _quoteLocalPath('/a/b/c.cif'))
69+
self.assertEqual(p2u('c:\\a.cif'), _quoteLocalPath('c:\\a.cif'))
70+
self.assertEqual(p2u('c:/a.cif'), _quoteLocalPath('c:/a.cif'))
71+
self.assertEqual('/x:y/c.cif', _quoteLocalPath('/x:y/c.cif'))
72+
self.assertEqual('http::cif.org/a.cif',
73+
_quoteLocalPath('http::cif.org/a.cif'))
74+
return
75+
6376
# End of class TestRoutines
6477

6578
# ----------------------------------------------------------------------------
@@ -157,7 +170,11 @@ def test_parseFile(self):
157170
self.assertEqual(str, type(c1.label))
158171
self.assertEqual('C1', c1.label)
159172
# filename with unicode encoding
160-
ugraphite = P_cif().parseFile(six.u(self.graphiteciffile))
173+
hasbs = '\\' in self.graphiteciffile
174+
uciffile = six.u(self.graphiteciffile.replace('\\', '/'))
175+
if hasbs: # pragma: no cover
176+
uciffile = uciffile.replace(u'/', u'\\')
177+
ugraphite = P_cif().parseFile(uciffile)
161178
self.assertEqual(4, len(ugraphite))
162179
# File with full space group name
163180
ptei = P_cif().parseFile(self.teiciffile)

0 commit comments

Comments
 (0)