Skip to content

Commit

Permalink
Add can_parse class method to MediaInfo, fixes #34
Browse files Browse the repository at this point in the history
  • Loading branch information
madebr authored and sbraz committed Feb 24, 2017
1 parent 7ef56d0 commit 506df85
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
23 changes: 16 additions & 7 deletions pymediainfo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,26 @@ def parse_xml_data_into_dom(xml_data):
except:
return None
@staticmethod
def parse(filename):
def _get_library():
if os.name in ("nt", "dos", "os2", "ce"):
lib = windll.MediaInfo
return windll.MediaInfo
elif sys.platform == "darwin":
try:
lib = CDLL("libmediainfo.0.dylib")
return CDLL("libmediainfo.0.dylib")
except OSError:
lib = CDLL("libmediainfo.dylib")
return CDLL("libmediainfo.dylib")
else:
lib = CDLL("libmediainfo.so.0")

return CDLL("libmediainfo.so.0")
@classmethod
def can_parse(cls):
try:
cls._get_library()
return True
except:
return False
@classmethod
def parse(cls, filename):
lib = cls._get_library()
# Test file is readable
with open(filename, "rb"):
pass
Expand Down Expand Up @@ -110,7 +119,7 @@ def parse(filename):
# Delete the handle
lib.MediaInfo_Close(handle)
lib.MediaInfo_Delete(handle)
return MediaInfo(xml)
return cls(xml)
def _populate_tracks(self):
if self.xml_dom is None:
return
Expand Down
2 changes: 2 additions & 0 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ def test_parse_invalid_xml(self):
class MediaInfoLibraryTest(unittest.TestCase):
def setUp(self):
self.mi = MediaInfo.parse(os.path.join(data_dir, "sample.mp4"))
def test_can_parse_true(self):
self.assertTrue(MediaInfo.can_parse())
def test_track_count(self):
self.assertEqual(len(self.mi.tracks), 3)
def test_track_types(self):
Expand Down

0 comments on commit 506df85

Please sign in to comment.