/var/lib/gnumed/server/pycommon/gmTools.py is in gnumed-server 21.15-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 | # -*- coding: utf-8 -*-
from __future__ import print_function
__doc__ = """GNUmed general tools."""
#===========================================================================
__author__ = "K. Hilbert <Karsten.Hilbert@gmx.net>"
__license__ = "GPL v2 or later (details at http://www.gnu.org)"
# std libs
import sys
import os
import os.path
import csv
import tempfile
import logging
import hashlib
import platform
import subprocess
import decimal
import getpass
import io
import functools
import json
import shutil
import zipfile
import datetime as pydt
import re as regex
import xml.sax.saxutils as xml_tools
# old:
import pickle, zlib
# GNUmed libs
if __name__ == '__main__':
sys.path.insert(0, '../../')
from Gnumed.pycommon import gmBorg
_log = logging.getLogger('gm.tools')
# CAPitalization modes:
( CAPS_NONE, # don't touch it
CAPS_FIRST, # CAP first char, leave rest as is
CAPS_ALLCAPS, # CAP all chars
CAPS_WORDS, # CAP first char of every word
CAPS_NAMES, # CAP in a way suitable for names (tries to be smart)
CAPS_FIRST_ONLY # CAP first char, lowercase the rest
) = range(6)
u_currency_pound = u'\u00A3' # Pound sign
u_currency_sign = u'\u00A4' # generic currency sign
u_currency_yen = u'\u00A5' # Yen sign
u_right_double_angle_quote = u'\u00AB' # <<
u_registered_trademark = u'\u00AE'
u_plus_minus = u'\u00B1'
u_left_double_angle_quote = u'\u00BB' # >>
u_one_quarter = u'\u00BC'
u_one_half = u'\u00BD'
u_three_quarters = u'\u00BE'
u_multiply = u'\u00D7' # x
u_greek_ALPHA = u'\u0391'
u_greek_alpha = u'\u03b1'
u_greek_OMEGA = u'\u03A9'
u_greek_omega = u'\u03c9'
u_triangular_bullet = u'\u2023' # triangular bullet (>)
u_ellipsis = u'\u2026' # ...
u_euro = u'\u20AC' # EURO sign
u_numero = u'\u2116' # No. / # sign
u_down_left_arrow = u'\u21B5' # <-'
u_left_arrow = u'\u2190' # <--
u_up_arrow = u'\u2191'
u_arrow2right = u'\u2192' # -->
u_down_arrow = u'\u2193'
u_left_arrow_with_tail = u'\u21a2' # <--<
u_arrow2right_from_bar = u'\u21a6' # |->
u_arrow2right_until_vertical_bar = u'\u21e5' # -->|
u_sum = u'\u2211' # sigma
u_almost_equal_to = u'\u2248' # approximately / nearly / roughly
u_corresponds_to = u'\u2258'
u_infinity = u'\u221E'
u_arrow2right_until_vertical_bar2 = u'\u2b72' # -->|
u_diameter = u'\u2300'
u_checkmark_crossed_out = u'\u237B'
u_box_vert_left = u'\u23b8'
u_box_vert_right = u'\u23b9'
u_box_horiz_single = u'\u2500' # -
u_box_vert_light = u'\u2502'
u_box_vert_light_4dashes = u'\u2506'
u_box_horiz_4dashes = u'\u2508'
u_box_T_right = u'\u251c'
u_box_T_down = u'\u252c'
u_box_T_up = u'\u2534'
u_box_plus = u'\u253c'
u_box_top_double = u'\u2550'
u_box_top_left_double_single = u'\u2552'
u_box_top_right_double_single = u'\u2555'
u_box_top_left_arc = u'\u256d'
u_box_bottom_right_arc = u'\u256f'
u_box_bottom_left_arc = u'\u2570'
u_box_horiz_light_heavy = u'\u257c'
u_box_horiz_heavy_light = u'\u257e'
u_skull_and_crossbones = u'\u2620'
u_frowning_face = u'\u2639'
u_smiling_face = u'\u263a'
u_black_heart = u'\u2665'
u_female = u'\u2640'
u_male = u'\u2642'
u_male_female = u'\u26a5'
u_checkmark_thin = u'\u2713'
u_checkmark_thick = u'\u2714'
u_arrow2right_thick = u'\u2794'
u_writing_hand = u'\u270d'
u_pencil_1 = u'\u270e'
u_pencil_2 = u'\u270f'
u_pencil_3 = u'\u2710'
u_latin_cross = u'\u271d'
u_arrow2right_until_black_diamond = u'\u291e' # ->*
u_kanji_yen = u'\u5186' # Yen kanji
u_replacement_character = u'\ufffd'
u_link_symbol = u'\u1f517'
_kB = 1024
_MB = 1024 * _kB
_GB = 1024 * _MB
_TB = 1024 * _GB
_PB = 1024 * _TB
#===========================================================================
def handle_uncaught_exception_console(t, v, tb):
print(".========================================================")
print("| Unhandled exception caught !")
print("| Type :", t)
print("| Value:", v)
print("`========================================================")
_log.critical('unhandled exception caught', exc_info = (t,v,tb))
sys.__excepthook__(t,v,tb)
#===========================================================================
# path level operations
#---------------------------------------------------------------------------
def mkdir(directory=None, mode=None):
try:
if mode is None:
os.makedirs(directory)
else:
old_umask = os.umask(0)
os.makedirs(directory, mode)
os.umask(old_umask)
except OSError as e:
if (e.errno == 17) and not os.path.isdir(directory):
raise
return True
#---------------------------------------------------------------------------
def rmdir(directory):
#-------------------------------
def _on_rm_error(func, path, exc):
_log.error(u'error while shutil.rmtree(%s)', path, exc_info=exc)
return True
#-------------------------------
error_count = 0
try:
shutil.rmtree(directory, False, _on_rm_error)
except StandardError:
_log.exception('cannot shutil.rmtree(%s)', directory)
error_count += 1
return error_count
#---------------------------------------------------------------------------
def mk_sandbox_dir(prefix=None, base_dir=None):
if prefix is None:
if base_dir is None:
prefix = u'sandbox-'
else:
prefix = u'gm_sandbox-'
return tempfile.mkdtemp (
prefix = prefix,
suffix = u'',
dir = base_dir
)
#---------------------------------------------------------------------------
def parent_dir(directory):
return os.path.abspath(os.path.join(directory, '..'))
#---------------------------------------------------------------------------
def dirname_stem(directory):
# /home/user/dir/ -> dir
# /home/user/dir -> dir
return os.path.basename(os.path.normpath(directory)) # normpath removes trailing slashes if any
#---------------------------------------------------------------------------
def dir_is_empty(directory=None):
return len(os.listdir(directory)) == 0
#---------------------------------------------------------------------------
class gmPaths(gmBorg.cBorg):
"""This class provides the following paths:
.home_dir user home
.local_base_dir script installation dir
.working_dir current dir
.user_config_dir
.system_config_dir
.system_app_data_dir (not writable)
.tmp_dir
"""
def __init__(self, app_name=None, wx=None):
"""Setup pathes.
<app_name> will default to (name of the script - .py)
"""
try:
self.already_inited
return
except AttributeError:
pass
self.init_paths(app_name=app_name, wx=wx)
self.already_inited = True
#--------------------------------------
# public API
#--------------------------------------
def init_paths(self, app_name=None, wx=None):
if wx is None:
_log.debug('wxPython not available')
_log.debug('detecting paths directly')
if app_name is None:
app_name, ext = os.path.splitext(os.path.basename(sys.argv[0]))
_log.info('app name detected as [%s]', app_name)
else:
_log.info('app name passed in as [%s]', app_name)
# the user home, doesn't work in Wine so work around that
self.__home_dir = None
# where the main script (the "binary") is installed
if getattr(sys, 'frozen', False):
_log.info('frozen app, installed into temporary path')
# this would find the path of *THIS* file
#self.local_base_dir = os.path.dirname(__file__)
# while this is documented on the web, the ${_MEIPASS2} does not exist
#self.local_base_dir = os.environ.get('_MEIPASS2')
# this is what Martin Zibricky <mzibr.public@gmail.com> told us to use
# when asking about this on pyinstaller@googlegroups.com
#self.local_base_dir = sys._MEIPASS
# however, we are --onedir, so we should look at sys.executable
# as per the pyinstaller manual
self.local_base_dir = os.path.dirname(sys.executable)
else:
self.local_base_dir = os.path.abspath(os.path.dirname(sys.argv[0]))
# the current working dir at the OS
self.working_dir = os.path.abspath(os.curdir)
# user-specific config dir, usually below the home dir
mkdir(os.path.join(self.home_dir, '.%s' % app_name))
self.user_config_dir = os.path.join(self.home_dir, '.%s' % app_name)
# system-wide config dir, usually below /etc/ under UN*X
try:
self.system_config_dir = os.path.join('/etc', app_name)
except ValueError:
#self.system_config_dir = self.local_base_dir
self.system_config_dir = self.user_config_dir
# system-wide application data dir
try:
self.system_app_data_dir = os.path.join(sys.prefix, 'share', app_name)
except ValueError:
self.system_app_data_dir = self.local_base_dir
# temporary directory
try:
self.__tmp_dir_already_set
_log.debug(u'temp dir already set')
except AttributeError:
_log.info(u'initial (user level) temp dir: %s', tempfile.gettempdir())
# $TMP/gnumed-$USER/
tmp_base = os.path.join(tempfile.gettempdir(), app_name + r'-' + getpass.getuser())
mkdir(tmp_base, 0o700)
tempfile.tempdir = tmp_base
_log.info(u'intermediate (app level) temp dir: %s', tempfile.gettempdir())
# $TMP/gnumed-$USER/g$UNIQUE/
self.tmp_dir = tempfile.mkdtemp(prefix = r'g')
_log.info(u'final (app instance level) temp dir: %s', tempfile.gettempdir())
self.__log_paths()
if wx is None:
return True
# retry with wxPython
_log.debug('re-detecting paths with wxPython')
std_paths = wx.StandardPaths.Get()
_log.info('wxPython app name is [%s]', wx.GetApp().GetAppName())
# user-specific config dir, usually below the home dir
mkdir(os.path.join(std_paths.GetUserConfigDir(), '.%s' % app_name))
self.user_config_dir = os.path.join(std_paths.GetUserConfigDir(), '.%s' % app_name)
# system-wide config dir, usually below /etc/ under UN*X
try:
tmp = std_paths.GetConfigDir()
if not tmp.endswith(app_name):
tmp = os.path.join(tmp, app_name)
self.system_config_dir = tmp
except ValueError:
# leave it at what it was from direct detection
pass
# system-wide application data dir
# Robin attests that the following doesn't always
# give sane values on Windows, so IFDEF it
if 'wxMSW' in wx.PlatformInfo:
_log.warning('this platform (wxMSW) sometimes returns a broken value for the system-wide application data dir')
else:
try:
self.system_app_data_dir = std_paths.GetDataDir()
except ValueError:
pass
self.__log_paths()
return True
#--------------------------------------
def __log_paths(self):
_log.debug('sys.argv[0]: %s', sys.argv[0])
_log.debug('sys.executable: %s', sys.executable)
_log.debug('sys._MEIPASS: %s', getattr(sys, '_MEIPASS', '<not found>'))
_log.debug('os.environ["_MEIPASS2"]: %s', os.environ.get('_MEIPASS2', '<not found>'))
_log.debug('__file__ : %s', __file__)
_log.debug('local application base dir: %s', self.local_base_dir)
_log.debug('current working dir: %s', self.working_dir)
_log.debug('user home dir: %s', self.home_dir)
_log.debug('user-specific config dir: %s', self.user_config_dir)
_log.debug('system-wide config dir: %s', self.system_config_dir)
_log.debug('system-wide application data dir: %s', self.system_app_data_dir)
_log.debug('temporary dir: %s', self.tmp_dir)
#--------------------------------------
# properties
#--------------------------------------
def _set_user_config_dir(self, path):
if not (os.access(path, os.R_OK) and os.access(path, os.X_OK)):
msg = '[%s:user_config_dir]: invalid path [%s]' % (self.__class__.__name__, path)
_log.error(msg)
raise ValueError(msg)
self.__user_config_dir = path
def _get_user_config_dir(self):
return self.__user_config_dir
user_config_dir = property(_get_user_config_dir, _set_user_config_dir)
#--------------------------------------
def _set_system_config_dir(self, path):
if not (os.access(path, os.R_OK) and os.access(path, os.X_OK)):
msg = '[%s:system_config_dir]: invalid path [%s]' % (self.__class__.__name__, path)
_log.error(msg)
raise ValueError(msg)
self.__system_config_dir = path
def _get_system_config_dir(self):
return self.__system_config_dir
system_config_dir = property(_get_system_config_dir, _set_system_config_dir)
#--------------------------------------
def _set_system_app_data_dir(self, path):
if not (os.access(path, os.R_OK) and os.access(path, os.X_OK)):
msg = '[%s:system_app_data_dir]: invalid path [%s]' % (self.__class__.__name__, path)
_log.error(msg)
raise ValueError(msg)
self.__system_app_data_dir = path
def _get_system_app_data_dir(self):
return self.__system_app_data_dir
system_app_data_dir = property(_get_system_app_data_dir, _set_system_app_data_dir)
#--------------------------------------
def _set_home_dir(self, path):
raise ValueError('invalid to set home dir')
def _get_home_dir(self):
if self.__home_dir is not None:
return self.__home_dir
tmp = os.path.expanduser('~')
if tmp == '~':
_log.error('this platform does not expand ~ properly')
try:
tmp = os.environ['USERPROFILE']
except KeyError:
_log.error('cannot access $USERPROFILE in environment')
if not (
os.access(tmp, os.R_OK)
and
os.access(tmp, os.X_OK)
and
os.access(tmp, os.W_OK)
):
msg = '[%s:home_dir]: invalid path [%s]' % (self.__class__.__name__, tmp)
_log.error(msg)
raise ValueError(msg)
self.__home_dir = tmp
return self.__home_dir
home_dir = property(_get_home_dir, _set_home_dir)
#--------------------------------------
def _set_tmp_dir(self, path):
if not (os.access(path, os.R_OK) and os.access(path, os.X_OK)):
msg = '[%s:tmp_dir]: invalid path [%s]' % (self.__class__.__name__, path)
_log.error(msg)
raise ValueError(msg)
_log.debug('previous temp dir: %s', tempfile.gettempdir())
self.__tmp_dir = path
tempfile.tempdir = self.__tmp_dir
_log.debug('new temp dir: %s', tempfile.gettempdir())
self.__tmp_dir_already_set = True
def _get_tmp_dir(self):
return self.__tmp_dir
tmp_dir = property(_get_tmp_dir, _set_tmp_dir)
#===========================================================================
# file related tools
#---------------------------------------------------------------------------
def recode_file(source_file=None, target_file=None, source_encoding=u'utf8', target_encoding=None, base_dir=None, error_mode='replace'):
if target_encoding is None:
return source_file
if target_encoding == source_encoding:
return source_file
if target_file is None:
target_file = get_unique_filename (
prefix = u'%s-%s_%s-' % (fname_stem(source_file), source_encoding, target_encoding),
suffix = fname_extension(source_file, u'.txt'),
tmp_dir = base_dir
)
_log.debug('[%s] -> [%s] (%s -> %s)', source_encoding, target_encoding, source_file, target_file)
in_file = io.open(source_file, mode = 'rt', encoding = source_encoding)
out_file = io.open(target_file, mode = 'wt', encoding = target_encoding, errors = error_mode)
for line in in_file:
out_file.write(line)
out_file.close()
in_file.close()
return target_file
#---------------------------------------------------------------------------
def unzip_archive(archive_name, target_dir=None, remove_archive=False):
_log.debug('unzipping [%s] -> [%s]', archive_name, target_dir)
success = False
try:
with zipfile.ZipFile(archive_name) as archive:
archive.extractall(target_dir)
success = True
except StandardError:
_log.exception('cannot unzip')
return False
if remove_archive:
remove_file(archive_name)
return success
#---------------------------------------------------------------------------
def remove_file(filename, log_error=True):
# attempt file remove and ignore (but log) errors
try:
os.remove(filename)
except StandardError:
if log_error:
_log.exception('cannot os.remove(%s)', filename)
return False
return True
#---------------------------------------------------------------------------
def gpg_decrypt_file(filename=None, passphrase=None):
if platform.system() == 'Windows':
exec_name = 'gpg.exe'
else:
exec_name = 'gpg'
tmp, fname = os.path.split(filename)
basename, tmp = os.path.splitext(fname)
filename_decrypted = get_unique_filename(prefix = '%s-decrypted-' % basename)
args = [exec_name, '--verbose', '--batch', '--yes', '--passphrase-fd', '0', '--output', filename_decrypted, '--decrypt', filename]
_log.debug('GnuPG args: %s' % str(args))
try:
gpg = subprocess.Popen (
args = args,
stdin = subprocess.PIPE,
stdout = subprocess.PIPE,
stderr = subprocess.PIPE,
close_fds = False
)
except (OSError, ValueError, subprocess.CalledProcessError):
_log.exception('there was a problem executing gpg')
gmDispatcher.send(signal = u'statustext', msg = _('Error running GnuPG. Cannot decrypt data.'), beep = True)
return
out, error = gpg.communicate(passphrase)
_log.debug('gpg returned [%s]', gpg.returncode)
if gpg.returncode != 0:
_log.debug('GnuPG STDOUT:\n%s', out)
_log.debug('GnuPG STDERR:\n%s', error)
return None
return filename_decrypted
#---------------------------------------------------------------------------
def file2md5(filename=None, return_hex=True):
blocksize = 2**10 * 128 # 128k, since md5 uses 128 byte blocks
_log.debug('md5(%s): <%s> byte blocks', filename, blocksize)
f = io.open(filename, mode = 'rb')
md5 = hashlib.md5()
while True:
data = f.read(blocksize)
if not data:
break
md5.update(data)
f.close()
_log.debug('md5(%s): %s', filename, md5.hexdigest())
if return_hex:
return md5.hexdigest()
return md5.digest()
#---------------------------------------------------------------------------
def file2chunked_md5(filename=None, chunk_size=500*_MB):
_log.debug('chunked_md5(%s, chunk_size=%s bytes)', filename, chunk_size)
md5_concat = u''
f = open(filename, 'rb')
while True:
md5 = hashlib.md5()
data = f.read(chunk_size)
if not data:
break
md5.update(data)
md5_concat += md5.hexdigest()
f.close()
md5 = hashlib.md5()
md5.update(md5_concat)
hex_digest = md5.hexdigest()
_log.debug('md5("%s"): %s', md5_concat, hex_digest)
return hex_digest
#---------------------------------------------------------------------------
def unicode2charset_encoder(unicode_csv_data, encoding='utf-8'):
for line in unicode_csv_data:
yield line.encode(encoding)
#def utf_8_encoder(unicode_csv_data):
# for line in unicode_csv_data:
# yield line.encode('utf-8')
default_csv_reader_rest_key = u'list_of_values_of_unknown_fields'
def unicode_csv_reader(unicode_csv_data, dialect=csv.excel, encoding='utf-8', **kwargs):
# csv.py doesn't do Unicode; encode temporarily as UTF-8:
try:
is_dict_reader = kwargs['dict']
del kwargs['dict']
if is_dict_reader is not True:
raise KeyError
kwargs['restkey'] = default_csv_reader_rest_key
csv_reader = csv.DictReader(unicode2charset_encoder(unicode_csv_data), dialect=dialect, **kwargs)
except KeyError:
is_dict_reader = False
csv_reader = csv.reader(unicode2charset_encoder(unicode_csv_data), dialect=dialect, **kwargs)
for row in csv_reader:
# decode ENCODING back to Unicode, cell by cell:
if is_dict_reader:
for key in row.keys():
if key == default_csv_reader_rest_key:
old_data = row[key]
new_data = []
for val in old_data:
new_data.append(unicode(val, encoding))
row[key] = new_data
if default_csv_reader_rest_key not in csv_reader.fieldnames:
csv_reader.fieldnames.append(default_csv_reader_rest_key)
else:
row[key] = unicode(row[key], encoding)
yield row
else:
yield [ unicode(cell, encoding) for cell in row ]
#yield [unicode(cell, 'utf-8') for cell in row]
#---------------------------------------------------------------------------
def fname_stem(filename):
# /home/user/dir/filename.ext -> filename
return os.path.splitext(os.path.basename(filename))[0]
#---------------------------------------------------------------------------
def fname_stem_with_path(filename):
# /home/user/dir/filename.ext -> /home/user/dir/filename
return os.path.splitext(filename)[0]
#---------------------------------------------------------------------------
def fname_extension(filename=None, fallback=None):
# /home/user/dir/filename.ext -> .ext
# '' or '.' -> fallback if any else ''
ext = os.path.splitext(filename)[1]
if ext.strip() not in [u'.', u'']:
return ext
if fallback is None:
return u''
return fallback
#---------------------------------------------------------------------------
def fname_dir(filename):
# /home/user/dir/filename.ext -> /home/user/dir
return os.path.split(filename)[0]
#---------------------------------------------------------------------------
def fname_from_path(filename):
# /home/user/dir/filename.ext -> filename.ext
return os.path.split(filename)[1]
#---------------------------------------------------------------------------
def get_unique_filename(prefix=None, suffix=None, tmp_dir=None, include_timestamp=False):
"""This introduces a race condition between the file.close() and
actually using the filename.
The file will NOT exist after calling this function.
"""
if tmp_dir is not None:
if (
not os.access(tmp_dir, os.F_OK)
or
not os.access(tmp_dir, os.X_OK | os.W_OK)
):
_log.warning('cannot find temporary dir [%s], using system default', tmp_dir)
tmp_dir = None
if include_timestamp:
ts = pydt.datetime.now().strftime('%m%d-%H%M%S-')
else:
ts = u''
kwargs = {
'dir': tmp_dir,
# make sure file gets deleted as soon as
# .close()d so we can safely open it again
'delete': True
}
if prefix is None:
kwargs['prefix'] = 'gmd-%s' % ts
else:
kwargs['prefix'] = prefix + ts
if suffix in [None, u'']:
kwargs['suffix'] = '.tmp'
else:
if not suffix.startswith('.'):
suffix = '.' + suffix
kwargs['suffix'] = suffix
f = tempfile.NamedTemporaryFile(**kwargs)
filename = f.name
f.close()
return filename
#---------------------------------------------------------------------------
def __make_symlink_on_windows(physical_name, link_name):
import ctypes
csl = ctypes.windll.kernel32.CreateSymbolicLinkW
csl.argtypes = (ctypes.c_wchar_p, ctypes.c_wchar_p, ctypes.c_uint32)
csl.restype = ctypes.c_ubyte
if os.path.isdir(physical_name):
flags = 1
else:
flags = 0
ret_code = csl(link_name, physical_name.replace('/', '\\'), flags)
_log.debug('ctypes.windll.kernel32.CreateSymbolicLinkW() exit code: %s', ret_code)
if ret_code == 0:
raise ctypes.WinError()
return ret_code
#---------------------------------------------------------------------------
def mklink(physical_name, link_name, overwrite=False):
_log.debug('creating symlink (overwrite = %s):', overwrite)
_log.debug('link [%s] =>', link_name)
_log.debug('=> physical [%s]', physical_name)
if os.path.exists(link_name):
_log.debug('link exists')
if overwrite:
return True
return False
try:
os.symlink(physical_name, link_name)
except AttributeError:
_log.debug('Windows does not have os.symlink(), resorting to ctypes')
__make_symlink_on_windows(physical_name, link_name)
return True
#===========================================================================
def import_module_from_directory(module_path=None, module_name=None, always_remove_path=False):
"""Import a module from any location."""
_log.debug('CWD: %s', os.getcwd())
remove_path = always_remove_path or False
if module_path not in sys.path:
_log.info('appending to sys.path: [%s]' % module_path)
sys.path.append(module_path)
remove_path = True
_log.debug('will remove import path: %s', remove_path)
if module_name.endswith('.py'):
module_name = module_name[:-3]
try:
module = __import__(module_name)
except Exception:
_log.exception('cannot __import__() module [%s] from [%s]' % (module_name, module_path))
while module_path in sys.path:
sys.path.remove(module_path)
raise
_log.info('imported module [%s] as [%s]' % (module_name, module))
if remove_path:
while module_path in sys.path:
sys.path.remove(module_path)
return module
#===========================================================================
# text related tools
#---------------------------------------------------------------------------
def size2str(size=0, template=u'%s'):
if size == 1:
return template % _('1 Byte')
if size < 10 * _kB:
return template % _('%s Bytes') % size
if size < _MB:
return template % u'%.1f kB' % (float(size) / _kB)
if size < _GB:
return template % u'%.1f MB' % (float(size) / _MB)
if size < _TB:
return template % u'%.1f GB' % (float(size) / _GB)
if size < _PB:
return template % u'%.1f TB' % (float(size) / _TB)
return template % u'%.1f PB' % (float(size) / _PB)
#---------------------------------------------------------------------------
def bool2subst(boolean=None, true_return=True, false_return=False, none_return=None):
if boolean is None:
return none_return
if boolean:
return true_return
if not boolean:
return false_return
raise ValueError('bool2subst(): <boolean> arg must be either of True, False, None')
#---------------------------------------------------------------------------
def bool2str(boolean=None, true_str='True', false_str='False'):
return bool2subst (
boolean = bool(boolean),
true_return = true_str,
false_return = false_str
)
#---------------------------------------------------------------------------
def none_if(value=None, none_equivalent=None, strip_string=False):
"""Modelled after the SQL NULLIF function."""
if value is None:
return None
if strip_string:
stripped = value.strip()
else:
stripped = value
if stripped == none_equivalent:
return None
return value
#---------------------------------------------------------------------------
def coalesce(initial=None, instead=None, template_initial=None, template_instead=None, none_equivalents=None, function_initial=None):
"""Modelled after the SQL coalesce function.
To be used to simplify constructs like:
if initial is None (or in none_equivalents):
real_value = (template_instead % instead) or instead
else:
real_value = (template_initial % initial) or initial
print real_value
@param initial: the value to be tested for <None>
@type initial: any Python type, must have a __str__ method if template_initial is not None
@param instead: the value to be returned if <initial> is None
@type instead: any Python type, must have a __str__ method if template_instead is not None
@param template_initial: if <initial> is returned replace the value into this template, must contain one <%s>
@type template_initial: string or None
@param template_instead: if <instead> is returned replace the value into this template, must contain one <%s>
@type template_instead: string or None
example:
function_initial = ('strftime', '%Y-%m-%d')
Ideas:
- list of insteads: initial, [instead, template], [instead, template], [instead, template], template_initial, ...
"""
if none_equivalents is None:
none_equivalents = [None]
if initial in none_equivalents:
if template_instead is None:
return instead
return template_instead % instead
if function_initial is not None:
funcname, args = function_initial
func = getattr(initial, funcname)
initial = func(args)
if template_initial is None:
return initial
try:
return template_initial % initial
except TypeError:
return template_initial
#---------------------------------------------------------------------------
def __cap_name(match_obj=None):
val = match_obj.group(0).lower()
if val in ['von', 'van', 'de', 'la', 'l', 'der', 'den']: # FIXME: this needs to expand, configurable ?
return val
buf = list(val)
buf[0] = buf[0].upper()
for part in ['mac', 'mc', 'de', 'la']:
if len(val) > len(part) and val[:len(part)] == part:
buf[len(part)] = buf[len(part)].upper()
return ''.join(buf)
#---------------------------------------------------------------------------
def capitalize(text=None, mode=CAPS_NAMES):
"""Capitalize the first character but leave the rest alone.
Note that we must be careful about the locale, this may
have issues ! However, for UTF strings it should just work.
"""
if (mode is None) or (mode == CAPS_NONE):
return text
if len(text) == 0:
return text
if mode == CAPS_FIRST:
if len(text) == 1:
return text[0].upper()
return text[0].upper() + text[1:]
if mode == CAPS_ALLCAPS:
return text.upper()
if mode == CAPS_FIRST_ONLY:
# if len(text) == 1:
# return text[0].upper()
return text[0].upper() + text[1:].lower()
if mode == CAPS_WORDS:
#return regex.sub(ur'(\w)(\w+)', lambda x: x.group(1).upper() + x.group(2).lower(), text)
return regex.sub(r'(\w)(\w+)', lambda x: x.group(1).upper() + x.group(2).lower(), text)
if mode == CAPS_NAMES:
#return regex.sub(r'\w+', __cap_name, text)
return capitalize(text=text, mode=CAPS_FIRST) # until fixed
print("ERROR: invalid capitalization mode: [%s], leaving input as is" % mode)
return text
#---------------------------------------------------------------------------
def input2decimal(initial=None):
if isinstance(initial, decimal.Decimal):
return True, initial
val = initial
# float ? -> to string first
if type(val) == type(float(1.4)):
val = str(val)
# string ? -> "," to "."
if isinstance(val, basestring):
val = val.replace(',', '.', 1)
val = val.strip()
try:
d = decimal.Decimal(val)
return True, d
except (TypeError, decimal.InvalidOperation):
return False, val
#---------------------------------------------------------------------------
def input2int(initial=None, minval=None, maxval=None):
val = initial
# string ? -> "," to "."
if isinstance(val, basestring):
val = val.replace(',', '.', 1)
val = val.strip()
try:
int_val = int(val)
except (TypeError, ValueError):
_log.exception('int(%s) failed', val)
return False, initial
if minval is not None:
if int_val < minval:
_log.debug('%s < min (%s)', val, minval)
return False, initial
if maxval is not None:
if int_val > maxval:
_log.debug('%s > max (%s)', val, maxval)
return False, initial
return True, int_val
#---------------------------------------------------------------------------
def strip_prefix(text, prefix, remove_repeats=False, remove_whitespace=False):
if remove_repeats:
if remove_whitespace:
while text.lstrip().startswith(prefix):
text = text.lstrip().replace(prefix, u'', 1).lstrip()
return text
while text.startswith(prefix):
text = text.replace(prefix, u'', 1)
return text
if remove_whitespace:
return text.lstrip().replace(prefix, u'', 1).lstrip()
return text.replace(prefix, u'', 1)
#---------------------------------------------------------------------------
def strip_suffix(text, suffix, remove_repeats=False, remove_whitespace=False):
suffix_len = len(suffix)
if remove_repeats:
if remove_whitespace:
while text.rstrip().endswith(suffix):
text = text.rstrip()[:-suffix_len].rstrip()
return text
while text.endswith(suffix):
text = text[:-suffix_len]
return text
if remove_whitespace:
return text.rstrip()[:-suffix_len].rstrip()
return text[:-suffix_len]
#---------------------------------------------------------------------------
def strip_leading_empty_lines(lines=None, text=None, eol=u'\n', return_list=True):
if lines is None:
lines = text.split(eol)
while True:
if lines[0].strip(eol).strip() != u'':
break
lines = lines[1:]
if return_list:
return lines
return eol.join(lines)
#---------------------------------------------------------------------------
def strip_trailing_empty_lines(lines=None, text=None, eol=u'\n', return_list=True):
if lines is None:
lines = text.split(eol)
while True:
if lines[-1].strip(eol).strip() != u'':
break
lines = lines[:-1]
if return_list:
return lines
return eol.join(lines)
#---------------------------------------------------------------------------
def strip_empty_lines(lines=None, text=None, eol=u'\n', return_list=True):
return strip_trailing_empty_lines (
lines = strip_leading_empty_lines(lines = lines, text = text, eol = eol, return_list = True),
text = None,
eol = eol,
return_list = return_list
)
#---------------------------------------------------------------------------
def list2text(lines, initial_indent=u'', subsequent_indent=u'', eol=u'\n', strip_leading_empty_lines=True, strip_trailing_empty_lines=True, strip_trailing_whitespace=True):
if len(lines) == 0:
return u''
if strip_leading_empty_lines:
lines = strip_leading_empty_lines(lines = lines, eol = eol, return_list = True)
if strip_trailing_empty_lines:
lines = strip_trailing_empty_lines(lines = lines, eol = eol, return_list = True)
if strip_trailing_whitespace:
lines = [ l.rstrip() for l in lines ]
indented_lines = [initial_indent + lines[0]]
indented_lines.extend([ subsequent_indent + l for l in lines[1:] ])
return eol.join(indented_lines)
#---------------------------------------------------------------------------
def wrap(text=None, width=None, initial_indent=u'', subsequent_indent=u'', eol=u'\n'):
"""A word-wrap function that preserves existing line breaks
and most spaces in the text. Expects that existing line
breaks are posix newlines (\n).
"""
if width is None:
return text
wrapped = initial_indent + functools.reduce (
lambda line, word, width=width: '%s%s%s' % (
line,
' \n'[(len(line) - line.rfind('\n') - 1 + len(word.split('\n',1)[0]) >= width)],
word
),
text.split(' ')
)
if subsequent_indent != u'':
wrapped = (u'\n%s' % subsequent_indent).join(wrapped.split('\n'))
if eol != u'\n':
wrapped = wrapped.replace('\n', eol)
return wrapped
#---------------------------------------------------------------------------
def unwrap(text=None, max_length=None, strip_whitespace=True, remove_empty_lines=True, line_separator = u' // '):
text = text.replace(u'\r', u'')
lines = text.split(u'\n')
text = u''
for line in lines:
if strip_whitespace:
line = line.strip().strip(u'\t').strip()
if remove_empty_lines:
if line == u'':
continue
text += (u'%s%s' % (line, line_separator))
text = text.rstrip(line_separator)
if max_length is not None:
text = text[:max_length]
text = text.rstrip(line_separator)
return text
#---------------------------------------------------------------------------
def shorten_text(text=None, max_length=None):
if len(text) <= max_length:
return text
return text[:max_length-1] + u_ellipsis
#---------------------------------------------------------------------------
def xml_escape_string(text=None):
"""check for special XML characters and transform them"""
return xml_tools.escape(text)
#---------------------------------------------------------------------------
def tex_escape_string(text=None, replace_known_unicode=True, replace_eol=False, keep_visual_eol=False):
"""Check for special TeX characters and transform them.
replace_eol:
replaces "\n" with "\\newline"
keep_visual_eol:
replaces "\n" with "\\newline \n" such that
both LaTeX will know to place a line break
at this point as well as the visual formatting
is preserved in the LaTeX source (think multi-
row table cells)
"""
text = text.replace(u'\\', u'\\textbackslash') # requires \usepackage{textcomp} in LaTeX source
text = text.replace(u'^', u'\\textasciicircum') # requires \usepackage{textcomp} in LaTeX source
text = text.replace(u'~', u'\\textasciitilde') # requires \usepackage{textcomp} in LaTeX source
text = text.replace(u'{', u'\\{')
text = text.replace(u'}', u'\\}')
text = text.replace(u'%', u'\\%')
text = text.replace(u'&', u'\\&')
text = text.replace(u'#', u'\\#')
text = text.replace(u'$', u'\\$')
text = text.replace(u'_', u'\\_')
if replace_eol:
if keep_visual_eol:
text = text.replace(u'\n', u'\\newline \n')
else:
text = text.replace(u'\n', u'\\newline ')
if replace_known_unicode:
# this should NOT be replaced for Xe(La)Tex
text = text.replace(u_euro, u'\\EUR')
return text
#---------------------------------------------------------------------------
def xetex_escape_string(text=None):
# a web search did not reveal anything else for Xe(La)Tex
# as opposed to LaTeX, except true unicode chars
return tex_escape_string(text = text, replace_known_unicode = False)
#---------------------------------------------------------------------------
__html_escape_table = {
u"&": u"&",
u'"': u""",
u"'": u"'",
u">": u">",
u"<": u"<",
}
def html_escape_string(text=None, replace_eol=False, keep_visual_eol=False):
text = u''.join(__html_escape_table.get(char, char) for char in text)
if replace_eol:
if keep_visual_eol:
text = text.replace(u'\n', u'<br>\n')
else:
text = text.replace(u'\n', u'<br>')
return text
#---------------------------------------------------------------------------
def dict2json(obj):
return json.dumps(obj, default = json_serialize)
#---------------------------------------------------------------------------
def json_serialize(obj):
if isinstance(obj, pydt.datetime):
return obj.isoformat()
raise TypeError('cannot json_serialize(%s)' % type(obj))
#---------------------------------------------------------------------------
#---------------------------------------------------------------------------
def compare_dict_likes(d1, d2, title1=None, title2=None):
_log.info('comparing dict-likes: %s[%s] vs %s[%s]', coalesce(title1, u'', u'"%s" '), type(d1), coalesce(title2, u'', u'"%s" '), type(d2))
try:
d1 = dict(d1)
except TypeError:
pass
try:
d2 = dict(d2)
except TypeError:
pass
keys_d1 = frozenset(d1.keys())
keys_d2 = frozenset(d2.keys())
different = False
if len(keys_d1) != len(keys_d2):
_log.info('different number of keys: %s vs %s', len(keys_d1), len(keys_d2))
different = True
for key in keys_d1:
if key in keys_d2:
if type(d1[key]) != type(d2[key]):
_log.info(u'%25.25s: type(dict1) = %s = >>>%s<<<' % (key, type(d1[key]), d1[key]))
_log.info(u'%25.25s type(dict2) = %s = >>>%s<<<' % (u'', type(d2[key]), d2[key]))
different = True
continue
if d1[key] == d2[key]:
_log.info(u'%25.25s: both = >>>%s<<<' % (key, d1[key]))
else:
_log.info(u'%25.25s: dict1 = >>>%s<<<' % (key, d1[key]))
_log.info(u'%25.25s dict2 = >>>%s<<<' % (u'', d2[key]))
different = True
else:
_log.info(u'%25.25s: %50.50s | <MISSING>' % (key, u'>>>%s<<<' % d1[key]))
different = True
for key in keys_d2:
if key in keys_d1:
continue
_log.info(u'%25.25s: %50.50s | %.50s' % (key, u'<MISSING>', u'>>>%s<<<' % d2[key]))
different = True
if different:
_log.info('dict-likes appear to be different from each other')
return False
_log.info('dict-likes appear equal to each other')
return True
#---------------------------------------------------------------------------
def format_dict_likes_comparison(d1, d2, title_left=None, title_right=None, left_margin=0, key_delim=u' || ', data_delim=u' | ', missing_string=u'=/=', difference_indicator=u'! ', ignore_diff_in_keys=None):
_log.info('comparing dict-likes: %s[%s] vs %s[%s]', coalesce(title_left, u'', u'"%s" '), type(d1), coalesce(title_right, u'', u'"%s" '), type(d2))
append_type = False
if None not in [title_left, title_right]:
append_type = True
type_left = type(d1)
type_right = type(d2)
if title_left is None:
title_left = u'%s' % type_left
if title_right is None:
title_right = u'%s' % type_right
try: d1 = dict(d1)
except TypeError: pass
try: d2 = dict(d2)
except TypeError: pass
keys_d1 = d1.keys()
keys_d2 = d2.keys()
data = {}
for key in keys_d1:
data[key] = [d1[key], u' ']
if key in d2:
data[key][1] = d2[key]
for key in keys_d2:
if key in keys_d1:
continue
data[key] = [u' ', d2[key]]
max1 = max([ len(u'%s' % k) for k in keys_d1 ])
max2 = max([ len(u'%s' % k) for k in keys_d2 ])
max_len = max(max1, max2, len(_('<type>')))
max_key_len_str = u'%' + u'%s.%s' % (max_len, max_len) + u's'
max1 = max([ len(u'%s' % d1[k]) for k in keys_d1 ])
max2 = max([ len(u'%s' % d2[k]) for k in keys_d2 ])
max_data_len = min(max(max1, max2), 100)
max_data_len_str = u'%' + u'%s.%s' % (max_data_len, max_data_len) + u's'
diff_indicator_len_str = u'%' + u'%s.%s' % (len(difference_indicator), len(difference_indicator)) + u's'
line_template = (u' ' * left_margin) + diff_indicator_len_str + max_key_len_str + key_delim + max_data_len_str + data_delim + u'%s'
lines = []
# debugging:
#lines.append(u' (40 regular spaces)')
#lines.append((u' ' * 40) + u"(u' ' * 40)")
#lines.append((u'%40.40s' % u'') + u"(u'%40.40s' % u'')")
#lines.append((u'%40.40s' % u' ') + u"(u'%40.40s' % u' ')")
#lines.append((u'%40.40s' % u'.') + u"(u'%40.40s' % u'.')")
#lines.append(line_template)
lines.append(line_template % (u'', u'', title_left, title_right))
if append_type:
lines.append(line_template % (u'', _('<type>'), type_left, type_right))
if ignore_diff_in_keys is None:
ignore_diff_in_keys = []
for key in keys_d1:
append_type = False
txt_left_col = u'%s' % d1[key]
try:
txt_right_col = u'%s' % d2[key]
if type(d1[key]) != type(d2[key]):
append_type = True
except KeyError:
txt_right_col = missing_string
lines.append(line_template % (
bool2subst (
((txt_left_col == txt_right_col) or (key in ignore_diff_in_keys)),
u'',
difference_indicator
),
key,
shorten_text(txt_left_col, max_data_len),
shorten_text(txt_right_col, max_data_len)
))
if append_type:
lines.append(line_template % (
u'',
_('<type>'),
shorten_text(u'%s' % type(d1[key]), max_data_len),
shorten_text(u'%s' % type(d2[key]), max_data_len)
))
for key in keys_d2:
if key in keys_d1:
continue
lines.append(line_template % (
bool2subst((key in ignore_diff_in_keys), u'', difference_indicator),
key,
shorten_text(missing_string, max_data_len),
shorten_text(u'%s' % d2[key], max_data_len)
))
return lines
#---------------------------------------------------------------------------
def format_dict_like(d, relevant_keys=None, template=None, missing_key_template=u'<[%(key)s] MISSING>', left_margin=0, tabular=False, value_delimiters=(u'>>>', u'<<<'), eol=u'\n'):
if template is not None:
# all keys in template better exist in d
try:
return template % d
except KeyError:
# or else
_log.exception('template contains %%()s key(s) which do not exist in dict')
# try to extend dict <d> to contain all required keys,
# for that to work <relevant_keys> better list all
# keys used in <template>
if relevant_keys is not None:
for key in relevant_keys:
try:
d[key]
except KeyError:
d[key] = missing_key_template % {u'key': key}
return template % d
if relevant_keys is None:
relevant_keys = d.keys()
lines = []
if value_delimiters is None:
delim_left = u''
delim_right = u''
else:
delim_left, delim_right = value_delimiters
if tabular:
max_len = max([ len(i) for i in relevant_keys ])
max_len_str = u'%s.%s' % (max_len, max_len)
line_template = (u' ' * left_margin) + u'%' + max_len_str + ('s: %s%%s%s' % (delim_left, delim_right))
else:
line_template = (u' ' * left_margin) + u'%%s: %s%%s%s' % (delim_left, delim_right)
for key in relevant_keys:
try:
lines.append(line_template % (key, d[key]))
except KeyError:
pass
if eol is None:
return lines
return eol.join(lines)
#---------------------------------------------------------------------------
def normalize_dict_like(d, required_keys, missing_key_template=u'<[%(key)s] MISSING>'):
for key in required_keys:
try:
d[key]
except KeyError:
if missing_key_template is None:
d[key] = None
else:
d[key] = missing_key_template % {'key': key}
return d
#---------------------------------------------------------------------------
#---------------------------------------------------------------------------
def prompted_input(prompt=None, default=None):
"""Obtains entry from standard input.
prompt: Prompt text to display in standard output
default: Default value (for user to press enter only)
CTRL-C: aborts and returns None
"""
if prompt is None:
msg = u'(CTRL-C aborts)'
else:
msg = u'%s (CTRL-C aborts)' % prompt
if default is None:
msg = msg + u': '
else:
msg = u'%s [%s]: ' % (msg, default)
try:
usr_input = raw_input(msg)
except KeyboardInterrupt:
return None
if usr_input == '':
return default
return usr_input
#===========================================================================
# image handling tools
#---------------------------------------------------------------------------
# builtin (ugly but tried and true) fallback icon
__icon_serpent = \
"""x\xdae\x8f\xb1\x0e\x83 \x10\x86w\x9f\xe2\x92\x1blb\xf2\x07\x96\xeaH:0\xd6\
\xc1\x85\xd5\x98N5\xa5\xef?\xf5N\xd0\x8a\xdcA\xc2\xf7qw\x84\xdb\xfa\xb5\xcd\
\xd4\xda;\xc9\x1a\xc8\xb6\xcd<\xb5\xa0\x85\x1e\xeb\xbc\xbc7b!\xf6\xdeHl\x1c\
\x94\x073\xec<*\xf7\xbe\xf7\x99\x9d\xb21~\xe7.\xf5\x1f\x1c\xd3\xbdVlL\xc2\
\xcf\xf8ye\xd0\x00\x90\x0etH \x84\x80B\xaa\x8a\x88\x85\xc4(U\x9d$\xfeR;\xc5J\
\xa6\x01\xbbt9\xceR\xc8\x81e_$\x98\xb9\x9c\xa9\x8d,y\xa9t\xc8\xcf\x152\xe0x\
\xe9$\xf5\x07\x95\x0cD\x95t:\xb1\x92\xae\x9cI\xa8~\x84\x1f\xe0\xa3ec"""
def get_icon(wx=None):
paths = gmPaths(app_name = u'gnumed', wx = wx)
candidates = [
os.path.join(paths.system_app_data_dir, 'bitmaps', 'gm_icon-serpent_and_gnu.png'),
os.path.join(paths.local_base_dir, 'bitmaps', 'gm_icon-serpent_and_gnu.png'),
os.path.join(paths.system_app_data_dir, 'bitmaps', 'serpent.png'),
os.path.join(paths.local_base_dir, 'bitmaps', 'serpent.png')
]
found_as = None
for candidate in candidates:
try:
open(candidate, 'r').close()
found_as = candidate
break
except IOError:
_log.debug('icon not found in [%s]', candidate)
if found_as is None:
_log.warning('no icon file found, falling back to builtin (ugly) icon')
icon_bmp_data = wx.BitmapFromXPMData(pickle.loads(zlib.decompress(__icon_serpent)))
icon.CopyFromBitmap(icon_bmp_data)
else:
_log.debug('icon found in [%s]', found_as)
icon = wx.EmptyIcon()
try:
icon.LoadFile(found_as, wx.BITMAP_TYPE_ANY) #_PNG
except AttributeError:
_log.exception(u"this platform doesn't support wx.Icon().LoadFile()")
return icon
#===========================================================================
# main
#---------------------------------------------------------------------------
if __name__ == '__main__':
if len(sys.argv) < 2:
sys.exit()
if sys.argv[1] != 'test':
sys.exit()
# for testing:
logging.basicConfig(level = logging.DEBUG)
from Gnumed.pycommon import gmI18N
gmI18N.activate_locale()
gmI18N.install_domain()
#-----------------------------------------------------------------------
def test_input2decimal():
tests = [
[None, False],
['', False],
[' 0 ', True, 0],
[0, True, 0],
[0.0, True, 0],
[.0, True, 0],
['0', True, 0],
['0.0', True, 0],
['0,0', True, 0],
['00.0', True, 0],
['.0', True, 0],
[',0', True, 0],
[0.1, True, decimal.Decimal('0.1')],
[.01, True, decimal.Decimal('0.01')],
['0.1', True, decimal.Decimal('0.1')],
['0,1', True, decimal.Decimal('0.1')],
['00.1', True, decimal.Decimal('0.1')],
['.1', True, decimal.Decimal('0.1')],
[',1', True, decimal.Decimal('0.1')],
[1, True, 1],
[1.0, True, 1],
['1', True, 1],
['1.', True, 1],
['1,', True, 1],
['1.0', True, 1],
['1,0', True, 1],
['01.0', True, 1],
['01,0', True, 1],
[' 01, ', True, 1],
[decimal.Decimal('1.1'), True, decimal.Decimal('1.1')]
]
for test in tests:
conversion_worked, result = input2decimal(initial = test[0])
expected2work = test[1]
if conversion_worked:
if expected2work:
if result == test[2]:
continue
else:
print("ERROR (conversion result wrong): >%s<, expected >%s<, got >%s<" % (test[0], test[2], result))
else:
print("ERROR (conversion worked but was expected to fail): >%s<, got >%s<" % (test[0], result))
else:
if not expected2work:
continue
else:
print("ERROR (conversion failed but was expected to work): >%s<, expected >%s<" % (test[0], test[2]))
#-----------------------------------------------------------------------
def test_input2int():
print(input2int(0))
print(input2int('0'))
print(input2int(u'0', 0, 0))
#-----------------------------------------------------------------------
def test_coalesce():
import datetime as dt
print(coalesce(initial = dt.datetime.now(), template_initial = u'-- %s --', function_initial = ('strftime', u'%Y-%m-%d')))
print('testing coalesce()')
print("------------------")
tests = [
[None, 'something other than <None>', None, None, 'something other than <None>'],
['Captain', 'Mr.', '%s.'[:4], 'Mr.', 'Capt.'],
['value to test', 'test 3 failed', 'template with "%s" included', None, 'template with "value to test" included'],
['value to test', 'test 4 failed', 'template with value not included', None, 'template with value not included'],
[None, 'initial value was None', 'template_initial: %s', None, 'initial value was None'],
[None, 'initial value was None', 'template_initial: %%(abc)s', None, 'initial value was None']
]
passed = True
for test in tests:
result = coalesce (
initial = test[0],
instead = test[1],
template_initial = test[2],
template_instead = test[3]
)
if result != test[4]:
print("ERROR")
print("coalesce: (%s, %s, %s, %s)" % (test[0], test[1], test[2], test[3]))
print("expected:", test[4])
print("received:", result)
passed = False
if passed:
print("passed")
else:
print("failed")
return passed
#-----------------------------------------------------------------------
def test_capitalize():
print('testing capitalize() ...')
success = True
pairs = [
# [original, expected result, CAPS mode]
[u'Boot', u'Boot', CAPS_FIRST_ONLY],
[u'boot', u'Boot', CAPS_FIRST_ONLY],
[u'booT', u'Boot', CAPS_FIRST_ONLY],
[u'BoOt', u'Boot', CAPS_FIRST_ONLY],
[u'boots-Schau', u'Boots-Schau', CAPS_WORDS],
[u'boots-sChau', u'Boots-Schau', CAPS_WORDS],
[u'boot camp', u'Boot Camp', CAPS_WORDS],
[u'fahrner-Kampe', u'Fahrner-Kampe', CAPS_NAMES],
[u'häkkönen', u'Häkkönen', CAPS_NAMES],
[u'McBurney', u'McBurney', CAPS_NAMES],
[u'mcBurney', u'McBurney', CAPS_NAMES],
[u'blumberg', u'Blumberg', CAPS_NAMES],
[u'roVsing', u'RoVsing', CAPS_NAMES],
[u'Özdemir', u'Özdemir', CAPS_NAMES],
[u'özdemir', u'Özdemir', CAPS_NAMES],
]
for pair in pairs:
result = capitalize(pair[0], pair[2])
if result != pair[1]:
success = False
print('ERROR (caps mode %s): "%s" -> "%s", expected "%s"' % (pair[2], pair[0], result, pair[1]))
if success:
print("... SUCCESS")
return success
#-----------------------------------------------------------------------
def test_import_module():
print("testing import_module_from_directory()")
path = sys.argv[1]
name = sys.argv[2]
try:
mod = import_module_from_directory(module_path = path, module_name = name)
except:
print("module import failed, see log")
return False
print("module import succeeded", mod)
print(dir(mod))
return True
#-----------------------------------------------------------------------
def test_mkdir():
print("testing mkdir(%s)" % sys.argv[1])
mkdir(sys.argv[1])
#-----------------------------------------------------------------------
def test_gmPaths():
print("testing gmPaths()")
print("-----------------")
paths = gmPaths(wx=None, app_name='gnumed')
print("user config dir:", paths.user_config_dir)
print("system config dir:", paths.system_config_dir)
print("local base dir:", paths.local_base_dir)
print("system app data dir:", paths.system_app_data_dir)
print("working directory :", paths.working_dir)
print("temp directory :", paths.tmp_dir)
#-----------------------------------------------------------------------
def test_none_if():
print("testing none_if()")
print("-----------------")
tests = [
[None, None, None],
['a', 'a', None],
['a', 'b', 'a'],
['a', None, 'a'],
[None, 'a', None],
[1, 1, None],
[1, 2, 1],
[1, None, 1],
[None, 1, None]
]
for test in tests:
if none_if(value = test[0], none_equivalent = test[1]) != test[2]:
print('ERROR: none_if(%s) returned [%s], expected [%s]' % (test[0], none_if(test[0], test[1]), test[2]))
return True
#-----------------------------------------------------------------------
def test_bool2str():
tests = [
[True, 'Yes', 'Yes', 'Yes'],
[False, 'OK', 'not OK', 'not OK']
]
for test in tests:
if bool2str(test[0], test[1], test[2]) != test[3]:
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]))
return True
#-----------------------------------------------------------------------
def test_bool2subst():
print(bool2subst(True, 'True', 'False', 'is None'))
print(bool2subst(False, 'True', 'False', 'is None'))
print(bool2subst(None, 'True', 'False', 'is None'))
#-----------------------------------------------------------------------
def test_get_unique_filename():
print(get_unique_filename())
print(get_unique_filename(prefix='test-'))
print(get_unique_filename(suffix='tst'))
print(get_unique_filename(prefix='test-', suffix='tst'))
print(get_unique_filename(tmp_dir='/home/ncq/Archiv/'))
#-----------------------------------------------------------------------
def test_size2str():
print("testing size2str()")
print("------------------")
tests = [0, 1, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000, 100000000000, 1000000000000, 10000000000000]
for test in tests:
print(size2str(test))
#-----------------------------------------------------------------------
def test_unwrap():
test = """
second line\n
3rd starts with tab \n
4th with a space \n
6th
"""
print(unwrap(text = test, max_length = 25))
#-----------------------------------------------------------------------
def test_wrap():
test = 'line 1\nline 2\nline 3'
print("wrap 5-6-7 initial 0, subsequent 0")
print(wrap(test, 5))
print()
print(wrap(test, 6))
print()
print(wrap(test, 7))
print("-------")
raw_input()
print("wrap 5 initial 1-1-3, subsequent 1-3-1")
print(wrap(test, 5, u' ', u' '))
print()
print(wrap(test, 5, u' ', u' '))
print()
print(wrap(test, 5, u' ', u' '))
print("-------")
raw_input()
print("wrap 6 initial 1-1-3, subsequent 1-3-1")
print(wrap(test, 6, u' ', u' '))
print()
print(wrap(test, 6, u' ', u' '))
print()
print(wrap(test, 6, u' ', u' '))
print("-------")
raw_input()
print("wrap 7 initial 1-1-3, subsequent 1-3-1")
print(wrap(test, 7, u' ', u' '))
print()
print(wrap(test, 7, u' ', u' '))
print()
print(wrap(test, 7, u' ', u' '))
#-----------------------------------------------------------------------
def test_md5():
print('md5 %s: %s' % (sys.argv[2], file2md5(sys.argv[2])))
print('chunked md5 %s: %s' % (sys.argv[2], file2chunked_md5(sys.argv[2])))
#-----------------------------------------------------------------------
def test_unicode():
print(u_link_symbol * 10)
#-----------------------------------------------------------------------
def test_xml_escape():
print(xml_escape_string(u'<'))
print(xml_escape_string(u'>'))
print(xml_escape_string(u'&'))
#-----------------------------------------------------------------------
def test_tex_escape():
tests = [u'\\', u'^', u'~', u'{', u'}', u'%', u'&', u'#', u'$', u'_', u_euro, u'abc\ndef\n\n1234']
tests.append(u' '.join(tests))
for test in tests:
print(u'%s:' % test, tex_escape_string(test))
#-----------------------------------------------------------------------
def test_gpg_decrypt():
fname = gpg_decrypt_file(filename = sys.argv[2], passphrase = sys.argv[3])
if fname is not None:
print("successfully decrypted:", fname)
#-----------------------------------------------------------------------
def test_strip_trailing_empty_lines():
tests = [
u'one line, no embedded line breaks ',
u'one line\nwith embedded\nline\nbreaks\n '
]
for test in tests:
print('as list:')
print(strip_trailing_empty_lines(text = test, eol=u'\n', return_list = True))
print('as string:')
print(u'>>>%s<<<' % strip_trailing_empty_lines(text = test, eol=u'\n', return_list = False))
tests = [
['list', 'without', 'empty', 'trailing', 'lines'],
['list', 'with', 'empty', 'trailing', 'lines', '', ' ', '']
]
for test in tests:
print('as list:')
print(strip_trailing_empty_lines(lines = test, eol = u'\n', return_list = True))
print('as string:')
print(strip_trailing_empty_lines(lines = test, eol = u'\n', return_list = False))
#-----------------------------------------------------------------------
def test_fname_stem():
tests = [
r'abc.exe',
r'\abc.exe',
r'c:\abc.exe',
r'c:\d\abc.exe',
r'/home/ncq/tmp.txt',
r'~/tmp.txt',
r'./tmp.txt',
r'./.././tmp.txt',
r'tmp.txt'
]
for t in tests:
print("[%s] -> [%s]" % (t, fname_stem(t)))
#-----------------------------------------------------------------------
def test_dir_is_empty():
print(sys.argv[2], 'empty:', dir_is_empty(sys.argv[2]))
#-----------------------------------------------------------------------
def test_compare_dicts():
d1 = {}
d2 = {}
d1[1] = 1
d1[2] = 2
d1[3] = 3
# 4
d1[5] = 5
d2[1] = 1
d2[2] = None
# 3
d2[4] = 4
compare_dict_likes(d1, d2)
d1 = {1: 1, 2: 2}
d2 = {1: 1, 2: 2}
compare_dict_likes(d1, d2, 'same1', 'same2')
print(format_dict_like(d1))
print(format_dict_like(d2))
#-----------------------------------------------------------------------
def test_format_compare_dicts():
d1 = {}
d2 = {}
d1[1] = 1
d1[2] = 2
d1[3] = 3
# 4
d1[5] = 5
d2[1] = 1
d2[2] = None
# 3
d2[4] = 4
print(u'\n'.join(format_dict_likes_comparison(d1, d2, u'd1', u'd2')))
d1 = {1: 1, 2: 2}
d2 = {1: 1, 2: 2}
print(u'\n'.join(format_dict_likes_comparison(d1, d2, u'd1', u'd2')))
#-----------------------------------------------------------------------
def test_rm_dir():
rmdir('cx:\windows\system3__2xxxxxxxxxxxxx')
#-----------------------------------------------------------------------
def test_strip_prefix():
tests = [
(u'', u'', u''),
(u'a', u'a', u''),
(u'\.br\MICROCYTES+1\.br\SPHEROCYTES present\.br\POLYCHROMASIAmoderate\.br\\', u'\.br\\', u'MICROCYTES+1\.br\SPHEROCYTES present\.br\POLYCHROMASIAmoderate\.br\\')
]
for test in tests:
text, prefix, expect = test
result = strip_prefix(text, prefix)
if result == expect:
continue
print('test failed:', test)
print('result:', result)
#-----------------------------------------------------------------------
def test_shorten_text():
tst = [
('123', 1),
('123', 2),
('123', 3),
('123', 4),
('', 1),
('1', 1),
('12', 1),
('', 2),
('1', 2),
('12', 2),
('123', 2)
]
for txt, lng in tst:
print(u'max', lng, 'of', txt, '=', shorten_text(txt, lng))
#-----------------------------------------------------------------------
#test_coalesce()
#test_capitalize()
#test_import_module()
#test_mkdir()
#test_gmPaths()
#test_none_if()
#test_bool2str()
#test_bool2subst()
#test_get_unique_filename()
#test_size2str()
#test_wrap()
#test_input2decimal()
#test_input2int()
#test_unwrap()
#test_md5()
#test_unicode()
#test_xml_escape()
#test_gpg_decrypt()
#test_strip_trailing_empty_lines()
#test_fname_stem()
#test_tex_escape()
#test_dir_is_empty()
test_compare_dicts()
#test_rm_dir()
#test_strip_prefix()
#test_shorten_text()
#test_format_compare_dicts()
#===========================================================================
|