misc.py 2.42 KB
Newer Older
1

2
# Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
nwillhoft's avatar
nwillhoft committed
3
# Copyright [2016-2021] EMBL-European Bioinformatics Institute
4 5 6 7 8 9 10 11 12 13 14 15 16
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#      http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
import json
import os.path

from docutils import nodes


hive_colours = {}

class hivestatus(nodes.Element):
    pass

def hivestatus_role(name, rawtext, text, lineno, inliner, options={}, content=[]):
    status = text[1:text.index('>')]
    text = (text[text.index('>')+1:]).strip()
    hivestatus_node = hivestatus()
32
    hivestatus_node.append(nodes.Text(text))
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
    hivestatus_node['status'] = status

    return [hivestatus_node], []

def visit_hivestatus_html(self, node):
    load_colours_if_needed()
    self.body.append('<span style="background-color:%s">' % hive_colours[node['status']])

def depart_hivestatus_html(self, node):
    self.body.append('</span>')

def visit_hivestatus_latex(self, node):
    load_colours_if_needed()
    self.body.append('\n\\colorbox{%s}{' % hive_colours[node['status']])

def depart_hivestatus_latex(self, node):
    self.body.append('}')

def load_colours_if_needed():
    # eHive's default configuration file
    default_config_file = os.environ["EHIVE_ROOT_DIR"] + os.path.sep + "hive_config.json"
    with open(default_config_file, "r") as fc:
        conf_content = json.load(fc)
        as_hash = conf_content["Graph"]["Node"]["AnalysisStatus"]
        for s in as_hash:
            if isinstance(as_hash[s], dict):
                hive_colours[s] = as_hash[s]["Colour"]
        js_hash = conf_content["Graph"]["Node"]["JobStatus"]
        for s in js_hash:
            if isinstance(js_hash[s], dict):
                hive_colours[s] = js_hash[s]["Colour"]

## Register the extension
def setup(app):
    app.add_role('hivestatus', hivestatus_role)
    app.add_node(hivestatus,
        html = (visit_hivestatus_html, depart_hivestatus_html),
        latex = (visit_hivestatus_latex, depart_hivestatus_latex),
    )