comparison env/lib/python3.7/site-packages/planemo/training/utils.py @ 0:26e78fe6e8c4 draft

"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
author shellac
date Sat, 02 May 2020 07:14:21 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:26e78fe6e8c4
1 """Module contains code for the Requirement, Reference and some general functions for training."""
2
3 import collections
4
5 import oyaml as yaml
6
7
8 class Requirement(object):
9 """Class to describe a training requirement."""
10
11 def __init__(self, req_type="internal", topic_name="introduction", title=None, tutorials=None, link=None):
12 """Init a Requirement instance."""
13 self.type = req_type
14 self.topic_name = topic_name
15 self.tutorials = tutorials
16 self.title = title
17 self.link = link
18
19 def init_from_dict(self, metadata):
20 """Init from a dictionary generated by export_to_ordered_dict."""
21 self.type = metadata['type']
22 if self.type == 'internal':
23 self.topic_name = metadata['topic_name']
24 if 'tutorials' in metadata:
25 self.tutorials = metadata['tutorials']
26 else:
27 self.title = metadata['title']
28 if self.type == 'external':
29 self.link = metadata['link']
30
31 def export_to_ordered_dict(self):
32 """Export the requirement into an ordered dictionary."""
33 req = collections.OrderedDict()
34 req['type'] = self.type
35 if self.type == 'internal':
36 req['topic_name'] = self.topic_name
37 if self.tutorials:
38 req['tutorials'] = self.tutorials
39 else:
40 req['title'] = self.title
41 if self.type == 'external':
42 req['link'] = self.link
43 return req
44
45
46 def load_yaml(filepath):
47 """Load the content of a YAML file to a dictionary."""
48 with open(filepath, "r") as m_file:
49 content = yaml.safe_load(m_file)
50 return content
51
52
53 def save_to_yaml(content, filepath):
54 """Save a dictionary to a YAML file."""
55 with open(filepath, 'w') as stream:
56 yaml.safe_dump(content,
57 stream,
58 indent=2,
59 default_flow_style=False,
60 default_style='',
61 explicit_start=True,
62 encoding='utf-8',
63 allow_unicode=True)