예제 #1
0
파일: kernel.py 프로젝트: nandor-poka/sos-r
 def preview(self, item):
     # return the preview of variable.
     try:
         return "", self.sos_kernel.get_response(
             f'..sos.preview("{item}")', ('stream', ),
             name=('stdout', ))[0][1]['text']
     except Exception as e:
         env.log_to_file('VARIABLE', f'Preview of {item} failed: {e}')
         return None
예제 #2
0
 def preview(self, item):
     try:
         response = self.sos_kernel.get_response(
             f'pickle.dumps(__preview_var("{item}"))',
             ['execute_result'])[-1][1]
         return self.load_pickled(eval(response['data']['text/plain']))
     except Exception as e:
         env.log_to_file('PREVIEW', f'Failed to preview {item}: {e}')
         return '', f'No preview is available {e}'
예제 #3
0
    def get_vars(self, var_names):
        """
        Functionality to transfer CAS objects and TypeSystem from SoS (python) kernel to the IRuta kernel.
        This function is called when a use invokes the line magic %get or %with.
        """
        if len(var_names) != 1:
            raise Exception(
                "%get takes exactly one variable name as argument."
                "If you want to transfer multiple CAS, then please write them to a directory and use `%inputDir` in IRuta kernel."
            )
        var_name = var_names[0]
        var_content = env.sos_dict[var_name]

        # Step 1: Writing Cas and TypeSystem to disk using dkpro-cassis
        temp_directory = tempfile.TemporaryDirectory()
        temp_typesystem_file = tempfile.NamedTemporaryFile(
            suffix=".xml", dir=temp_directory.name, delete=False)
        temp_typesystem_file_path = os.path.normpath(
            temp_typesystem_file.name).replace('\\', "/")
        temp_xmi_file = tempfile.NamedTemporaryFile(suffix=".xmi",
                                                    dir=temp_directory.name,
                                                    delete=False)
        temp_xmi_file_path = os.path.normpath(temp_xmi_file.name).replace(
            '\\', "/")

        if isinstance(var_content, cassis.Cas):
            var_content.to_xmi(temp_xmi_file_path)
            var_content.typesystem.to_xml(temp_typesystem_file_path)
            cmd_transfer_var = "%displayMode NONE\n" \
                               f"%loadCas {temp_xmi_file_path}\n" \
                               f"%loadTypeSystem {temp_typesystem_file_path}"

        elif isinstance(var_content, cassis.TypeSystem):
            var_content.to_xml(temp_typesystem_file_path)
            cmd_transfer_var = "%displayMode NONE\n" \
                               f"%loadTypeSystem {temp_typesystem_file_path}"

        else:
            raise Exception(
                '%get only support transfering UIMA CAS objects or TypeSystem objects. '
                'Use %expand for transfering string variables. Received datatype {}'
                .format(short_repr(var_content)))

        # Step 2: Loading files
        env.log_to_file('KERNEL', f'Executing "{cmd_transfer_var}"')
        self.ruta_kernel.run_cell(
            cmd_transfer_var,
            silent=True,
            store_history=False,
            on_error=f'Failed to get variable {var_name}')

        # Step 3: Clean-up temp files
        temp_typesystem_file.close()
        temp_xmi_file.close()
        temp_directory.cleanup()
예제 #4
0
파일: kernel.py 프로젝트: nandor-poka/sos-r
 def get_vars(self, names):
     for name in names:
         if name.startswith('_'):
             self.sos_kernel.warn(
                 f'Variable {name} is passed from SoS to kernel {self.kernel_name} as {"." + name[1:]}'
             )
             newname = '.' + name[1:]
         else:
             newname = name
         r_repr = _R_repr(env.sos_dict[name])
         env.log_to_file('VARIABLE', r_repr)
         self.sos_kernel.run_cell(
             f'{newname} <- {r_repr}',
             True,
             False,
             on_error=f'Failed to get variable {name} to R')
예제 #5
0
    def put_vars(self, items, to_kernel=None):
        """
        Functionality to transfer CAS objects from the IRuta kernel to the SoS (Python) kernel.
        This function is called when a user invokes the line magic %put or %with.
        """

        if len(items) != 1:
            raise Exception(
                "%put takes exactly one variable name as argument. ")
        var_name = items[0]

        temp_directory = tempfile.TemporaryDirectory()
        temp_typesystem_file = tempfile.NamedTemporaryFile(
            suffix=".xml", dir=temp_directory.name, delete=False)
        temp_typesystem_file_path = os.path.normpath(
            temp_typesystem_file.name).replace('\\', "/")
        temp_xmi_file = tempfile.NamedTemporaryFile(suffix=".xmi",
                                                    dir=temp_directory.name,
                                                    delete=False)
        temp_xmi_file_path = os.path.normpath(temp_xmi_file.name).replace(
            '\\', "/")

        # Step 1: Writing CAS and TypeSystem to disk with Ruta
        cmd_transfer_var = f"%displayMode NONE\n" \
                           f"%saveTypeSystem {temp_typesystem_file_path}\n" \
                           f"%saveCas {temp_xmi_file_path}"

        env.log_to_file('KERNEL', f'Executing "{cmd_transfer_var}"')
        self.ruta_kernel.run_cell(cmd_transfer_var,
                                  silent=True,
                                  store_history=False,
                                  on_error='Failed to write UIMA CAS to disk.')

        # Step 2: Reading CAS and TypeSystem from disk with python/cassis
        typesystem = cassis.load_typesystem(temp_typesystem_file)
        cas = cassis.load_cas_from_xmi(temp_xmi_file, typesystem=typesystem)

        # Step 3: Clean-up temp files
        temp_typesystem_file.close()
        temp_xmi_file.close()
        temp_directory.cleanup()

        return {var_name: cas}
예제 #6
0
def Rmarkdown(
    script=None,
    input=None,
    output=None,
    args="{input:r}, output_file={output:ar}",
    **kwargs,
):
    """Convert input file to output using Rmarkdown

    The input can be specified in three ways:

    1. instant script, which is assumed to be in md format

    Rmarkdown:   output='report.html'
      script

    2. one or more input files. The format is determined by extension of input file

    Rmarkdown(input, output='report.html')

    3. input file specified by command line option `-r` .
    Rmarkdown(output='report.html')

    If no output is specified, it is assumed to be in html format
    and is written to standard output.

    You can specify more options using the args parameter of the action. The default value
    of args is `${input!r} --output ${output!ar}'
    """
    if not R_library("rmarkdown").target_exists():
        raise RuntimeError("Library rmarkdown does not exist")

    input = sos_targets(collect_input(script, input))

    output = sos_targets(output)
    if len(output) == 0:
        write_to_stdout = True
        output = sos_targets(
            tempfile.NamedTemporaryFile(mode="w+t",
                                        suffix=".html",
                                        delete=False).name)
    else:
        write_to_stdout = False
    #
    ret = 1
    try:
        #   render(input, output_format = NULL, output_file = NULL, output_dir = NULL,
        #        output_options = NULL, intermediates_dir = NULL,
        #        runtime = c("auto", "static", "shiny"),
        #        clean = TRUE, params = NULL, knit_meta = NULL, envir = parent.frame(),
        #        run_Rmarkdown = TRUE, quiet = FALSE, encoding = getOption("encoding"))
        cmd = interpolate(
            f'Rscript -e "rmarkdown::render({args})"',
            {
                "input": input,
                "output": output
            },
        )
        if "ACTION" in env.config["SOS_DEBUG"] or "ALL" in env.config[
                "SOS_DEBUG"]:
            env.log_to_file("ACTION", f'Running command "{cmd}"')
        if env.config["run_mode"] == "interactive":
            # need to catch output and send to python output, which will in trun be hijacked by SoS notebook
            p = subprocess.Popen(cmd,
                                 shell=True,
                                 stderr=subprocess.PIPE,
                                 stdout=subprocess.PIPE)
            # pid = p.pid
            out, err = p.communicate()
            sys.stdout.write(out.decode())
            sys.stderr.write(err.decode())
            ret = p.returncode
        else:
            p = subprocess.Popen(cmd, shell=True)
            # pid = p.pid
            ret = p.wait()
    except Exception as e:
        env.logger.error(str(e))
    if ret != 0:
        temp_file = os.path.join(".sos", f'{"Rmarkdown"}_{os.getpid()}.md')
        shutil.copyfile(str(input), temp_file)
        cmd = interpolate(
            f'Rscript -e "rmarkdown::render({args})"',
            {
                "input": input,
                "output": sos_targets(temp_file)
            },
        )
        raise RuntimeError(
            f'Failed to execute script. Please use command \n"{cmd}"\nunder {os.getcwd()} to test it.'
        )
    if write_to_stdout:
        with open(str(output[0])) as out:
            sys.stdout.write(out.read())
    else:
        env.logger.info(f"Report saved to {output}")
예제 #7
0
def execute_scratch_cell(code, raw_args, kernel):
    # we then have to change the parse to disable args.workflow when
    # there is no workflow option.
    raw_args = shlex.split(raw_args) if isinstance(raw_args, str) else raw_args
    if code is None or '-h' in raw_args:
        parser = get_run_parser(interactive=True, with_workflow=True)
        parser.print_help()
        return
    if raw_args and raw_args[0].lstrip().startswith('-'):
        parser = get_run_parser(interactive=True, with_workflow=False)
        parser.error = _parse_error
        args, workflow_args = parser.parse_known_args(raw_args)
        args.workflow = None
    else:
        parser = get_run_parser(interactive=True, with_workflow=True)
        parser.error = _parse_error
        args, workflow_args = parser.parse_known_args(raw_args)

    if not code.strip():
        return

    # for reporting purpose
    sys.argv = ['%run'] + raw_args
    env.verbosity = args.verbosity

    if not any(
            isinstance(x, NotebookLoggingHandler)
            for x in env.logger.handlers):
        env.logger.handlers = [
            x for x in env.logger.handlers
            if type(x) is not logging.StreamHandler
        ]
        levels = {
            0: logging.ERROR,
            1: logging.WARNING,
            2: logging.INFO,
            3: logging.DEBUG,
            4: logging.DEBUG,
            None: logging.INFO
        }
        env.logger.addHandler(
            NotebookLoggingHandler(levels[env.verbosity],
                                   kernel,
                                   title=' '.join(sys.argv)))
    else:
        env.logger.handers[0].setTitle(' '.join(sys.argv))

    global last_cell_id
    # we retain step_input etc only when we step through a cell  #256
    if kernel and kernel.cell_id != last_cell_id:
        # clear __step_input__, __step_output__ etc because there is
        # no concept of passing input/outputs across cells.
        env.sos_dict.set('__step_output__', sos_targets([]))
        for k in [
                '__step_input__', '__default_output__', 'step_input',
                'step_output', 'step_depends', '_input', '_output', '_depends'
        ]:
            env.sos_dict.pop(k, None)

    last_cell_id = kernel.cell_id

    config = {
        'config_file':
        args.__config__,
        'default_queue':
        args.__queue__,
        'run_mode':
        'dryrun' if args.dryrun else 'interactive',
        # issue 230, ignore sig mode in interactive mode
        'sig_mode':
        'ignore',
        'verbosity':
        args.verbosity,
        # for backward compatibility, we try both args.__worker_procs__ and args.__max_procs__
        'worker_procs':
        args.__worker_procs__
        if hasattr(args, '__worker_procs__') else args.__max_procs__,
        'max_running_jobs':
        args.__max_running_jobs__,
        # for infomration and resume only
        'workdir':
        os.getcwd(),
        'workflow':
        args.workflow,
        'targets':
        args.__targets__,
        'workflow_args':
        workflow_args,
        'workflow_id':
        textMD5(code),

        # interactive work is also a slave of the controller
        'slave_id':
        kernel.cell_id,
    }

    env.sos_dict.set('workflow_id', config['workflow_id'])
    env.config.update(config)

    try:
        if not any([
                SOS_SECTION_HEADER.match(line) or line.startswith('%from')
                or line.startswith('%include') for line in code.splitlines()
        ]):
            code = f'[cell_{str(kernel.cell_id)[:8] if kernel and kernel.cell_id else "0"}]\n' + code
            script = SoS_Script(content=code)
        else:
            return
        workflow = script.workflow(args.workflow)
        section = workflow.sections[0]
        res = analyze_section(section)
        env.sos_dict.quick_update({
            '__signature_vars__': res['signature_vars'],
            '__environ_vars__': res['environ_vars'],
            '__changed_vars__': res['changed_vars']
        })
        executor = Interactive_Step_Executor(section, mode='interactive')
        ret = executor.run()
        try:
            return ret['__last_res__']
        except Exception as e:
            raise RuntimeError(
                f'Unknown result returned from executor {ret}: {e}')
    except (UnknownTarget, RemovedTarget) as e:
        raise RuntimeError(f'Unavailable target {e.target}')
    except TerminateExecution as e:
        return
    except SystemExit:
        # this happens because the executor is in resume mode but nothing
        # needs to be resumed, we simply pass
        return
    except Exception:
        env.log_to_file('PROCESS', get_traceback())
        raise