Skip to content

Commit

Permalink
Python 3 compatibility with six + demo.py added
Browse files Browse the repository at this point in the history
  • Loading branch information
jabbalaci committed Dec 29, 2014
1 parent 241a1aa commit a24c4b7
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
41 changes: 41 additions & 0 deletions demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env python
# encoding: utf-8

"""
a demo that shows how to call pymediainfo
"""

from __future__ import (absolute_import, division,
print_function, unicode_literals)

import sys
from pprint import pprint

from pymediainfo import MediaInfo


def print_frame(text):
print("+-{}-+".format("-" * len(text)))
print("| {} |".format(text))
print("+-{}-+".format("-" * len(text)))


def process(fname):
media_info = MediaInfo.parse(fname)
for track in media_info.tracks:
print_frame(track.track_type)
pprint(track.to_data())
#
print()
for track in media_info.tracks:
if track.track_type == 'General':
print("Duration: {} sec.".format(track.duration / 1000.0))

##############################################################################

if __name__ == "__main__":
if len(sys.argv) == 1:
print("Usage: {} <media_file>".format(sys.argv[0]))
sys.exit(0)
# else
process(sys.argv[1])
15 changes: 7 additions & 8 deletions pymediainfo/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import json
import os
import sys

from subprocess import Popen
from tempfile import mkstemp

import six
from bs4 import BeautifulSoup, NavigableString

_py3 = sys.version_info >= (3,)

__version__ = '1.4.0'
__version__ = '1.4.1'

ENV_DICT = os.environ

Expand Down Expand Up @@ -60,7 +57,7 @@ def __repr__(self):

def to_data(self):
data = {}
for k, v in self.__dict__.iteritems():
for k, v in six.iteritems(self.__dict__):
if k != 'xml_dom_fragment':
data[k] = v
return data
Expand All @@ -70,8 +67,10 @@ class MediaInfo(object):

def __init__(self, xml):
self.xml_dom = xml
if _py3: xml_types = (str,) # no unicode type in python3
else: xml_types = (str, unicode)
if six.PY3:
xml_types = (str,) # no unicode type in python3
else:
xml_types = (str, unicode)

if isinstance(xml, xml_types):
self.xml_dom = MediaInfo.parse_xml_data_into_dom(xml)
Expand Down

0 comments on commit a24c4b7

Please sign in to comment.