[vlc-commits] python: add support for Path-like objects (#113)

phill-85 git at videolan.org
Sun Oct 27 20:13:40 CET 2019


vlc/python | branch: master | phill-85 <phill.ridout at gmail.com> | Wed Oct  9 21:55:32 2019 +0100| [2bc85c796acac992f21a4b8933396b3201c5b260] | committer: Olivier Aubert

python: add support for Path-like objects (#113)


> http://git.videolan.org/gitweb.cgi/vlc/python.git/?a=commit;h=2bc85c796acac992f21a4b8933396b3201c5b260
---

 generator/templates/header.py   |  9 +++++++++
 generator/templates/override.py | 16 ++++++++++++++--
 tests/test.py                   | 24 ++++++++++++++++++++++++
 3 files changed, 47 insertions(+), 2 deletions(-)

diff --git a/generator/templates/header.py b/generator/templates/header.py
index 1aef0ae..a2f1acf 100755
--- a/generator/templates/header.py
+++ b/generator/templates/header.py
@@ -255,6 +255,15 @@ def get_default_instance():
         _default_instance = Instance()
     return _default_instance
 
+def try_fspath(path):
+    """Try calling os.fspath
+    os.fspath is only available from py3.6
+    """
+    try:
+        return os.fspath(path)
+    except (AttributeError, TypeError):
+        return path
+
 _Cfunctions = {}  # from LibVLC __version__
 _Globals = globals()  # sys.modules[__name__].__dict__
 
diff --git a/generator/templates/override.py b/generator/templates/override.py
index 93fc823..595b04f 100644
--- a/generator/templates/override.py
+++ b/generator/templates/override.py
@@ -40,7 +40,7 @@ class Instance:
     def media_player_new(self, uri=None):
         """Create a new MediaPlayer instance.
 
-        @param uri: an optional URI to play in the player.
+        @param uri: an optional URI to play in the player as a str, bytes or PathLike object.
         """
         p = libvlc_media_player_new(self)
         if uri:
@@ -72,8 +72,10 @@ class Instance:
         Alternatively, options can be added to the media using the
         Media.add_options method (with the same limitation).
 
+        @param mrl: A str, bytes or PathLike object
         @param options: optional media option=value strings
         """
+        mrl = try_fspath(mrl)
         if ':' in mrl and mrl.index(':') > 1:
             # Assume it is a URL
             m = libvlc_media_new_location(self, str_to_bytes(mrl))
@@ -85,9 +87,18 @@ class Instance:
         m._instance = self
         return m
 
+    def media_new_path(self, path):
+        """Create a media for a certain file path.
+        See L{media_release}.
+        @param path: A str, byte, or PathLike object representing a local filesystem path.
+        @return: the newly created media or None on error.
+        """
+        path = try_fspath(path)
+        return libvlc_media_new_path(self, str_to_bytes(path))
+
     def media_list_new(self, mrls=None):
         """Create a new MediaList instance.
-        @param mrls: optional list of MRL strings
+        @param mrls: optional list of MRL strings, bytes, or PathLike objects.
         """
         l = libvlc_media_list_new(self)
         # We should take the lock, but since we did not leak the
@@ -208,6 +219,7 @@ class MediaList:
         @param mrl: a media instance or a MRL.
         @return: 0 on success, -1 if the media list is read-only.
         """
+        mrl = tryfspath(mrl)
         if isinstance(mrl, basestring):
             mrl = (self.get_instance() or get_default_instance()).media_new(mrl)
         return libvlc_media_list_add_media(self, mrl)
diff --git a/tests/test.py b/tests/test.py
index 3abba83..c224964 100755
--- a/tests/test.py
+++ b/tests/test.py
@@ -33,11 +33,17 @@ import ctypes
 import re
 import os
 import unittest
+from unittest.mock import MagicMock
 try:
     import urllib.parse as urllib # python3
 except ImportError:
     import urllib # python2
 
+try:
+    from pathlib import Path
+except ImportError:
+    pass
+
 try:
     import vlc
 except ImportError:
@@ -48,6 +54,24 @@ from generator import generate
 SAMPLE = os.path.join(os.path.dirname(__file__), 'samples/sample.mp4')
 print ("Checking " + vlc.__file__)
 
+
+class TestAuxMethods(unittest.TestCase):
+    def test_try_fspath_incompatible_object(self):
+        test_object = MagicMock()
+        result = vlc.try_fspath(test_object)
+        self.assertIs(result, test_object)
+
+    def test_try_fspath_path_like_object(self):
+        test_object = Path('test', 'path')
+        result = vlc.try_fspath(test_object)
+        self.assertEqual(result, os.path.join('test', 'path'))
+
+    def test_try_fspath_str_object(self):
+        test_object = os.path.join('test', 'path')
+        result = vlc.try_fspath(test_object)
+        self.assertEqual(result, os.path.join('test', 'path'))
+
+
 class TestVLCAPI(unittest.TestCase):
     #def setUp(self):
     #    self.seq = range(10)



More information about the vlc-commits mailing list