Commit 7f2b5c38 authored by payno's avatar payno

modifications for the representation example

parent c83fff2a
......@@ -32,6 +32,8 @@ import logging
import traceback
from collections import namedtuple
import inspect
import importlib
from importlib.machinery import SourceFileLoader
_logger = logging.getLogger(__file__)
......@@ -97,17 +99,24 @@ class Node(object):
"""Set of downstream nodes"""
self.__process_instance = None
""""""
self.callback = callback
self._process_pt = callback
"""process instance"""
self._handlers = {}
"""handlers with link name as key and callback as value.
The default handler is store under the 'None' value"""
self._error_handler = error_handler
self.outData = None
@property
def callback(self):
def handlers(self):
return self._handlers
@property
def _process_pt(self):
return self.__callback
@callback.setter
def callback(self, callback):
@_process_pt.setter
def _process_pt(self, callback):
need_instanciation = type(callback) is str or inspect.isclass(callback)
self.__callback = _callback_info(callback=callback, handler=None,
need_instanciation=need_instanciation)
......@@ -118,6 +127,57 @@ class Node(object):
def isstart(self):
return len(self.upstream_nodes) is 0
def load_handlers(self):
self._handlers.clear()
assert self._process_pt is not None
if callable(self._process_pt):
self.__process_instance = self._process_pt
self._handlers[None] = self._process_pt
else:
if not type(self._process_pt) is str:
raise ValueError('process_pt should be a callable or path to a class or function')
else:
sname = self._process_pt.rsplit('.')
if not (len(sname) > 1):
raise ValueError('Invalid name')
class_name = sname[-1]
del sname[-1]
module_name = '.'.join(sname)
if module_name.endswith('.py'):
# warning: in this case the file should not have any relative
# import
module = SourceFileLoader(module_name,
module_name).load_module()
else:
module = importlib.import_module(module_name)
class_ = getattr(module, class_name)
self.__process_instance = class_()
if callable(self.__process_instance):
self._handlers[None] = self.__process_instance
# manage the case where a class has several input handler
if hasattr(self.__process_instance, 'inputs'):
for input_ in self.__process_instance.inputs:
input_name, input_type, input_handler = input_
self._handlers[input_name] = input_handler
if len(self._handlers) == 0:
raise ValueError('Fail to init handlers, none defined for ' + str(self._process_pt))
@staticmethod
def exec_process(process_pt, properties, input_name, input_data):
node = Node(callback=process_pt, properties=properties)
if input_name in node.handlers:
out = node.handlers[input_name](input_data)
elif None in node.handlers:
out = node.handlers[None](input_data)
else:
raise KeyError(input_name, 'is not managed by', str(node._process_pt))
if hasattr(out, 'to_dict'):
return out.to_dict()
else:
return out
class WorkflowException(Exception):
def __init__(self, traceBack="", data=None, msg=None):
......
......@@ -5,7 +5,8 @@ __date__ = "29/05/2017"
from xml.etree.ElementTree import parse
from .parser import _scheme, _node, _link
from .parser import _scheme, _node, _link, _data, _annotation, _text_params, _arrow_params
from ast import literal_eval
import ast
import logging
from .scheme import Scheme
......@@ -25,6 +26,8 @@ class OwsParser(Parser):
@staticmethod
def parse_ows_etree_v_2_0(tree):
aliases = Parser.get_aliases()
print('aliases are: ', aliases)
scheme = tree.getroot()
nodes, links, annotations = [], [], []
......@@ -42,13 +45,18 @@ class OwsParser(Parser):
# Collect all nodes
for node in tree.findall("nodes/node"):
node_id = node.get("id")
qualified_name = node.get("qualified_name")
if qualified_name in aliases:
logger.info('replace', qualified_name, 'by', aliases[qualified_name])
qualified_name = aliases[qualified_name]
node = _node(
id=node_id,
title=node.get("title"),
name=node.get("name"),
position=tuple_eval(node.get("position", None)),
project_name=node.get("project_name", None),
qualified_name=node.get("qualified_name"),
qualified_name=qualified_name,
version=node.get("version", ""),
data=properties.get(node_id, None)
)
......@@ -112,7 +120,7 @@ class OwsParser(Parser):
if scheme_el.find("widgets") is not None:
version = "1.0"
else:
log.warning("<scheme> tag does not have a 'version' attribute")
logger.warning("<scheme> tag does not have a 'version' attribute")
version = "2.0"
if version == "1.0":
......
......@@ -28,6 +28,10 @@ __license__ = "MIT"
__date__ = "19/08/2019"
from collections import namedtuple
import importlib
import logging
_logger = logging.getLogger(__name__)
class Parser(object):
......@@ -40,6 +44,29 @@ class Parser(object):
"""
raise NotImplementedError()
@staticmethod
def get_aliases():
aliases = {}
try:
import pypushflowaddon
except ImportError:
return aliases
else:
import pkgutil
for importer, modname, ispkg in pkgutil.iter_modules(pypushflowaddon.__path__):
try:
mod_name = '.'.join((pypushflowaddon.__name__, modname, 'aliases'))
module = importlib.import_module(mod_name)
except ImportError:
_logger.warning(modname + ' does not fit the add-on design, skip it')
else:
if hasattr(module, 'aliases'):
new_aliases = getattr(module, 'aliases')
if not isinstance(new_aliases, dict):
raise TypeError('aliases should be an instance of dict')
else:
aliases.update(new_aliases)
__main__ = scheme_load
......
......@@ -274,6 +274,10 @@ class Scheme(object):
return scheme
def load_handlers(self):
for node in self.nodes:
node.load_handlers()
def contains_control_nodes(nodes_list):
for _node in nodes_list:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment