Meshroom/meshroom/ui/components/filepath.py
Yann Lanthony c3750a33c3 [ui] Inspector3D: add media contextual menu
Contextual menu to expose additional actions:
 * open media containing folder
 * copy media path
 * advanced manual control over media (un)loading
+ fix MediaLibrary to avoid binding loop on 'visible' when directly modifying 'request' property
2018-12-10 15:13:26 +01:00

80 lines
2.3 KiB
Python

#!/usr/bin/env python
# coding:utf-8
from meshroom.core import pyCompatibility
from PySide2.QtCore import QUrl
from PySide2.QtCore import QObject, Slot
import os
class FilepathHelper(QObject):
"""
FilepathHelper gives access to file path methods not available from JS.
It should be non-instantiable and expose only static methods, but this is not yet
possible in PySide.
"""
@staticmethod
def asStr(path):
"""
Accepts strings and QUrls and always returns 'path' as a string.
Args:
path (str or QUrl): the filepath to consider
Returns:
str: String representation of 'path'
"""
if not isinstance(path, (QUrl, pyCompatibility.basestring)):
raise TypeError("Unexpected data type: {}".format(path.__class__))
if isinstance(path, QUrl):
path = path.toLocalFile()
return path
@Slot(str, result=str)
@Slot(QUrl, result=str)
def basename(self, path):
""" Returns the final component of a pathname """
return os.path.basename(self.asStr(path))
@Slot(str, result=str)
@Slot(QUrl, result=str)
def dirname(self, path):
""" Returns the directory component of a pathname """
return os.path.dirname(self.asStr(path))
@Slot(str, result=str)
@Slot(QUrl, result=str)
def extension(self, path):
""" Returns the extension (.ext) of a pathname """
return os.path.splitext(self.asStr(path))[-1]
@Slot(str, result=bool)
@Slot(QUrl, result=bool)
def isFile(self, path):
""" Test whether a path is a regular file """
return os.path.isfile(self.asStr(path))
@Slot(str, result=bool)
@Slot(QUrl, result=bool)
def exists(self, path):
""" Test whether a path exists """
return os.path.exists(self.asStr(path))
@Slot(QUrl, result=str)
def urlToString(self, url):
""" Convert QUrl to a string using 'QUrl.toLocalFile' method """
return self.asStr(url)
@Slot(str, result=QUrl)
def stringToUrl(self, path):
""" Convert a path (string) to a QUrl using 'QUrl.fromLocalFile' method """
return QUrl.fromLocalFile(path)
@Slot(str, result=str)
@Slot(QUrl, result=str)
def normpath(self, path):
""" Returns native normalized path """
return os.path.normpath(self.asStr(path))