Beispiel #1
0
    def test_context_content(self):
        script = PythonScriptRunner(code=self.code, app=self.app)
        for key in EXPECTED_CONTEXT_ENTRIES:
            self.assertIn(key, script.context)

        self.assertIs(script.context["app"], self.app)
        self.assertIs(script.context["user_datasource"], self.app.datasource)
    def run_python_script(self, path="", code=""):
        """ Run the content of the python file in-process.

        Parameters
        ----------
        path : str [OPTIONAL]
            Path to the script file. Cannot be empty if the code is empty.

        code : str [OPTIONAL]
            Content of the script file. Cannot be empty if the path is empty.
        """
        from kromatography.tools.python_script_runner import PythonScriptRunner

        if not code:
            if not path:
                msg = "Cannot run any script as neither a path nor code has" \
                      " been provided (path={}, code={}).".format(path, code)
                logger.exception(msg)
                raise ValueError(msg)

            code = open(path).read()

        script = PythonScriptRunner(code=code,
                                    task=self,
                                    app=self._app,
                                    path=path)
        try:
            output = script.run()
        except Exception as e:
            msg = "Failed to run the script {}: error as {}"
            msg = msg.format(path, e)
            logger.exception(msg)
            error(None, msg)
        else:
            msg = "Script {} ran successfully with the following output:\n\n{}"
            msg = msg.format(path, output)
            information(None, msg)

        logger.debug(msg)
Beispiel #3
0
 def test_app_raises_on_bad_code(self):
     code = "BAD CODE THAT CAN'T RUN"
     script = PythonScriptRunner(code=code, app=self.app)
     with self.assertRaises(PythonScriptRunError):
         script.run()
Beispiel #4
0
 def test_output_from_run(self):
     script = PythonScriptRunner(code="print('ABCDE')", app=self.app)
     output = script.run()
     self.assertEqual(output, 'ABCDE\n')
Beispiel #5
0
 def test_app_runs_no_gui_script(self):
     script = PythonScriptRunner(code=self.code, app=self.app)
     output = script.run()
     self.assertIsInstance(output, str)
Beispiel #6
0
 def test_script_no_context_without_app(self):
     script = PythonScriptRunner(code=self.code)
     with self.assertRaises(ValueError):
         getattr(script, "context")
Beispiel #7
0
 def test_script_cant_run_without_app(self):
     script = PythonScriptRunner(code=self.code)
     with self.assertRaises(ValueError):
         script.run()