mirror of
https://github.com/alicevision/Meshroom.git
synced 2025-05-31 09:56:32 +02:00
Remove workarounds and backports for Python 2 support
In particular: - In common/, remove the backport of weakref.WeakMethod - In docs/ and ui/, use the standard FileNotFoundError class - Use built-in open() instead of io.open(), and stop importing io - In core/stats.py, use the standard implementation of xml.etree instead of the C one
This commit is contained in:
parent
635f85e7fd
commit
8bef398bed
9 changed files with 10 additions and 88 deletions
|
@ -144,9 +144,8 @@ with multiview.GraphModification(graph):
|
||||||
publish.output.value = args.output
|
publish.output.value = args.output
|
||||||
|
|
||||||
if args.overrides:
|
if args.overrides:
|
||||||
import io
|
|
||||||
import json
|
import json
|
||||||
with io.open(args.overrides, 'r', encoding='utf-8', errors='ignore') as f:
|
with open(args.overrides, 'r', encoding='utf-8', errors='ignore') as f:
|
||||||
data = json.load(f)
|
data = json.load(f)
|
||||||
for nodeName, overrides in data.items():
|
for nodeName, overrides in data.items():
|
||||||
for attrName, value in overrides.items():
|
for attrName, value in overrides.items():
|
||||||
|
|
|
@ -17,12 +17,6 @@ from docutils.nodes import SparseNodeVisitor
|
||||||
from docutils.parsers.rst import Directive
|
from docutils.parsers.rst import Directive
|
||||||
from utils import md_to_docutils, get_link_key
|
from utils import md_to_docutils, get_link_key
|
||||||
|
|
||||||
# Python2 compatibility
|
|
||||||
try:
|
|
||||||
FileNotFoundError
|
|
||||||
except NameError:
|
|
||||||
FileNotFoundError = IOError
|
|
||||||
|
|
||||||
|
|
||||||
class Relinker(SparseNodeVisitor):
|
class Relinker(SparseNodeVisitor):
|
||||||
|
|
||||||
|
|
|
@ -13,60 +13,8 @@ import inspect
|
||||||
import sys
|
import sys
|
||||||
import weakref
|
import weakref
|
||||||
from functools import partial
|
from functools import partial
|
||||||
|
|
||||||
|
|
||||||
# weakref.WeakMethod backport
|
|
||||||
try:
|
|
||||||
from weakref import WeakMethod
|
from weakref import WeakMethod
|
||||||
|
|
||||||
except ImportError:
|
|
||||||
import types
|
|
||||||
|
|
||||||
class WeakMethod(object):
|
|
||||||
"""Light WeakMethod backport compiled from various sources. Tested in 2.7"""
|
|
||||||
|
|
||||||
def __init__(self, func):
|
|
||||||
if inspect.ismethod(func):
|
|
||||||
self._obj = weakref.ref(func.__self__)
|
|
||||||
self._func = weakref.ref(func.__func__)
|
|
||||||
|
|
||||||
else:
|
|
||||||
self._obj = None
|
|
||||||
|
|
||||||
try:
|
|
||||||
self._func = weakref.ref(func.__func__)
|
|
||||||
|
|
||||||
# Rather than attempting to handle this, raise the same exception
|
|
||||||
# you get from WeakMethod.
|
|
||||||
except AttributeError:
|
|
||||||
raise TypeError("argument should be a bound method, not %s" % type(func))
|
|
||||||
|
|
||||||
def __call__(self):
|
|
||||||
if self._obj is not None:
|
|
||||||
obj = self._obj()
|
|
||||||
func = self._func()
|
|
||||||
if func is None or obj is None:
|
|
||||||
return None
|
|
||||||
|
|
||||||
else:
|
|
||||||
return types.MethodType(func, obj, obj.__class__)
|
|
||||||
|
|
||||||
elif self._func is not None:
|
|
||||||
return self._func()
|
|
||||||
|
|
||||||
else:
|
|
||||||
return None
|
|
||||||
|
|
||||||
def __eq__(self, other):
|
|
||||||
try:
|
|
||||||
return type(self) is type(other) and self() == other()
|
|
||||||
|
|
||||||
except Exception:
|
|
||||||
return False
|
|
||||||
|
|
||||||
def __ne__(self, other):
|
|
||||||
return not self.__eq__(other)
|
|
||||||
|
|
||||||
|
|
||||||
class Signal(object):
|
class Signal(object):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
from meshroom.common import BaseObject, Property, Variant, VariantList, JSValue
|
from meshroom.common import BaseObject, Property, Variant, VariantList, JSValue
|
||||||
|
|
||||||
from collections.abc import Iterable
|
from collections.abc import Iterable
|
||||||
from enum import Enum # available by default in python3. For python2: "pip install enum34"
|
from enum import Enum
|
||||||
import math
|
import math
|
||||||
import os
|
import os
|
||||||
import psutil
|
import psutil
|
||||||
|
@ -254,9 +254,7 @@ class IntParam(Param):
|
||||||
def validateValue(self, value):
|
def validateValue(self, value):
|
||||||
# handle unsigned int values that are translated to int by shiboken and may overflow
|
# handle unsigned int values that are translated to int by shiboken and may overflow
|
||||||
try:
|
try:
|
||||||
return long(value) # Python 2
|
return int(value)
|
||||||
except NameError:
|
|
||||||
return int(value) # Python 3
|
|
||||||
except:
|
except:
|
||||||
raise ValueError('IntParam only supports int value (param:{}, value:{}, type:{})'.format(self.name, value, type(value)))
|
raise ValueError('IntParam only supports int value (param:{}, value:{}, type:{})'.format(self.name, value, type(value)))
|
||||||
|
|
||||||
|
|
|
@ -510,8 +510,7 @@ class BaseNode(BaseObject):
|
||||||
def __getattr__(self, k):
|
def __getattr__(self, k):
|
||||||
try:
|
try:
|
||||||
# Throws exception if not in prototype chain
|
# Throws exception if not in prototype chain
|
||||||
# return object.__getattribute__(self, k) # doesn't work in python2
|
return object.__getattribute__(self, k)
|
||||||
return object.__getattr__(self, k)
|
|
||||||
except AttributeError as e:
|
except AttributeError as e:
|
||||||
try:
|
try:
|
||||||
return self.attribute(k)
|
return self.attribute(k)
|
||||||
|
|
|
@ -8,10 +8,6 @@ import platform
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
if sys.version_info[0] == 2:
|
|
||||||
# On Python 2 use C implementation for performance and to avoid lots of warnings
|
|
||||||
from xml.etree import cElementTree as ET
|
|
||||||
else:
|
|
||||||
import xml.etree.ElementTree as ET
|
import xml.etree.ElementTree as ET
|
||||||
|
|
||||||
|
|
||||||
|
@ -95,10 +91,6 @@ class ComputerStatistics:
|
||||||
return
|
return
|
||||||
try:
|
try:
|
||||||
p = subprocess.Popen([self.nvidia_smi, "-q", "-x"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
p = subprocess.Popen([self.nvidia_smi, "-q", "-x"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||||
if sys.version_info[0] == 2:
|
|
||||||
# no timeout in python-2
|
|
||||||
xmlGpu, stdError = p.communicate()
|
|
||||||
else:
|
|
||||||
xmlGpu, stdError = p.communicate(timeout=10) # 10 seconds
|
xmlGpu, stdError = p.communicate(timeout=10) # 10 seconds
|
||||||
|
|
||||||
smiTree = ET.fromstring(xmlGpu)
|
smiTree = ET.fromstring(xmlGpu)
|
||||||
|
|
|
@ -348,7 +348,7 @@ class TaskManager(BaseObject):
|
||||||
raise ValueError("Argument 'context' must be: 'COMPUTATION' or 'SUBMITTING'")
|
raise ValueError("Argument 'context' must be: 'COMPUTATION' or 'SUBMITTING'")
|
||||||
|
|
||||||
if len(ready) + len(computed) != len(toNodes):
|
if len(ready) + len(computed) != len(toNodes):
|
||||||
del toNodes[:] # for python 2 compatibility, else use: toNodes.clear()
|
toNodes.clear()
|
||||||
toNodes.extend(ready)
|
toNodes.extend(ready)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
|
@ -94,9 +94,8 @@ def readSfMData(sfmFile):
|
||||||
Returns:
|
Returns:
|
||||||
The views and intrinsics of the .sfm as two separate lists
|
The views and intrinsics of the .sfm as two separate lists
|
||||||
"""
|
"""
|
||||||
import io # use io.open for Python2/3 compatibility (allow to specify encoding + errors handling)
|
|
||||||
# skip decoding errors to avoid potential exceptions due to non utf-8 characters in images metadata
|
# skip decoding errors to avoid potential exceptions due to non utf-8 characters in images metadata
|
||||||
with io.open(sfmFile, 'r', encoding='utf-8', errors='ignore') as f:
|
with open(sfmFile, 'r', encoding='utf-8', errors='ignore') as f:
|
||||||
data = json.load(f)
|
data = json.load(f)
|
||||||
|
|
||||||
intrinsicsKeys = [i.name for i in Intrinsic]
|
intrinsicsKeys = [i.name for i in Intrinsic]
|
||||||
|
|
|
@ -17,12 +17,6 @@ from meshroom.core.node import Node, CompatibilityNode, Status, Position
|
||||||
from meshroom.ui.graph import UIGraph
|
from meshroom.ui.graph import UIGraph
|
||||||
from meshroom.ui.utils import makeProperty
|
from meshroom.ui.utils import makeProperty
|
||||||
|
|
||||||
# Python2 compatibility
|
|
||||||
try:
|
|
||||||
FileNotFoundError
|
|
||||||
except NameError:
|
|
||||||
FileNotFoundError = IOError
|
|
||||||
|
|
||||||
|
|
||||||
class Message(QObject):
|
class Message(QObject):
|
||||||
""" Simple structure wrapping a high-level message. """
|
""" Simple structure wrapping a high-level message. """
|
||||||
|
@ -623,9 +617,8 @@ class Reconstruction(UIGraph):
|
||||||
sfmFile = panoramaInit.attribute('outSfMData').value
|
sfmFile = panoramaInit.attribute('outSfMData').value
|
||||||
if not os.path.exists(sfmFile):
|
if not os.path.exists(sfmFile):
|
||||||
return QVector3D(0.0, 0.0, 0.0)
|
return QVector3D(0.0, 0.0, 0.0)
|
||||||
import io # use io.open for Python2/3 compatibility (allow to specify encoding + errors handling)
|
|
||||||
# skip decoding errors to avoid potential exceptions due to non utf-8 characters in images metadata
|
# skip decoding errors to avoid potential exceptions due to non utf-8 characters in images metadata
|
||||||
with io.open(sfmFile, 'r', encoding='utf-8', errors='ignore') as f:
|
with open(sfmFile, 'r', encoding='utf-8', errors='ignore') as f:
|
||||||
data = json.load(f)
|
data = json.load(f)
|
||||||
|
|
||||||
intrinsics = data.get('intrinsics', [])
|
intrinsics = data.get('intrinsics', [])
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue