Skip to content

Commit 52a6f01

Browse files
AartGoossenspR0Ps
authored andcommitted
Add pathlib support to fileish_open (dtcooper#101)
1 parent 7c0b764 commit 52a6f01

2 files changed

Lines changed: 21 additions & 3 deletions

File tree

fitparse/utils.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
except ImportError:
66
from collections import Iterable
77

8+
try:
9+
# Python 3.4+
10+
from pathlib import PurePath
11+
except ImportError:
12+
PurePath = None
13+
814

915
class FitParseError(ValueError):
1016
pass
@@ -56,9 +62,13 @@ def fileish_open(fileish, mode):
5662
return open(fileish, mode)
5763
except TypeError:
5864
return io.BytesIO(fileish)
59-
else:
60-
# Python 3 - file contents
61-
return io.BytesIO(fileish)
65+
66+
# Python 3 - pathlib obj
67+
if PurePath and isinstance(fileish, PurePath):
68+
return fileish.open(mode)
69+
70+
# Python 3 - file contents
71+
return io.BytesIO(fileish)
6272

6373

6474
def is_iterable(obj):

tests/test_utils.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
import sys
66
import tempfile
77

8+
try:
9+
# Python 3.4+
10+
from pathlib import Path
11+
except ImportError:
12+
Path = None
13+
814
from fitparse.utils import fileish_open, is_iterable
915

1016
if sys.version_info >= (2, 7):
@@ -38,6 +44,8 @@ def test_fopen(fileish):
3844
test_fopen(f.read())
3945
with open(testfile("nametest.FIT"), 'rb') as f:
4046
test_fopen(io.BytesIO(f.read()))
47+
if Path:
48+
test_fopen(Path(testfile('nametest.FIT')))
4149

4250
def test_fileish_open_write(self):
4351

0 commit comments

Comments
 (0)