mirror of
https://github.com/alicevision/Meshroom.git
synced 2025-04-29 18:27:23 +02:00
[core] add pyCompatibility module
For python 2 / 3 compatibility
This commit is contained in:
parent
1c16199ca4
commit
d83022714f
4 changed files with 28 additions and 23 deletions
|
@ -1,4 +1,5 @@
|
|||
from meshroom.common import BaseObject, Property, Variant
|
||||
from meshroom.core import pyCompatibility
|
||||
from enum import Enum # available by default in python3. For python2: "pip install enum34"
|
||||
import collections
|
||||
import math
|
||||
|
@ -93,7 +94,7 @@ class File(Attribute):
|
|||
super(File, self).__init__(name=name, label=label, description=description, value=value, uid=uid, group=group)
|
||||
|
||||
def validateValue(self, value):
|
||||
if not isinstance(value, basestring):
|
||||
if not isinstance(value, pyCompatibility.basestring):
|
||||
raise ValueError('File only supports string input (param:{}, value:{}, type:{})'.format(self.name, value, type(value)))
|
||||
return os.path.normpath(value).replace('\\', '/') if value else ''
|
||||
|
||||
|
@ -183,7 +184,7 @@ class StringParam(Param):
|
|||
super(StringParam, self).__init__(name=name, label=label, description=description, value=value, uid=uid, group=group)
|
||||
|
||||
def validateValue(self, value):
|
||||
if not isinstance(value, basestring):
|
||||
if not isinstance(value, pyCompatibility.basestring):
|
||||
raise ValueError('StringParam value should be a string (param:{}, value:{}, type:{})'.format(self.name, value, type(value)))
|
||||
return value
|
||||
|
||||
|
|
|
@ -21,6 +21,7 @@ from . import desc
|
|||
import meshroom.core
|
||||
from meshroom.common import BaseObject, DictModel, Slot, Signal, Property, Variant, ListModel
|
||||
from meshroom.core.exception import UnknownNodeTypeError
|
||||
from meshroom.core import pyCompatibility
|
||||
|
||||
# Replace default encoder to support Enums
|
||||
DefaultJSONEncoder = json.JSONEncoder # store the original one
|
||||
|
@ -35,21 +36,6 @@ class MyJSONEncoder(DefaultJSONEncoder): # declare a new one with Enum support
|
|||
|
||||
json.JSONEncoder = MyJSONEncoder # replace the default implementation with our new one
|
||||
|
||||
try:
|
||||
unicode = unicode
|
||||
except NameError:
|
||||
# 'unicode' is undefined, must be Python 3
|
||||
str = str
|
||||
unicode = str
|
||||
bytes = bytes
|
||||
basestring = (str, bytes)
|
||||
else:
|
||||
# 'unicode' exists, must be Python 2
|
||||
str = str
|
||||
unicode = unicode
|
||||
bytes = str
|
||||
basestring = basestring
|
||||
|
||||
stringIsLinkRe = re.compile('^\{[A-Za-z]+[A-Za-z0-9_.]*\}$')
|
||||
|
||||
|
||||
|
@ -58,11 +44,11 @@ def isLink(value):
|
|||
Return whether the given argument is a link expression.
|
||||
A link expression is a string matching the {nodeName.attrName} pattern.
|
||||
"""
|
||||
return isinstance(value, basestring) and stringIsLinkRe.match(value)
|
||||
return isinstance(value, pyCompatibility.basestring) and stringIsLinkRe.match(value)
|
||||
|
||||
|
||||
def isCollection(v):
|
||||
return isinstance(v, collections.Iterable) and not isinstance(v, basestring)
|
||||
return isinstance(v, collections.Iterable) and not isinstance(v, pyCompatibility.basestring)
|
||||
|
||||
@contextmanager
|
||||
def GraphModification(graph):
|
||||
|
@ -184,7 +170,7 @@ class Attribute(BaseObject):
|
|||
if self._value == value:
|
||||
return
|
||||
|
||||
if isinstance(value, Attribute) or (isinstance(value, basestring) and isLink(value)):
|
||||
if isinstance(value, Attribute) or (isinstance(value, pyCompatibility.basestring) and isLink(value)):
|
||||
# if we set a link to another attribute
|
||||
self._value = value
|
||||
else:
|
||||
|
@ -250,7 +236,7 @@ class Attribute(BaseObject):
|
|||
if isinstance(v, Attribute):
|
||||
g.addEdge(v, self)
|
||||
self._value = ""
|
||||
elif self.isInput and isinstance(v, basestring) and isLink(v):
|
||||
elif self.isInput and isinstance(v, pyCompatibility.basestring) and isLink(v):
|
||||
# value is a link to another attribute
|
||||
link = v[1:-1]
|
||||
linkNode, linkAttr = link.split('.')
|
||||
|
@ -266,7 +252,7 @@ class Attribute(BaseObject):
|
|||
|
||||
def getValueStr(self):
|
||||
if isinstance(self.attributeDesc, desc.ChoiceParam) and not self.attributeDesc.exclusive:
|
||||
assert(isinstance(self.value, collections.Sequence) and not isinstance(self.value, basestring))
|
||||
assert(isinstance(self.value, collections.Sequence) and not isinstance(self.value, pyCompatibility.basestring))
|
||||
return self.attributeDesc.joinChar.join(self.value)
|
||||
if isinstance(self.attributeDesc, (desc.StringParam, desc.File)):
|
||||
return '"{}"'.format(self.value)
|
||||
|
|
15
meshroom/core/pyCompatibility.py
Normal file
15
meshroom/core/pyCompatibility.py
Normal file
|
@ -0,0 +1,15 @@
|
|||
|
||||
try:
|
||||
unicode = unicode
|
||||
except NameError:
|
||||
# 'unicode' is undefined, must be Python 3
|
||||
str = str
|
||||
unicode = str
|
||||
bytes = bytes
|
||||
basestring = (str, bytes)
|
||||
else:
|
||||
# 'unicode' exists, must be Python 2
|
||||
str = str
|
||||
unicode = unicode
|
||||
bytes = str
|
||||
basestring = basestring
|
|
@ -1,7 +1,10 @@
|
|||
#!/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
|
||||
|
||||
|
||||
|
@ -24,7 +27,7 @@ class FilepathHelper(QObject):
|
|||
Returns:
|
||||
str: String representation of 'path'
|
||||
"""
|
||||
if not isinstance(path, (QUrl, basestring)):
|
||||
if not isinstance(path, (QUrl, pyCompatibility.basestring)):
|
||||
raise TypeError("Unexpected data type: {}".format(path.__class__))
|
||||
if isinstance(path, QUrl):
|
||||
path = path.toLocalFile()
|
||||
|
|
Loading…
Add table
Reference in a new issue