Skip to content

Commit 57a03fa

Browse files
committed
Removed f-strings and pathlib depdencies for compatibility with Python 2.7 and 3.5.
Signed-off-by: Jonathan Walker <[email protected]>
1 parent bce7040 commit 57a03fa

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

swat/cas/datamsghandlers.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
'''
2323

2424
from __future__ import print_function, division, absolute_import, unicode_literals
25-
from pathlib import Path
25+
from glob import glob
2626

2727
import base64
2828
import copy
@@ -1219,7 +1219,8 @@ class Image(CASDataMsgHandler):
12191219
behavior should be similar to that of the image.loadImages_ CAS action for loading
12201220
server-side images:
12211221
1222-
.. _image.loadImages: https://go.documentation.sas.com/doc/en/pgmsascdc/v_028/casactml/casactml_image_details22.htm # noqa: E501
1222+
.. _image.loadImages: https://go.documentation.sas.com/doc/en/pgmsascdc/v_028/casactml/casactml_image_details22
1223+
.htm # noqa: E501
12231224
12241225
Although images will be stored in binary format to a CAS table column
12251226
labeled "_image_", the CAS table metadata will not indicate that this column should
@@ -1237,8 +1238,12 @@ class Image(CASDataMsgHandler):
12371238
"""
12381239

12391240
def __init__(self, data, nrecs=1000, subdirs=True):
1240-
if isinstance(data, (str, Path)):
1241-
path = Path(data)
1241+
1242+
# To maintain Py2.7 compatibility, use strings instead of Paths.
1243+
if type(data).__module__ == 'pathlib':
1244+
data = str(data)
1245+
1246+
if isinstance(data, str):
12421247
files = []
12431248

12441249
# Search for all images in the directory and (optionally) in subdirectories
@@ -1247,9 +1252,11 @@ def __init__(self, data, nrecs=1000, subdirs=True):
12471252
'tif', 'tiff', 'webp'):
12481253

12491254
if subdirs:
1250-
files.extend(path.glob(f'**/*.{extension}'))
1255+
pattern = os.path.join(data, '**', '*.%s' % extension)
12511256
else:
1252-
files.extend(path.glob(f'*.{extension}'))
1257+
pattern = os.path.join(data, '*.%s' % extension)
1258+
1259+
files.extend(glob(pattern, recursive=subdirs))
12531260
self._data = files
12541261
else:
12551262
self._data = list(data)
@@ -1286,14 +1293,18 @@ def getrow(self, row):
12861293

12871294
record = self._data[row]
12881295

1296+
# Convert Path instances to str for Py2.7 compatibility.
1297+
if type(record).__module__ == 'pathlib':
1298+
record = str(record)
1299+
12891300
# Default value. Will be overridden if disk location is known.
12901301
path = 'Image_%d.png' % (row + 1)
12911302

12921303
# Input is path to an image on disk. Can just read bytes directly.
1293-
if isinstance(record, (str, Path)):
1304+
if isinstance(record, str):
12941305
with open(record, 'rb') as f:
12951306
image = f.read()
1296-
path = str(record)
1307+
path = record
12971308
else:
12981309
# Otherwise, PIL package is required to format data as an image.
12991310
if PIL is None:

0 commit comments

Comments
 (0)