annotate server.py @ 0:b58b229c4cbf draft

planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
author lain
date Fri, 03 Mar 2023 14:10:24 +0000
parents
children 7e3085fc60c1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1 #!/usr/bin/env python3
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
2
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
3 import atexit
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
4 import csv
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
5 import http.server
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
6 import json
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
7 import logging
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
8 import os
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
9 import re
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
10 import shutil
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
11 import socketserver
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
12 import sys
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
13 import tempfile
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
14 import yaml
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
15
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
16 TAB_LIST_PLACEHOLDER = "TAB_LIST_PLACEHOLDER"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
17 MS_PEAK_VALUES_PLACEHOLDER = "MS_PEAK_VALUES_PLACEHOLDER"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
18 COMPOUND_NAME_PLACEHOLDER = "COMPOUND_NAME_PLACEHOLDER"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
19 TAB_INDEX_PLACEHOLDER = "TAB_INDEX_PLACEHOLDER"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
20 EMBED_JS_PLACEHOLDER = "EMBED_JS"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
21 ACTIVE_TAB_PLACEHOLDER = "ACTIVE_TAB_PLACEHOLDER"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
22 ADD_SPECTRUM_FORM = "ADD_SPECTRUM_FORM"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
23 PRODUCE_JSON_PLACEHOLDER = "PRODUCE_JSON_PLACEHOLDER"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
24
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
25 COMPOUND_REF = "compound-ref"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
26 COMPOUND_MIX = "compound-mix"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
27
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
28 END_MS_PEAK_VALUES_PLACEHOLDER = " ]"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
29 MS_DATA_COLUMN_NUMBER = 9
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
30 DEFAULT_MS_PEAK_VALUES = (
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
31 "[\n"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
32 + (" [" + ','.join([' ""'] * MS_DATA_COLUMN_NUMBER) + "],\n") * 17
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
33 + END_MS_PEAK_VALUES_PLACEHOLDER
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
34 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
35
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
36 FRAGNOT_HEADER = {
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
37 "m/z": "fragment_mz",
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
38 "absolute_intensity": "abs_intensity",
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
39 "relative_intensity": "rel_intensity",
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
40 "theo_mass": "",
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
41 "delta_ppm": "ppm",
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
42 "rdbequiv": "",
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
43 "composition": "",
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
44 "attribution": "fragment",
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
45 }
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
46
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
47 MS_2_SNOOP_HEADER = {
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
48 "name": str,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
49 "inchikey": str,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
50 "composition": str,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
51 "fragment": str,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
52 "fragment_mz": str,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
53 "ppm": str,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
54 "fileid": str,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
55 "correlation": str,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
56 "abs_intensity": lambda x:float(x) * 100,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
57 "rel_intensity": lambda x:float(x) * 100,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
58 "valid_corelation": str
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
59 }
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
60
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
61
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
62 class ConfigException(ValueError):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
63 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
64 An exception raised when something went wrong in the config and we
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
65 cannot continue - i.e: when there's no token for peakforest
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
66 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
67
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
68 class YAMLConfig(dict):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
69
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
70 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
71 Dictionary that handles key with dot in them:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
72 test["truc.chose"]
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
73 is equivalant to
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
74 test["truc"]["chose"]
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
75 Assignation works too.
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
76 Add the possibility to use placeholders:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
77 --- yaml
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
78 test: {{ truc.chose }}
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
79 truc:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
80 chose: bidule
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
81 ---
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
82 here, test's value is "bidule"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
83 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
84
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
85 def __init__(self, *args, **kwargs):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
86 meta_conf = kwargs.pop("__meta_config__", {})
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
87 self._debug = meta_conf.get("__debug__", False)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
88 self._stream_name = meta_conf.get("__debug_stream__", "stdout")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
89 self._debug_stream = getattr(sys, self._stream_name)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
90 self._only_root_debug = meta_conf.get("__only_root_debug__", False)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
91 if "__root__" in kwargs:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
92 if self._only_root_debug:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
93 self._debug = False
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
94 self._name = kwargs.pop("__name__")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
95 self._debugger("Is not root config.")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
96 self._root = kwargs.pop("__root__")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
97 else:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
98 self._name = "root"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
99 self._debugger("Is root config.")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
100 self._root = self
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
101 super().__init__(*args, **kwargs)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
102 for key, value in self.copy().items():
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
103 if isinstance(value, dict) and not isinstance(value, YAMLConfig):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
104 self._debugger(f"Parsing sub-config for {key}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
105 self[key] = self._propagate(value, key)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
106 self._replace_placeholders(self)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
107 self._extract_defaults()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
108
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
109 def _propagate(self, sub_dict, name):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
110 if isinstance(sub_dict, dict) and not isinstance(sub_dict, self.__class__):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
111 return YAMLConfig(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
112 **sub_dict,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
113 __name__=name,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
114 __root__=self._root,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
115 __meta_config__={
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
116 "__debug__": self._debug,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
117 "__debug_stream__": self._stream_name,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
118 "__only_root_debug__": self._only_root_debug,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
119 }
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
120 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
121 return sub_dict
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
122
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
123 def _debugger(self, message):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
124 if self._debug:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
125 self._debug_stream.write(f"[{self._name}]: {message}\n")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
126 self._debug_stream.flush()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
127
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
128 def __getattr__(self, attr):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
129 if attr in self:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
130 return self[attr]
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
131 if '.' in attr:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
132 attr, sub = attr.split('.', 1)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
133 return getattr(getattr(self, attr), sub)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
134 return super().__getattribute__(attr)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
135
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
136 def _replace_placeholders(self, subpart):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
137 self._debugger("Replacing placeholders...")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
138 for sub_key, sub_item in subpart.copy().items():
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
139 if isinstance(sub_item, str):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
140 for placeholder in re.findall("{{ (?P<placeholder>.*?) }}", sub_item):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
141 if placeholder not in self._root:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
142 self._debugger(f"Could not fine replacement for {placeholder}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
143 continue
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
144 replacement = self._root[placeholder]
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
145 if isinstance(replacement, str):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
146 self._debugger(f"Found placeholder: {placeholder} -> {replacement}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
147 sub_item = sub_item.replace(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
148 "{{ " + placeholder + " }}",
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
149 replacement
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
150 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
151 else:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
152 self._debugger(f"Found placeholder: {placeholder} -> {replacement.__class__.__name__}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
153 sub_item = self._propagate(replacement, placeholder)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
154 dict.__setitem__(subpart, sub_key, sub_item)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
155 elif isinstance(sub_item, dict):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
156 super().__setitem__(sub_key, self._propagate(sub_item, sub_key))
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
157
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
158 def _extract_defaults(self):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
159 if self._root is not self:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
160 return
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
161 if "defaults" not in self:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
162 self._debugger("No defaults here.")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
163 return
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
164 if "arguments" not in self:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
165 self._debugger("Arguments creation...")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
166 self["arguments"] = self._propagate({}, "arguments")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
167 self._debugger("Populating arguments with defaults values")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
168 for key, value in self.defaults.items():
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
169 if key not in self:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
170 if isinstance(value, dict):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
171 value = self._propagate(value, key)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
172 self.arguments[key] = value
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
173 self._debugger(f"Default {key} = {value}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
174
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
175 def __setitem__(self, key, value):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
176 if isinstance(value, dict):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
177 value = self._propagate(value, key)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
178 if "." not in key:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
179 return super().__setitem__(key, value)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
180 curent = self
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
181 key, subkey = key.rsplit(".", 1)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
182 self[key][subkey] = value
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
183
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
184 def __getitem__(self, key):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
185 if super().__contains__(key):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
186 return super().__getitem__(key)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
187 if "." not in key:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
188 return super().__getitem__(key)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
189 curent = self
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
190 while "." in key:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
191 key, subkey = key.split(".", 1)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
192 curent = curent[key]
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
193 key = subkey
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
194 if subkey not in curent:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
195 curent[subkey] = self._propagate({}, subkey)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
196 result = curent[subkey]
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
197 return result
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
198
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
199 def __contains__(self, key):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
200 if "." not in key:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
201 return super().__contains__(key)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
202 key, subkey = key.split(".", 1)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
203 if not super().__contains__(key):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
204 return False
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
205 return subkey in self[key]
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
206
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
207 def copy(self):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
208 return {
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
209 key: (
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
210 value if not isinstance(value, dict)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
211 else value.copy()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
212 ) for key, value in self.items()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
213 }
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
214
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
215 class YAMLParameters(YAMLConfig):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
216
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
217 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
218 Parses parameters from the command line and put them
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
219 in the config.
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
220 Uses the config to know which parameter is recognized, or not,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
221 to know the metadata (author, version),
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
222 which command is a flag, is optional, the help strings, etc...
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
223 Assigns default small parameter if not defined in the "shortcut"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
224 section of the config file.
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
225 CLI config must be in the root section "parameters":
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
226 ---
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
227 parameters:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
228 mandatory:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
229 input: input file path
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
230 flags:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
231 help: Show this help
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
232 optional:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
233 method: "default is {{ defaults.method }}"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
234 meta:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
235 author: Lain Pavot
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
236 version: 1.1.0
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
237 shortcuts:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
238 help: h
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
239 ## will autogenerate -i for input and -m for method
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
240 ---
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
241 default parameters are searched in the "default" root section.
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
242 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
243
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
244 def __init__(self, *args, **kwargs):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
245 super().__init__(*args, **kwargs)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
246 self._errors = list()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
247 if not self.parameters.shortcuts:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
248 self.parameters["shortcuts"] = YAMLConfig()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
249 self._mandatory = self.parameters.mandatory
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
250 self._optional = self.parameters.optional
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
251 self._flags = {
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
252 flag: False
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
253 for flag in self.parameters.flags
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
254 }
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
255 self._all_params = self._optional.copy()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
256 self._all_params.update(self._mandatory)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
257 self._all_params.update(self._flags)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
258 self._small_params = dict()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
259 self._determine_small_params()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
260
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
261 @property
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
262 def in_error(self):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
263 return bool(self._errors)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
264
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
265 @property
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
266 def sorted_keys(self):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
267 return sorted(self._all_params.keys())
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
268
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
269 @property
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
270 def sorted_items(self):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
271 return sorted(self._all_params.items())
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
272
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
273 def _determine_small_params(self, verbose=False):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
274 self._small_params = (self.parameters.shortcuts or {}).copy()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
275 chars = list(map(chr, range(97, 123))) + list(map(chr, range(65, 91)))
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
276 all_params = self._all_params.copy()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
277 for long, short in self._small_params.items():
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
278 chars.remove(short)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
279 del all_params[long]
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
280 for param in all_params.copy().keys():
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
281 for operation in (
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
282 lambda x:x[0], ## select first char
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
283 lambda x:x.split('-', 1)[-1][0], ## first char after -
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
284 lambda x:x.split('_', 1)[-1][0], ## first char after _
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
285 lambda x:x.split('.', 1)[-1][0], ## first char after .
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
286 lambda x:x[0].upper(), ## select first char
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
287 lambda x:x.split('-', 1)[-1][0].upper(), ## first char after -
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
288 lambda x:x.split('_', 1)[-1][0].upper(), ## first char after _
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
289 lambda x:x.split('.', 1)[-1][0].upper(), ## first char after .
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
290 lambda x: chars[0], ## first letter in the alphabet
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
291 ):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
292 char = operation(param)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
293 if char not in self._small_params.values():
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
294 self._small_params[param] = char
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
295 chars.remove(char)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
296 del all_params[param]
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
297 break
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
298
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
299 def _get_parameter_index(self, parameter, original):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
300 if f"--{parameter}" in sys.argv:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
301 return sys.argv.index(f"--{parameter}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
302 parameter = self._small_params[original]
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
303 if f"-{parameter}" in sys.argv:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
304 return sys.argv.index(f"-{parameter}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
305 return None
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
306
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
307 def as_parameter(self, string):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
308 return (
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
309 string
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
310 .replace('.', '-')
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
311 .replace('_', '-')
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
312 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
313
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
314 def show_version(self):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
315 print(self.parameters.meta.version)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
316
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
317 def show_help(self):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
318 parameters = [
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
319 f"-{self._small_params[arg]}|--{self.as_parameter(arg)} {arg}"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
320 for arg in self._mandatory
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
321 ] + [
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
322 f"[-{self._small_params[arg]}|--{self.as_parameter(arg)} {arg}]"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
323 for arg in self._optional
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
324 ] + [
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
325 f"[-{self._small_params[arg]}|--{self.as_parameter(arg)}]"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
326 for arg in self._flags
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
327 ]
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
328 print(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
329 f"Usage: {__file__} " + ' '.join(parameters)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
330 + "\n\n"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
331 + '\n'.join(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
332 f" -{self._small_params[args]}|--{self.as_parameter(args)}: {help_str}"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
333 for args, help_str in self.sorted_items
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
334 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
335 + "\n\n"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
336 + '\n'.join(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
337 f"{key}: {value}"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
338 for key, value in self.parameters.meta.items()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
339 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
340 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
341 sys.exit(0)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
342
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
343 def parse_args(self):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
344 errors = list()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
345 for kind in ("mandatory", "optional", "flags"):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
346 keys = list(sorted(getattr(self, f"_{kind}").keys()))
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
347 for original_param, actual_param in zip(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
348 keys,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
349 map(self.as_parameter, keys),
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
350 ):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
351 if original_param in self.defaults:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
352 self.arguments[original_param] = self.defaults[original_param]
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
353 elif kind == "flags":
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
354 self.arguments[original_param] = False
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
355 parser = getattr(self, f"parse_{kind}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
356 if (error := parser(original_param, actual_param)):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
357 errors.append(error)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
358 self._errors = errors
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
359 return self
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
360
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
361 def parse_mandatory(self, original, actual):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
362 if (index := self._get_parameter_index(actual, original)) is None:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
363 return f"The parameter --{actual} is mandatory."
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
364 if index == len(sys.argv) - 1:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
365 return f"The parameter --{actual} needs a value."
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
366 self.arguments[original] = sys.argv[index + 1]
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
367
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
368 def parse_optional(self, original, actual):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
369 if (index := self._get_parameter_index(actual, original)) is None:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
370 return
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
371 if index == len(sys.argv) - 1:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
372 return f"The parameter --{actual} needs a value."
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
373 self.arguments[original] = sys.argv[index + 1]
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
374
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
375 def parse_flags(self, original, actual):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
376 if (index := self._get_parameter_index(actual, original)) is None:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
377 return
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
378 self.arguments[original] = True
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
379
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
380 def parse_config(**kwargs):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
381 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
382 opens the config file, extract it using pyyaml's safe loader
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
383 and tries to extract and apply a maximum of informations/directives
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
384 from the config:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
385 - token retrieval
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
386 - workdir management
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
387 - tempfile management
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
388 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
389 root_dir = os.path.dirname(os.path.abspath(__file__))
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
390 with open(os.path.join(root_dir, "config.yml")) as config_file:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
391 config = YAMLConfig(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
392 **yaml.load(config_file.read(), Loader=yaml.SafeLoader),
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
393 **kwargs
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
394 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
395
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
396 if not config.token.value:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
397 if config.token.use_file:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
398 if (not os.path.exists(path := config.token.file_path)):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
399 raise ConfigException("Missing token value or token file.")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
400 with open(path) as token_file:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
401 config.token["value"] = token_file.read()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
402 elif config.defaults.peakforest.token:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
403 config.token["value"] = config.defaults.peakforest.token
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
404
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
405 if config.workdir.create_tmp:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
406 tmp_dir = tempfile.mkdtemp()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
407 atexit.register(lambda:shutil.rmtree(tmp_dir))
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
408 else:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
409 tmp_dir = tempfile.gettempdir()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
410 config.workdir["tmp_dir"] = tmp_dir
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
411
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
412 config["root_dir"] = root_dir
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
413 config["tab_list"] = []
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
414 config["form_template"] = os.path.join(root_dir, config.templates.form)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
415 config["meta_template"] = os.path.join(root_dir, config.templates.main)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
416 config["js_template"] = os.path.join(root_dir, config.templates.js)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
417 config["tab_list_template"] = os.path.join(root_dir, config.templates.tab_list)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
418 config["placeholders"] = dict()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
419 config.placeholders[MS_PEAK_VALUES_PLACEHOLDER] = DEFAULT_MS_PEAK_VALUES
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
420 config.placeholders[TAB_INDEX_PLACEHOLDER] = "1"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
421 config.placeholders[ACTIVE_TAB_PLACEHOLDER] = "active"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
422 config.placeholders[ADD_SPECTRUM_FORM] = ""
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
423 config.placeholders[EMBED_JS_PLACEHOLDER] = ""
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
424 config.placeholders[TAB_LIST_PLACEHOLDER] = ""
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
425 config.placeholders["DEFAULT_MIN_MZ"] = "50"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
426 config.placeholders["DEFAULT_MAX_MZ"] = "500"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
427 config.placeholders["DEFAULT_RESOLUTION_LOW"] = ""
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
428 config.placeholders["DEFAULT_RESOLUTION_HIGH"] = "selected=\"selected\""
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
429 config.placeholders["DEFAULT_RESOLUTION_UNSET"] = ""
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
430 config.placeholders["DEFAULT_MIN_RT"] = "0.9"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
431 config.placeholders["DEFAULT_MAX_RT"] = "1.4"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
432 return config
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
433
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
434 def parse_parameters(config):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
435 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
436 parses command line and checks provided values are acceptable/usable.
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
437 Raises some error if not.
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
438 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
439 parameters = YAMLParameters(**config)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
440 parameters.parse_args()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
441
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
442 parameters["json_result"] = []
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
443
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
444 get_logger(parameters)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
445
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
446 arguments = parameters.arguments
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
447 if arguments.help:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
448 parameters.show_help()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
449 sys.exit(0)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
450
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
451 if arguments.version:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
452 parameters.show_version()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
453 sys.exit(0)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
454
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
455 if parameters.in_error:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
456 raise ValueError(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
457 "Some errors occured during parameters extraction: \n"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
458 + '\n'.join(parameters.errors)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
459 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
460
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
461 if arguments.sample_type == COMPOUND_MIX:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
462 parameters["form_template"] = os.path.join(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
463 parameters["root_dir"],
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
464 parameters.templates.form_mix
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
465 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
466 parameters["meta_template"] = os.path.join(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
467 parameters["root_dir"],
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
468 parameters.templates.main_mix
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
469 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
470 elif arguments.sample_type == COMPOUND_REF:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
471 parameters["form_template"] = os.path.join(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
472 parameters["root_dir"],
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
473 parameters.templates.form_ref
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
474 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
475 parameters["meta_template"] = os.path.join(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
476 parameters["root_dir"],
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
477 parameters.templates.main_ref
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
478 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
479
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
480 arguments["produce_json"] = (
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
481 "output_json" in arguments
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
482 and arguments["output_json"] != ""
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
483 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
484 if arguments.produce_json:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
485 parameters.placeholders[PRODUCE_JSON_PLACEHOLDER] = "true"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
486 parameters.json_result = []
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
487 arguments["output_json"] = os.path.abspath(arguments["output_json"])
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
488 atexit.register(save_json, parameters)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
489 else:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
490 parameters.placeholders[PRODUCE_JSON_PLACEHOLDER] = "false"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
491
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
492 if arguments.run_dry_html:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
493 arguments["do_run_dry"] = True
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
494 parameters.generated["html"] = os.path.abspath(arguments.run_dry_html)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
495
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
496 if arguments.run_dry_js:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
497 arguments["do_run_dry"] = True
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
498 parameters.generated["js"] = os.path.abspath(arguments.run_dry_js)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
499
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
500 if arguments.do_run_dry:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
501 parameters.logger.info("Dry run. Server will ne be run.")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
502 if arguments.run_dry_html:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
503 parameters.logger.info(f"HTML file will be put in {arguments.run_dry_html}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
504 if arguments.run_dry_js:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
505 parameters.logger.info(f"JS file will be put in {arguments.run_dry_js}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
506
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
507 if arguments.peakforest.token:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
508 config.token["value"] = arguments.peakforest.token
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
509 if not config.token.value:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
510 raise ConfigException(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
511 "No token provided. We will not be able to connect to peakforest."
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
512 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
513
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
514 if os.path.exists(arguments.input):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
515 single_file = True
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
516 file_paths = [arguments.input]
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
517 else:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
518 path_list = arguments.input.split(',')
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
519 if all(map(os.path.exists, path_list)):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
520 single_file = False
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
521 file_paths = path_list
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
522 else:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
523 raise ValueError(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
524 f"Some files cannot be found: "
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
525 + ', '.join(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
526 path for path in path_list
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
527 if not os.path.exists(path)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
528 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
529 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
530 arguments["input"] = list(map(os.path.abspath, file_paths))
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
531
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
532 if single_file:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
533 arguments["name"] = [arguments.name]
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
534 arguments["raw_metadata"] = [arguments.raw_metadata]
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
535 parameters.logger.info(f"Single file processing: {arguments.input}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
536 else:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
537 parameters.logger.info(f"Multiple file processing:")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
538 arguments["raw_metadata"] = arguments.raw_metadata.split(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
539 arguments.raw_metadata_sep
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
540 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
541 if not arguments.name:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
542 arguments["name"] = arguments["raw_metadata"]
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
543 else:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
544 arguments["name"] = arguments.name.split(',')
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
545 for i in range(len(arguments.name)):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
546 parameters.logger.info(f" - file: {arguments.input[i]}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
547 parameters.logger.info(f" - name: {arguments.name[i]}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
548 parameters.logger.info(f" - metadata: {arguments.raw_metadata[i]}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
549 parameters.logger.info(f" ")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
550 if (
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
551 len(arguments.name) != len(arguments.raw_metadata)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
552 or len(arguments.name) != len(arguments.input)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
553 ):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
554 raise ValueError(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
555 "name, raw_metadata and input parameters have different lengths: \n"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
556 f"input is {len(arguments.input)} elements long, "
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
557 f"raw_metadata is {len(arguments.raw_metadata)} elements long "
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
558 f"and name is {len(arguments.name)} elements long."
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
559 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
560 if arguments.spectrum_type == "LC_MS":
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
561 arguments["scan_type"] = "ms"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
562 elif arguments.spectrum_type == "LC_MSMS":
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
563 arguments["scan_type"] = "ms2"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
564 if arguments.method == "test":
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
565 if arguments.spectrum_type == "LC_MS":
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
566 arguments["method"] = "cf_pfem_urine_qtof"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
567 else:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
568 arguments["method"] = "cf_pfem_urine_method1_qtof-msms"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
569 if arguments["sample_type"] == COMPOUND_MIX:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
570 check_mix_compound_files(parameters)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
571 more_info_in_logs(parameters)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
572 return parameters
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
573
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
574 def check_mix_compound_files(parameters):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
575 arguments = parameters.arguments
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
576 try:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
577 numbarz = [
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
578 list(map(int, os.path.basename(metadata).split("_", 1)[0].split("-")))
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
579 for metadata in arguments.raw_metadata
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
580 ]
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
581 except ValueError:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
582 parameters.logger.error(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
583 "Metadata/file names does not start with `[0-9]+-[0-9]+_.*` . "
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
584 "This is necessary in the case of compounds mix."
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
585 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
586 sys.exit(-1)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
587 runs, samples = zip(*numbarz)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
588 if not all(runs[0] == i for i in runs[1:]):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
589 parameters.logger.error(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
590 "Run numbers in metadata/file names are not identical. "
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
591 "You mixed some files."
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
592 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
593 sys.exit(-1)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
594 length = len(samples)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
595 if list(sorted(samples)) != list(range(1, length+1)):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
596 if not all(samples.count(i) == 1 for i in samples):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
597 parameters.logger.error("Some samples are duplicated. ")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
598 else:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
599 parameters.logger.error("Some samples files are missing. ")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
600 sys.exit(-1)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
601
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
602 def more_info_in_logs(config):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
603 arguments = config.arguments
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
604 if arguments.embed_js:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
605 config.logger.info(f"JS will be embed in HTML page to form a HTML bundle.")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
606 else:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
607 config.logger.info(f"JS are separated files, needed to be served.")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
608 config.logger.info(f"Choosen parameters:")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
609 config.logger.info(f" - method: {arguments.method}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
610 config.logger.info(f" - peakforest instance: {arguments.peakforest.url}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
611 config.logger.info(f" - polarity instance: {arguments.polarity}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
612 config.logger.info(f" - spectrum type: {arguments.spectrum_type}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
613 config.logger.info(f" - scan type: {arguments.scan_type}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
614 config.logger.info(f" - produce JSON: {arguments.produce_json}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
615 config.logger.info(f" - sample type: {arguments.sample_type}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
616
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
617 def process_all_files(config):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
618 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
619 for each file and its metadata, read and process them,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
620 then fills the meta html template file with the whole result.
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
621 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
622 arguments = config.arguments
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
623 extra_defaults = [
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
624 process_fragnot_metadata(metadata, config)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
625 for metadata in arguments.raw_metadata
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
626 ]
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
627 for i, name in enumerate(arguments.name):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
628 extra_defaults[i]["name"] = name
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
629
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
630 if not extra_defaults:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
631 extra_defaults = [{}] * len(arguments.input)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
632
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
633 index = 0
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
634 for input_path, extra_default in zip(arguments.input, extra_defaults):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
635 config.logger.info(f"Processing file at {input_path}...")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
636 curent_defaults = arguments.copy()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
637 curent_defaults.update(extra_default)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
638 if config.arguments.verbose:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
639 config.logger.info(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
640 "[VERBOSE] Defaults for curent file: "
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
641 + ';'.join(f"{key}={value}" for key, value in curent_defaults.items())
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
642 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
643 tsv_content, tsv_data_extractor = read_input(input_path, config)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
644 index = process_tsv(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
645 tsv_content,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
646 tsv_data_extractor,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
647 config,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
648 defaults_data = curent_defaults,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
649 index = index+1,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
650 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
651 if arguments.embed_js:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
652 config.logger.info(f"Embeding JS in HTML file... ")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
653 for index in range(len(config.tab_list)):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
654 config.placeholders[EMBED_JS_PLACEHOLDER] += "<script type='text/javascript'>"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
655 with open(f"add-one-spectrum-{index+1}.js") as js_file:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
656 config.placeholders[EMBED_JS_PLACEHOLDER] += js_file.read()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
657 config.placeholders[EMBED_JS_PLACEHOLDER] += "</script>"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
658 config.placeholders[EMBED_JS_PLACEHOLDER] += "\n"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
659 config.logger.info(f" - add-one-spectrum-{index+1}.js embed.")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
660 config.placeholders[TAB_LIST_PLACEHOLDER] = "\n".join(config.tab_list)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
661 else:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
662 config.placeholders[EMBED_JS_PLACEHOLDER] += "<script type='text/javascript'>"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
663 config.placeholders[EMBED_JS_PLACEHOLDER] += "</script>"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
664 config.placeholders[EMBED_JS_PLACEHOLDER] += "\n".join(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
665 [""] + [
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
666 " "*12 + f"<script src=\"./add-one-spectrum-{index+1}.js\"></script>"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
667 for index in range(len(config.tab_list))
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
668 ]
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
669 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
670 config.placeholders[EMBED_JS_PLACEHOLDER] += "\n"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
671 config.placeholders[TAB_LIST_PLACEHOLDER] = "\n".join(config.tab_list)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
672
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
673 fill_template("meta_template", "pf_path", config)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
674
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
675 def fill_template(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
676 template_name,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
677 output_name,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
678 config,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
679 additional_placeholders=dict()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
680 ):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
681 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
682 Fills a template, replaces the placeholders.
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
683 Either outputs the result in a given file, or returns it if path is none.
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
684 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
685 template_path = config[template_name]
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
686 config.logger.debug(f"Filling template {template_name} at {template_path}...")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
687 with open(template_path) as template_file:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
688 template_content = template_file.read()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
689 placeholders = config.placeholders.copy()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
690 placeholders.update(additional_placeholders)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
691 for placeholder, replacement in placeholders.items():
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
692 if not placeholder.startswith(config.templates.placeholders.start):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
693 placeholder = placeholder.join((
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
694 config.templates.placeholders.start,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
695 config.templates.placeholders.stop
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
696 ))
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
697 template_content = template_content.replace(placeholder, replacement)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
698 if output_name is None:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
699 config.logger.debug(f"Returning template content")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
700 return template_content
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
701 output_path = config[output_name]
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
702 if "{{ index }}" in output_path:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
703 index_value = additional_placeholders["{{ index }}"]
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
704 config.logger.debug(f"Changing index value for {index_value}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
705 output_path = output_path.replace("{{ index }}", index_value)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
706 config.logger.debug(f"Full output path {output_path}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
707 with open(output_path, "w") as output_file:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
708 output_file.write(template_content)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
709
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
710 def read_input(input_path, config):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
711 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
712 reads a tsv file and determin its processor, based on its header.
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
713 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
714 with open(input_path) as input_file:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
715 config.logger.info(f"Reading {input_path}...")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
716 tsv_file = csv.reader(input_file, delimiter='\t')
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
717 header = next(tsv_file)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
718 tsv_file = list(tsv_file)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
719 config.logger.info(f"Header is: {', '.join(header)}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
720 if header == list(FRAGNOT_HEADER):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
721 config.logger.info(f"Fragnot recognized.")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
722 processor = fragnot_extractor
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
723 return uniformize_fragnot(tsv_file, header), processor
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
724 else:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
725 config.logger.info(f"MS2Snoop recognized.")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
726 processor = ms2snoop_extractor
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
727 return uniformize_ms2snoop(tsv_file, header), processor
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
728
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
729 def uniformize_fragnot(content, header):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
730 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
731 sorts fragnot data so they appear always in the same order
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
732 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
733 return sorted(content, key=lambda x:(float(x[0]), float(x[4])))
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
734
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
735 def uniformize_ms2snoop(content, header):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
736 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
737 sorts ms2snoop data so they appear always in the same order
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
738 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
739 return sorted(content, key=lambda x:(x[0], float(x[4])))
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
740
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
741 def process_fragnot_metadata(raw_metadata, config):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
742 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
743 Tries to extract informations from the metadata provided by fragnot
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
744 files names.
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
745 Heavily based on regex defined in conf file.
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
746 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
747 regex = config.regex.copy()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
748 del regex["values"]
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
749 result = {}
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
750 config.logger.info(f"Extracting info from {raw_metadata}...")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
751 count = 0
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
752 for name, expression in regex.items():
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
753 if (match := re.search(expression, raw_metadata)):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
754 result[name] = match[name]
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
755 count += 1
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
756 did = "+ did"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
757 else:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
758 did = "- did not"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
759 if config.arguments.verbose:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
760 config.logger.info(f" {did} match {expression}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
761 config.logger.info(f"{count} useful informations extracted.")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
762 return result
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
763
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
764 def process_tsv(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
765 tsv_content,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
766 tsv_data_extractor,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
767 config,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
768 defaults_data={},
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
769 index=1
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
770 ):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
771 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
772 processes one tsv file, containing one or multiple compounds.
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
773 Creation of the peak table for each compound
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
774 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
775 tsv_content = list(tsv_content)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
776 curent_name, ms_data = get_ms_data(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
777 tsv_content[0],
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
778 tsv_data_extractor,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
779 defaults_data,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
780 config
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
781 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
782 _, second_ms_data = get_ms_data(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
783 tsv_content[1],
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
784 tsv_data_extractor,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
785 defaults_data,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
786 config
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
787 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
788 ms_peak_table = []
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
789 config.logger.info(f"Processing compound {curent_name}...")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
790
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
791 for line in tsv_content:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
792 name, new_ms_data = get_ms_data(line, tsv_data_extractor, defaults_data, config)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
793 if name != curent_name:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
794 new_compound(curent_name, index, ms_data, config, ms_peak_table)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
795 curent_name = name
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
796 index += 1
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
797 config.logger.info(f"Processing compound {curent_name}...")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
798 ms_peak_table = []
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
799 ms_data = new_ms_data
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
800 ms_peak_table.append(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
801 ", ".join(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
802 f'"{value}"' if value not in ("na", "NA")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
803 else '""'
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
804 for value in (
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
805 ms_data["fragment_mz"],
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
806 ms_data["abs_intensity"],
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
807 ms_data["rel_intensity"],
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
808 ms_data["ppm"],
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
809 ms_data["composition"],
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
810 ms_data["fragment"],
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
811 str(ms_data["valid_corelation"] == "TRUE").lower(),
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
812 "true" if ms_data.get("correlation") == "1" else "false"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
813 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
814 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
815 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
816 new_compound(curent_name, index, ms_data, config, ms_peak_table)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
817 return index
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
818
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
819 def get_ms_data(line, extractor, defaults, config):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
820 ms_data = defaults.copy()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
821 ms_data.update(extractor(config, *line))
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
822 return ms_data["name"], ms_data
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
823
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
824 def new_compound(name, index, ms_data, config, ms_peak_table):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
825 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
826 aggregates informations to form the peak table,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
827 adds the compound to the tab list,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
828 creates the js file for this tab
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
829 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
830 if len([x for x in ms_peak_table if x.split(", ")[7] == "\"true\""]) > 1:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
831 for i in range(len(ms_peak_table)):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
832 ms_peak_table[i] = ", ".join(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
833 ms_peak_table[i].split(", ")[:-1] + [", \"false\""]
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
834 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
835 config.placeholders[MS_PEAK_VALUES_PLACEHOLDER] = f"""[
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
836 {','.join('['+line+']' for line in ms_peak_table)}
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
837 ]"""
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
838 tab_list = fill_template(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
839 "tab_list_template",
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
840 None,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
841 config, {
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
842 COMPOUND_NAME_PLACEHOLDER: name,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
843 TAB_INDEX_PLACEHOLDER: str(index),
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
844 })
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
845 config.tab_list.append(tab_list)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
846 create_js_file(index, ms_data, config)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
847 config.placeholders[ADD_SPECTRUM_FORM] += fill_template(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
848 "form_template",
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
849 None,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
850 config,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
851 {TAB_INDEX_PLACEHOLDER: str(index)},
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
852 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
853 if index == 1:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
854 config.placeholders[ACTIVE_TAB_PLACEHOLDER] = ""
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
855
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
856 def fragnot_extractor(config, *line):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
857 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
858 Fragnot processor - extracts one fragnot line of content and
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
859 produces a uniformised output.
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
860 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
861 fragnot_data = {
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
862 FRAGNOT_HEADER[header]: line[i].strip()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
863 for i, header in enumerate(FRAGNOT_HEADER)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
864 }
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
865 fragnot_data["composition"] = "unknown"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
866 fragnot_data["valid_corelation"] = config.arguments.validation
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
867 return fragnot_data
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
868
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
869 def ms2snoop_extractor(config, *line):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
870 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
871 Fragnot processor - extracts one ms2snoop line of content and
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
872 produces a uniformised output.
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
873 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
874 ms2snoop_data = {
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
875 header: MS_2_SNOOP_HEADER[header](line[i])
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
876 for i, header in enumerate(MS_2_SNOOP_HEADER)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
877 }
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
878 return ms2snoop_data
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
879
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
880 def create_js_file(index, ms_data, config):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
881 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
882 fills the js template file for one tab (compound)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
883 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
884 if (method := ms_data["method"]):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
885 method = f'"{method}"'
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
886 else:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
887 method = "null"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
888 if config.arguments.verbose:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
889 config.logger.info(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
890 "[VERBOSE] "
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
891 + ';'.join(f"{key}={value}" for key, value in ms_data.items())
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
892 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
893 fill_template(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
894 "js_template",
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
895 "js_file",
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
896 config,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
897 {
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
898 TAB_INDEX_PLACEHOLDER: str(index),
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
899 "INCHIKEY_PLACEHOLDER": ms_data["inchikey"],
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
900 "DEFAULT_DATA": f"""{{
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
901 name: "{ms_data["name"]}",
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
902 inchikey: "{ms_data["inchikey"]}",
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
903 method: {method},
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
904 spectrum_type: "{ms_data["spectrum_type"]}",
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
905 scan_type: "{ms_data["scan_type"]}",
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
906 polarity: "{ms_data["polarity"]}",
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
907 resolution: "{ms_data["resolution"]}",
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
908 sample_type: "{ms_data["sample_type"]}",
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
909 }}""",
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
910 "{{ index }}": str(index)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
911 },
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
912 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
913
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
914 def prepare_workplace(config):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
915 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
916 prepares the directory we will work in.
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
917 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
918 if config.workdir.work_in_tmp:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
919 os.chdir(config.workdir.tmp_dir)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
920 config.logger.info(f"Moving to {os.getcwd()}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
921 if config.workdir.generate_in_tmp:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
922 gen_dir = config.workdir.tmp_dir
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
923 else:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
924 gen_dir = tempfile.gettempdir()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
925 config.workdir.tmp_dir = gen_dir
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
926 shutil.copy(os.path.join(config["root_dir"], "common.js"), gen_dir)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
927 config.logger.info(f"Outputs will be generated in {config.workdir.tmp_dir}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
928 return gen_dir
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
929
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
930 def get_hander_for(directory, config):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
931 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
932 generates the handler class for the directory we provide.
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
933 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
934 config["json_result"] = [{}] * len(config.tab_list)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
935
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
936 class HTTPHandler(http.server.SimpleHTTPRequestHandler):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
937
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
938 def __init__(self, *args, **kwargs):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
939 super().__init__(*args, **kwargs, directory=directory)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
940
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
941 def do_POST(self):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
942 content_length = int(self.headers.get("Content-Length"))
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
943 json_bytes = self.rfile.read(content_length).decode("utf-8")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
944 json_list = json.loads(json_bytes)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
945 for i, obj in enumerate(json_list):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
946 print(obj)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
947 if obj:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
948 config["json_result"][i] = obj
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
949 save_json(config)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
950 self.send_head()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
951 self.wfile.write(json_bytes.encode("utf-8"))
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
952 return
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
953
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
954 def do_GET(self):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
955 if self.path == "/quit":
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
956 self.path = "/"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
957 super().do_GET()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
958 exit(0)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
959 self.path = os.path.join(directory, self.path)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
960 if self.path == "/":
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
961 self.path = config.generated.html
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
962 return super().do_GET()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
963
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
964 return HTTPHandler
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
965
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
966
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
967 def save_json(config):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
968 json_string = json.dumps(config["json_result"])
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
969 print(json_string)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
970 with open(config.arguments.output_json, "w") as json_file:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
971 json_file.write(json_string)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
972
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
973 def run_server(config):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
974 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
975 prepare and runs the server, with the handler for the given directory
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
976 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
977 ip, port = config.network.ip, config.network.port
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
978 config.logger.debug(f"IP and port: {ip}:{port}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
979 socketserver.TCPServer.allow_reuse_address = True
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
980 config.logger.debug(f"Allow reuse adress.")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
981 handler = get_hander_for(config.workdir.tmp_dir, config)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
982 config.logger.debug(f"Created server handler for {config.workdir.tmp_dir}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
983 config.logger.debug(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
984 f"Content of directory {config.workdir.tmp_dir}: "
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
985 + "\n"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
986 + '\n'.join(sorted(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
987 f" - {path}"for path in os.listdir(config.workdir.tmp_dir)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
988 ))
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
989 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
990 config.logger.debug(f"Creating TCP server...")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
991 server = socketserver.TCPServer((ip, port), handler)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
992 if ip == "0.0.0.0":
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
993 displayed_ip = "localhost"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
994 else:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
995 displayed_ip = ip
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
996 config.logger.debug(f"Serving...")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
997 print()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
998 print(f"http://{displayed_ip}:{port}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
999 server.serve_forever()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1000
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1001 def get_logger(config, dummy=False):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1002 dummy_log = lambda msg:dummy and config.logger.info(msg)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1003 arguments = config.arguments
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1004 if not dummy:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1005 logger = logging.getLogger(__file__)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1006 if arguments.debug:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1007 dummy_log(f"Output debug info.")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1008 level = logging.DEBUG
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1009 else:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1010 level = logging.INFO
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1011 if not dummy:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1012 logger.setLevel(level)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1013 formatter = logging.Formatter(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1014 "%(asctime)s - %(levelname)s - %(message)s"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1015 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1016 if arguments.logging.std == "err":
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1017 dummy_log(f"Handler added to output logs in stderr.")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1018 if not dummy:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1019 handler = logging.StreamHandler(sys.stderr)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1020 handler.setLevel(level)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1021 handler.setFormatter(formatter)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1022 logger.addHandler(handler)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1023 elif arguments.logging.std == "out":
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1024 dummy_log(f"Handler added to output logs in stdout.")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1025 if not dummy:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1026 handler = logging.StreamHandler(sys.stdout)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1027 handler.setLevel(level)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1028 handler.setFormatter(formatter)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1029 logger.addHandler(handler)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1030 else:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1031 dummy_log(f"Logs will not be output in stderr not stdout.")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1032 if (path := arguments.logging.file.path):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1033 dummy_log(f"Add log file: {arguments.logging.file.path}.")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1034 if not arguments.logging.file.append:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1035 dummy_log(f"Log file content cleaned.")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1036 with open(path, "w"):pass
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1037 else:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1038 dummy_log(f"Logs appended to log file.")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1039 if not dummy:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1040 file_handler = logging.FileHandler(filename=path)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1041 file_handler.setLevel(level)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1042 file_handler.setFormatter(formatter)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1043 logger.addHandler(file_handler)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1044 if not dummy:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1045 config["logger"] = logger
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1046 starting_sequence(logger)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1047 get_logger(config, dummy=True)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1048 return logger
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1049
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1050 def starting_sequence(logger):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1051 logger.info("*bip* *bop*")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1052 logger.info("starting...")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1053 logger.info("program...")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1054 logger.info("MS2PF is running...")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1055 logger.info("*bip* *bop* am a robot")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1056 atexit.register(stoping_sequence, logger)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1057
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1058 def stoping_sequence(logger):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1059 logger.info("*bip* *bop*")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1060 logger.info("ending...")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1061 logger.info("program...")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1062 logger.info("MS2PF is shuting down...")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1063 logger.info("...robot")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1064 logger.info("*bip* *bop*")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1065 logger.info("shutdown")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1066 logger.info("...")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1067
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1068 if __name__ == "__main__":
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1069
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1070 base_config = parse_config()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1071 config = parse_parameters(base_config)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1072
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1073 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1074 The config contains result of the parsed config file.
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1075 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1076 arguments = config.arguments
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1077
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1078 config.logger.info(f"Starting MS2PF from {os.getcwd()}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1079
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1080 gen_dir = prepare_workplace(config)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1081
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1082 config["pf_path"] = os.path.join(gen_dir, config.generated.html)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1083 config.logger.info(f"HTML output file will be {config.pf_path}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1084 config["js_file"] = os.path.join(gen_dir, config.generated.js)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1085 config.logger.info(f"JS output files will like {config.js_file}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1086 config.placeholders["PF_URL_PLACEHOLDER"] = arguments.peakforest.url
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1087 config.placeholders["PF_TOKEN_PLACEHOLDER"] = (
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1088 arguments.peakforest.token
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1089 or config.token.value
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1090 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1091 if (token := config.placeholders.PF_TOKEN_PLACEHOLDER):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1092 config.logger.info(f"Using a token for authentification - length: {len(token)}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1093 else:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1094 config.logger.info(f"No token provided for peakforest authentification.")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1095
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1096 process_all_files(config)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1097
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1098 if not arguments.do_run_dry:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1099 config.logger.debug(f"Running the server.")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1100 if arguments.firefox or arguments.chromium:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1101 config.logger.debug(f"Running the server.")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1102 import threading
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1103 import time
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1104 if arguments.firefox:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1105 browser = "firefox"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1106 else:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1107 browser = "chromium"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1108 if (ip := config.network.ip) == "0.0.0.0":
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1109 ip = "localhost"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1110 adress = f"http://{ip}:{config.network.port}"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1111 threading.Thread(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1112 target=lambda:(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1113 time.sleep(1),
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1114 os.system(f"{browser} {adress}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1115 ),
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1116 daemon=True
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1117 ).start()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1118 run_server(config)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1119 else:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1120 config.logger.debug(f"Server not run.")