Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions fitparse/utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import io
import os
import re
from collections.abc import Iterable

from pathlib import PurePath


class FitParseError(ValueError):
pass
Expand Down Expand Up @@ -48,14 +47,10 @@ def fileish_open(fileish, mode):
if hasattr(fileish, attr) and hasattr(fileish, 'seek'):
# BytesIO-like object
return fileish
elif isinstance(fileish, str):
elif isinstance(fileish, (str, os.PathLike)):
# file path
return open(fileish, mode)

# pathlib obj
if isinstance(fileish, PurePath):
return fileish.open(mode)

# file contents
return io.BytesIO(fileish)

Expand Down
10 changes: 10 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from pathlib import Path

from fitparse import FitFile
from fitparse.utils import fileish_open, is_iterable

import unittest
Expand All @@ -16,6 +17,15 @@ def testfile(filename):


class UtilsTestCase(unittest.TestCase):
def test_fitfile_supports_os_pathlike(self):
class FilePath:
def __fspath__(self):
return testfile('nametest.FIT')

fit_file = FitFile(FilePath())
fit_file.parse()

self.assertTrue(fit_file.messages)

def test_fileish_open_read(self):
"""Test the constructor does the right thing when given different types
Expand Down