1
2
3
4 __doc__ = """GNUmed general tools."""
5
6
7 __author__ = "K. Hilbert <Karsten.Hilbert@gmx.net>"
8 __license__ = "GPL v2 or later (details at http://www.gnu.org)"
9
10
11 import sys
12 import os
13 import os.path
14 import csv
15 import tempfile
16 import logging
17 import hashlib
18 import platform
19 import subprocess
20 import decimal
21 import getpass
22 import io
23 import functools
24 import json
25 import shutil
26 import zipfile
27 import datetime as pydt
28 import re as regex
29 import xml.sax.saxutils as xml_tools
30
31 import pickle, zlib
32
33 du_core = None
34
35
36
37 if __name__ == '__main__':
38 sys.path.insert(0, '../../')
39 from Gnumed.pycommon import gmBorg
40
41
42 _log = logging.getLogger('gm.tools')
43
44
45 ( CAPS_NONE,
46 CAPS_FIRST,
47 CAPS_ALLCAPS,
48 CAPS_WORDS,
49 CAPS_NAMES,
50 CAPS_FIRST_ONLY
51 ) = range(6)
52
53
54 u_currency_pound = '\u00A3'
55 u_currency_sign = '\u00A4'
56 u_currency_yen = '\u00A5'
57 u_right_double_angle_quote = '\u00AB'
58 u_registered_trademark = '\u00AE'
59 u_plus_minus = '\u00B1'
60 u_superscript_one = '\u00B9'
61 u_left_double_angle_quote = '\u00BB'
62 u_one_quarter = '\u00BC'
63 u_one_half = '\u00BD'
64 u_three_quarters = '\u00BE'
65 u_multiply = '\u00D7'
66 u_greek_ALPHA = '\u0391'
67 u_greek_alpha = '\u03b1'
68 u_greek_OMEGA = '\u03A9'
69 u_greek_omega = '\u03c9'
70 u_dagger = '\u2020'
71 u_triangular_bullet = '\u2023'
72 u_ellipsis = '\u2026'
73 u_euro = '\u20AC'
74 u_numero = '\u2116'
75 u_down_left_arrow = '\u21B5'
76 u_left_arrow = '\u2190'
77 u_up_arrow = '\u2191'
78 u_arrow2right = '\u2192'
79 u_down_arrow = '\u2193'
80 u_left_arrow_with_tail = '\u21a2'
81 u_arrow2right_from_bar = '\u21a6'
82 u_arrow2right_until_vertical_bar = '\u21e5'
83 u_sum = '\u2211'
84 u_almost_equal_to = '\u2248'
85 u_corresponds_to = '\u2258'
86 u_infinity = '\u221E'
87 u_arrow2right_until_vertical_bar2 = '\u2b72'
88
89 u_diameter = '\u2300'
90 u_checkmark_crossed_out = '\u237B'
91 u_box_horiz_high = '\u23ba'
92 u_box_vert_left = '\u23b8'
93 u_box_vert_right = '\u23b9'
94
95 u_space_as_open_box = '\u2423'
96
97 u_box_horiz_single = '\u2500'
98 u_box_vert_light = '\u2502'
99 u_box_horiz_light_3dashes = '\u2504'
100 u_box_vert_light_4dashes = '\u2506'
101 u_box_horiz_4dashes = '\u2508'
102 u_box_T_right = '\u251c'
103 u_box_T_left = '\u2524'
104 u_box_T_down = '\u252c'
105 u_box_T_up = '\u2534'
106 u_box_plus = '\u253c'
107 u_box_top_double = '\u2550'
108 u_box_top_left_double_single = '\u2552'
109 u_box_top_right_double_single = '\u2555'
110 u_box_top_left_arc = '\u256d'
111 u_box_top_right_arc = '\u256e'
112 u_box_bottom_right_arc = '\u256f'
113 u_box_bottom_left_arc = '\u2570'
114 u_box_horiz_light_heavy = '\u257c'
115 u_box_horiz_heavy_light = '\u257e'
116
117 u_skull_and_crossbones = '\u2620'
118 u_caduceus = '\u2624'
119 u_frowning_face = '\u2639'
120 u_smiling_face = '\u263a'
121 u_black_heart = '\u2665'
122 u_female = '\u2640'
123 u_male = '\u2642'
124 u_male_female = '\u26a5'
125 u_chain = '\u26d3'
126
127 u_checkmark_thin = '\u2713'
128 u_checkmark_thick = '\u2714'
129 u_heavy_greek_cross = '\u271a'
130 u_arrow2right_thick = '\u2794'
131 u_writing_hand = '\u270d'
132 u_pencil_1 = '\u270e'
133 u_pencil_2 = '\u270f'
134 u_pencil_3 = '\u2710'
135 u_latin_cross = '\u271d'
136
137 u_arrow2right_until_black_diamond = '\u291e'
138
139 u_kanji_yen = '\u5186'
140 u_replacement_character = '\ufffd'
141 u_link_symbol = '\u1f517'
142
143
144 _kB = 1024
145 _MB = 1024 * _kB
146 _GB = 1024 * _MB
147 _TB = 1024 * _GB
148 _PB = 1024 * _TB
149
150
151 _GM_TITLE_PREFIX = 'GMd'
152
153
155
156 print(".========================================================")
157 print("| Unhandled exception caught !")
158 print("| Type :", t)
159 print("| Value:", v)
160 print("`========================================================")
161 _log.critical('unhandled exception caught', exc_info = (t,v,tb))
162 sys.__excepthook__(t,v,tb)
163
164
165
166
167 -def mkdir(directory=None, mode=None):
168 """Create directory.
169
170 - creates parent dirs if necessary
171 - does not fail if directory exists
172 <mode>: numeric, say 0o0700 for "-rwx------"
173 """
174 if os.path.isdir(directory):
175 if mode is None:
176 return True
177
178 changed = False
179 old_umask = os.umask(0)
180 try:
181
182
183 os.chmod(directory, mode)
184 changed = True
185 finally:
186 os.umask(old_umask)
187 return changed
188
189 if mode is None:
190 os.makedirs(directory)
191 return True
192
193 old_umask = os.umask(0)
194 try:
195 os.makedirs(directory, mode)
196 finally:
197 os.umask(old_umask)
198 return True
199
200
202
203 def _on_rm_error(func, path, exc):
204 _log.error('error while shutil.rmtree(%s)', path, exc_info=exc)
205 return True
206
207 error_count = 0
208 try:
209 shutil.rmtree(directory, False, _on_rm_error)
210 except Exception:
211 _log.exception('cannot shutil.rmtree(%s)', directory)
212 error_count += 1
213 return error_count
214
215
216 -def rm_dir_content(directory):
217 _log.debug('cleaning out [%s]', directory)
218 try:
219 items = os.listdir(directory)
220 except OSError:
221 return False
222 for item in items:
223
224 full_item = os.path.join(directory, item)
225 try:
226 os.remove(full_item)
227 except OSError:
228 _log.debug('[%s] seems to be a subdirectory', full_item)
229 errors = rmdir(full_item)
230 if errors > 0:
231 return False
232 except Exception:
233 _log.exception('cannot os.remove(%s) [a file or a link]', full_item)
234 return False
235
236 return True
237
238
240 if base_dir is None:
241 base_dir = gmPaths().tmp_dir
242 else:
243 if not os.path.isdir(base_dir):
244 mkdir(base_dir, mode = 0o0700)
245 if prefix is None:
246 prefix = 'sndbx-'
247 return tempfile.mkdtemp(prefix = prefix, suffix = '', dir = base_dir)
248
249
251 return os.path.abspath(os.path.join(directory, '..'))
252
253
255
256
257 return os.path.basename(os.path.normpath(directory))
258
259
261 try:
262 empty = (len(os.listdir(directory)) == 0)
263 except OSError as exc:
264 if exc.errno != 2:
265 raise
266 empty = None
267 return empty
268
269
270 -def copy_tree_content(directory, target_directory):
271 """Copy the *content* of <directory> *into* <target_directory>
272 which is created if need be.
273 """
274 assert (directory is not None), 'source <directory> should not be None'
275 _log.debug('copying content of [%s] into [%s]', directory, target_directory)
276 try:
277 items = os.listdir(directory)
278 except OSError:
279 return None
280
281 for item in items:
282 full_item = os.path.join(directory, item)
283 if os.path.isdir(full_item):
284 target_subdir = os.path.join(target_directory, item)
285 try:
286 shutil.copytree(full_item, target_subdir)
287 except Exception:
288 _log.exception('cannot copy subdir [%s]', full_item)
289 return None
290 else:
291 try:
292 shutil.copy2(full_item, target_directory)
293 except Exception:
294 _log.exception('cannot copy file [%s]', full_item)
295 return None
296
297 return target_directory
298
299
300
302 """This class provides the following paths:
303
304 .home_dir user home
305 .local_base_dir script installation dir
306 .working_dir current dir
307 .user_config_dir
308 .system_config_dir
309 .system_app_data_dir (not writable)
310 .tmp_dir instance-local
311 .user_tmp_dir user-local (NOT per instance)
312 .bytea_cache_dir caches downloaded BYTEA data
313 """
314 - def __init__(self, app_name=None, wx=None):
315 """Setup pathes.
316
317 <app_name> will default to (name of the script - .py)
318 """
319 try:
320 self.already_inited
321 return
322 except AttributeError:
323 pass
324
325 self.init_paths(app_name=app_name, wx=wx)
326 self.already_inited = True
327
328
329
330
332
333 if wx is None:
334 _log.debug('wxPython not available')
335 _log.debug('detecting paths directly')
336
337 if app_name is None:
338 app_name, ext = os.path.splitext(os.path.basename(sys.argv[0]))
339 _log.info('app name detected as [%s]', app_name)
340 else:
341 _log.info('app name passed in as [%s]', app_name)
342
343
344 self.__home_dir = None
345
346
347 if getattr(sys, 'frozen', False):
348 _log.info('frozen app, installed into temporary path')
349
350
351
352
353
354
355
356
357
358 self.local_base_dir = os.path.dirname(sys.executable)
359 else:
360 self.local_base_dir = os.path.abspath(os.path.dirname(sys.argv[0]))
361
362
363 self.working_dir = os.path.abspath(os.curdir)
364
365
366 mkdir(os.path.join(self.home_dir, '.%s' % app_name))
367 self.user_config_dir = os.path.join(self.home_dir, '.%s' % app_name)
368
369
370 try:
371 self.system_config_dir = os.path.join('/etc', app_name)
372 except ValueError:
373
374 self.system_config_dir = self.user_config_dir
375
376
377 try:
378 self.system_app_data_dir = os.path.join(sys.prefix, 'share', app_name)
379 except ValueError:
380 self.system_app_data_dir = self.local_base_dir
381
382
383 try:
384 self.__tmp_dir_already_set
385 _log.debug('temp dir already set')
386 except AttributeError:
387 _log.info('temp file prefix: %s', tempfile.gettempprefix())
388 _log.info('initial (user level) temp dir: %s', tempfile.gettempdir())
389 bytes_free = shutil.disk_usage(tempfile.gettempdir()).free
390 _log.info('free disk space for temp dir: %s (%s bytes)', size2str(size = bytes_free), bytes_free)
391
392 self.user_tmp_dir = os.path.join(tempfile.gettempdir(), '%s-%s' % (app_name, getpass.getuser()))
393 mkdir(self.user_tmp_dir, 0o700)
394 _log.info('intermediate (app+user level) temp dir: %s', self.user_tmp_dir)
395
396 tempfile.tempdir = self.user_tmp_dir
397 self.tmp_dir = tempfile.mkdtemp(prefix = 'g-')
398 _log.info('final (app instance level) temp dir: %s', tempfile.gettempdir())
399
400
401 cache_dir = os.path.join(self.user_tmp_dir, '.bytea_cache')
402 try:
403 stat = os.stat(cache_dir)
404 _log.warning('reusing BYTEA cache dir: %s', cache_dir)
405 _log.debug(stat)
406 except FileNotFoundError:
407 mkdir(cache_dir, mode = 0o0700)
408 self.bytea_cache_dir = cache_dir
409
410 self.__log_paths()
411 if wx is None:
412 return True
413
414
415 _log.debug('re-detecting paths with wxPython')
416
417 std_paths = wx.StandardPaths.Get()
418 _log.info('wxPython app name is [%s]', wx.GetApp().GetAppName())
419
420
421 mkdir(os.path.join(std_paths.GetUserConfigDir(), '.%s' % app_name))
422 self.user_config_dir = os.path.join(std_paths.GetUserConfigDir(), '.%s' % app_name)
423
424
425 try:
426 tmp = std_paths.GetConfigDir()
427 if not tmp.endswith(app_name):
428 tmp = os.path.join(tmp, app_name)
429 self.system_config_dir = tmp
430 except ValueError:
431
432 pass
433
434
435
436
437 if 'wxMSW' in wx.PlatformInfo:
438 _log.warning('this platform (wxMSW) sometimes returns a broken value for the system-wide application data dir')
439 else:
440 try:
441 self.system_app_data_dir = std_paths.GetDataDir()
442 except ValueError:
443 pass
444
445 self.__log_paths()
446 return True
447
448
450 _log.debug('sys.argv[0]: %s', sys.argv[0])
451 _log.debug('sys.executable: %s', sys.executable)
452 _log.debug('sys._MEIPASS: %s', getattr(sys, '_MEIPASS', '<not found>'))
453 _log.debug('os.environ["_MEIPASS2"]: %s', os.environ.get('_MEIPASS2', '<not found>'))
454 _log.debug('__file__ : %s', __file__)
455 _log.debug('local application base dir: %s', self.local_base_dir)
456 _log.debug('current working dir: %s', self.working_dir)
457 _log.debug('user home dir: %s', self.home_dir)
458 _log.debug('user-specific config dir: %s', self.user_config_dir)
459 _log.debug('system-wide config dir: %s', self.system_config_dir)
460 _log.debug('system-wide application data dir: %s', self.system_app_data_dir)
461 _log.debug('temporary dir (user): %s', self.user_tmp_dir)
462 _log.debug('temporary dir (instance): %s', self.tmp_dir)
463 _log.debug('temporary dir (tempfile.tempdir): %s', tempfile.tempdir)
464 _log.debug('temporary dir (tempfile.gettempdir()): %s', tempfile.gettempdir())
465 _log.debug('BYTEA cache dir: %s', self.bytea_cache_dir)
466
467
468
469
471 if not (os.access(path, os.R_OK) and os.access(path, os.X_OK)):
472 msg = '[%s:user_config_dir]: invalid path [%s]' % (self.__class__.__name__, path)
473 _log.error(msg)
474 raise ValueError(msg)
475 self.__user_config_dir = path
476
478 return self.__user_config_dir
479
480 user_config_dir = property(_get_user_config_dir, _set_user_config_dir)
481
483 if not (os.access(path, os.R_OK) and os.access(path, os.X_OK)):
484 msg = '[%s:system_config_dir]: invalid path [%s]' % (self.__class__.__name__, path)
485 _log.error(msg)
486 raise ValueError(msg)
487 self.__system_config_dir = path
488
490 return self.__system_config_dir
491
492 system_config_dir = property(_get_system_config_dir, _set_system_config_dir)
493
495 if not (os.access(path, os.R_OK) and os.access(path, os.X_OK)):
496 msg = '[%s:system_app_data_dir]: invalid path [%s]' % (self.__class__.__name__, path)
497 _log.error(msg)
498 raise ValueError(msg)
499 self.__system_app_data_dir = path
500
502 return self.__system_app_data_dir
503
504 system_app_data_dir = property(_get_system_app_data_dir, _set_system_app_data_dir)
505
507 raise ValueError('invalid to set home dir')
508
510 if self.__home_dir is not None:
511 return self.__home_dir
512
513 tmp = os.path.expanduser('~')
514 if tmp == '~':
515 _log.error('this platform does not expand ~ properly')
516 try:
517 tmp = os.environ['USERPROFILE']
518 except KeyError:
519 _log.error('cannot access $USERPROFILE in environment')
520
521 if not (
522 os.access(tmp, os.R_OK)
523 and
524 os.access(tmp, os.X_OK)
525 and
526 os.access(tmp, os.W_OK)
527 ):
528 msg = '[%s:home_dir]: invalid path [%s]' % (self.__class__.__name__, tmp)
529 _log.error(msg)
530 raise ValueError(msg)
531
532 self.__home_dir = tmp
533 return self.__home_dir
534
535 home_dir = property(_get_home_dir, _set_home_dir)
536
537
539 if not (os.access(path, os.R_OK) and os.access(path, os.X_OK)):
540 msg = '[%s:tmp_dir]: invalid path [%s]' % (self.__class__.__name__, path)
541 _log.error(msg)
542 raise ValueError(msg)
543 _log.debug('previous temp dir: %s', tempfile.gettempdir())
544 self.__tmp_dir = path
545 tempfile.tempdir = self.__tmp_dir
546 _log.debug('new temp dir: %s', tempfile.gettempdir())
547 self.__tmp_dir_already_set = True
548
550 return self.__tmp_dir
551
552 tmp_dir = property(_get_tmp_dir, _set_tmp_dir)
553
554
555
556
557 -def recode_file(source_file=None, target_file=None, source_encoding='utf8', target_encoding=None, base_dir=None, error_mode='replace'):
558 if target_encoding is None:
559 return source_file
560 if target_encoding == source_encoding:
561 return source_file
562 if target_file is None:
563 target_file = get_unique_filename (
564 prefix = '%s-%s_%s-' % (fname_stem(source_file), source_encoding, target_encoding),
565 suffix = fname_extension(source_file, '.txt'),
566 tmp_dir = base_dir
567 )
568
569 _log.debug('[%s] -> [%s] (%s -> %s)', source_encoding, target_encoding, source_file, target_file)
570
571 in_file = io.open(source_file, mode = 'rt', encoding = source_encoding)
572 out_file = io.open(target_file, mode = 'wt', encoding = target_encoding, errors = error_mode)
573 for line in in_file:
574 out_file.write(line)
575 out_file.close()
576 in_file.close()
577
578 return target_file
579
580
581 -def unzip_archive(archive_name, target_dir=None, remove_archive=False):
582 _log.debug('unzipping [%s] -> [%s]', archive_name, target_dir)
583 success = False
584 try:
585 with zipfile.ZipFile(archive_name) as archive:
586 archive.extractall(target_dir)
587 success = True
588 except Exception:
589 _log.exception('cannot unzip')
590 return False
591 if remove_archive:
592 remove_file(archive_name)
593 return success
594
595
596 -def remove_file(filename, log_error=True, force=False):
621
622
623 -def file2md5(filename=None, return_hex=True):
624 blocksize = 2**10 * 128
625 _log.debug('md5(%s): <%s> byte blocks', filename, blocksize)
626
627 f = io.open(filename, mode = 'rb')
628
629 md5 = hashlib.md5()
630 while True:
631 data = f.read(blocksize)
632 if not data:
633 break
634 md5.update(data)
635 f.close()
636
637 _log.debug('md5(%s): %s', filename, md5.hexdigest())
638
639 if return_hex:
640 return md5.hexdigest()
641 return md5.digest()
642
643
645 _log.debug('chunked_md5(%s, chunk_size=%s bytes)', filename, chunk_size)
646 md5_concat = ''
647 f = open(filename, 'rb')
648 while True:
649 md5 = hashlib.md5()
650 data = f.read(chunk_size)
651 if not data:
652 break
653 md5.update(data)
654 md5_concat += md5.hexdigest()
655 f.close()
656 md5 = hashlib.md5()
657 md5.update(md5_concat)
658 hex_digest = md5.hexdigest()
659 _log.debug('md5("%s"): %s', md5_concat, hex_digest)
660 return hex_digest
661
662
663 default_csv_reader_rest_key = 'list_of_values_of_unknown_fields'
664
666 try:
667 is_dict_reader = kwargs['dict']
668 del kwargs['dict']
669 except KeyError:
670 is_dict_reader = False
671
672 if is_dict_reader:
673 kwargs['restkey'] = default_csv_reader_rest_key
674 return csv.DictReader(unicode_csv_data, dialect=dialect, **kwargs)
675 return csv.reader(unicode_csv_data, dialect=dialect, **kwargs)
676
677
678
679
681 for line in unicode_csv_data:
682 yield line.encode(encoding)
683
684
685
686
687
689
690
691 try:
692 is_dict_reader = kwargs['dict']
693 del kwargs['dict']
694 if is_dict_reader is not True:
695 raise KeyError
696 kwargs['restkey'] = default_csv_reader_rest_key
697 csv_reader = csv.DictReader(unicode2charset_encoder(unicode_csv_data), dialect=dialect, **kwargs)
698 except KeyError:
699 is_dict_reader = False
700 csv_reader = csv.reader(unicode2charset_encoder(unicode_csv_data), dialect=dialect, **kwargs)
701
702 for row in csv_reader:
703
704 if is_dict_reader:
705 for key in row.keys():
706 if key == default_csv_reader_rest_key:
707 old_data = row[key]
708 new_data = []
709 for val in old_data:
710 new_data.append(str(val, encoding))
711 row[key] = new_data
712 if default_csv_reader_rest_key not in csv_reader.fieldnames:
713 csv_reader.fieldnames.append(default_csv_reader_rest_key)
714 else:
715 row[key] = str(row[key], encoding)
716 yield row
717 else:
718 yield [ str(cell, encoding) for cell in row ]
719
720
721
723 """Normalizes unicode, removes non-alpha characters, converts spaces to underscores."""
724
725 dir_part, name_part = os.path.split(filename)
726 if name_part == '':
727 return filename
728
729 import unicodedata
730 name_part = unicodedata.normalize('NFKD', name_part)
731
732 name_part = regex.sub (
733 '[^.\w\s[\]()%§+-]',
734 '',
735 name_part,
736 flags = regex.UNICODE
737 ).strip()
738
739 name_part = regex.sub (
740 '\s+',
741 '_',
742 name_part,
743 flags = regex.UNICODE
744 )
745 return os.path.join(dir_part, name_part)
746
747
749 """/home/user/dir/filename.ext -> filename"""
750 return os.path.splitext(os.path.basename(filename))[0]
751
752
754 """/home/user/dir/filename.ext -> /home/user/dir/filename"""
755 return os.path.splitext(filename)[0]
756
757
759 """ /home/user/dir/filename.ext -> .ext
760 '' or '.' -> fallback if any else ''
761 """
762 ext = os.path.splitext(filename)[1]
763 if ext.strip() not in ['.', '']:
764 return ext
765 if fallback is None:
766 return ''
767 return fallback
768
769
771
772 return os.path.split(filename)[0]
773
774
776
777 return os.path.split(filename)[1]
778
779
781 """This function has a race condition between
782 its file.close()
783 and actually
784 using the filename in callers.
785
786 The file will NOT exist after calling this function.
787 """
788 if tmp_dir is None:
789 gmPaths()
790 else:
791 if (
792 not os.access(tmp_dir, os.F_OK)
793 or
794 not os.access(tmp_dir, os.X_OK | os.W_OK)
795 ):
796 _log.warning('cannot os.access() temporary dir [%s], using system default', tmp_dir)
797 tmp_dir = None
798
799 if include_timestamp:
800 ts = pydt.datetime.now().strftime('%m%d-%H%M%S-')
801 else:
802 ts = ''
803
804 kwargs = {
805 'dir': tmp_dir,
806
807
808 'delete': True
809 }
810
811 if prefix is None:
812 kwargs['prefix'] = 'gm-%s' % ts
813 else:
814 kwargs['prefix'] = prefix + ts
815
816 if suffix in [None, '']:
817 kwargs['suffix'] = '.tmp'
818 else:
819 if not suffix.startswith('.'):
820 suffix = '.' + suffix
821 kwargs['suffix'] = suffix
822
823 f = tempfile.NamedTemporaryFile(**kwargs)
824 filename = f.name
825 f.close()
826
827 return filename
828
829
831 import ctypes
832
833 kernel32 = ctype.WinDLL('kernel32', use_last_error = True)
834 windows_create_symlink = kernel32.CreateSymbolicLinkW
835 windows_create_symlink.argtypes = (ctypes.c_wchar_p, ctypes.c_wchar_p, ctypes.c_uint32)
836 windows_create_symlink.restype = ctypes.c_ubyte
837 if os.path.isdir(physical_name):
838 flags = 1
839 else:
840 flags = 0
841 ret_code = windows_create_symlink(link_name, physical_name.replace('/', '\\'), flags)
842 _log.debug('ctypes.windll.kernel32.CreateSymbolicLinkW() [%s] exit code: %s', windows_create_symlink, ret_code)
843 if ret_code == 0:
844 raise ctypes.WinError()
845 return ret_code
846
847
848 -def mklink(physical_name, link_name, overwrite=False):
849
850 _log.debug('creating symlink (overwrite = %s):', overwrite)
851 _log.debug('link [%s] =>', link_name)
852 _log.debug('=> physical [%s]', physical_name)
853
854 if os.path.exists(link_name):
855 _log.debug('link exists')
856 if overwrite:
857 return True
858 return False
859
860 try:
861 os.symlink(physical_name, link_name)
862 except (AttributeError, NotImplementedError):
863 _log.debug('this Python does not have os.symlink(), trying via ctypes')
864 __make_symlink_on_windows(physical_name, link_name)
865 except PermissionError:
866 _log.exception('cannot create link')
867 return False
868
869
870 return True
871
872
874 """Import a module from any location."""
875
876 _log.debug('CWD: %s', os.getcwd())
877
878 remove_path = always_remove_path or False
879 if module_path not in sys.path:
880 _log.info('appending to sys.path: [%s]' % module_path)
881 sys.path.append(module_path)
882 remove_path = True
883
884 _log.debug('will remove import path: %s', remove_path)
885
886 if module_name.endswith('.py'):
887 module_name = module_name[:-3]
888
889 try:
890 module = __import__(module_name)
891 except Exception:
892 _log.exception('cannot __import__() module [%s] from [%s]' % (module_name, module_path))
893 while module_path in sys.path:
894 sys.path.remove(module_path)
895 raise
896
897 _log.info('imported module [%s] as [%s]' % (module_name, module))
898 if remove_path:
899 while module_path in sys.path:
900 sys.path.remove(module_path)
901
902 return module
903
904
905
906
908 if size == 1:
909 return template % _('1 Byte')
910 if size < 10 * _kB:
911 return template % _('%s Bytes') % size
912 if size < _MB:
913 return template % '%.1f kB' % (float(size) / _kB)
914 if size < _GB:
915 return template % '%.1f MB' % (float(size) / _MB)
916 if size < _TB:
917 return template % '%.1f GB' % (float(size) / _GB)
918 if size < _PB:
919 return template % '%.1f TB' % (float(size) / _TB)
920 return template % '%.1f PB' % (float(size) / _PB)
921
922
923 -def bool2subst(boolean=None, true_return=True, false_return=False, none_return=None):
924 if boolean is None:
925 return none_return
926 if boolean:
927 return true_return
928 if not boolean:
929 return false_return
930 raise ValueError('bool2subst(): <boolean> arg must be either of True, False, None')
931
932
933 -def bool2str(boolean=None, true_str='True', false_str='False'):
934 return bool2subst (
935 boolean = bool(boolean),
936 true_return = true_str,
937 false_return = false_str
938 )
939
940
941 -def none_if(value=None, none_equivalent=None, strip_string=False):
942 """Modelled after the SQL NULLIF function."""
943 if value is None:
944 return None
945 if strip_string:
946 stripped = value.strip()
947 else:
948 stripped = value
949 if stripped == none_equivalent:
950 return None
951 return value
952
953
954 -def coalesce(value2test=None, return_instead=None, template4value=None, template4instead=None, none_equivalents=None, function4value=None, value2return=None):
955 """Modelled after the SQL coalesce function.
956
957 To be used to simplify constructs like:
958
959 if value2test is None (or in none_equivalents):
960 value = (template4instead % return_instead) or return_instead
961 else:
962 value = (template4value % value2test) or value2test
963 print value
964
965 @param value2test: the value to be tested for <None>
966 @type value2test: any Python type, must have a __str__ method if template4value is not None
967 @param return_instead: the value to be returned if <initial> is None
968 @type return_instead: any Python type, must have a __str__ method if template4instead is not None
969 @param template4value: if <initial> is returned replace the value into this template, must contain one <%s>
970 @type template4value: string or None
971 @param template4instead: if <return_instead> is returned replace the value into this template, must contain one <%s>
972 @type template4instead: string or None
973
974 example:
975 function4value = ('strftime', '%Y-%m-%d')
976
977 Ideas:
978 - list of return_insteads: initial, [return_instead, template], [return_instead, template], [return_instead, template], template4value, ...
979 """
980 if none_equivalents is None:
981 none_equivalents = [None]
982
983 if value2test in none_equivalents:
984 if template4instead is None:
985 return return_instead
986 return template4instead % return_instead
987
988
989
990
991 if value2return is not None:
992 return value2return
993
994 value2return = value2test
995
996 if function4value is not None:
997 funcname, args = function4value
998 func = getattr(value2test, funcname)
999 value2return = func(args)
1000
1001
1002 if template4value is None:
1003 return value2return
1004
1005 try:
1006 return template4value % value2return
1007 except TypeError:
1008
1009
1010
1011
1012 if hasattr(_log, 'log_stack_trace'):
1013 _log.log_stack_trace(message = 'deprecated use of <template4value> for <value2return>')
1014 else:
1015 _log.error('deprecated use of <template4value> for <value2return>')
1016 _log.error(locals())
1017 return template4value
1018
1019
1021 val = match_obj.group(0).lower()
1022 if val in ['von', 'van', 'de', 'la', 'l', 'der', 'den']:
1023 return val
1024 buf = list(val)
1025 buf[0] = buf[0].upper()
1026 for part in ['mac', 'mc', 'de', 'la']:
1027 if len(val) > len(part) and val[:len(part)] == part:
1028 buf[len(part)] = buf[len(part)].upper()
1029 return ''.join(buf)
1030
1031
1033 """Capitalize the first character but leave the rest alone.
1034
1035 Note that we must be careful about the locale, this may
1036 have issues ! However, for UTF strings it should just work.
1037 """
1038 if (mode is None) or (mode == CAPS_NONE):
1039 return text
1040
1041 if len(text) == 0:
1042 return text
1043
1044 if mode == CAPS_FIRST:
1045 if len(text) == 1:
1046 return text[0].upper()
1047 return text[0].upper() + text[1:]
1048
1049 if mode == CAPS_ALLCAPS:
1050 return text.upper()
1051
1052 if mode == CAPS_FIRST_ONLY:
1053
1054
1055 return text[0].upper() + text[1:].lower()
1056
1057 if mode == CAPS_WORDS:
1058
1059 return regex.sub(r'(\w)(\w+)', lambda x: x.group(1).upper() + x.group(2).lower(), text)
1060
1061 if mode == CAPS_NAMES:
1062
1063 return capitalize(text=text, mode=CAPS_FIRST)
1064
1065 print("ERROR: invalid capitalization mode: [%s], leaving input as is" % mode)
1066 return text
1067
1068
1090
1091
1117
1118
1119 -def strip_prefix(text, prefix, remove_repeats=False, remove_whitespace=False):
1120 if remove_whitespace:
1121 text = text.lstrip()
1122 if not text.startswith(prefix):
1123 return text
1124
1125 text = text.replace(prefix, '', 1)
1126 if not remove_repeats:
1127 if remove_whitespace:
1128 return text.lstrip()
1129 return text
1130
1131 return strip_prefix(text, prefix, remove_repeats = True, remove_whitespace = remove_whitespace)
1132
1133
1141
1142
1145
1146
1147 -def strip_suffix(text, suffix, remove_repeats=False, remove_whitespace=False):
1148 suffix_len = len(suffix)
1149 if remove_repeats:
1150 if remove_whitespace:
1151 while text.rstrip().endswith(suffix):
1152 text = text.rstrip()[:-suffix_len].rstrip()
1153 return text
1154 while text.endswith(suffix):
1155 text = text[:-suffix_len]
1156 return text
1157 if remove_whitespace:
1158 return text.rstrip()[:-suffix_len].rstrip()
1159 return text[:-suffix_len]
1160
1161
1163 if lines is None:
1164 lines = text.split(eol)
1165
1166 while True:
1167 if lines[0].strip(eol).strip() != '':
1168 break
1169 lines = lines[1:]
1170
1171 if return_list:
1172 return lines
1173
1174 return eol.join(lines)
1175
1176
1178 if lines is None:
1179 lines = text.split(eol)
1180
1181 while True:
1182 if lines[-1].strip(eol).strip() != '':
1183 break
1184 lines = lines[:-1]
1185
1186 if return_list:
1187 return lines
1188
1189 return eol.join(lines)
1190
1191
1199
1200
1201 -def list2text(lines, initial_indent='', subsequent_indent='', eol='\n', strip_leading_empty_lines=True, strip_trailing_empty_lines=True, strip_trailing_whitespace=True, max_line_width=None):
1202
1203 if len(lines) == 0:
1204 return ''
1205
1206 if strip_leading_empty_lines:
1207 lines = strip_leading_empty_lines(lines = lines, eol = eol, return_list = True)
1208
1209 if strip_trailing_empty_lines:
1210 lines = strip_trailing_empty_lines(lines = lines, eol = eol, return_list = True)
1211
1212 if strip_trailing_whitespace:
1213 lines = [ l.rstrip() for l in lines ]
1214
1215 if max_line_width is not None:
1216 wrapped_lines = []
1217 for l in lines:
1218 wrapped_lines.extend(wrap(l, max_line_width).split('\n'))
1219 lines = wrapped_lines
1220
1221 indented_lines = [initial_indent + lines[0]]
1222 indented_lines.extend([ subsequent_indent + l for l in lines[1:] ])
1223
1224 return eol.join(indented_lines)
1225
1226
1227 -def wrap(text=None, width=None, initial_indent='', subsequent_indent='', eol='\n'):
1228 """A word-wrap function that preserves existing line breaks
1229 and most spaces in the text. Expects that existing line
1230 breaks are posix newlines (\n).
1231 """
1232 if width is None:
1233 return text
1234
1235 wrapped = initial_indent + functools.reduce (
1236 lambda line, word, width=width: '%s%s%s' % (
1237 line,
1238 ' \n'[(len(line) - line.rfind('\n') - 1 + len(word.split('\n',1)[0]) >= width)],
1239 word
1240 ),
1241 text.split(' ')
1242 )
1243 if subsequent_indent != '':
1244 wrapped = ('\n%s' % subsequent_indent).join(wrapped.split('\n'))
1245 if eol != '\n':
1246 wrapped = wrapped.replace('\n', eol)
1247 return wrapped
1248
1249
1250 -def unwrap(text=None, max_length=None, strip_whitespace=True, remove_empty_lines=True, line_separator = ' // '):
1251
1252 text = text.replace('\r', '')
1253 lines = text.split('\n')
1254 text = ''
1255 for line in lines:
1256
1257 if strip_whitespace:
1258 line = line.strip().strip('\t').strip()
1259
1260 if remove_empty_lines:
1261 if line == '':
1262 continue
1263
1264 text += ('%s%s' % (line, line_separator))
1265
1266 text = text.rstrip(line_separator)
1267
1268 if max_length is not None:
1269 text = text[:max_length]
1270
1271 text = text.rstrip(line_separator)
1272
1273 return text
1274
1275
1276 -def shorten_text(text=None, max_length=None):
1277
1278 if len(text) <= max_length:
1279 return text
1280
1281 return text[:max_length-1] + u_ellipsis
1282
1283
1285 if text is None:
1286 return None
1287 if max_length is None:
1288 max_length = len(text)
1289 else:
1290 if len(text) <= max_length:
1291 return text
1292 old_words = regex.split('\s+', text, flags = regex.UNICODE)
1293 no_old_words = len(old_words)
1294 max_word_length = max(min_word_length, (max_length // no_old_words))
1295 words = []
1296 for word in old_words:
1297 if len(word) <= max_word_length:
1298 words.append(word)
1299 continue
1300 if ignore_numbers:
1301 tmp = word.replace('-', '').replace('+', '').replace('.', '').replace(',', '').replace('/', '').replace('&', '').replace('*', '')
1302 if tmp.isdigit():
1303 words.append(word)
1304 continue
1305 words.append(word[:max_word_length] + ellipsis)
1306 return ' '.join(words)
1307
1308
1310 """check for special XML characters and transform them"""
1311 return xml_tools.escape(text)
1312
1313
1314 -def tex_escape_string(text=None, replace_known_unicode=True, replace_eol=False, keep_visual_eol=False):
1315 """Check for special TeX characters and transform them.
1316
1317 replace_eol:
1318 replaces "\n" with "\\newline"
1319 keep_visual_eol:
1320 replaces "\n" with "\\newline \n" such that
1321 both LaTeX will know to place a line break
1322 at this point as well as the visual formatting
1323 is preserved in the LaTeX source (think multi-
1324 row table cells)
1325 """
1326 text = text.replace('\\', '\\textbackslash')
1327 text = text.replace('^', '\\textasciicircum')
1328 text = text.replace('~', '\\textasciitilde')
1329
1330 text = text.replace('{', '\\{')
1331 text = text.replace('}', '\\}')
1332 text = text.replace('%', '\\%')
1333 text = text.replace('&', '\\&')
1334 text = text.replace('#', '\\#')
1335 text = text.replace('$', '\\$')
1336 text = text.replace('_', '\\_')
1337 if replace_eol:
1338 if keep_visual_eol:
1339 text = text.replace('\n', '\\newline \n')
1340 else:
1341 text = text.replace('\n', '\\newline ')
1342
1343 if replace_known_unicode:
1344
1345 text = text.replace(u_euro, '\\EUR')
1346 text = text.replace(u_sum, '$\\Sigma$')
1347
1348 return text
1349
1350
1352 global du_core
1353 if du_core is None:
1354 try:
1355 from docutils import core as du_core
1356 except ImportError:
1357 _log.warning('cannot turn ReST into LaTeX: docutils not installed')
1358 return tex_escape_string(text = rst_text)
1359
1360 parts = du_core.publish_parts (
1361 source = rst_text.replace('\\', '\\\\'),
1362 source_path = '<internal>',
1363 writer_name = 'latex',
1364
1365 settings_overrides = {
1366 'input_encoding': 'unicode'
1367 },
1368 enable_exit_status = True
1369 )
1370 return parts['body']
1371
1372
1373 -def rst2html(rst_text, replace_eol=False, keep_visual_eol=False):
1374 global du_core
1375 if du_core is None:
1376 try:
1377 from docutils import core as du_core
1378 except ImportError:
1379 _log.warning('cannot turn ReST into HTML: docutils not installed')
1380 return html_escape_string(text = rst_text, replace_eol=False, keep_visual_eol=False)
1381
1382 parts = du_core.publish_parts (
1383 source = rst_text.replace('\\', '\\\\'),
1384 source_path = '<internal>',
1385 writer_name = 'latex',
1386
1387 settings_overrides = {
1388 'input_encoding': 'unicode'
1389 },
1390 enable_exit_status = True
1391 )
1392 return parts['body']
1393
1394
1399
1400
1401 __html_escape_table = {
1402 "&": "&",
1403 '"': """,
1404 "'": "'",
1405 ">": ">",
1406 "<": "<",
1407 }
1408
1417
1418
1421
1422
1424 if isinstance(obj, pydt.datetime):
1425 return obj.isoformat()
1426 raise TypeError('cannot json_serialize(%s)' % type(obj))
1427
1428
1429
1431 _log.info('comparing dict-likes: %s[%s] vs %s[%s]', coalesce(title1, '', '"%s" '), type(d1), coalesce(title2, '', '"%s" '), type(d2))
1432 try:
1433 d1 = dict(d1)
1434 except TypeError:
1435 pass
1436 try:
1437 d2 = dict(d2)
1438 except TypeError:
1439 pass
1440 keys_d1 = frozenset(d1.keys())
1441 keys_d2 = frozenset(d2.keys())
1442 different = False
1443 if len(keys_d1) != len(keys_d2):
1444 _log.info('different number of keys: %s vs %s', len(keys_d1), len(keys_d2))
1445 different = True
1446 for key in keys_d1:
1447 if key in keys_d2:
1448 if type(d1[key]) != type(d2[key]):
1449 _log.info('%25.25s: type(dict1) = %s = >>>%s<<<' % (key, type(d1[key]), d1[key]))
1450 _log.info('%25.25s type(dict2) = %s = >>>%s<<<' % ('', type(d2[key]), d2[key]))
1451 different = True
1452 continue
1453 if d1[key] == d2[key]:
1454 _log.info('%25.25s: both = >>>%s<<<' % (key, d1[key]))
1455 else:
1456 _log.info('%25.25s: dict1 = >>>%s<<<' % (key, d1[key]))
1457 _log.info('%25.25s dict2 = >>>%s<<<' % ('', d2[key]))
1458 different = True
1459 else:
1460 _log.info('%25.25s: %50.50s | <MISSING>' % (key, '>>>%s<<<' % d1[key]))
1461 different = True
1462 for key in keys_d2:
1463 if key in keys_d1:
1464 continue
1465 _log.info('%25.25s: %50.50s | %.50s' % (key, '<MISSING>', '>>>%s<<<' % d2[key]))
1466 different = True
1467 if different:
1468 _log.info('dict-likes appear to be different from each other')
1469 return False
1470 _log.info('dict-likes appear equal to each other')
1471 return True
1472
1473
1566
1567
1613
1614
1615 -def dicts2table(dict_list, left_margin=0, eol='\n', keys2ignore=None, headers=None, show_only_changes=False, equality_value='<=>', date_format=None):
1616
1617 keys2show = []
1618 dict_max_size = {}
1619 max_row_label_size = 0
1620 if keys2ignore is None:
1621 keys2ignore = []
1622
1623
1624 for d in dict_list:
1625 dict_max_size[id(d)] = 0
1626 for key in d.keys():
1627 if key in keys2ignore:
1628 continue
1629 dict_max_size[id(d)] = max(dict_max_size[id(d)], len('%s' % d[key]))
1630 if key in keys2show:
1631 continue
1632 keys2show.append(key)
1633 max_row_label_size = max(max_row_label_size, len('%s' % key))
1634
1635
1636 lines = { k: [] for k in keys2show }
1637 prev_vals = {}
1638 for d in dict_list:
1639 if show_only_changes:
1640 max_size = max(dict_max_size[id(d)], len(equality_value))
1641 else:
1642 max_size = dict_max_size[id(d)]
1643 max_len_str = '%s.%s' % (max_size, max_size)
1644 field_template = ' %' + max_len_str + 's'
1645 for key in keys2show:
1646 try:
1647 val = d[key]
1648 except KeyError:
1649 lines[key].append(field_template % _('<missing>'))
1650 continue
1651 if isinstance(val, pydt.datetime):
1652 if date_format is not None:
1653 val = val.strftime(date_format)
1654 lines[key].append(field_template % val)
1655 if show_only_changes:
1656 if key not in prev_vals:
1657 prev_vals[key] = '%s' % lines[key][-1]
1658 continue
1659 if lines[key][-1] != prev_vals[key]:
1660 prev_vals[key] = '%s' % lines[key][-1]
1661 continue
1662 lines[key][-1] = field_template % equality_value
1663
1664
1665 table_lines = []
1666 max_len_str = '%s.%s' % (max_row_label_size, max_row_label_size)
1667 row_label_template = '%' + max_len_str + 's'
1668 for key in lines:
1669 line = (' ' * left_margin) + row_label_template % key + '|'
1670 line += '|'.join(lines[key])
1671 table_lines.append(line)
1672
1673
1674 if headers is not None:
1675 header_line1 = (' ' * left_margin) + row_label_template % ''
1676 header_line2 = (' ' * left_margin) + u_box_horiz_single * (max_row_label_size)
1677 header_sizes = [ max(dict_max_size[dict_id], len(equality_value)) for dict_id in dict_max_size ]
1678 for idx in range(len(headers)):
1679 max_len_str = '%s.%s' % (header_sizes[idx], header_sizes[idx])
1680 header_template = '%' + max_len_str + 's'
1681 header_line1 += '| '
1682 header_line1 += header_template % headers[idx]
1683 header_line2 += '%s%s' % (u_box_plus, u_box_horiz_single)
1684 header_line2 += u_box_horiz_single * header_sizes[idx]
1685 table_lines.insert(0, header_line2)
1686 table_lines.insert(0, header_line1)
1687
1688 if eol is None:
1689 return table_lines
1690
1691 return ('|' + eol).join(table_lines) + '|' + eol
1692
1693
1695 for key in required_keys:
1696 try:
1697 d[key]
1698 except KeyError:
1699 if missing_key_template is None:
1700 d[key] = None
1701 else:
1702 d[key] = missing_key_template % {'key': key}
1703 return d
1704
1705
1707 try:
1708 import pyudev
1709 import psutil
1710 except ImportError:
1711 _log.error('pyudev and/or psutil not installed')
1712 return {}
1713
1714 removable_partitions = {}
1715 ctxt = pyudev.Context()
1716 removable_devices = [ dev for dev in ctxt.list_devices(subsystem='block', DEVTYPE='disk') if dev.attributes.get('removable') == b'1' ]
1717 all_mounted_partitions = { part.device: part for part in psutil.disk_partitions() }
1718 for device in removable_devices:
1719 _log.debug('removable device: %s', device.properties['ID_MODEL'])
1720 partitions_on_removable_device = {
1721 part.device_node: {
1722 'type': device.properties['ID_TYPE'],
1723 'bus': device.properties['ID_BUS'],
1724 'device': device.properties['DEVNAME'],
1725 'partition': part.properties['DEVNAME'],
1726 'vendor': part.properties['ID_VENDOR'],
1727 'model': part.properties['ID_MODEL'],
1728 'fs_label': part.properties['ID_FS_LABEL'],
1729 'is_mounted': False,
1730 'mountpoint': None,
1731 'fs_type': None,
1732 'size_in_bytes': -1,
1733 'bytes_free': 0
1734 } for part in ctxt.list_devices(subsystem='block', DEVTYPE='partition', parent=device)
1735 }
1736 for part in partitions_on_removable_device:
1737 try:
1738 partitions_on_removable_device[part]['mountpoint'] = all_mounted_partitions[part].mountpoint
1739 partitions_on_removable_device[part]['is_mounted'] = True
1740 partitions_on_removable_device[part]['fs_type'] = all_mounted_partitions[part].fstype
1741 du = shutil.disk_usage(all_mounted_partitions[part].mountpoint)
1742 partitions_on_removable_device[part]['size_in_bytes'] = du.total
1743 partitions_on_removable_device[part]['bytes_free'] = du.free
1744 except KeyError:
1745 pass
1746 removable_partitions.update(partitions_on_removable_device)
1747 return removable_partitions
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1760 try:
1761 import pyudev
1762 except ImportError:
1763 _log.error('pyudev not installed')
1764 return []
1765
1766 optical_writers = []
1767 ctxt = pyudev.Context()
1768 for dev in [ dev for dev in ctxt.list_devices(subsystem='block', DEVTYPE='disk') if dev.properties.get('ID_CDROM_CD_RW', None) == '1' ]:
1769 optical_writers.append ({
1770 'type': dev.properties['ID_TYPE'],
1771 'bus': dev.properties['ID_BUS'],
1772 'device': dev.properties['DEVNAME'],
1773 'model': dev.properties['ID_MODEL']
1774 })
1775 return optical_writers
1776
1777
1778
1805
1806
1807
1808
1809
1810 __icon_serpent = \
1811 """x\xdae\x8f\xb1\x0e\x83 \x10\x86w\x9f\xe2\x92\x1blb\xf2\x07\x96\xeaH:0\xd6\
1812 \xc1\x85\xd5\x98N5\xa5\xef?\xf5N\xd0\x8a\xdcA\xc2\xf7qw\x84\xdb\xfa\xb5\xcd\
1813 \xd4\xda;\xc9\x1a\xc8\xb6\xcd<\xb5\xa0\x85\x1e\xeb\xbc\xbc7b!\xf6\xdeHl\x1c\
1814 \x94\x073\xec<*\xf7\xbe\xf7\x99\x9d\xb21~\xe7.\xf5\x1f\x1c\xd3\xbdVlL\xc2\
1815 \xcf\xf8ye\xd0\x00\x90\x0etH \x84\x80B\xaa\x8a\x88\x85\xc4(U\x9d$\xfeR;\xc5J\
1816 \xa6\x01\xbbt9\xceR\xc8\x81e_$\x98\xb9\x9c\xa9\x8d,y\xa9t\xc8\xcf\x152\xe0x\
1817 \xe9$\xf5\x07\x95\x0cD\x95t:\xb1\x92\xae\x9cI\xa8~\x84\x1f\xe0\xa3ec"""
1818
1820
1821 paths = gmPaths(app_name = 'gnumed', wx = wx)
1822
1823 candidates = [
1824 os.path.join(paths.system_app_data_dir, 'bitmaps', 'gm_icon-serpent_and_gnu.png'),
1825 os.path.join(paths.local_base_dir, 'bitmaps', 'gm_icon-serpent_and_gnu.png'),
1826 os.path.join(paths.system_app_data_dir, 'bitmaps', 'serpent.png'),
1827 os.path.join(paths.local_base_dir, 'bitmaps', 'serpent.png')
1828 ]
1829
1830 found_as = None
1831 for candidate in candidates:
1832 try:
1833 open(candidate, 'r').close()
1834 found_as = candidate
1835 break
1836 except IOError:
1837 _log.debug('icon not found in [%s]', candidate)
1838
1839 if found_as is None:
1840 _log.warning('no icon file found, falling back to builtin (ugly) icon')
1841 icon_bmp_data = wx.BitmapFromXPMData(pickle.loads(zlib.decompress(__icon_serpent)))
1842 icon.CopyFromBitmap(icon_bmp_data)
1843 else:
1844 _log.debug('icon found in [%s]', found_as)
1845 icon = wx.Icon()
1846 try:
1847 icon.LoadFile(found_as, wx.BITMAP_TYPE_ANY)
1848 except AttributeError:
1849 _log.exception("this platform doesn't support wx.Icon().LoadFile()")
1850
1851 return icon
1852
1853
1854 -def create_qrcode(text=None, filename=None, qr_filename=None, verbose=False):
1855 assert (not ((text is None) and (filename is None))), 'either <text> or <filename> must be specified'
1856
1857 try:
1858 import pyqrcode
1859 except ImportError:
1860 _log.exception('cannot import <pyqrcode>')
1861 return None
1862 if text is None:
1863 with io.open(filename, mode = 'rt', encoding = 'utf8') as input_file:
1864 text = input_file.read()
1865 if qr_filename is None:
1866 if filename is None:
1867 qr_filename = get_unique_filename(prefix = 'gm-qr-', suffix = '.png')
1868 else:
1869 qr_filename = get_unique_filename (
1870 prefix = fname_stem(filename) + '-',
1871 suffix = fname_extension(filename) + '.png'
1872 )
1873 _log.debug('[%s] -> [%s]', filename, qr_filename)
1874 qr = pyqrcode.create(text, encoding = 'utf8')
1875 if verbose:
1876 print('input file:', filename)
1877 print('output file:', qr_filename)
1878 print('text to encode:', text)
1879 print(qr.terminal())
1880 qr.png(qr_filename, quiet_zone = 1)
1881 return qr_filename
1882
1883
1884
1885
1886 if __name__ == '__main__':
1887
1888 if len(sys.argv) < 2:
1889 sys.exit()
1890
1891 if sys.argv[1] != 'test':
1892 sys.exit()
1893
1894
1895 logging.basicConfig(level = logging.DEBUG)
1896 from Gnumed.pycommon import gmI18N
1897 gmI18N.activate_locale()
1898 gmI18N.install_domain()
1899
1900
1958
1963
1965
1966 val = None
1967 print(val, coalesce(val, 'is None', 'is not None'))
1968 val = 1
1969 print(val, coalesce(val, 'is None', 'is not None'))
1970 return
1971
1972 import datetime as dt
1973 print(coalesce(value2test = dt.datetime.now(), template4value = '-- %s --', function4value = ('strftime', '%Y-%m-%d')))
1974
1975 print('testing coalesce()')
1976 print("------------------")
1977 tests = [
1978 [None, 'something other than <None>', None, None, 'something other than <None>'],
1979 ['Captain', 'Mr.', '%s.'[:4], 'Mr.', 'Capt.'],
1980 ['value to test', 'test 3 failed', 'template with "%s" included', None, 'template with "value to test" included'],
1981 ['value to test', 'test 4 failed', 'template with value not included', None, 'template with value not included'],
1982 [None, 'initial value was None', 'template4value: %s', None, 'initial value was None'],
1983 [None, 'initial value was None', 'template4value: %%(abc)s', None, 'initial value was None']
1984 ]
1985 passed = True
1986 for test in tests:
1987 result = coalesce (
1988 value2test = test[0],
1989 return_instead = test[1],
1990 template4value = test[2],
1991 template4instead = test[3]
1992 )
1993 if result != test[4]:
1994 print("ERROR")
1995 print("coalesce: (%s, %s, %s, %s)" % (test[0], test[1], test[2], test[3]))
1996 print("expected:", test[4])
1997 print("received:", result)
1998 passed = False
1999
2000 if passed:
2001 print("passed")
2002 else:
2003 print("failed")
2004 return passed
2005
2007 print('testing capitalize() ...')
2008 success = True
2009 pairs = [
2010
2011 ['Boot', 'Boot', CAPS_FIRST_ONLY],
2012 ['boot', 'Boot', CAPS_FIRST_ONLY],
2013 ['booT', 'Boot', CAPS_FIRST_ONLY],
2014 ['BoOt', 'Boot', CAPS_FIRST_ONLY],
2015 ['boots-Schau', 'Boots-Schau', CAPS_WORDS],
2016 ['boots-sChau', 'Boots-Schau', CAPS_WORDS],
2017 ['boot camp', 'Boot Camp', CAPS_WORDS],
2018 ['fahrner-Kampe', 'Fahrner-Kampe', CAPS_NAMES],
2019 ['häkkönen', 'Häkkönen', CAPS_NAMES],
2020 ['McBurney', 'McBurney', CAPS_NAMES],
2021 ['mcBurney', 'McBurney', CAPS_NAMES],
2022 ['blumberg', 'Blumberg', CAPS_NAMES],
2023 ['roVsing', 'RoVsing', CAPS_NAMES],
2024 ['Özdemir', 'Özdemir', CAPS_NAMES],
2025 ['özdemir', 'Özdemir', CAPS_NAMES],
2026 ]
2027 for pair in pairs:
2028 result = capitalize(pair[0], pair[2])
2029 if result != pair[1]:
2030 success = False
2031 print('ERROR (caps mode %s): "%s" -> "%s", expected "%s"' % (pair[2], pair[0], result, pair[1]))
2032
2033 if success:
2034 print("... SUCCESS")
2035
2036 return success
2037
2039 print("testing import_module_from_directory()")
2040 path = sys.argv[1]
2041 name = sys.argv[2]
2042 try:
2043 mod = import_module_from_directory(module_path = path, module_name = name)
2044 except Exception:
2045 print("module import failed, see log")
2046 return False
2047
2048 print("module import succeeded", mod)
2049 print(dir(mod))
2050 return True
2051
2053 print("testing mkdir(%s)" % sys.argv[2])
2054 mkdir(sys.argv[2], 0o0700)
2055
2066
2068 print("testing none_if()")
2069 print("-----------------")
2070 tests = [
2071 [None, None, None],
2072 ['a', 'a', None],
2073 ['a', 'b', 'a'],
2074 ['a', None, 'a'],
2075 [None, 'a', None],
2076 [1, 1, None],
2077 [1, 2, 1],
2078 [1, None, 1],
2079 [None, 1, None]
2080 ]
2081
2082 for test in tests:
2083 if none_if(value = test[0], none_equivalent = test[1]) != test[2]:
2084 print('ERROR: none_if(%s) returned [%s], expected [%s]' % (test[0], none_if(test[0], test[1]), test[2]))
2085
2086 return True
2087
2089 tests = [
2090 [True, 'Yes', 'Yes', 'Yes'],
2091 [False, 'OK', 'not OK', 'not OK']
2092 ]
2093 for test in tests:
2094 if bool2str(test[0], test[1], test[2]) != test[3]:
2095 print('ERROR: bool2str(%s, %s, %s) returned [%s], expected [%s]' % (test[0], test[1], test[2], bool2str(test[0], test[1], test[2]), test[3]))
2096
2097 return True
2098
2100
2101 print(bool2subst(True, 'True', 'False', 'is None'))
2102 print(bool2subst(False, 'True', 'False', 'is None'))
2103 print(bool2subst(None, 'True', 'False', 'is None'))
2104
2111
2113 print("testing size2str()")
2114 print("------------------")
2115 tests = [0, 1, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000, 100000000000, 1000000000000, 10000000000000]
2116 for test in tests:
2117 print(size2str(test))
2118
2120
2121 test = """
2122 second line\n
2123 3rd starts with tab \n
2124 4th with a space \n
2125
2126 6th
2127
2128 """
2129 print(unwrap(text = test, max_length = 25))
2130
2132 test = 'line 1\nline 2\nline 3'
2133
2134 print("wrap 5-6-7 initial 0, subsequent 0")
2135 print(wrap(test, 5))
2136 print()
2137 print(wrap(test, 6))
2138 print()
2139 print(wrap(test, 7))
2140 print("-------")
2141 input()
2142 print("wrap 5 initial 1-1-3, subsequent 1-3-1")
2143 print(wrap(test, 5, ' ', ' '))
2144 print()
2145 print(wrap(test, 5, ' ', ' '))
2146 print()
2147 print(wrap(test, 5, ' ', ' '))
2148 print("-------")
2149 input()
2150 print("wrap 6 initial 1-1-3, subsequent 1-3-1")
2151 print(wrap(test, 6, ' ', ' '))
2152 print()
2153 print(wrap(test, 6, ' ', ' '))
2154 print()
2155 print(wrap(test, 6, ' ', ' '))
2156 print("-------")
2157 input()
2158 print("wrap 7 initial 1-1-3, subsequent 1-3-1")
2159 print(wrap(test, 7, ' ', ' '))
2160 print()
2161 print(wrap(test, 7, ' ', ' '))
2162 print()
2163 print(wrap(test, 7, ' ', ' '))
2164
2166 print('md5 %s: %s' % (sys.argv[2], file2md5(sys.argv[2])))
2167 print('chunked md5 %s: %s' % (sys.argv[2], file2chunked_md5(sys.argv[2])))
2168
2171
2176
2178 tests = ['\\', '^', '~', '{', '}', '%', '&', '#', '$', '_', u_euro, 'abc\ndef\n\n1234']
2179 tests.append(' '.join(tests))
2180 for test in tests:
2181 print('%s:' % test, tex_escape_string(test))
2182
2183
2185 tests = ['\\', '^', '~', '{', '}', '%', '&', '#', '$', '_', u_euro, 'abc\ndef\n\n1234']
2186 tests.append(' '.join(tests))
2187 tests.append('C:\Windows\Programme\System 32\lala.txt')
2188 tests.extend([
2189 'should be identical',
2190 'text *some text* text',
2191 """A List
2192 ======
2193
2194 1. 1
2195 2. 2
2196
2197 3. ist-list
2198 1. more
2199 2. noch was ü
2200 #. nummer x"""
2201 ])
2202 for test in tests:
2203 print('==================================================')
2204 print('raw:')
2205 print(test)
2206 print('---------')
2207 print('ReST 2 LaTeX:')
2208 latex = rst2latex_snippet(test)
2209 print(latex)
2210 if latex.strip() == test.strip():
2211 print('=> identical')
2212 print('---------')
2213 print('tex_escape_string:')
2214 print(tex_escape_string(test))
2215 input()
2216
2217
2219 tests = [
2220 'one line, no embedded line breaks ',
2221 'one line\nwith embedded\nline\nbreaks\n '
2222 ]
2223 for test in tests:
2224 print('as list:')
2225 print(strip_trailing_empty_lines(text = test, eol='\n', return_list = True))
2226 print('as string:')
2227 print('>>>%s<<<' % strip_trailing_empty_lines(text = test, eol='\n', return_list = False))
2228 tests = [
2229 ['list', 'without', 'empty', 'trailing', 'lines'],
2230 ['list', 'with', 'empty', 'trailing', 'lines', '', ' ', '']
2231 ]
2232 for test in tests:
2233 print('as list:')
2234 print(strip_trailing_empty_lines(lines = test, eol = '\n', return_list = True))
2235 print('as string:')
2236 print(strip_trailing_empty_lines(lines = test, eol = '\n', return_list = False))
2237
2239 tests = [
2240 r'abc.exe',
2241 r'\abc.exe',
2242 r'c:\abc.exe',
2243 r'c:\d\abc.exe',
2244 r'/home/ncq/tmp.txt',
2245 r'~/tmp.txt',
2246 r'./tmp.txt',
2247 r'./.././tmp.txt',
2248 r'tmp.txt'
2249 ]
2250 for t in tests:
2251 print("[%s] -> [%s]" % (t, fname_stem(t)))
2252
2254 print(sys.argv[2], 'empty:', dir_is_empty(sys.argv[2]))
2255
2256
2258 d1 = {}
2259 d2 = {}
2260 d1[1] = 1
2261 d1[2] = 2
2262 d1[3] = 3
2263
2264 d1[5] = 5
2265
2266 d2[1] = 1
2267 d2[2] = None
2268
2269 d2[4] = 4
2270
2271
2272
2273 d1 = {1: 1, 2: 2}
2274 d2 = {1: 1, 2: 2}
2275
2276
2277 print(format_dict_like(d1, tabular = False))
2278 print(format_dict_like(d1, tabular = True))
2279
2280
2281
2302
2303
2305 rmdir('cx:\windows\system3__2xxxxxxxxxxxxx')
2306
2307
2309
2310 print(rm_dir_content('/tmp/user/1000/tmp'))
2311
2312
2314 tests = [
2315 ('', '', ''),
2316 ('a', 'a', ''),
2317 ('GMd: a window title', _GM_TITLE_PREFIX + ':', 'a window title'),
2318 ('\.br\MICROCYTES+1\.br\SPHEROCYTES present\.br\POLYCHROMASIAmoderate\.br\\', '\.br\\', 'MICROCYTES+1\.br\SPHEROCYTES present\.br\POLYCHROMASIAmoderate\.br\\')
2319 ]
2320 for test in tests:
2321 text, prefix, expect = test
2322 result = strip_prefix(text, prefix, remove_whitespace = True)
2323 if result == expect:
2324 continue
2325 print('test failed:', test)
2326 print('result:', result)
2327
2328
2330 tst = [
2331 ('123', 1),
2332 ('123', 2),
2333 ('123', 3),
2334 ('123', 4),
2335 ('', 1),
2336 ('1', 1),
2337 ('12', 1),
2338 ('', 2),
2339 ('1', 2),
2340 ('12', 2),
2341 ('123', 2)
2342 ]
2343 for txt, lng in tst:
2344 print('max', lng, 'of', txt, '=', shorten_text(txt, lng))
2345
2347 tests = [
2348 '/tmp/test.txt',
2349 '/tmp/ test.txt',
2350 '/tmp/ tes\\t.txt',
2351 'test'
2352 ]
2353 for test in tests:
2354 print (test, fname_sanitize(test))
2355
2356
2359
2360
2362 parts = enumerate_removable_partitions()
2363 for part_name in parts:
2364 part = parts[part_name]
2365 print(part['device'])
2366 print(part['partition'])
2367 if part['is_mounted']:
2368 print('%s@%s: %s on %s by %s @ %s (FS=%s: %s free of %s total)' % (
2369 part['type'],
2370 part['bus'],
2371 part['fs_label'],
2372 part['model'],
2373 part['vendor'],
2374 part['mountpoint'],
2375 part['fs_type'],
2376 part['bytes_free'],
2377 part['size_in_bytes']
2378 ))
2379 else:
2380 print('%s@%s: %s on %s by %s (not mounted)' % (
2381 part['type'],
2382 part['bus'],
2383 part['fs_label'],
2384 part['model'],
2385 part['vendor']
2386 ))
2387
2388
2390 for writer in enumerate_optical_writers():
2391 print('%s@%s: %s @ %s' % (
2392 writer['type'],
2393 writer['bus'],
2394 writer['model'],
2395 writer['device']
2396 ))
2397
2398
2400 print(sys.argv[2], '->', sys.argv[3])
2401 print(copy_tree_content(sys.argv[2], sys.argv[3]))
2402
2403
2406
2407
2409 dicts = [
2410 {'pkey': 1, 'value': 'a1'},
2411 {'pkey': 2, 'value': 'b2'},
2412 {'pkey': 3, 'value': 'c3'},
2413 {'pkey': 4, 'value': 'd4'},
2414 {'pkey': 5, 'value': 'd4'},
2415 {'pkey': 5, 'value': 'c5'},
2416 ]
2417 with open('x.txt', 'w', encoding = 'utf8') as f:
2418 f.write(dicts2table(dicts, left_margin=2, eol='\n', keys2ignore=None, show_only_changes=True, headers = ['d1', 'd2', 'd3', 'd4', 'd5', 'd6']))
2419
2420
2421
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462 test_decorate_window_title()
2463
2464
2465