Exemple #1
0
class ToString(Actor):
    """
    Transform data to JSON-string

    Exception tokens will produce "null" as output unless another value is supplied
    through the optional 'exception_output' argument.

    Inputs:
      data : any kind of token
    Outputs:
      string : JSON-formatted string
    """

    def exception_handler(self, action, args):
        return (self.json.tostring(self.default),)

    @manage(['default'])
    def init(self, exception_output=None):
        self.default = exception_output
        self.setup()

    def did_migrate(self):
        self.setup()

    def setup(self):
        self.json = calvinlib.use("json")


    @condition(['data'], ['string'])
    def dump(self, value):
        return (self.json.tostring(value),)

    action_priority = (dump,)
    requires = ['json']


    test_set = [
        {
            'inports': {'data': [1]},
            'outports': {'string': ['1']},
        },
        {
            'inports': {'data': [{"a": 1}]},
            'outports': {'string': ['{"a": 1}']},
        },
        {
            'inports': {'data': [EOSToken()]},
            'outports': {'string': ['null']},
        },
        {
            'inports': {'data': [ExceptionToken()]},
            'outports': {'string': ['null']},
        },
        {
            'setup': [lambda self: self.init(exception_output={})],
            'inports': {'data': [ExceptionToken()]},
            'outports': {'string': ['{}']},
        },
    ]
Exemple #2
0
    def testPeek_Exception(self):
        self.setup_writers(3)

        for i in [1, 2, 3]:
            self.inport.write(Token("data-%d" % i), "writer-%d" % i)
        self.inport.write(Token("data-%d" % (1 + 3)), "writer-%d" % 1)
        self.inport.write(ExceptionToken(), "writer-%d" % 2)
        self.inport.write(Token("data-%d" % (3 + 3)), "writer-%d" % 3)
        for i in [1, 2, 3]:
            self.inport.write(Token("data-%d" % (i + 6)), "writer-%d" % i)
        """
            w1: 1 4 7
            w2: 2 e 8
            w3: 3 6 9
        """
        data_1 = self.inport.peek(None).value
        self.assertEqual(data_1,
                         {"writer-%d" % i: "data-%d" % i
                          for i in [1, 2, 3]})
        """
            w1: 4 7
            w2: e 8
            w3: 6 9
        """
        data_2 = self.inport.peek(None).value
        self.assertEqual(data_2, {"writer-2": 'Exception'})
        """
            w1: 4 7
            w2: 8
            w3: 6 9
        """
        data_3 = self.inport.peek(None).value
        result = {"writer-%d" % i: "data-%d" % (i + 3) for i in [1, 2, 3]}
        result["writer-2"] = "data-8"
        self.assertEqual(data_3, result)
 def exception_handler(self, action, args):
     try:
         e = args[0]
     except:
         e = ExceptionToken()
     self.status = e
     self.token = EOSToken()
Exemple #4
0
class IsEOS(Actor):
    """
    Return 'true' if token is EOS-token


    Inputs:
      token  : any token
    Outputs:
      status : 'true' if input token is EOS-token, false otherwise
    """
    def exception_handler(self, action, args, context):
        self.token = type(args[0]) is EOSToken
        return ActionResult()

    @manage([])
    def init(self):
        self.token = None

    @condition([], ['status'])
    @guard(lambda self: self.token is not None)
    def produce(self):
        tok = self.token
        self.token = None
        return ActionResult(production=(tok, ))

    @condition(['token'])
    @guard(lambda self, tok: self.token is None)
    def consume(self, tok):
        self.token = False
        return ActionResult()

    action_priority = (produce, consume)

    test_set = [
        {  # normal token
            'in': {
                'token': 42
            },
            'out': {
                'status': [False]
            }
        },
        {  # Exception
            'in': {
                'token': ExceptionToken()
            },
            'out': {
                'status': [False]
            }
        },
        {  # Exception
            'in': {
                'token': EOSToken()
            },
            'out': {
                'status': [True]
            }
        },
    ]
Exemple #5
0
 def compute(self, a, b):
     if self.expr:
         result = self.eval(self.expr, {'x':a, 'y':b})
     else :
         result = None
     if isinstance(result, basestring):
         result = ExceptionToken(result)
     return (result, )
Exemple #6
0
 def exception_handler(self, action, args, context):
     try:
         e = args[context['exceptions']['token'][0]]
     except:
         e = ExceptionToken()
     self.status = e
     self.token = EOSToken()
     return ActionResult()
Exemple #7
0
class FileNotFoundHandler(Actor):
    """
    Scan tokens for Exceptions other than EOS.

    Any non-exception or EOS is simply passed on. Exceptions are replaced with EOS token.
    The 'status' outport indicates when an exception occurs by producing 1 instead of 0.

    Inputs:
      token  : any token
    Outputs:
      token  : input token or EOS on exception
      status : for each input token, indicate exception (1) or normal (0) token
    """
    def exception_handler(self, action, args, exceptions):
        self.token = EOSToken()
        self.status = 1
        return ActionResult()

    @manage([])
    def init(self):
        self.token = None

    @condition([], ['token', 'status'])
    @guard(lambda self: self.token)
    def produce(self):
        tok = self.token
        self.token = None
        return ActionResult(production=(tok, self.status))

    @condition(['token'])
    def consume(self, tok):
        self.token = tok
        self.status = 0
        return ActionResult()

    action_priority = (produce, consume)

    test_set = [
        {  # normal token
            'in': {
                'token': 42
            },
            'out': {
                'token': [42],
                'status': [0]
            }
        },
        {  # Exception
            'in': {
                'token': ExceptionToken()
            },
            'out': {
                'token': ['End of stream'],
                'status': [1]
            }
        },
    ]
 def deployed(self, status, header, data):
     try:
         response = self['json'].loads(data)
     except:
         response = None
     if status < 200 or status >= 300:
         # Failed deployed
         response = ExceptionToken(value=status)
     return (response, )
Exemple #9
0
 def process(self, data):
     try:
         res = eval(self.eval_str, {}, {"kwargs": self.kwargs, "data": data})
         if self.dump:
             _log.info("TestProcessing (%s, %s, %s) data:%s, result:%s" % (self._name, self._id, self.inports['data'].id, str(data), str(res)))
     except Exception as e:
         _log.exception("Test processing failed %s" % self.eval_str)
         res = ExceptionToken(value=str(e))
     self.last = res
     return (res, )
Exemple #10
0
 def _get_value(self, data, key):
     keylist = key if type(key) is list else [key]
     try:
         res = data
         for key in keylist:
             self._check_type_mismatch(res, key)
             res = res[key]
     except Exception as e:
         res = ExceptionToken()
     return res
Exemple #11
0
 def get_value(self, data, key):
     keylist = key if type(key) is list else [key]
     try:
         res = data
         for key in keylist:
             if self._type_mismatch(res, key):
                 raise Exception()
             res = res[key]
     except Exception as e:
         res = ExceptionToken()
     return ActionResult(production=(res, ))
Exemple #12
0
 def _set_value(self, container, key, value):
     keylist = key if type(key) is list else [key]
     try:
         res = container
         for key in keylist[:-1]:
             self._check_type_mismatch(res, key)
             res = res[key]
         self._check_type_mismatch(res, keylist[-1])
         res[keylist[-1]] = value
     except:
         container = ExceptionToken()
     return container
Exemple #13
0
 def consume_list(self, data):
     if type(data) is not list:
         self.data = [ExceptionToken()]
         self.has_data = True
         return
     if not data:
         # Empty list => no output
         return
     try:
         self.data = self.copy.copy(data)
         self.has_data = True
     except Exception as e:
         _log.info("An error occurred: {}".format(e))
Exemple #14
0
 def consume_list(self, data):
     if type(data) is not list:
         self.data = [ExceptionToken()]
         self.has_data = True
         return
     if not data:
         # Empty list => no output
         return
     try:
         self.data = copy(data)
         self.has_data = True
     except:
         pass
Exemple #15
0
    def stringify(self, input):
        try:
            # Always unicode
            if self.encoding:
                new_token = unicode(input, self.encoding)
            else:
                new_token = unicode(input)

            return (new_token, )
        except Exception as exc:
            _log.error("Error %s, cant decode token '%s'", str(exc),
                       repr(input))

        return (ExceptionToken("Decode error"), )
Exemple #16
0
 def set_value(self, data, key, value):
     keylist = key if type(key) is list else [key]
     container = deepcopy(data)
     try:
         res = container
         for key in keylist[:-1]:
             if self._type_mismatch(res, key):
                 raise Exception()
             res = res[key]
         if self._type_mismatch(res, keylist[-1]):
             raise Exception()
         res[keylist[-1]] = value
     except:
         container = ExceptionToken()
     return ActionResult(production=(container, ))
Exemple #17
0
 def file_not_found(self):
     token = ExceptionToken(value="File not found")
     self.file_not_found = False  # Only report once
     return (token, )
Exemple #18
0
 def file_not_found(self):
     token = ExceptionToken(value="File not found")
     self.file_not_found = False  # Only report once
     return ActionResult(production=(token, ))
class ExceptionHandler(Actor):
    """
    Scan tokens for Exceptions.

    Any non-exception or EOS is simply passed on. Exceptions other than EOS are replaced
    with an EOS token on the ouput 'token' port unless optional 'replace' argument is true,
    in which case 'replacement' argument (defaults to null) is produced.
    Any exception (including EOS) are produces its reason on the 'status' output port.

    Inputs:
      token  : any token
    Outputs:
      token  : input token or EOS/replacement on exception
      status : reason for any exception tokens encountered (including EOS)
    """
    def exception_handler(self, action, args):
        try:
            e = args[0]
        except:
            e = ExceptionToken()
        self.status = e
        self.token = EOSToken()

    @manage(['status', 'token', 'replace', 'replacement'])
    def init(self, replace=False, replacement=None):
        self.replace = replace
        self.replacement = replacement
        self.status = None
        self.token = None

    @stateguard(lambda self: self.token is not None and self.status)
    @condition([], ['token', 'status'])
    def produce_with_exception(self):
        tok = self.replacement if self.replace else self.token
        status = self.status
        self.token = None
        self.status = None
        return (tok, status.value)

    @stateguard(lambda self: self.token is not None and not self.status)
    @condition([], ['token'])
    def produce(self):
        tok = self.token
        self.token = None
        return (tok, )

    @stateguard(lambda self: not self.status and self.token is None)
    @condition(['token'])
    def consume(self, tok):
        self.token = tok
        self.status = None

    action_priority = (produce_with_exception, produce, consume)

    test_set = [
        {  # normal token
            'inports': {
                'token': 42
            },
            'outports': {
                'token': [42],
                'status': []
            }
        },
        {  # Exception
            'inports': {
                'token': ExceptionToken()
            },
            'outports': {
                'token': ['End of stream'],
                'status': ['Exception']
            }
        },
        {  # Exception
            'inports': {
                'token': EOSToken()
            },
            'outports': {
                'token': ['End of stream'],
                'status': ['End of stream']
            }
        },
        {  # Long list with Exceptions in middle
            'setup': [lambda self: self.init(replace=True, replacement="EOS")],
            'inports': {
                'token': [0, 1, 2,
                          EOSToken(), 0, 1, 2,
                          EOSToken(), 0, 1, 2]
            },
            'outports': {
                'token': [0, 1, 2, 'EOS', 0, 1, 2, 'EOS', 0, 1, 2],
                'status': ['End of stream', 'End of stream']
            }
        },
        {  # Exception with replace (default)
            'setup': [lambda self: self.init(replace=True)],
            'inports': {
                'token': EOSToken()
            },
            'outports': {
                'token': [None],
                'status': ['End of stream']
            }
        },
        {  # Exception with replace
            'setup': [lambda self: self.init(replace=True, replacement={})],
            'inports': {
                'token': EOSToken()
            },
            'outports': {
                'token': [{}],
                'status': ['End of stream']
            }
        },
        {  # Exception with replace
            'setup': [lambda self: self.init(replace=True, replacement={})],
            'inports': {
                'token': ExceptionToken()
            },
            'outports': {
                'token': [{}],
                'status': ['Exception']
            }
        },
    ]
Exemple #20
0
class List(Actor):
    """
    Create a list.

    Consumes 'n' tokens  to produce a list, 'n' defaults to 1. If 'n' is zero or negative,
    consumes tokens until EOS encountered (variable list length).
    The optional arguments pre_list and post_list are used to prepend and extend the list before
    delivering the final list.
    Will produce an ExceptionToken if EOS is encountered when n > 0, or if an ExceptionToken is
    encountered regardless of value of 'n'.

    Inputs:
      item: items to append to list
    Outputs:
      list: a list of consumed items
    """
    def exception_handler(self, action, args):
        if self.n or type(args[0]) is not EOSToken:
            self._list = ExceptionToken()
        self.done = True

    @manage(['n', '_list', 'done'])
    def init(self, n=1, pre_list=None, post_list=None):
        self.n = n if n > 0 else 0
        self._list = []
        self.pre_list = pre_list
        self.post_list = post_list
        self.done = False

    @stateguard(lambda self: not self.n and not self.done)
    @condition(['item'], [])
    def add_item_EOS(self, item):
        self._list.append(item)

    @stateguard(lambda self: self.n and not self.done)
    @condition(['item'], [])
    def add_item(self, item):
        self._list.append(item)
        if len(self._list) == self.n:
            self.done = True

    @stateguard(lambda self: self.done)
    @condition([], ['list'])
    def produce_list(self):
        if isinstance(self._list, list):
            res = (self.pre_list if self.pre_list else []) + self._list + (
                self.post_list if self.post_list else [])
        else:
            res = self._list
        self.done = False
        self._list = []
        return (res, )

    action_priority = (produce_list, add_item, add_item_EOS)

    test_args = []
    test_kwargs = {}

    test_set = [
        {
            'in': {
                'item': [1, 2]
            },
            'out': {
                'list': [[1], [2]]
            },
        },
        {
            'setup': [lambda self: self.init(n=2)],
            'in': {
                'item': [1, 2]
            },
            'out': {
                'list': [[1, 2]]
            },
        },
        {
            'setup': [lambda self: self.init(n=2, pre_list=[5, 7])],
            'in': {
                'item': [1, 2]
            },
            'out': {
                'list': [[5, 7, 1, 2]]
            },
        },
        {
            'setup': [lambda self: self.init(n=2, post_list=[5, 7])],
            'in': {
                'item': [1, 2]
            },
            'out': {
                'list': [[1, 2, 5, 7]]
            },
        },
        {
            'setup':
            [lambda self: self.init(n=2, pre_list=[8, 9], post_list=[5, 7])],
            'in': {
                'item': [1, 2]
            },
            'out': {
                'list': [[8, 9, 1, 2, 5, 7]]
            },
        },
        {
            'setup': [lambda self: self.init(n=0)],
            'in': {
                'item': [1, 2, EOSToken()]
            },
            'out': {
                'list': [[1, 2]]
            },
        },
        # Error conditions
        {
            'setup': [lambda self: self.init(n=2)],
            'in': {
                'item': [1, EOSToken(), 3, 4]
            },
            'out': {
                'list': ['Exception', [3, 4]]
            },
        },
        {
            'setup': [lambda self: self.init(n=0)],
            'in': {
                'item': [1, ExceptionToken(), 3,
                         EOSToken()]
            },
            'out': {
                'list': ['Exception', [3]]
            },
        },
    ]
Exemple #21
0
 def exception_handler(self, action, args):
     if self.n or type(args[0]) is not EOSToken:
         self._list = ExceptionToken()
     self.done = True
Exemple #22
0
 def exception_handler(self, action, args):
     if self.n or type(args[0]) is not EOSToken:
         self._list = ExceptionToken()
     self.done = True
Exemple #23
0
 def divide(self, numerator, denumerator):
     if denumerator != 0:
         result = numerator / denumerator
     else:
         result = ExceptionToken("Division by 0")
     return (result, )
Exemple #24
0
 def avg(self, temperatures):
     if(temperatures != 'ignore'):
         self.result = sum(temperatures)/ len(temperatures)
     else:
         self.result = ExceptionToken("Division by 0")
     return ActionResult(production=(self.result,))
Exemple #25
0
 def device_not_found(self):
     token = ExceptionToken(value="Device not found")
     self.not_found = False  # Only report once
     return (token, )
Exemple #26
0
 def setup(self):
     self.default = ExceptionToken(
     ) if self.exception_output is None else self.exception_output
     self.use('calvinsys.native.python-json', shorthand='json')
Exemple #27
0
 def exception_handler(self, action, args):
     return (ExceptionToken(), )
Exemple #28
0
class List(Actor):

    """
    Create a list.

    Consumes 'n' tokens  to produce a list, 'n' defaults to 1. If 'n' is zero or negative,
    consumes tokens until EOS encountered (variable list length).
    Will produce an ExceptionToken if EOS is encountered when n > 0, or if an ExceptionToken is
    encountered regardless of value of 'n'.

    Inputs:
      item: items to append to list
    Outputs:
      list: a list of consumed items
    """

    def exception_handler(self, action, args, context):
        exception = args[context['exceptions']['item'][0]]
        if self.n or type(exception) is not EOSToken:
            self._list = ExceptionToken()
        self.done = True
        return ActionResult()

    @manage(['n', '_list', 'done'])
    def init(self, n=1):
        self.n = n if n > 0 else 0
        self._list = []
        self.done = False

    @condition(['item'], [])
    @guard(lambda self, item: not self.n and not self.done)
    def add_item_EOS(self, item):
        self._list.append(item)
        return ActionResult()

    @condition(['item'], [])
    @guard(lambda self, item: self.n and not self.done)
    def add_item(self, item):
        self._list.append(item)
        if len(self._list) == self.n:
            self.done = True
        return ActionResult()

    @condition([], ['list'])
    @guard(lambda self: self.done)
    def produce_list(self):
        res = self._list
        self.done = False
        self._list = []
        return ActionResult(production=(res, ))

    action_priority = (produce_list, add_item, add_item_EOS)

    test_args = []
    test_kwargs = {}

    test_set = [
        {
            'in': {'item': [1, 2]},
            'out': {'list': [[1], [2]]},
        },
        {
            'setup': [lambda self: self.init(n=2)],
            'in': {'item': [1, 2]},
            'out': {'list': [[1, 2]]},
        },
        {
            'setup': [lambda self: self.init(n=0)],
            'in': {'item': [1, 2, EOSToken()]},
            'out': {'list': [[1, 2]]},
        },
        # Error conditions
        {
            'setup': [lambda self: self.init(n=2)],
            'in': {'item': [1, EOSToken(), 3, 4]},
            'out': {'list': ['Exception', [3, 4]]},
        },
        {
            'setup': [lambda self: self.init(n=0)],
            'in': {'item': [1, ExceptionToken(), 3, EOSToken()]},
            'out': {'list': ['Exception', [3]]},
        },

    ]
Exemple #29
0
 def exception_handler(self, action, args, context):
     return ActionResult(production=(ExceptionToken(), ))
Exemple #30
0
 def compute(self, a, b):
     try:
         res = self.op(a, b)
     except Exception as e:
         res = ExceptionToken(str(e))
     return (res, )
Exemple #31
0
 def exception_handler(self, action, args, context):
     exception = args[context['exceptions']['item'][0]]
     if self.n or type(exception) is not EOSToken:
         self._list = ExceptionToken()
     self.done = True
     return ActionResult()
Exemple #32
0
class List(Actor):

    """
    Create a list.

    Consumes 'n' tokens  to produce a list, 'n' defaults to 1. If 'n' is zero or negative,
    consumes tokens until EOS encountered (variable list length).
    The optional arguments pre_list and post_list are used to prepend and extend the list before
    delivering the final list.
    Will produce an ExceptionToken if EOS is encountered when n > 0, or if an ExceptionToken is
    encountered regardless of value of 'n'.

    Inputs:
      item: items to append to list
    Outputs:
      list: a list of consumed items
    """

    def exception_handler(self, action, args):
        if self.n or type(args[0]) is not EOSToken:
            self._list = ExceptionToken()
        self.done = True


    @manage(['n', '_list', 'done'])
    def init(self, n=1, pre_list=None, post_list=None):
        self.n = n if n > 0 else 0
        self._list = []
        self.pre_list = pre_list
        self.post_list = post_list
        self.done = False

    @stateguard(lambda self: not self.n and not self.done)
    @condition(['item'], [])
    def add_item_EOS(self, item):
        self._list.append(item)


    @stateguard(lambda self: self.n and not self.done)
    @condition(['item'], [])
    def add_item(self, item):
        self._list.append(item)
        if len(self._list) == self.n:
            self.done = True


    @stateguard(lambda self: self.done)
    @condition([], ['list'])
    def produce_list(self):
        if isinstance(self._list, list):
            res = (self.pre_list if self.pre_list else []) + self._list +(self.post_list if self.post_list else [])
        else:
            res = self._list
        self.done = False
        self._list = []
        return (res, )

    action_priority = (produce_list, add_item, add_item_EOS)

    test_args = []
    test_kwargs = {}

    test_set = [
        {
            'inports': {'item': [1, 2]},
            'outports': {'list': [[1], [2]]},
        },
        {
            'setup': [lambda self: self.init(n=2)],
            'inports': {'item': [1, 2]},
            'outports': {'list': [[1, 2]]},
        },
        {
            'setup': [lambda self: self.init(n=2, pre_list=[5, 7])],
            'inports': {'item': [1, 2]},
            'outports': {'list': [[5, 7, 1, 2]]},
        },
        {
            'setup': [lambda self: self.init(n=2, post_list=[5, 7])],
            'inports': {'item': [1, 2]},
            'outports': {'list': [[1, 2, 5, 7]]},
        },
        {
            'setup': [lambda self: self.init(n=2, pre_list=[8, 9], post_list=[5, 7])],
            'inports': {'item': [1, 2]},
            'outports': {'list': [[8, 9, 1, 2, 5, 7]]},
        },
        {
            'setup': [lambda self: self.init(n=0)],
            'inports': {'item': [1, 2, EOSToken()]},
            'outports': {'list': [[1, 2]]},
        },
        # Error conditions
        {
            'setup': [lambda self: self.init(n=2)],
            'inports': {'item': [1, EOSToken(), 3, 4]},
            'outports': {'list': ['Exception', [3, 4]]},
        },
        {
            'setup': [lambda self: self.init(n=0)],
            'inports': {'item': [1, ExceptionToken(), 3, EOSToken()]},
            'outports': {'list': ['Exception', [3]]},
        },

    ]