scipion-pyworkflow 3.11.0__py3-none-any.whl → 3.11.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (98) hide show
  1. pyworkflow/apps/__init__.py +29 -0
  2. pyworkflow/apps/pw_manager.py +37 -0
  3. pyworkflow/apps/pw_plot.py +51 -0
  4. pyworkflow/apps/pw_project.py +113 -0
  5. pyworkflow/apps/pw_protocol_list.py +143 -0
  6. pyworkflow/apps/pw_protocol_run.py +51 -0
  7. pyworkflow/apps/pw_run_tests.py +267 -0
  8. pyworkflow/apps/pw_schedule_run.py +322 -0
  9. pyworkflow/apps/pw_sleep.py +37 -0
  10. pyworkflow/apps/pw_sync_data.py +439 -0
  11. pyworkflow/apps/pw_viewer.py +78 -0
  12. pyworkflow/constants.py +1 -1
  13. pyworkflow/gui/__init__.py +36 -0
  14. pyworkflow/gui/browser.py +760 -0
  15. pyworkflow/gui/canvas.py +1190 -0
  16. pyworkflow/gui/dialog.py +979 -0
  17. pyworkflow/gui/form.py +2726 -0
  18. pyworkflow/gui/graph.py +247 -0
  19. pyworkflow/gui/graph_layout.py +271 -0
  20. pyworkflow/gui/gui.py +566 -0
  21. pyworkflow/gui/matplotlib_image.py +233 -0
  22. pyworkflow/gui/plotter.py +247 -0
  23. pyworkflow/gui/project/__init__.py +25 -0
  24. pyworkflow/gui/project/base.py +192 -0
  25. pyworkflow/gui/project/constants.py +139 -0
  26. pyworkflow/gui/project/labels.py +205 -0
  27. pyworkflow/gui/project/project.py +491 -0
  28. pyworkflow/gui/project/searchprotocol.py +238 -0
  29. pyworkflow/gui/project/searchrun.py +181 -0
  30. pyworkflow/gui/project/steps.py +171 -0
  31. pyworkflow/gui/project/utils.py +332 -0
  32. pyworkflow/gui/project/variables.py +179 -0
  33. pyworkflow/gui/project/viewdata.py +472 -0
  34. pyworkflow/gui/project/viewprojects.py +510 -0
  35. pyworkflow/gui/project/viewprotocols.py +2116 -0
  36. pyworkflow/gui/project/viewprotocols_extra.py +562 -0
  37. pyworkflow/gui/text.py +771 -0
  38. pyworkflow/gui/tooltip.py +185 -0
  39. pyworkflow/gui/tree.py +684 -0
  40. pyworkflow/gui/widgets.py +307 -0
  41. pyworkflow/mapper/__init__.py +26 -0
  42. pyworkflow/mapper/mapper.py +222 -0
  43. pyworkflow/mapper/sqlite.py +1581 -0
  44. pyworkflow/mapper/sqlite_db.py +145 -0
  45. pyworkflow/project/__init__.py +31 -0
  46. pyworkflow/project/config.py +454 -0
  47. pyworkflow/project/manager.py +180 -0
  48. pyworkflow/project/project.py +2095 -0
  49. pyworkflow/project/usage.py +165 -0
  50. pyworkflow/protocol/__init__.py +38 -0
  51. pyworkflow/protocol/bibtex.py +48 -0
  52. pyworkflow/protocol/constants.py +87 -0
  53. pyworkflow/protocol/executor.py +483 -0
  54. pyworkflow/protocol/hosts.py +317 -0
  55. pyworkflow/protocol/launch.py +277 -0
  56. pyworkflow/protocol/package.py +42 -0
  57. pyworkflow/protocol/params.py +781 -0
  58. pyworkflow/protocol/protocol.py +2707 -0
  59. pyworkflow/tests/__init__.py +29 -0
  60. pyworkflow/tests/test_utils.py +25 -0
  61. pyworkflow/tests/tests.py +341 -0
  62. pyworkflow/utils/__init__.py +38 -0
  63. pyworkflow/utils/dataset.py +414 -0
  64. pyworkflow/utils/echo.py +104 -0
  65. pyworkflow/utils/graph.py +169 -0
  66. pyworkflow/utils/log.py +293 -0
  67. pyworkflow/utils/path.py +528 -0
  68. pyworkflow/utils/process.py +153 -0
  69. pyworkflow/utils/profiler.py +92 -0
  70. pyworkflow/utils/progressbar.py +154 -0
  71. pyworkflow/utils/properties.py +617 -0
  72. pyworkflow/utils/reflection.py +129 -0
  73. pyworkflow/utils/utils.py +880 -0
  74. pyworkflow/utils/which.py +229 -0
  75. pyworkflow/webservices/__init__.py +8 -0
  76. pyworkflow/webservices/config.py +8 -0
  77. pyworkflow/webservices/notifier.py +152 -0
  78. pyworkflow/webservices/repository.py +59 -0
  79. pyworkflow/webservices/workflowhub.py +74 -0
  80. pyworkflowtests/tests/__init__.py +0 -0
  81. pyworkflowtests/tests/test_canvas.py +72 -0
  82. pyworkflowtests/tests/test_domain.py +45 -0
  83. pyworkflowtests/tests/test_logs.py +74 -0
  84. pyworkflowtests/tests/test_mappers.py +392 -0
  85. pyworkflowtests/tests/test_object.py +507 -0
  86. pyworkflowtests/tests/test_project.py +42 -0
  87. pyworkflowtests/tests/test_protocol_execution.py +146 -0
  88. pyworkflowtests/tests/test_protocol_export.py +78 -0
  89. pyworkflowtests/tests/test_protocol_output.py +158 -0
  90. pyworkflowtests/tests/test_streaming.py +47 -0
  91. pyworkflowtests/tests/test_utils.py +210 -0
  92. {scipion_pyworkflow-3.11.0.dist-info → scipion_pyworkflow-3.11.1.dist-info}/METADATA +2 -2
  93. scipion_pyworkflow-3.11.1.dist-info/RECORD +161 -0
  94. scipion_pyworkflow-3.11.0.dist-info/RECORD +0 -71
  95. {scipion_pyworkflow-3.11.0.dist-info → scipion_pyworkflow-3.11.1.dist-info}/WHEEL +0 -0
  96. {scipion_pyworkflow-3.11.0.dist-info → scipion_pyworkflow-3.11.1.dist-info}/entry_points.txt +0 -0
  97. {scipion_pyworkflow-3.11.0.dist-info → scipion_pyworkflow-3.11.1.dist-info}/licenses/LICENSE.txt +0 -0
  98. {scipion_pyworkflow-3.11.0.dist-info → scipion_pyworkflow-3.11.1.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,880 @@
1
+ # **************************************************************************
2
+ # *
3
+ # * Authors: J.M. De la Rosa Trevin (delarosatrevin@scilifelab.se) [1]
4
+ # *
5
+ # * [1] SciLifeLab, Stockholm University
6
+ # *
7
+ # * This program is free software: you can redistribute it and/or modify
8
+ # * it under the terms of the GNU General Public License as published by
9
+ # * the Free Software Foundation, either version 3 of the License, or
10
+ # * (at your option) any later version.
11
+ # *
12
+ # * This program is distributed in the hope that it will be useful,
13
+ # * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # * GNU General Public License for more details.
16
+ # *
17
+ # * You should have received a copy of the GNU General Public License
18
+ # * along with this program. If not, see <https://www.gnu.org/licenses/>.
19
+ # *
20
+ # * All comments concerning this program package may be sent to the
21
+ # * e-mail address 'scipion@cnb.csic.es'
22
+ # *
23
+ # **************************************************************************
24
+ import logging
25
+
26
+ logger = logging.getLogger(__name__)
27
+
28
+ import contextlib
29
+ import sys
30
+ import platform
31
+ import os
32
+ import re
33
+ from datetime import datetime, timezone
34
+ import traceback
35
+ import sysconfig
36
+
37
+ import bibtexparser
38
+ import numpy as np
39
+ import math
40
+ from pyworkflow.constants import StrColors, TRUE_YES_ON_
41
+ from pyworkflow import Config
42
+
43
+
44
+ def prettyDate(time=False):
45
+ """
46
+ Get a datetime object or a int() Epoch timestamp and return a
47
+ pretty string like 'an hour ago', 'Yesterday', '3 months ago',
48
+ 'just now', etc
49
+ """
50
+ now = datetime.now()
51
+ if type(time) is int:
52
+ diff = now - datetime.fromtimestamp(time)
53
+ elif type(time) is float:
54
+ diff = now - datetime.fromtimestamp(int(time))
55
+ elif isinstance(time, datetime):
56
+ diff = now - time
57
+ elif not time:
58
+ # Avoid now - now (sonar cloud bug)
59
+ copy = now
60
+ diff = now - copy
61
+ second_diff = diff.seconds
62
+ day_diff = diff.days
63
+
64
+ if day_diff < 0:
65
+ return ''
66
+
67
+ if day_diff == 0:
68
+ if second_diff < 10:
69
+ return "just now"
70
+ if second_diff < 60:
71
+ return str(second_diff) + " seconds ago"
72
+ if second_diff < 120:
73
+ return "a minute ago"
74
+ if second_diff < 3600:
75
+ return str(int(second_diff / 60)) + " minutes ago"
76
+ if second_diff < 7200:
77
+ return "an hour ago"
78
+ if second_diff < 86400:
79
+ return str(int(second_diff / 3600)) + " hours ago"
80
+ if day_diff == 1:
81
+ return "Yesterday"
82
+ if day_diff < 7:
83
+ return str(day_diff) + " days ago"
84
+ if day_diff < 31:
85
+ return str(int(day_diff / 7)) + " weeks ago"
86
+ if day_diff < 365:
87
+ return str(int(day_diff / 30)) + " months ago"
88
+ return str(int(day_diff / 365)) + " years ago"
89
+
90
+
91
+ def dateStr(dt=None, time=True, secs=False, dateFormat=None):
92
+ """ Get a normal string representation of datetime.
93
+ If dt is None, use NOW.
94
+ """
95
+ if dt is None:
96
+ dt = datetime.now()
97
+ elif isinstance(dt, float) or isinstance(dt, int):
98
+ dt = datetime.fromtimestamp(dt)
99
+
100
+ if dateFormat is None:
101
+ dateFormat = '%d-%m-%Y'
102
+ if time:
103
+ dateFormat += ' %H:%M'
104
+ if secs:
105
+ dateFormat += ':%S'
106
+
107
+ return dt.strftime(dateFormat)
108
+
109
+
110
+ prettyTime = dateStr
111
+
112
+
113
+ def prettyTimestamp(dt=None, format='%Y-%m-%d_%H%M%S'):
114
+ if dt is None:
115
+ dt = datetime.now()
116
+
117
+ return dt.strftime(format)
118
+
119
+
120
+ def prettySize(size):
121
+ """ Human friendly file size. """
122
+ unit_list = list(zip(['bytes', 'kB', 'MB', 'GB', 'TB', 'PB'],
123
+ [0, 0, 1, 2, 2, 2]))
124
+ if size > 1:
125
+ exponent = min(int(math.log(size, 1024)), len(unit_list) - 1)
126
+ quotient = float(size) / 1024 ** exponent
127
+ unit, num_decimals = unit_list[exponent]
128
+ format_string = '{:.%sf} {}' % num_decimals
129
+ return format_string.format(quotient, unit)
130
+ if size == 0:
131
+ return '0 bytes'
132
+ if size == 1:
133
+ return '1 byte'
134
+
135
+
136
+ def prettyDelta(timedelta):
137
+ """ Remove the milliseconds of the timedelta. """
138
+ return str(timedelta).split('.')[0]
139
+
140
+
141
+ def to_utc(t):
142
+ """ Make date conversions to utc"""
143
+ return datetime.fromtimestamp(t, tz=timezone.utc)
144
+
145
+
146
+ def prettyLog(msg):
147
+ logger.info(cyanStr(msg))
148
+
149
+
150
+ class Timer(object):
151
+ """ Simple Timer base in datetime.now and timedelta. """
152
+
153
+ def __init__(self, message=""):
154
+ self._message = message
155
+
156
+ def tic(self):
157
+ self._dt = datetime.now()
158
+
159
+ def getElapsedTime(self):
160
+ return datetime.now() - self._dt
161
+
162
+ def toc(self, message='Elapsed:'):
163
+ logger.info(message + str(self.getElapsedTime()))
164
+
165
+ def getToc(self):
166
+ return prettyDelta(self.getElapsedTime())
167
+
168
+ def __enter__(self):
169
+ self.tic()
170
+
171
+ def __exit__(self, type, value, traceback):
172
+ self.toc(self._message)
173
+
174
+
175
+ def timeit(func):
176
+ """
177
+ Decorator function to have a simple measurement of the execution time of a given function.
178
+ To use it ::
179
+
180
+ @timeit
181
+ def func(...)
182
+ ...
183
+
184
+ """
185
+
186
+ def timedFunc(*args, **kwargs):
187
+ t = Timer()
188
+ t.tic()
189
+ result = func(*args, **kwargs)
190
+ t.toc("Function '%s' took" % func)
191
+
192
+ return result
193
+
194
+ return timedFunc
195
+
196
+
197
+ def trace(nlevels, separator=' --> ', stream=sys.stdout):
198
+ # Example:
199
+ # @trace(3)
200
+ # def doRefresh(...
201
+ # gives as output whenever doRefresh is called lines like:
202
+ # text.py:486 _addFileTab --> text.py:330 __init__ --> doRefresh
203
+
204
+ def realTrace(f):
205
+ """ Decorator function to print stack call in a human-readable way.
206
+ """
207
+
208
+ def tracedFunc(*args, **kwargs):
209
+ stack = traceback.extract_stack()[-nlevels - 1:-1]
210
+ fmt = lambda x: '%s:%d %s' % (os.path.basename(x[0]), x[1], x[2])
211
+ stList = list(map(fmt, stack))
212
+ stream.write(separator.join(stList + [f.__name__]) + '\n')
213
+ return f(*args, **kwargs)
214
+
215
+ return tracedFunc
216
+
217
+ return realTrace
218
+
219
+
220
+ def prettyDict(d):
221
+ print("{")
222
+ for k, v in d.items():
223
+ print(" %s: %s" % (k, v))
224
+ print("}")
225
+
226
+
227
+ def prettyXml(elem, level=0):
228
+ """ Add indentation for XML elements for more human readable text. """
229
+ i = "\n" + level * " "
230
+ if len(elem):
231
+ if not elem.text or not elem.text.strip():
232
+ elem.text = i + " "
233
+ if not elem.tail or not elem.tail.strip():
234
+ elem.tail = i
235
+ for _elem in elem:
236
+ prettyXml(_elem, level + 1)
237
+ if not _elem.tail or not _elem.tail.strip():
238
+ _elem.tail = i
239
+
240
+
241
+ def getUniqueItems(originalList):
242
+ """ Method to remove repeated items from one list
243
+ originalList -- Original list with repeated items, or not.
244
+ returns -- New list with the content of original list without repeated items
245
+ """
246
+ auxDict = {}
247
+ resultList = [auxDict.setdefault(x, x) for x in originalList if x not in auxDict]
248
+ return resultList
249
+
250
+ def sortListByList(inList, priorityList):
251
+ """ Returns a list sorted by some elements in a second priorityList"""
252
+ if priorityList:
253
+ sortedList = priorityList + [item for item in inList
254
+ if item not in priorityList]
255
+ return sortedList
256
+ else:
257
+ return inList
258
+
259
+
260
+ def getLocalUserName():
261
+ """ Recover local machine user name.
262
+ returns: Local machine user name.
263
+ """
264
+ import getpass
265
+ return getpass.getuser()
266
+
267
+
268
+ def getLocalHostName():
269
+ return getHostName()
270
+
271
+
272
+ def getHostName():
273
+ """ Return the name of the local machine. """
274
+ import socket
275
+ return socket.gethostname()
276
+
277
+
278
+ def getHostFullName():
279
+ """ Return the fully-qualified name of the local machine. """
280
+ import socket
281
+ return socket.getfqdn()
282
+
283
+ def getPython():
284
+ return sys.executable
285
+
286
+ def getPythonPackagesFolder():
287
+ # This does not work on MAC virtual envs
288
+ # import site
289
+ # return site.getsitepackages()[0]
290
+
291
+ return sysconfig.get_path("platlib")
292
+
293
+
294
+ # ******************************File utils *******************************
295
+
296
+ def isInFile(text, filePath):
297
+ """
298
+ Checks if given text is in the given file.
299
+
300
+ :param text: Text to check.
301
+ :param filePath: File path to check.
302
+
303
+ :returns True if the given text is in the given file,
304
+ False if it is not in the file.
305
+
306
+ """
307
+ return any(text in line for line in open(filePath))
308
+
309
+
310
+ def getLineInFile(text, fileName):
311
+ """ Find the line where the given text is located in the given file.
312
+
313
+ :param text: Text to check.
314
+ :param filePath: File path to check.
315
+
316
+ :return line number where the text was located.
317
+
318
+ """
319
+ with open(fileName) as f:
320
+ for i, line in enumerate(f):
321
+ if text in line:
322
+ return i + 1
323
+ return None
324
+
325
+ def hasAnyFileChanged(files, time):
326
+ """ Returns true if any of the files in files list has been changed after 'time'"""
327
+ for file in files:
328
+ if hasFileChangedSince(file, time):
329
+ return True
330
+
331
+ return False
332
+
333
+ def hasFileChangedSince(file, time):
334
+ """ Returns true if the file has changed after 'time'"""
335
+ modTime = datetime.fromtimestamp(os.path.getmtime(file))
336
+ return time < modTime
337
+
338
+
339
+ # ------------- Colored message strings -----------------------------
340
+
341
+
342
+ def getColorStr(text, color, bold=False):
343
+ """ Add ANSI color codes to the string if there is a terminal sys.stdout.
344
+
345
+ :param text: text to be colored
346
+ :param color: red or green
347
+ :param bold: bold the text
348
+ """
349
+ if not Config.colorsInTerminal():
350
+ return text
351
+
352
+ attr = [str(color.value)]
353
+
354
+ if bold:
355
+ attr.append('1')
356
+ return '\x1b[%sm%s\x1b[0m' % (';'.join(attr), text)
357
+
358
+
359
+ def grayStr(text):
360
+ return getColorStr(text, color=StrColors.gray)
361
+
362
+
363
+ def redStr(text):
364
+ return getColorStr(text, color=StrColors.red)
365
+
366
+
367
+ def greenStr(text):
368
+ return getColorStr(text, color=StrColors.green)
369
+
370
+
371
+ def yellowStr(text):
372
+ return getColorStr(text, color=StrColors.yellow)
373
+
374
+
375
+ def blueStr(text):
376
+ return getColorStr(text, color=StrColors.blue)
377
+
378
+
379
+ def magentaStr(text):
380
+ return getColorStr(text, color=StrColors.magenta)
381
+
382
+
383
+ def cyanStr(text):
384
+ return getColorStr(text, color=StrColors.cyan)
385
+
386
+
387
+ def ansi(n, bold=False):
388
+ """Return function that escapes text with ANSI color n."""
389
+ return lambda txt: '\x1b[%d%sm%s\x1b[0m' % (n, ';1' if bold else '', txt)
390
+
391
+
392
+ black, red, green, yellow, blue, magenta, cyan, white = map(ansi, range(30, 38))
393
+ blackB, redB, greenB, yellowB, blueB, magentaB, cyanB, whiteB = [
394
+ ansi(i, bold=True) for i in range(30, 38)]
395
+
396
+ # -------------- Hyper text highlighting ----------------------------
397
+ #
398
+ # We use a subset of TWiki hyper text conventions.
399
+ # In particular:
400
+ # *some_text* will display some_text in bold
401
+ # _some_text_ will display some_text in italic
402
+ # Links:
403
+ # http://www.link-page.com -> hyperlink using the url as label
404
+ # [[http://www.link-page.com][Link page]] -> hyperlink using "Link page" as label
405
+
406
+ # Types of recognized styles
407
+ HYPER_BOLD = 'bold'
408
+ HYPER_ITALIC = 'italic'
409
+ HYPER_LINK1 = 'link1'
410
+ HYPER_SCIPION_OPEN = 'sci-open'
411
+ HYPER_LINK2 = 'link2'
412
+ HYPER_ALL = 'all'
413
+
414
+ # Associated regular expressions
415
+ PATTERN_BOLD = r"(^|[\s])[*](?P<bold>[^\s*][^*]*[^\s*]|[^\s*])[*]"
416
+ # PATTERN_BOLD = r"[\s]+[*]([^\s][^*]+[^\s])[*][\s]+"
417
+ PATTERN_ITALIC = r"(^|[\s])[_](?P<italic>[^\s_][^_]*[^\s_]|[^\s_])[_]"
418
+ # PATTERN_ITALIC = r"[\s]+[_]([^\s][^_]+[^\s])[_][\s]+"
419
+ PATTERN_LINK1 = r'(?P<link1>http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+#]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+)'
420
+ PATTERN_LINK2 = r"[\[]{2}(?P<link2>[^\s][^\]]+[^\s])[\]][\[](?P<link2_label>[^\s][^\]]+[^\s])[\]]{2}"
421
+ # __PATTERN_LINK2 should be first since it could contain __PATTERN_LINK1
422
+ PATTERN_ALL = '|'.join([PATTERN_BOLD, PATTERN_ITALIC, PATTERN_LINK2, PATTERN_LINK1])
423
+
424
+ # Compiled regex
425
+ # Not need now, each pattern compiled separately
426
+ # HYPER_REGEX = {
427
+ # HYPER_BOLD: re.compile(PATTERN_BOLD),
428
+ # HYPER_ITALIC: re.compile(PATTERN_ITALIC),
429
+ # HYPER_LINK1: re.compile(PATTERN_LINK1),
430
+ # HYPER_LINK2: re.compile(PATTERN_LINK1),
431
+ # }
432
+ HYPER_ALL_RE = re.compile(PATTERN_ALL)
433
+
434
+
435
+ def parseHyperText(text, matchCallback):
436
+ """ Parse the text recognizing Hyper definitions below.
437
+
438
+ :param matchCallback: a callback function to processing each matching,
439
+ it should accept the type of match (HYPER_BOLD, ITALIC or LINK)
440
+
441
+ :return The input text with the replacements made by matchCallback
442
+ """
443
+
444
+ def _match(match):
445
+ """ Call the proper matchCallback with some extra info. """
446
+ m = match.group().strip()
447
+ if m.startswith('*'):
448
+ tag = HYPER_BOLD
449
+ elif m.startswith('_'):
450
+ tag = HYPER_ITALIC
451
+ elif m.startswith('http'):
452
+ tag = HYPER_LINK1
453
+ elif m.startswith('[['):
454
+ tag = HYPER_LINK2
455
+ else:
456
+ raise Exception("Bad prefix for HyperText match")
457
+ return matchCallback(match, tag)
458
+
459
+ return HYPER_ALL_RE.sub(_match, text)
460
+
461
+
462
+ # for hyperMode, hyperRegex in HYPER_REGEX.iteritems():
463
+ # text = hyperRegex.sub(lambda match: matchCallback(match, hyperMode), text)
464
+ #
465
+ # return text
466
+
467
+ class LazyDict(object):
468
+ """ Dictionary to be initialized at the moment it is accessed for the first time.
469
+ Initialization is done by a callback passed at instantiation"""
470
+ def __init__(self, callback=dict):
471
+ """ :param callback: method to initialize the dictionary. Should return a dictionary"""
472
+ self.data = None
473
+ self.callback = callback
474
+
475
+ def evaluate_callback(self):
476
+ self.data = self.callback()
477
+
478
+ def __getitem__(self, name):
479
+ if self.data is None:
480
+ self.evaluate_callback()
481
+ return self.data.__getitem__(name)
482
+
483
+ def __setitem__(self, name, value):
484
+ if self.data is None:
485
+ self.evaluate_callback()
486
+ return self.data.__setitem__(name, value)
487
+
488
+ def __getattr__(self, name):
489
+ if self.data is None:
490
+ self.evaluate_callback()
491
+ return getattr(self.data, name)
492
+
493
+ def __iter__(self):
494
+ if self.data is None:
495
+ self.evaluate_callback()
496
+ return self.data.__iter__()
497
+
498
+
499
+ def parseBibTex(bibtexStr):
500
+ """ Parse a bibtex file and return a dictionary. """
501
+
502
+ return bibtexparser.loads(bibtexStr,
503
+ parser=bibtexparser.bparser.BibTexParser(common_strings=True)
504
+ ).entries_dict
505
+
506
+
507
+
508
+ def isPower2(num):
509
+ """ Return True if 'num' is a power of 2. """
510
+ return num != 0 and ((num & (num - 1)) == 0)
511
+
512
+
513
+ # ---------------------------------------------------------------------------
514
+ # Parsing of arguments
515
+ # ---------------------------------------------------------------------------
516
+
517
+ def getListFromRangeString(rangeStr):
518
+ """ Create a list of integers from a string with range definitions.
519
+ Examples:
520
+ "1,5-8,10" -> [1,5,6,7,8,10]
521
+ "2,6,9-11" -> [2,6,9,10,11]
522
+ "2 5, 6-8" -> [2,5,6,7,8]
523
+ """
524
+ # Split elements by command or space
525
+ elements = re.split(r',|\s', rangeStr)
526
+ values = []
527
+ for e in elements:
528
+ if '-' in e:
529
+ limits = e.split('-')
530
+ values += range(int(limits[0]), int(limits[1]) + 1)
531
+ else:
532
+ # If values are separated by comma also splitted
533
+ values += map(int, e.split())
534
+ return values
535
+
536
+
537
+ def getRangeStringFromList(list):
538
+ left = None
539
+ right = None
540
+ ranges = []
541
+
542
+ def addRange():
543
+ if left == right: # Single element
544
+ ranges.append("%d" % right)
545
+ else:
546
+ ranges.append("%(left)d-%(right)d" % locals())
547
+
548
+ for item in list:
549
+ if right is None:
550
+ left = right = item
551
+ else:
552
+ if item == right + 1:
553
+ right += 1
554
+ else:
555
+ addRange()
556
+ left = right = item
557
+ addRange()
558
+ return ','.join(ranges)
559
+
560
+
561
+ def getListFromValues(valuesStr, length=None, caster=str):
562
+ """ Convert a string representing list items into a list.
563
+ The items should be separated by spaces or commas and a multiplier 'x' can be used.
564
+ If length is not None, then the last element will be repeated
565
+ until the desired length is reached.
566
+
567
+ Examples:
568
+ '1 1 2x2 4 4' -> ['1', '1', '2', '2', '4', '4']
569
+ '2x3, 3x4, 1' -> ['3', '3', '4', '4', '4', '1']
570
+
571
+ """
572
+ result = []
573
+ valuesStr = valuesStr.replace(","," ")
574
+ for chunk in valuesStr.split():
575
+ if caster != str:
576
+ values = chunk.split('x')
577
+ else:
578
+ values=[chunk]
579
+
580
+ n = len(values)
581
+ if n == 1: # 'x' is not present in the chunk, single value
582
+ result += [caster(values[0])]
583
+ elif n == 2: # multiple the values by the number after 'x'
584
+ result += [caster(values[1])] * int(values[0])
585
+ else:
586
+ raise Exception("More than one 'x' is not allowed in list string value.")
587
+
588
+ # If length is passed, we fill the list with
589
+ # the last element until length is reached
590
+ if length is not None and length > len(result):
591
+ item = result[-1]
592
+ result += [caster(item)] * (length - len(result))
593
+
594
+ return result
595
+
596
+
597
+ def getFloatListFromValues(valuesStr, length=None):
598
+ """ Convert a string to a list of floats"""
599
+ return [v for v in getListFromValues(valuesStr, length, caster=float)]
600
+
601
+
602
+ def getBoolListFromValues(valuesStr, length=None):
603
+ """ Convert a string to a list of booleans"""
604
+ from pyworkflow.object import Boolean
605
+ return [v.get() for v in getListFromValues(valuesStr, length, caster=Boolean)]
606
+
607
+
608
+ def getStringListFromValues(valuesStr, length=None):
609
+ """ Convert a string to a list of booleans"""
610
+ from pyworkflow.object import String
611
+ return [String(value=v).get() for v in getListFromValues(valuesStr, length)]
612
+
613
+
614
+ class Environ(dict):
615
+ """ Some utilities to handle environment settings. """
616
+ REPLACE = 0
617
+ BEGIN = 1
618
+ END = 2
619
+
620
+ def getFirst(self, keys, mandatory=False):
621
+ """ Return the value of the first key present in the environment.
622
+ If none is found, returns the 'defaultValue' parameter.
623
+ """
624
+ for k in keys:
625
+ if k in self:
626
+ return self.get(k)
627
+
628
+ if mandatory:
629
+ logger.info("None of the variables: %s found in the Environment. "
630
+ "Please check scipion.conf files." % (str(keys)))
631
+
632
+ return None
633
+
634
+ def set(self, varName, varValue, position=REPLACE):
635
+ """ Modify the value for some variable.
636
+
637
+ :param varName: for example LD_LIBRARY_PATH
638
+ :param varValue: the value to set, prefix or suffix.
639
+ :param position: controls how the value will be changed.
640
+ If REPLACE, it will overwrite the value of
641
+ the var. BEGIN or END will preserve the current value
642
+ and will add, at the beginning or end, the new value.
643
+
644
+ """
645
+ if varName in self and position != self.REPLACE:
646
+ if position == self.BEGIN:
647
+ self[varName] = varValue + os.pathsep + self[varName]
648
+ elif position == self.END:
649
+ self[varName] = self[varName] + os.pathsep + varValue
650
+ else:
651
+ self[varName] = varValue
652
+
653
+ def update(self, valuesDict, position=REPLACE):
654
+ """ Use set for each key, value pair in valuesDict. """
655
+ for k, v in valuesDict.items():
656
+ self.set(k, v, position)
657
+
658
+ def addLibrary(self, libraryPath, position=BEGIN):
659
+ """ Adds a path to LD_LIBRARY_PATH at the requested position
660
+ if the provided paths exist. """
661
+
662
+ if libraryPath is None:
663
+ return
664
+
665
+ if existsVariablePaths(libraryPath):
666
+ self.update({'LD_LIBRARY_PATH': libraryPath}, position=position)
667
+ else:
668
+ logger.info("Some paths do not exist in: % s" % libraryPath)
669
+
670
+ def setPrepend(self, prepend):
671
+ """ Use this method to set a prepend string that will be added at
672
+ the beginning of any command that will be run in this environment.
673
+ This can be useful for example when 'modules' need to be loaded and
674
+ a simple environment variables setup is not enough.
675
+ """
676
+ setattr(self, '__prepend', prepend)
677
+
678
+ def getPrepend(self):
679
+ """ Return if there is any prepend value. See setPrepend function. """
680
+ return getattr(self, '__prepend', '')
681
+
682
+
683
+ def existsVariablePaths(variableValue):
684
+ """ Check if the path (or paths) in variableValue exists.
685
+ Multiple paths are allowed if separated by os."""
686
+ return all(os.path.exists(p)
687
+ for p in variableValue.split(os.pathsep) if p.split())
688
+
689
+
690
+ def environAdd(varName, newValue, valueFirst=False):
691
+ """ Add a new value to some environ variable.
692
+ If valueFirst is true, the new value will be at the beginning.
693
+ """
694
+ varList = [os.environ[varName]]
695
+ i = 1
696
+ if valueFirst:
697
+ i = 0
698
+ varList.insert(i, newValue)
699
+ os.environ[varName] = os.pathsep.join(varList)
700
+
701
+
702
+ def envVarOn(varName, env=None):
703
+ """ Is variable set to True in the environment? """
704
+ v = env.get(varName) if env else os.environ.get(varName)
705
+ return strToBoolean(v)
706
+
707
+ def strToBoolean(string):
708
+ """ Converts a string into a Boolean if the string is on of true, yes, on, 1. Case insensitive."""
709
+ return string is not None and string.lower() in TRUE_YES_ON_
710
+
711
+ def strToDuration(durationStr):
712
+ """ Converts a string representing an elapsed time to seconds
713
+ E.g.: for "1m 10s" it'll return 70 """
714
+
715
+ toEval = durationStr.replace("d", "*3600*24")\
716
+ .replace("h", "*3600")\
717
+ .replace("m", "*60") \
718
+ .replace("s", "") \
719
+ .replace(" ", "+")
720
+ return eval(toEval)
721
+
722
+ def getMemoryAvailable():
723
+ """ Return the total memory of the system in MB """
724
+ from psutil import virtual_memory
725
+ return virtual_memory().total // 1024 ** 2
726
+
727
+
728
+ def getFreePort(basePort=0, host=''):
729
+ import socket
730
+ try:
731
+ s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
732
+ s.bind((host, basePort))
733
+ ipaddr, port = s.getsockname()
734
+ s.close()
735
+ except Exception as e:
736
+ logger.error("Can't get a free port", exc_info=e)
737
+ return 0
738
+ return port
739
+
740
+
741
+ def readProperties(propsFile):
742
+ myprops = {}
743
+ with open(propsFile, 'r') as f:
744
+ for line in f:
745
+ line = line.rstrip() # removes trailing whitespace and '\n' chars
746
+
747
+ if "=" not in line:
748
+ continue # skips blanks and comments w/o =
749
+ if line.startswith("#"):
750
+ continue # skips comments which contain =
751
+
752
+ k, v = line.split("=", 1)
753
+ myprops[k] = v
754
+ return myprops
755
+
756
+
757
+ # ---------------------Color utils --------------------------
758
+ def hex_to_rgb(value):
759
+ value = value.lstrip('#')
760
+ lv = len(value)
761
+ return tuple(int(value[i:i + lv // 3], 16) for i in range(0, lv, lv // 3))
762
+
763
+
764
+ def rgb_to_hex(rgb):
765
+ return '#%02x%02x%02x' % (int(rgb[0]), int(rgb[1]), int(rgb[2]))
766
+
767
+
768
+ def lighter(color, percent):
769
+ """assumes color is rgb between (0, 0, 0) and (255, 255, 255)"""
770
+ color = np.array(color)
771
+ white = np.array([255, 255, 255])
772
+ vector = white - color
773
+ return tuple(np.around(color + vector * percent))
774
+
775
+
776
+ def formatExceptionInfo(level=6):
777
+ error_type, error_value, trbk = sys.exc_info()
778
+ tb_list = traceback.format_tb(trbk, level)
779
+ s = "Error: %s \nDescription: %s \nTraceback:" % (error_type.__name__,
780
+ error_value)
781
+ for i in tb_list:
782
+ s += "\n" + i
783
+ return s
784
+
785
+
786
+ def printTraceBack():
787
+ traceback.print_stack()
788
+
789
+
790
+ def getEnvVariable(variableName, default=None, exceptionMsg=None):
791
+ """ Returns the value of an environment variable or raise an exception message.
792
+ Useful when adding variable to the config file and report accurate messages"""
793
+ value = os.getenv(variableName)
794
+
795
+ if exceptionMsg is None:
796
+ exceptionMsg = ("Environment variable %s not found. "
797
+ "Please check scipion configuration. "
798
+ "Try running : scipion config." % variableName)
799
+
800
+ if value is None:
801
+ if default is None:
802
+ raise Exception(exceptionMsg)
803
+ else:
804
+ return default
805
+ else:
806
+ return value
807
+
808
+
809
+ @contextlib.contextmanager
810
+ def weakImport(package, msg=None):
811
+ """
812
+ This method can be used to tolerate imports that may fail.
813
+
814
+ e.g::
815
+
816
+ from .protocol_ctffind import CistemProtCTFFind
817
+ with weakImport('tomo'):
818
+ from .protocol_ts_ctffind import CistemProtTsCtffind
819
+
820
+ In this case CistemProtTsCtffind should fail if tomo package is missing,
821
+ but exception is captured and all the imports above should be available
822
+
823
+ :param package: name of the package that is expected to fail
824
+
825
+ """
826
+ try:
827
+ yield
828
+ except ImportError as e:
829
+ if "'%s'" % package not in str(e):
830
+ raise e
831
+ elif msg is not None:
832
+ logger.warning(msg)
833
+ # To be removed once developers have installed distro. 20-Nov-2023.
834
+ with weakImport("distro", msg='You are missing distro package. '
835
+ 'Did you "git pulled"?. Please run "scipion3 pip install distro==1.8".'):
836
+ import distro
837
+
838
+ class OS:
839
+ @staticmethod
840
+ def getPlatform():
841
+ return platform.system()
842
+
843
+ @classmethod
844
+ def getDistro(cls):
845
+ return distro
846
+
847
+ @classmethod
848
+ def isWSL(cls):
849
+
850
+ # For now lets assume that if WSL_DISTRO_NAME exists is a WLS
851
+ return cls.getWLSNAME() is not None
852
+
853
+ @classmethod
854
+ def getWLSNAME(cls):
855
+ return os.environ.get("WSL_DISTRO_NAME", None)
856
+
857
+ @classmethod
858
+ def isUbuntu(cls):
859
+ return distro.id() == "ubuntu"
860
+
861
+ @classmethod
862
+ def isCentos(cls):
863
+ return distro.id() == "centos"
864
+
865
+ @classmethod
866
+ def WLSfile2Windows(cls, file):
867
+ # Links in WSL are not valid in windows
868
+ file = os.path.realpath(file).replace("/", "\\")
869
+ file = ("\\\\wsl.localhost\\" + cls.getWLSNAME() + file)
870
+ return file
871
+
872
+
873
+ def valueToList(value):
874
+ """ Returns a list containing value, unless value is already a list. If value is None returns an empty list"""
875
+ if value is None:
876
+ return []
877
+ elif isinstance(value, list):
878
+ return value
879
+ else:
880
+ return [value]