annotate cp_common_functions.py @ 0:4395ea2a01f4 draft

"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
author bgruening
date Sat, 06 Feb 2021 10:00:10 +0000
parents
children bad171ed1e96
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
1 INDENTATION = " "
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
2 LINE_NUM_MODULES = 4
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
3
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
4
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
5 def get_json_value(json_input, keys_path):
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
6 """Returns the value specified in keys_path (using dot notation) or an empty string if the key doesn't exist"""
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
7 if not isinstance(json_input, dict):
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
8 return ""
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
9 keys = keys_path.split(".")
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
10 try:
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
11 value = json_input[keys[0]]
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
12 for key in keys[1:]:
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
13 value = value[key]
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
14 return value
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
15 except KeyError:
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
16 return ""
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
17
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
18
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
19 def concat_conditional(a, b):
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
20 if a == "" or b == "":
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
21 return ""
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
22 else:
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
23 return f"{a}_{b}"
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
24
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
25
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
26 def get_total_number_of_modules(pipeline_lines):
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
27 """Gets the number of modules from the header of the previous pipeline"""
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
28 number_of_modules = pipeline_lines[LINE_NUM_MODULES].strip().split(':')[1]
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
29 return int(number_of_modules)
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
30
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
31
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
32 def get_pipeline_lines(input_pipeline):
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
33 """Returns a list with the lines in the .cppipe file"""
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
34 with open(input_pipeline) as f:
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
35 lines = f.readlines()
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
36 return lines
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
37
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
38
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
39 def update_module_count(pipeline_lines, count):
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
40 """Updates the number of modules in the .cppipe header"""
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
41 module_count_entry = pipeline_lines[LINE_NUM_MODULES].strip().split(':')[0]
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
42 pipeline_lines[4] = f"{module_count_entry}:{count}\n"
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
43 return pipeline_lines
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
44
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
45
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
46 def write_pipeline(filename, lines_pipeline):
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
47 """Writes the lines composing the pipeline into a file"""
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
48 with open(filename, "w") as f:
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
49 f.writelines(lines_pipeline)
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
50
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
51
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
52 def build_header(module_name, module_number):
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
53 """Creates the first line of a module given the name and module number"""
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
54 result = "|".join([f"{module_name}:[module_num:{module_number}",
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
55 "svn_version:\\'Unknown\\'",
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
56 "variable_revision_number:4",
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
57 "show_window:False",
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
58 "notes:\\x5B\\x5D",
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
59 "batch_state:array(\\x5B\\x5D, dtype=uint8)",
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
60 "enabled:True",
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
61 "wants_pause:False]\n"])
4395ea2a01f4 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 35da2dcd86747c9bff138e100dbe08c6106f3780"
bgruening
parents:
diff changeset
62 return result