[973924]: / qiita_pet / util.py

Download this file

225 lines (185 with data), 6.8 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
r"""
Util functions (:mod: `qiita_pet.util`)
======================================
..currentmodule:: qiita_pet.util
This module provides different util functions for qiita_pet.
Methods
-------
..autosummary::
:toctree: generated/
clean_str
"""
# -----------------------------------------------------------------------------
# Copyright (c) 2014--, The Qiita Development Team.
#
# Distributed under the terms of the BSD 3-clause License.
#
# The full license is in the file LICENSE, distributed with this software.
# -----------------------------------------------------------------------------
from tornado.escape import linkify as tornado_linkify, xhtml_unescape
from qiita_core.util import execute_as_transaction
from qiita_db.reference import Reference
STATUS_STYLER = {
'sandbox':
('glyphicon glyphicon-eye-close', 'glyphicon glyphicon-lock', 'gray'),
'awaiting_approval':
('glyphicon glyphicon-eye-open', 'glyphicon glyphicon-lock', 'peru'),
'private':
('glyphicon glyphicon-eye-open', 'glyphicon glyphicon-lock',
'#3599FD'),
'public':
('glyphicon glyphicon-eye-open', 'glyphicon glyphicon-globe', 'green')}
EBI_LINKIFIER = ('<a href="http://www.ebi.ac.uk/ena/data/view/{0}" '
'target="_blank">{0}</a>')
def linkify(link_template, item):
"""Formats a strings into a URL using string replacement
Paramters
---------
link_template : str
The template for the URL.
item : list or tuple of str
The strings that will be inserted into the template
"""
return link_template.format(*item)
def clean_str(item):
"""Converts input to string and replaces spaces with underscores
Parameters
----------
item : anything convertable to string
item to convert and clean
Returns
-------
str
cleaned string
"""
return str(item).replace(" ", "_").replace(":", "")
def convert_text_html(message):
"""Linkify URLs and turn newlines into <br/> for HTML"""
html = xhtml_unescape(tornado_linkify(message))
return html.replace('\n', '<br/>')
@execute_as_transaction
def generate_param_str(param):
"""Generate an html string with the parameter values
Parameters
----------
param : BaseParameters
The parameter to generate the str
Returns
-------
str
The html string with the parameter set values
"""
values = param.values
ref = Reference(values['reference'])
result = ["<b>Reference:</b> %s %s" % (ref.name, ref.version)]
result.extend("<b>%s:</b> %s" % (name, value)
for name, value in values.items()
if name != 'reference')
return "<br/>".join(result)
def is_localhost(host):
"""Verifies if the connection is local
Parameters
----------
host : str
The requesting host, in general self.request.headers['host']
Returns
-------
bool
True if local request
"""
localhost = ('localhost', '127.0.0.1')
return host.startswith(localhost)
def get_artifact_processing_status(artifact):
"""Gets the processing status of the artifact
Parameters
----------
artifact : qiita_db.artifact.Artifact
The artifact to get the processing status
Returns
-------
str, str
The processing status {'processing', 'failed', 'success',
'Not processed'}
A summary of the jobs attached to the artifact
"""
preprocessing_status = 'Not processed'
preprocessing_status_msg = []
for job in artifact.jobs():
job_status = job.status
if job_status == 'error':
if preprocessing_status != 'success':
preprocessing_status = 'failed'
preprocessing_status_msg.append(
"<b>Job %s</b>: failed - %s"
% (job.id, job.log.msg))
elif job_status == 'success':
preprocessing_status = 'success'
else:
if preprocessing_status != 'success':
preprocessing_status = 'processing'
preprocessing_status_msg.append(
"<b>Job %s</b>: %s" % (job.id, job_status))
if not preprocessing_status_msg:
preprocessing_status_msg = 'Not processed'
else:
preprocessing_status_msg = convert_text_html(
'<br/>'.join(preprocessing_status_msg))
return preprocessing_status, preprocessing_status_msg
def get_network_nodes_edges(graph, full_access, nodes=None, edges=None):
"""Returns the JavaScript friendly representation of the graph
Parameters
----------
graph : networkx.DiGraph
The artifact/jobs graph
full_access : bool
Whether the user has full access to the graph or not
nodes : list, optional
A pre-populated list of nodes. Useful for the analysis pipeline
edges : list, optional
A pre-populated list of edges. Useful for the analysis pipeline
Returns
-------
(list, list, int)
The list of nodes, the list of edges, and the worklfow id if there is
any job on construction
"""
nodes = nodes if nodes is not None else []
edges = edges if edges is not None else []
workflow_id = None
# n[0] is the data type: job/artifact/type
# n[1] is the object
for n in graph.nodes():
if n[0] == 'job':
# ignoring internal Jobs
if n[1].command.software.name == 'Qiita':
continue
atype = 'job'
name = n[1].command.name
status = n[1].status
wkflow = n[1].processing_job_workflow
if status == 'in_construction' and wkflow is not None:
workflow_id = wkflow.id
elif n[0] == 'artifact':
atype = n[1].artifact_type
status = 'artifact'
pp = n[1].processing_parameters
if pp is not None:
cmd = pp.command
if cmd.software.deprecated:
status = 'deprecated'
elif not cmd.active:
status = 'outdated'
if full_access or n[1].visibility == 'public':
name = '%s\n(%s)' % (n[1].name, n[1].artifact_type)
else:
continue
elif n[0] == 'type':
atype = n[1].type
name = '%s\n(%s)' % (n[1].name, n[1].type)
status = 'type'
else:
# this should never happen but let's add it just in case
raise ValueError('not valid node type: %s' % n[0])
nodes.append((n[0], atype, n[1].id, name, status))
edges.extend([(n[1].id, m[1].id) for n, m in graph.edges()])
return nodes, edges, workflow_id