Skip to content

Commit

Permalink
Add pathlib paths support, fixes #40
Browse files Browse the repository at this point in the history
  • Loading branch information
sbraz committed Aug 24, 2017
1 parent 8471152 commit e375963
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
20 changes: 14 additions & 6 deletions pymediainfo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
import xml.etree.ElementTree as ET
from ctypes import *

try:
import pathlib
except ImportError:
pathlib = None

if sys.version_info < (3,):
import urlparse
else:
Expand Down Expand Up @@ -91,12 +96,15 @@ def can_parse(cls):
@classmethod
def parse(cls, filename):
lib = cls._get_library()
url = urlparse.urlparse(filename)
# Test whether the filename is actually a URL
if url.scheme is None:
# Test file is readable
with open(filename, "rb"):
pass
if pathlib is not None and isinstance(filename, pathlib.PurePath):
filename = str(filename)
else:
url = urlparse.urlparse(filename)
# Test whether the filename is actually a URL
if url.scheme is None:
# Test whether the file is readable
with open(filename, "rb"):
pass
# Define arguments and return types
lib.MediaInfo_Inform.restype = c_wchar_p
lib.MediaInfo_New.argtypes = []
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
include_package_data=True,
zip_safe=False,
license='MIT',
tests_require=["nose"],
tests_require=["pytest"],
test_suite="nose.collector",
classifiers=[
"Development Status :: 5 - Production/Stable",
Expand Down
10 changes: 10 additions & 0 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import sys
import unittest

import pytest

from pymediainfo import MediaInfo

data_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "data")
Expand Down Expand Up @@ -85,3 +87,11 @@ def setUp(self):
self.mi = MediaInfo.parse("https://github.com/sbraz/pymediainfo/blob/master/tests/data/sample.mkv?raw=true")
def test_parse_url(self):
self.assertEqual(len(self.mi.tracks), 2)

class MediaInfoPathlibTest(unittest.TestCase):
def setUp(self):
pathlib = pytest.importorskip("pathlib")
self.path = pathlib.Path(data_dir) / "sample.mp4"
def test_parse_pathlib_path(self):
mi = MediaInfo.parse(self.path)
self.assertEqual(len(mi.tracks), 3)

0 comments on commit e375963

Please sign in to comment.