comparison env/lib/python3.7/site-packages/planemo/training/topic.py @ 2:6af9afd405e9 draft

"planemo upload commit 0a63dd5f4d38a1f6944587f52a8cd79874177fc1"
author shellac
date Thu, 14 May 2020 14:56:58 -0400
parents 26e78fe6e8c4
children
comparison
equal deleted inserted replaced
1:75ca89e9b81c 2:6af9afd405e9
1 """Module contains code for the Topic class, dealing with the creation of a training topic."""
2
3 import collections
4 import os
5
6 from planemo import templates
7 from .utils import (
8 load_yaml,
9 Requirement,
10 save_to_yaml
11 )
12
13
14 INDEX_FILE_TEMPLATE = """---
15 layout: topic
16 topic_name: {{ topic }}
17 ---
18 """
19
20 README_FILE_TEMPLATE = """
21 {{ topic }}
22 ==========
23
24 Please refer to the [CONTRIBUTING.md](../../CONTRIBUTING.md) before adding or updating any material
25 """
26
27
28 DOCKER_FILE_TEMPLATE = """
29 # Galaxy - {{ topic_title }}
30 #
31 # to build the docker image, go to root of training repo and
32 # docker build -t {{ topic_name }} -f topics/{{ topic_name }}/docker/Dockerfile .
33 #
34 # to run image:
35 # docker run -p "8080:80" -t {{ topic_name }}
36 # use -d to automatically dowload the datalibraries in the container
37
38 FROM bgruening/galaxy-stable:latest
39
40 MAINTAINER Galaxy Training Material
41
42 ENV GALAXY_CONFIG_BRAND "GTN: {{ topic_title }}"
43
44 # copy the tutorials directory for your topic
45 ADD topics/{{ topic_name }}/tutorials/ /tutorials/
46
47 # install everything for tutorials
48 ADD bin/docker-install-tutorials.sh /setup-tutorials.sh
49 ADD bin/mergeyaml.py /mergeyaml.py
50 ADD bin/data_libarary_download.sh /data_libarary_download.sh
51 RUN /setup-tutorials.sh
52
53 ENTRYPOINT ["/data_libarary_download.sh"]
54 """
55
56
57 INTRO_SLIDES_FILE_TEMPLATE = """---
58 layout: introduction_slides
59 logo: "GTN"
60
61 title: {{ title }}
62 type: {{ type }}
63 contributors:
64 - contributor
65 ---
66
67 ### How to fill the slide decks?
68
69 Please follow our
70 [tutorial to learn how to fill the slides]({{ '{{' }} site.baseurl {{ '}}' }}/topics/contributing/tutorials/create-new-tutorial-slides/slides.html)
71 """
72
73
74 class Topic(object):
75 """Class to describe a training topic."""
76
77 def __init__(self, name="new_topic", target="use", title="The new topic", summary="Summary", parent_dir="topics"):
78 """Init a topic instance."""
79 self.name = name
80 self.type = target
81 self.title = title
82 self.summary = summary
83 self.docker_image = ""
84 self.maintainers = ["maintainers"]
85 self.parent_dir = parent_dir
86 self.set_default_requirement()
87 self.set_paths()
88
89 def init_from_kwds(self, kwds):
90 """Init a topic instance from a kwds dictionary."""
91 self.name = kwds["topic_name"]
92 self.type = kwds["topic_target"]
93 self.title = kwds["topic_title"]
94 self.summary = kwds["topic_summary"]
95 self.set_default_requirement()
96 self.set_paths()
97
98 def init_from_metadata(self):
99 """Init a topic instance from the metadata file."""
100 metadata = load_yaml(self.metadata_fp)
101 self.name = metadata['name']
102 self.type = metadata['type']
103 self.title = metadata['title']
104 self.summary = metadata['summary']
105 self.requirements = []
106 if 'requirements' in metadata:
107 for r in metadata['requirements']:
108 req = Requirement()
109 req.init_from_dict(r)
110 self.requirements.append(req)
111 if 'docker_image' in metadata:
112 self.docker_image = metadata['docker_image']
113 self.maintainers = metadata['maintainers']
114 self.set_paths()
115
116 # GETTERS
117 def get_requirements(self):
118 """Get the requirements as a list of ordered dictionaries."""
119 reqs = []
120 for req in self.requirements:
121 reqs.append(req.export_to_ordered_dict())
122 return reqs
123
124 def export_metadata_to_ordered_dict(self):
125 """Export the topic metadata into an ordered dictionary."""
126 metadata = collections.OrderedDict()
127 metadata['name'] = self.name
128 metadata['type'] = self.type
129 metadata['title'] = self.title
130 metadata['summary'] = self.summary
131 metadata['requirements'] = self.get_requirements()
132 metadata['docker_image'] = self.docker_image
133 metadata['maintainers'] = self.maintainers
134 return metadata
135
136 # SETTERS
137 def set_default_requirement(self):
138 """Set default requirement: Galaxy introduction."""
139 self.requirements = []
140 if self.type == 'use':
141 self.requirements.append(Requirement())
142
143 def set_paths(self):
144 """Set the paths to folder and files."""
145 self.dir = os.path.join(self.parent_dir, self.name)
146 self.img_folder = os.path.join(self.dir, "images")
147 self.tuto_folder = os.path.join(self.dir, "tutorials")
148 self.index_fp = os.path.join(self.dir, "index.md")
149 self.readme_fp = os.path.join(self.dir, "README.md")
150 self.metadata_fp = os.path.join(self.dir, "metadata.yaml")
151 self.docker_folder = os.path.join(self.dir, "docker")
152 self.dockerfile_fp = os.path.join(self.docker_folder, "Dockerfile")
153 self.slides_folder = os.path.join(self.dir, "slides")
154
155 # TESTS
156 def exists(self):
157 """Test if the topic exists."""
158 return os.path.isdir(self.dir)
159
160 # OTHER METHODS
161 def create_topic_structure(self):
162 """Create the skeleton of a new topic.
163
164 1. create the folder and its structure
165 2. update the index.md to match your topic's name
166 3. fill the metadata
167 4. add a symbolic link to the metadata.yaml from the metadata folder
168 """
169 # create the folder and its structure
170 os.makedirs(self.dir)
171 self.img_folder = os.path.join(self.dir, "images")
172 os.makedirs(self.img_folder)
173 self.tuto_folder = os.path.join(self.dir, "tutorials")
174 os.makedirs(self.tuto_folder)
175
176 # create the index.md and add the topic name
177 self.index_fp = os.path.join(self.dir, "index.md")
178 with open(self.index_fp, 'w') as index_f:
179 index_f.write(
180 templates.render(INDEX_FILE_TEMPLATE, **{'topic': self.name}))
181
182 # create the README file
183 self.readme_fp = os.path.join(self.dir, "README.md")
184 with open(self.readme_fp, 'w') as readme_f:
185 readme_f.write(
186 templates.render(README_FILE_TEMPLATE, **{'topic': self.title}))
187
188 # create the metadata file
189 self.metadata_fp = os.path.join(self.dir, "metadata.yaml")
190 save_to_yaml(self.export_metadata_to_ordered_dict(), self.metadata_fp)
191
192 # create Dockerfile
193 self.docker_folder = os.path.join(self.dir, "docker")
194 os.makedirs(self.docker_folder)
195 self.dockerfile_fp = os.path.join(self.docker_folder, "Dockerfile")
196 with open(self.dockerfile_fp, 'w') as dockerfile:
197 dockerfile.write(
198 templates.render(
199 DOCKER_FILE_TEMPLATE,
200 **{'topic_name': self.name, 'topic_title': self.title}))
201
202 # create empty introduction slides
203 self.slides_folder = os.path.join(self.dir, "slides")
204 os.makedirs(self.slides_folder)
205 self.intro_slide_fp = os.path.join(self.slides_folder, "introduction.html")
206 with open(self.intro_slide_fp, 'w') as intro_slide_f:
207 intro_slide_f.write(
208 templates.render(
209 INTRO_SLIDES_FILE_TEMPLATE,
210 **{'title': "Introduction to %s" % self.title, 'type': "introduction"}))
211
212 # add a symbolic link to the metadata.yaml
213 metadata_dir = "metadata"
214 if not os.path.isdir(metadata_dir):
215 os.makedirs(metadata_dir)
216 os.chdir(metadata_dir)
217 os.symlink(os.path.join("..", self.metadata_fp), "%s.yaml" % self.name)
218 os.chdir("..")