Exemple #1
0
    def render(self, env, rst_links, categories, files):
        if self.raw_caption is not None:
            try:
                from jinja2 import Template
            except ImportError as e:
                raise WorkflowError(
                    "Python package jinja2 must be installed to create reports."
                )

            job = self.job
            snakemake = Snakemake(
                job.input,
                job.output,
                job.params,
                self._wildcards,
                job.threads,
                job.resources,
                job.log,
                job.dag.workflow.config,
                job.rule.name,
                None,
            )

            try:
                caption = open(self.raw_caption).read() + rst_links
                caption = env.from_string(caption).render(
                    snakemake=snakemake, categories=categories, files=files)
                self.caption = json.dumps(
                    publish_parts(caption, writer_name="html")["body"])
            except Exception as e:
                raise WorkflowError(
                    "Error loading caption file of output marked for report.",
                    e)
Exemple #2
0
    def __init__(self, path, job, caption=None):
        self.caption = ""
        if caption is not None:
            try:
                from jinja2 import Template
            except ImportError as e:
                raise WorkflowError(
                    "Pyhton package jinja2 must be installed to create reports."
                )

            snakemake = Snakemake(job.input, job.output, job.params,
                                  job.wildcards, job.threads, job.resources,
                                  job.log, job.dag.workflow.config,
                                  job.rule.name)
            try:
                caption = Template(
                    open(caption).read()).render(snakemake=snakemake)
                self.caption = publish_parts(caption,
                                             writer_name="html")["body"]
            except Exception as e:
                raise WorkflowError(
                    "Error loading caption file of output "
                    "marked for report.", e)
        self.data, self.mime = data_uri(path)
        self.id = uuid.uuid4()
        self.path = path
def mock_snakemake(rulename, **wildcards):
    """
    This function is expected to be executed from the 'scripts'-directory of '
    the snakemake project. It returns a snakemake.script.Snakemake object,
    based on the Snakefile.

    If a rule has wildcards, you have to specify them in **wildcards.

    Parameters
    ----------
    rulename: str
        name of the rule for which the snakemake object should be generated
    **wildcards:
        keyword arguments fixing the wildcards. Only necessary if wildcards are
        needed.
    """
    import snakemake as sm
    import os
    from pypsa.descriptors import Dict
    from snakemake.script import Snakemake

    script_dir = Path(__file__).parent.resolve()
    assert Path.cwd().resolve() == script_dir, \
      f'mock_snakemake has to be run from the repository scripts directory {script_dir}'
    os.chdir(script_dir.parent)
    for p in sm.SNAKEFILE_CHOICES:
        if os.path.exists(p):
            snakefile = p
            break
    workflow = sm.Workflow(snakefile)
    workflow.include(snakefile)
    workflow.global_resources = {}
    rule = workflow.get_rule(rulename)
    dag = sm.dag.DAG(workflow, rules=[rule])
    wc = Dict(wildcards)
    job = sm.jobs.Job(rule, dag, wc)

    def make_accessable(*ios):
        for io in ios:
            for i in range(len(io)):
                io[i] = os.path.abspath(io[i])

    make_accessable(job.input, job.output, job.log)
    snakemake = Snakemake(job.input, job.output, job.params, job.wildcards,
                          job.threads, job.resources, job.log,
                          job.dag.workflow.config, job.rule.name, None,)
    # create log and output dir if not existent
    for path in list(snakemake.log) + list(snakemake.output):
        Path(path).parent.mkdir(parents=True, exist_ok=True)

    os.chdir(script_dir)
    return snakemake
Exemple #4
0
    def render(self, env, rst_links, categories, files):
        if self.raw_caption is not None:
            try:
                from jinja2 import Template
            except ImportError as e:
                raise WorkflowError(
                    "Python package jinja2 must be installed to create reports."
                )

            job = self.job
            snakemake = Snakemake(
                job.input,
                job.output,
                job.params,
                self._wildcards,
                job.threads,
                job.resources,
                job.log,
                job.rule.module_globals["config"],
                job.rule.name,
                None,
            )

            try:
                caption = (
                    self.workflow.sourcecache.open(self.raw_caption).read() +
                    rst_links)
                caption = env.from_string(caption).render(
                    snakemake=snakemake, categories=categories, files=files)
                self.caption = publish_parts(caption,
                                             writer_name="html")["body"]
            except Exception as e:
                raise WorkflowError(
                    "Error loading caption file {} of output marked for report."
                    .format(self.raw_caption.get_path_or_uri()),
                    e,
                )
        else:
            self.caption = ""