Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 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 73 74 75 76 77 78 79 80 81 82 83
from  Configuration.PyReleaseValidation.relval_steps import Matrix, InputInfo, Steps
import os
import json
import collections


workflows = Matrix()
steps = Steps()


def get_json_files():
    cwd = os.path.join(os.getcwd(), "json_data")
    if not os.path.exists(cwd):
        return []

    json_files = []
    for f in os.listdir(cwd):
        full_path = os.path.join(cwd, f)
        if os.path.isfile(full_path) and f.endswith(".json"):
            json_files.append(full_path)
    return json_files


def fix_run(run):
    runs = run.replace(" ", "").replace("[", "").replace("]", "").split(",")
    int_runs = []
    for item in runs:
        if item.isdigit():
            int_runs.append(int(item))
        else:
            print("WARNING: run is in bad format: {0}".format(run))
    return int_runs

def convert_keys_to_string(dictionary):
    """ Recursively converts dictionary keys to strings.
        Utility to help deal with unicode keys in dictionaries created from json requests.
        In order to pass dict to function as **kwarg we should transform key/value to str.
    """
    if isinstance(dictionary, str):
        return str(dictionary)
    elif isinstance(dictionary, collections.Mapping):
        return dict(map(convert_keys_to_string, dictionary.items()))
    elif isinstance(dictionary, collections.Iterable):
        return type(dictionary)(map(convert_keys_to_string, dictionary))
    else:
        return dictionary

def load_steps_and_workflows():
    data_files = get_json_files()
    for index, data_file in enumerate(data_files):
        with open(data_file, "r") as f:
            data = json.load(f)
            data = convert_keys_to_string(data)
            label = data["label"]
            steps_names = []
            for step_name, step in data["steps"].items():
                steps_names.append((step_name, step["sequence_number"]))
                if step_name in steps:
                    continue  # this step was inserted already

                # inputInfo case
                if "inputInfo" in step:
                    input_info = step["inputInfo"]
                    if "run" in input_info:
                        input_info["run"] = fix_run(input_info["run"])

                    steps[step_name] = {
                        'INPUT': InputInfo(**input_info)
                    }
                # step with parameters
                elif "parameters" in step:
                    steps[step_name] = step["parameters"]
                else:
                    raise Exception("Wrong step format in {0} file".format(data_file))

            sorted_steps = sorted(steps_names, key=lambda step: step[1]) # sort steps by sequence number
            sorted_steps_names = [step_name[0] for step_name in sorted_steps]  # filter only step names

            workflows[1000000.0 + 0.1*index] = [label, sorted_steps_names]


load_steps_and_workflows()