code formatting

* auto formatting based on pep8 recommendations
* """ for docstrings
This commit is contained in:
Yann Lanthony 2017-09-19 12:04:48 +02:00
parent 7e1a66663a
commit 5bfb55ddd3
4 changed files with 123 additions and 101 deletions

View file

@ -8,18 +8,19 @@ import sys
def trim(s):
'''
"""
All repetition of any kind of space is replaced by a single space
and remove trailing space at beginning or end.
'''
"""
# regex to replace all space groups by a single space
# use split() to remove trailing space at beginning/end
return re.sub('\s+', ' ', s).strip()
def quotesForStrings(valueStr):
'''
"""
Return the input string with quotes if it cannot be cast into another builtin type.
'''
"""
v = valueStr
try:
int(valueStr)
@ -52,6 +53,7 @@ if sys.stdin.isatty():
inputCmdLineDoc = ''.join([line for line in sys.stdin])
def convertToLabel(name):
camelCaseToLabel = re.sub('(.)([A-Z][a-z]+)', r'\1 \2', name)
snakeToLabel = ' '.join(word.capitalize() for word in camelCaseToLabel.split('_'))

View file

@ -1,5 +1,6 @@
from meshroom.processGraph import desc
class AppendText(desc.CommandLineNode):
commandLine = 'cat {inputValue} > {outputValue} && echo {inputTextValue} >> {outputValue}'
input = desc.FileAttribute(

View file

@ -1,37 +1,34 @@
import inspect
import os
class Attribute:
'''
'''
"""
"""
isOutput = False
uid = []
group = 'allParams'
commandLine = '{nodeType} --help' # need to be overridden
def __init__(self):
pass
def __init__(self, **kwargs):
for k, v in kwargs.items():
setattr(self, k, v)
class FileAttribute(Attribute):
'''
'''
"""
"""
def __init__(self, **kwargs):
for k, v in kwargs.items():
setattr(self, k, v)
super(FileAttribute, self).__init__(**kwargs)
class ParamAttribute(Attribute):
'''
'''
"""
"""
def __init__(self, **kwargs):
for k, v in kwargs.items():
setattr(self, k, v)
super(ParamAttribute, self).__init__(**kwargs)
class Node:
'''
'''
"""
"""
internalFolder = '{nodeType}/{uid0}/'
def __init__(self):
@ -39,7 +36,5 @@ class Node:
class CommandLineNode(Node):
'''
'''
def __init__(self):
pass
"""
"""

View file

@ -17,14 +17,17 @@ from meshroom import processGraph as pg
# Replace default encoder to support Enums
DefaultJSONEncoder = json.JSONEncoder # store the original one
class MyJSONEncoder(DefaultJSONEncoder): # declare a new one with Enum support
def default(self, obj):
if isinstance(obj, Enum):
return obj.name
return DefaultJSONEncoder.default(self, obj) # use the default one for all other types
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:
@ -47,28 +50,30 @@ def hash(v):
class Attribute:
'''
'''
"""
"""
def __init__(self, name, node, attributeDesc):
self.attrName = name
self.node = node
self._value = attributeDesc.__dict__.get('value', None)
self.attributeDesc = attributeDesc
def aboluteName(self):
def absoluteName(self):
return '{}.{}.{}'.format(self.node.graph.name, self.node.name, self.attrName)
def name(self):
'''
"""
Name inside the Graph.
'''
"""
return '{}.{}'.format(self.node.name, self.attrName)
def uid(self):
'''
'''
"""
"""
if self.attributeDesc.isOutput:
# only dependent of the linked node uid, so it is independant of the cache folder which may be used in the filepath.
# only dependent of the linked node uid, so it is independent
# from the cache folder which may be used in the filepath.
return self.node.uid()
if self.isLink():
return self.node.graph.edges[self].uid()
@ -77,9 +82,9 @@ class Attribute:
return hash(self._value)
def isLink(self):
'''
"""
If the attribute is a link to another attribute.
'''
"""
if self.attributeDesc.isOutput:
return False
else:
@ -91,11 +96,11 @@ class Attribute:
return self.node.graph.edges[self]
def _applyExpr(self):
'''
"""
For string parameters with an expression (when loaded from file),
this function convert the expression into a real edge in the graph
and clear the string value.
'''
"""
v = self._value
if isinstance(v, basestring) and len(v) > 2 and v[0] == '{' and v[-1] == '}':
# value is a link to another attribute
@ -114,8 +119,8 @@ class Attribute:
class Status(Enum):
'''
'''
"""
"""
NONE = 1
SUBMITTED_EXTERN = 2
SUBMITTED_LOCAL = 3
@ -125,8 +130,9 @@ class Status(Enum):
class Statistics:
'''
'''
"""
"""
def __init__(self):
self.duration = 0 # computation time set at the end of the execution
self.cpuUsage = []
@ -138,15 +144,18 @@ class Statistics:
self.vramAvailable = 0 # GB
self.swapUsage = []
self.swapAvailable = 0
def toDict(self):
return self.__dict__
def fromDict(self, d):
self.__dict__ = d
class StatusData:
'''
'''
"""
"""
def __init__(self, nodeName, nodeType):
self.status = Status.NONE
self.nodeName = nodeName
@ -167,8 +176,8 @@ class StatusData:
bytesPerGiga = 1024. * 1024. * 1024.
class StatisticsThread(threading.Thread):
class StatisticsThread(threading.Thread):
def __init__(self, node):
threading.Thread.__init__(self)
self.node = node
@ -206,8 +215,8 @@ class StatisticsThread(threading.Thread):
class Node:
'''
'''
"""
"""
name = None
graph = None
@ -256,7 +265,8 @@ class Node:
return self.nodeUid
def _updateUid(self):
hashInputParams = [(attr.attrName, attr.uid()) for attr in self.attributes.values() if not attr.attributeDesc.isOutput]
hashInputParams = [(attr.attrName, attr.uid()) for attr in self.attributes.values() if
not attr.attributeDesc.isOutput]
hashInputParams.sort()
self.nodeUid = hash(tuple([b for a, b in hashInputParams]))
return self.nodeUid
@ -295,7 +305,8 @@ class Node:
self._cmdVars[name] = '--{name} {value}'.format(name=name, value=v)
self._cmdVars[name + 'Value'] = str(v)
self._cmdVars[attr.attributeDesc.group] = self._cmdVars.get(attr.attributeDesc.group, '') + ' ' + self._cmdVars[name]
self._cmdVars[attr.attributeDesc.group] = self._cmdVars.get(attr.attributeDesc.group, '') + ' ' + \
self._cmdVars[name]
def internalFolder(self):
return self.nodeDesc.internalFolder.format(nodeType=self.nodeType(), **self._cmdVars)
@ -310,9 +321,9 @@ class Node:
return os.path.join(pg.cacheFolder, self.internalFolder(), 'log')
def updateStatusFromCache(self):
'''
"""
Need up-to-date UIDs.
'''
"""
statusFile = self.statusFile()
if not os.path.exists(statusFile):
self.status.status = Status.NONE
@ -322,9 +333,9 @@ class Node:
self.status.fromDict(statusData)
def saveStatusFile(self):
'''
"""
Need up-to-date UIDs.
'''
"""
data = self.status.toDict()
statusFilepath = self.statusFile()
folder = os.path.dirname(statusFilepath)
@ -337,7 +348,8 @@ class Node:
def upgradeStatusTo(self, newStatus):
if int(newStatus.value) <= int(self.status.status.value):
print('WARNING: downgrade status on node "{}" from {} to {}'.format(self.name, self.status.status.name, newStatus))
print('WARNING: downgrade status on node "{}" from {} to {}'.format(self.name, self.status.status.name,
newStatus))
self.status.status = newStatus
self.saveStatusFile()
@ -368,10 +380,12 @@ class Node:
def endSequence(self):
pass
WHITE = 0
GRAY = 1
BLACK = 2
class Visitor:
# def initializeVertex(self, s, g):
# '''is invoked on every vertex of the graph before the start of the graph search.'''
@ -380,8 +394,9 @@ class Visitor:
# '''is invoked on the source vertex once before the start of the search.'''
# pass
def discoverVertex(self, u, g):
'''is invoked when a vertex is encountered for the first time.'''
""" is invoked when a vertex is encountered for the first time. """
pass
# def examineEdge(self, e, g):
# '''is invoked on every out-edge of each vertex after it is discovered.'''
# pass
@ -398,12 +413,13 @@ class Visitor:
# '''is invoked on the non-tree edges in the graph as well as on each tree edge after its target vertex is finished.'''
# pass
def finishVertex(self, u, g):
'''is invoked on a vertex after all of its out edges have been added to the search tree and all of the adjacent vertices have been discovered (but before their out-edges have been examined).'''
""" is invoked on a vertex after all of its out edges have been added to the search tree and all of the
adjacent vertices have been discovered (but before their out-edges have been examined). """
pass
class Graph:
'''
"""
_________________ _________________ _________________
| | | | | |
| Node A | | Node B | | Node C |
@ -416,8 +432,8 @@ class Graph:
nodes = {'A': <nodeA>, 'B': <nodeB>, 'C': <nodeC>}
edges = {B.input: A.output, C.input: B.output,}
"""
'''
def __init__(self, name):
self.name = name
self.nodes = {}
@ -425,10 +441,11 @@ class Graph:
def addNode(self, node, uniqueName=None):
if node.graph is not None and node.graph != self:
raise RuntimeError('Node "{}" cannot be part of the Graph "{}", as it is already part of the other graph "{}".'.format(
raise RuntimeError(
'Node "{}" cannot be part of the Graph "{}", as it is already part of the other graph "{}".'.format(
node.nodeType(), self.name, node.graph.name))
if uniqueName:
assert(uniqueName not in self.nodes)
assert uniqueName not in self.nodes
node.name = uniqueName
else:
node.name = self._createUniqueNodeName(node.nodeType())
@ -437,8 +454,15 @@ class Graph:
return node
def addNewNode(self, nodeName, **kwargs):
return self.addNode(Node(nodeDesc=nodeName, **kwargs))
def addNewNode(self, nodeType, **kwargs):
"""
:param nodeType:
:param kwargs:
:return:
:rtype: Node
"""
return self.addNode(Node(nodeDesc=nodeType, **kwargs))
def _createUniqueNodeName(self, inputName):
i = 1
@ -449,13 +473,13 @@ class Graph:
i += 1
def getLeaves(self):
nodesWithOuput = set([outputAttr.node for outputAttr in self.edges.values()])
return set(self.nodes.values()) - nodesWithOuput
nodesWithOutput = set([outputAttr.node for outputAttr in self.edges.values()])
return set(self.nodes.values()) - nodesWithOutput
def addEdge(self, outputAttr, inputAttr):
assert(isinstance(outputAttr, Attribute))
assert(isinstance(inputAttr, Attribute))
if(outputAttr.node.graph != self or inputAttr.node.graph != self):
assert isinstance(outputAttr, Attribute)
assert isinstance(inputAttr, Attribute)
if outputAttr.node.graph != self or inputAttr.node.graph != self:
raise RuntimeError('The attributes of the edge should be part of a common graph.')
if inputAttr in self.edges:
raise RuntimeError('Input attribute "{}" is already connected.'.format(inputAttr.fullName()))
@ -477,28 +501,28 @@ class Graph:
return nodeEdges
def dfs(self, visitor, startNodes=None):
nodeChildrens = self._getNodeEdges()
nodeChildren = self._getNodeEdges()
colors = {}
for u in self.nodes.values():
colors[u] = WHITE
time = 0
if startNodes:
for startNode in startNodes:
self.dfsVisit(startNode, visitor, colors, nodeChildrens)
self.dfsVisit(startNode, visitor, colors, nodeChildren)
else:
leaves = self.getLeaves()
for u in leaves:
if colors[u] == WHITE:
self.dfsVisit(u, visitor, colors, nodeChildrens)
self.dfsVisit(u, visitor, colors, nodeChildren)
def dfsVisit(self, u, visitor, colors, nodeChildrens):
def dfsVisit(self, u, visitor, colors, nodeChildren):
colors[u] = GRAY
visitor.discoverVertex(u, self)
# d_time[u] = time = time + 1
for v in nodeChildrens[u]:
for v in nodeChildren[u]:
if colors[v] == WHITE:
# (u,v) is a tree edge
self.dfsVisit(v, visitor, colors, nodeChildrens) # TODO: avoid recursion
self.dfsVisit(v, visitor, colors, nodeChildren) # TODO: avoid recursion
elif colors[v] == GRAY:
pass # (u,v) is a back edge
elif colors[v] == BLACK:
@ -516,6 +540,7 @@ class Graph:
def dfsNodesToProcess(self, startNodes=None):
nodes = []
visitor = Visitor()
def finishVertex(vertex, graph):
if vertex.status.status in (Status.SUBMITTED_EXTERN,
Status.SUBMITTED_LOCAL):
@ -524,6 +549,7 @@ class Graph:
print('WARNING: node "{}" is already running.'.format(vertex.name))
if vertex.status.status is not Status.SUCCESS:
nodes.append(vertex)
visitor.finishVertex = finishVertex
self.dfs(visitor=visitor, startNodes=startNodes)
return nodes
@ -536,8 +562,8 @@ class Graph:
return {k: node.toDict() for k, node in self.nodes.items()}
def save(self, filepath):
'''
'''
"""
"""
data = self.toDict()
pprint(data)
with open(filepath, 'w') as jsonFile:
@ -558,9 +584,8 @@ class Graph:
def loadGraph(filepath):
'''
'''
graphData = None
"""
"""
with open(filepath) as jsonFile:
graphData = json.load(jsonFile)
if not isinstance(graphData, dict):
@ -577,8 +602,8 @@ def loadGraph(filepath):
def execute(graph, startNodes=None, force=False):
'''
'''
"""
"""
if force:
nodes = graph.dfsNodesOnFinish(startNodes=startNodes)
else:
@ -597,4 +622,3 @@ def execute(graph, startNodes=None, force=False):
for node in nodes:
node.endSequence()