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.common import BaseObject, Property, Variant
|
||||||
|
from meshroom.core import pyCompatibility
|
||||||
from enum import Enum # available by default in python3. For python2: "pip install enum34"
|
from enum import Enum # available by default in python3. For python2: "pip install enum34"
|
||||||
import collections
|
import collections
|
||||||
import math
|
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)
|
super(File, self).__init__(name=name, label=label, description=description, value=value, uid=uid, group=group)
|
||||||
|
|
||||||
def validateValue(self, value):
|
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)))
|
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 ''
|
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)
|
super(StringParam, self).__init__(name=name, label=label, description=description, value=value, uid=uid, group=group)
|
||||||
|
|
||||||
def validateValue(self, value):
|
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)))
|
raise ValueError('StringParam value should be a string (param:{}, value:{}, type:{})'.format(self.name, value, type(value)))
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
|
@ -21,6 +21,7 @@ from . import desc
|
||||||
import meshroom.core
|
import meshroom.core
|
||||||
from meshroom.common import BaseObject, DictModel, Slot, Signal, Property, Variant, ListModel
|
from meshroom.common import BaseObject, DictModel, Slot, Signal, Property, Variant, ListModel
|
||||||
from meshroom.core.exception import UnknownNodeTypeError
|
from meshroom.core.exception import UnknownNodeTypeError
|
||||||
|
from meshroom.core import pyCompatibility
|
||||||
|
|
||||||
# Replace default encoder to support Enums
|
# Replace default encoder to support Enums
|
||||||
DefaultJSONEncoder = json.JSONEncoder # store the original one
|
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
|
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_.]*\}$')
|
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.
|
Return whether the given argument is a link expression.
|
||||||
A link expression is a string matching the {nodeName.attrName} pattern.
|
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):
|
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
|
@contextmanager
|
||||||
def GraphModification(graph):
|
def GraphModification(graph):
|
||||||
|
@ -184,7 +170,7 @@ class Attribute(BaseObject):
|
||||||
if self._value == value:
|
if self._value == value:
|
||||||
return
|
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
|
# if we set a link to another attribute
|
||||||
self._value = value
|
self._value = value
|
||||||
else:
|
else:
|
||||||
|
@ -250,7 +236,7 @@ class Attribute(BaseObject):
|
||||||
if isinstance(v, Attribute):
|
if isinstance(v, Attribute):
|
||||||
g.addEdge(v, self)
|
g.addEdge(v, self)
|
||||||
self._value = ""
|
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
|
# value is a link to another attribute
|
||||||
link = v[1:-1]
|
link = v[1:-1]
|
||||||
linkNode, linkAttr = link.split('.')
|
linkNode, linkAttr = link.split('.')
|
||||||
|
@ -266,7 +252,7 @@ class Attribute(BaseObject):
|
||||||
|
|
||||||
def getValueStr(self):
|
def getValueStr(self):
|
||||||
if isinstance(self.attributeDesc, desc.ChoiceParam) and not self.attributeDesc.exclusive:
|
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)
|
return self.attributeDesc.joinChar.join(self.value)
|
||||||
if isinstance(self.attributeDesc, (desc.StringParam, desc.File)):
|
if isinstance(self.attributeDesc, (desc.StringParam, desc.File)):
|
||||||
return '"{}"'.format(self.value)
|
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
|
#!/usr/bin/env python
|
||||||
# coding:utf-8
|
# coding:utf-8
|
||||||
|
from meshroom.core import pyCompatibility
|
||||||
|
|
||||||
from PySide2.QtCore import QUrl
|
from PySide2.QtCore import QUrl
|
||||||
from PySide2.QtCore import QObject, Slot
|
from PySide2.QtCore import QObject, Slot
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
@ -24,7 +27,7 @@ class FilepathHelper(QObject):
|
||||||
Returns:
|
Returns:
|
||||||
str: String representation of 'path'
|
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__))
|
raise TypeError("Unexpected data type: {}".format(path.__class__))
|
||||||
if isinstance(path, QUrl):
|
if isinstance(path, QUrl):
|
||||||
path = path.toLocalFile()
|
path = path.toLocalFile()
|
||||||
|
|
Loading…
Add table
Reference in a new issue