|
| 1 | +import requests |
| 2 | +import yaml |
| 3 | + |
| 4 | + |
| 5 | +class Yaml(): |
| 6 | + """ |
| 7 | + is the parent of every class that can be generated from yaml files. |
| 8 | + """ |
| 9 | + MUST_HAVE = [] |
| 10 | + |
| 11 | + def __init__(self, yaml_dict): |
| 12 | + """ |
| 13 | + Forms an object from the given dictionary. |
| 14 | +
|
| 15 | + :param yaml_dict: a dictionary as it is parsed from yaml code. |
| 16 | + :return: Yaml base |
| 17 | + """ |
| 18 | + self.validate_yaml(yaml_dict) |
| 19 | + |
| 20 | + def validate_yaml(self, yaml_dict): |
| 21 | + """ |
| 22 | + validates the given dictionary against the MUST_HAVE keys. |
| 23 | +
|
| 24 | + :param yaml_dict: a dictionary as it is parsed from yaml code. |
| 25 | + :return: None |
| 26 | + :raises: LookupError when key requirements aren't fulfilled. |
| 27 | + """ |
| 28 | + for key in self.MUST_HAVE: |
| 29 | + if yaml_dict.has_key(key) and yaml_dict.get(key): |
| 30 | + pass |
| 31 | + else: |
| 32 | + raise LookupError, 'error with the "%s" key' % key |
| 33 | + |
| 34 | + |
| 35 | +class Biobox(Yaml): |
| 36 | + """ |
| 37 | + represents a generic biobox container description. |
| 38 | + """ |
| 39 | + KEY_IMAGE = 'image' |
| 40 | + KEY_ID = 'pmid' |
| 41 | + KEY_HOME_PAGE = 'homepage' |
| 42 | + KEY_MAILING_LIST = 'mailing_list' |
| 43 | + KEY_DESCRIPTION = 'description' |
| 44 | + KEY_TASKS = 'tasks' |
| 45 | + KEY_TITLE = 'title' |
| 46 | + |
| 47 | + MUST_HAVE = [ |
| 48 | + KEY_IMAGE, |
| 49 | + KEY_ID, |
| 50 | + KEY_DESCRIPTION, |
| 51 | + KEY_TASKS, |
| 52 | + KEY_TITLE |
| 53 | + ] |
| 54 | + |
| 55 | + def __init__(self, yaml_dict): |
| 56 | + """ |
| 57 | + Forms an object from the given dictionary. |
| 58 | +
|
| 59 | + :param interface: the interface of the biobox. |
| 60 | + :param yaml_dict: a dictionary as it is parsed from yaml code. |
| 61 | + :return: Yaml base |
| 62 | + """ |
| 63 | + Yaml.__init__(self, yaml_dict) |
| 64 | + self.image = self.Image(yaml_dict.get(self.KEY_IMAGE)) |
| 65 | + self.tasks = [self.Task(task) for task in yaml_dict.get(self.KEY_TASKS)] |
| 66 | + self.pmid = yaml_dict.get(self.KEY_ID) |
| 67 | + self.homepage = yaml_dict.get(self.KEY_HOME_PAGE) |
| 68 | + self.mailing_list = yaml_dict.get(self.KEY_MAILING_LIST) |
| 69 | + self.description = yaml_dict.get(self.KEY_DESCRIPTION) |
| 70 | + self.title = yaml_dict.get(self.KEY_TITLE) |
| 71 | + |
| 72 | + def get_dict(self): |
| 73 | + dictionary = self.__dict__ |
| 74 | + dictionary[self.KEY_IMAGE] = self.image.__dict__ |
| 75 | + dictionary[self.KEY_TASKS] = [task.__dict__ for task in self.tasks] |
| 76 | + return dictionary |
| 77 | + |
| 78 | + class Image(Yaml): |
| 79 | + """ |
| 80 | + is a helper class for :class:`Biobox`. |
| 81 | + """ |
| 82 | + KEY_CONTAINER_URI = 'dockerhub' |
| 83 | + KEY_REPO_URL = 'repo' |
| 84 | + KEY_SRC_URL = 'source' |
| 85 | + |
| 86 | + MUST_HAVE = [ |
| 87 | + KEY_CONTAINER_URI, |
| 88 | + KEY_REPO_URL |
| 89 | + ] |
| 90 | + |
| 91 | + def __init__(self, yaml_dict): |
| 92 | + """ |
| 93 | + Forms an object from the given dictionary. |
| 94 | +
|
| 95 | + :param yaml_dict: a dictionary as it is parsed from yaml code. |
| 96 | + :return: Yaml base |
| 97 | + """ |
| 98 | + Yaml.__init__(self, yaml_dict) |
| 99 | + self.dockerhub = yaml_dict.get(self.KEY_CONTAINER_URI) |
| 100 | + self.repo = yaml_dict.get(self.KEY_REPO_URL) |
| 101 | + self.source = yaml_dict.get(self.KEY_SRC_URL) |
| 102 | + |
| 103 | + class Task(Yaml): |
| 104 | + KEY_NAME = 'name' |
| 105 | + KEY_INTERFACE = 'interface' |
| 106 | + |
| 107 | + MUST_HAVE = [ |
| 108 | + KEY_NAME, |
| 109 | + KEY_INTERFACE |
| 110 | + ] |
| 111 | + |
| 112 | + def __init__(self, yaml_dict): |
| 113 | + Yaml.__init__(self, yaml_dict) |
| 114 | + self.name = yaml_dict.get(self.KEY_NAME) |
| 115 | + self.interface = yaml_dict.get(self.KEY_INTERFACE) |
| 116 | + |
| 117 | + |
| 118 | +def get_current_bioboxes(): |
| 119 | + response = requests.get('https://raw.githubusercontent.com/pbelmann/data/feature/new-image-list/images.yml') |
| 120 | + return from_list(yaml.load(response.text)) |
| 121 | + |
| 122 | + |
| 123 | +def from_list(yaml_dict): |
| 124 | + """ |
| 125 | + creates bioboxes from an interface list. |
| 126 | + :param yaml_dict: the list with bioboxes as it is parsed from a yaml file. |
| 127 | + :return: a list with lists of :class:`Biobox`. |
| 128 | + """ |
| 129 | + interfaces = [] |
| 130 | + for yaml_box in yaml_dict.get('images'): |
| 131 | + biobox = Biobox(yaml_box) |
| 132 | + interfaces.append(biobox) |
| 133 | + |
| 134 | + return interfaces |
0 commit comments