Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to determine libmediainfo is installed? #34

Closed
madebr opened this issue Feb 23, 2017 · 4 comments
Closed

How to determine libmediainfo is installed? #34

madebr opened this issue Feb 23, 2017 · 4 comments

Comments

@madebr
Copy link
Contributor

madebr commented Feb 23, 2017

Currently, when libmediainfo is not installed, pymediainfo imports fine.
At parse time, an OSError is thrown when libmediainfo is not found.

Is there a portable (supported) way to determine pymediainfo is working as intended?
Depending on throwing an OSError that pymediainfo is not working is, in my opinion, not robust.

As a workaround, what I do now is:

import os.path
import pymediainfo # Succeeds since pymediainfo is installed

try:
    pymediainfo.MediaInfo.parse(os.path.realpath(__file__)
    libmediainfo_available = True
except OSError:
    libmediainfo_available = False

But I doubt this is kosher and supported.

@madebr madebr changed the title ght How to determine libmediainfo is installed? Feb 23, 2017
@sbraz
Copy link
Owner

sbraz commented Feb 23, 2017

Well we don't really need libmediainfo in order to parse the XML files it created…
Theoretically, I may want to load an XML file created on another computer although I don't have the lib installed locally.

@madebr
Copy link
Contributor Author

madebr commented Feb 23, 2017

How about a can_parse member class function of the MediaInfo class? (or something similar)
What I want is a way to check whether parsing works before doing any actual parsing.

Something like this:

class MediaInfo(object):
    @classmethod
    def _get_library(cls):
        if os.name in ("nt", "dos", "os2", "ce"):
            return windll.MediaInfo
        elif sys.platform == "darwin":
            try:
                return CDLL("libmediainfo.0.dylib")
            except OSError:
                return CDLL("libmediainfo.dylib")
        else:
            return CDLL("libmediainfo.so.0")

    @classmethod
    def can_parse(cls):
        try:
            cls._get_library()
            return True
        except:
            return False

@sbraz
Copy link
Owner

sbraz commented Feb 23, 2017

That sounds reasonable, yeah. I'm not too experienced with staticmethod/classmethod, any reason why you chose the latter?

@madebr
Copy link
Contributor Author

madebr commented Feb 23, 2017

I chose classmethod where the method needs information about the class.
Staticmethod where it does nog.
So _get_library is ok with staticmethod (different from my code),
the can_parse has some connection with MediaInfo.

It's a matter of choice. The code would also work with 2 staticmethods.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants