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
|
#
# Copyright (c) 2003, 2004 Art Haas
#
# This file is part of PythonCAD.
#
# PythonCAD is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# PythonCAD is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with PythonCAD; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#
# This code handles loading the global and user preference
# files
#
import imp
import string
import types
import sys
import os
from PythonCAD.Generic import globals
from PythonCAD.Generic import units
from PythonCAD.Generic import text
from PythonCAD.Generic import color
from PythonCAD.Generic import linetype
from PythonCAD.Generic import style
from PythonCAD.Generic import dimension
#
# global variables
#
pref_file = '/etc/pythoncad/prefs.py'
#
def _test_font_weight(val):
_weight = None
if isinstance(val, str):
_uval = string.upper(val)
if _uval == 'NORMAL':
_weight = text.TextStyle.WEIGHT_NORMAL
elif _uval == 'LIGHT':
_weight = text.TextStyle.WEIGHT_LIGHT
elif _uval == 'BOLD':
_weight = text.TextStyle.WEIGHT__BOLD
elif _uval == 'HEAVY':
_weight = text.TextStyle.WEIGHT_HEAVY
else:
sys.stderr.write("Invalid font weight: '%s'\n" % val)
else:
sys.stderr.write("invalid font weight: '" + str(val) + "'\n")
return _weight
def _test_font_style(val):
_style = None
if isinstance(val, str):
_uval = string.upper(val)
if _uval == 'NORMAL':
_style = text.TextStyle.FONT_NORMAL
elif _uval == 'OBLIQUE':
_style = text.TextStyle.FONT_OBLIQUE
elif _uval == 'ITALIC':
_uval = text.TextStyle.FONT_ITALIC
else:
sys.stderr.write("Invalid font style: '%s'\n" % val)
else:
sys.stderr.write("Invalid font style: '" + str(val) + "'\n")
return _style
def _test_color(val):
_color = None
try:
_color = color.Color(val)
except:
sys.stderr.write("Invalid color: '%s'\n" % val)
return _color
def _test_dim_position(val):
_pos = None
if isinstance(val, str):
_uval = string.upper(val)
if _uval == 'SPLIT':
_pos = dimension.Dimension.DIM_TEXT_POS_SPLIT
elif _uval == 'ABOVE':
_pos = dimension.Dimension.DIM_TEXT_POS_ABOVE
elif _uval == 'BELOW':
_pos = dimension.Dimension.DIM_TEXT_POS_BELOW
else:
sys.stderr.write("Invalid dimension position: '%s'\n" % val)
else:
sys.stderr.write("Invalid dimension position: '" + str(val) + "'\n")
return _pos
def _test_dim_endpoint(val):
_ept = None
if val is not None:
if isinstance(val, str):
_uval = string.upper(val)
if _uval == 'NO_ENDPOINT' or _uval == 'NONE':
_ept = dimension.Dimension.DIM_ENDPT_NONE
elif _uval == 'ARROW':
_ept = dimension.Dimension.DIM_ENDPT_ARROW
elif _uval == 'FILLED_ARROW':
_ept = dimension.Dimension.DIM_ENDPT_FILLED_ARROW
elif _uval == 'SLASH':
_ept = dimension.Dimension.DIM_ENDPT_SLASH
elif _uval == 'CIRCLE':
_ept = dimension.Dimension.DIM_ENDPT_CIRCLE
else:
sys.stderr.write("Invalid dimension endpoint: '%s'\n" % val)
else:
sys.stderr.write("Invalid dimension endpoint: '" + str(val) + "'\n")
return _ept
def _test_units(val):
_unit = None
if isinstance(val, str):
_uval = string.upper(val)
if _uval == 'MILLIMETERS':
_unit = units.MILLIMETERS
elif _uval == 'MICROMETERS':
_unit = units.MICROMETERS
elif _uval == 'METERS':
_unit = units.METERS
elif _uval == 'KILOMETERS':
_unit = units.KILOMETERS
elif _uval == 'INCHES':
_unit = units.INCHES
elif _uval == 'FEET':
_unit = units.FEET
elif _uval == 'YARDS':
_unit = units.YARDS
elif _uval == 'MILES':
_unit = units.MILES
else:
sys.stderr.write("Invalid unit: '%s'\n" % val)
else:
sys.stderr.write("Invalid unit: '" + str(val) + "'\n")
return _unit
def _test_boolean(val):
_bool = None
if val is True or val is False:
_bool = val
else:
sys.stderr.write("Invalid boolean flag: '%s'\n" % val)
return _bool
def _test_float(val):
_float = None
if isinstance(val, float):
_float = val
else:
try:
_float = float(val)
except:
sys.stderr.write("Invalid float value: '%s'\n" % val)
return _float
def _test_int(val):
_int = None
if isinstance(val, int):
_int = val
else:
try:
_int = int(val)
except:
sys.stderr.write("Invalid integer value: '%s'\n" % val)
return _int
def _test_unicode(val):
_uni = None
if isinstance(val, unicode):
_uni = val
else:
try:
_uni = unicode(val)
except:
sys.stderr.write("Invalid unicode string: '%s'\n" % val)
return _uni
def initialize_prefs():
"""This method sets the initial default value for image variables.
initialize_prefs()
"""
#
# default dimension parameters
#
_dimstyles = []
globals.prefs['DIMSTYLES'] = _dimstyles
globals.prefs['DEFAULT_DIMSTYLE'] = None
#
# drawing and text parameters
#
globals.prefs['TEXTSTYLES'] = []
globals.prefs['DEFAULT_TEXTSTYLE'] = None
#
# miscellaneous things
#
globals.prefs['USER_PREFS'] = True
#
# colors
#
# these will be replaced ...
#
_colors = []
_red = color.Color(255,0,0)
_colors.append(_red)
_green = color.Color(0,255,0)
_colors.append(_green)
_blue = color.Color(0,0,255)
_colors.append(_blue)
_violet = color.Color(255,0,255)
_colors.append(_violet)
_yellow = color.Color(255,255,0)
_colors.append(_yellow)
_cyan = color.Color(0,255,255)
_colors.append(_cyan)
_white = color.Color(255,255,255)
_colors.append(_white)
_black = color.Color(0,0,0)
_colors.append(_black)
globals.prefs['COLORS'] = _colors
#
# linetypes
#
# these will be replaced
#
_linetypes = []
_solid = linetype.Linetype(u'Solid')
_linetypes.append(_solid)
_dash1 = linetype.Linetype(u'Dash1', [4,1])
_linetypes.append(_dash1)
_dash2 = linetype.Linetype(u'Dash2', [8,2])
_linetypes.append(_dash2)
_dash3 = linetype.Linetype(u'Dash3', [12,2])
_linetypes.append(_dash3)
_dash4 = linetype.Linetype(u'Dash4', [10,2,2,2])
_linetypes.append(_dash4)
_dash5 = linetype.Linetype(u'Dash5', [15,5,5,5])
_linetypes.append(_dash5)
globals.prefs['LINETYPES'] = _linetypes
#
# line styles
#
# these will be replaced
#
_styles = []
_st = style.Style('Solid White Line', _solid, _white, 1.0)
globals.prefs['STANDARD_STYLE'] = 'Solid White Line'
_styles.append(_st)
_st = style.Style('Solid Black Line', _solid, _black, 1.0)
_styles.append(_st)
_st = style.Style('Dashed Red Line', _dash1, _red, 1.0)
_styles.append(_st)
_st = style.Style('Dashed Green Line', _dash1, _green, 1.0)
_styles.append(_st)
_st = style.Style('Dashed Blue Line', _dash1, _blue, 1.0)
_styles.append(_st)
_st = style.Style('Dashed Yellow Line', _dash2, _yellow, 1.0)
_styles.append(_st)
_st = style.Style('Dashed Violet Line', _dash2, _violet, 1.0)
_styles.append(_st)
_st = style.Style('Dashed Cyan Line', _dash2, _cyan, 1.0)
_styles.append(_st)
globals.prefs['STYLES'] = _styles
globals.prefs['DEFAULT_STYLE'] = 'Solid White Line'
#
# validate the available options and set them if they seem alright
#
def _set_units(prefmod):
_unit = _test_units(prefmod.units)
if _unit is not None:
globals.prefs['UNITS'] = _unit
def _set_user_prefs(prefmod):
_flag = _test_boolean(prefmod.user_prefs)
if _flag is not None:
globals.prefs['USER_PREFS'] = _flag
def _set_primary_font_family(prefmod):
_family = prefmod.primary_font_family
globals.prefs['DIM_PRIMARY_FONT_FAMILY'] = _family # needs a test ...
def _set_primary_font_size(prefmod):
_size = _test_int(prefmod.primary_font_size)
if _size is not None:
if _size > 0:
globals.prefs['DIM_PRIMARY_FONT_SIZE'] = _size
globals.prefs['DIM_PRIMARY_TEXT_SIZE'] = float(_size)
else:
sys.stderr.write("Invalid primary font size: %d\n" % _size)
def _set_primary_text_size(prefmod):
_size = _test_float(prefmod.primary_text_size)
if _size is not None:
if _size > 0.0:
globals.prefs['DIM_PRIMARY_TEXT_SIZE'] = _size
else:
sys.stderr.write("Invalid primary text size: %g\n" % _size)
def _set_primary_font_weight(prefmod):
_weight = _test_font_weight(prefmod.primary_font_weight)
if _weight is not None:
globals.prefs['DIM_PRIMARY_FONT_WEIGHT'] = _weight
def _set_primary_font_style(prefmod):
_style = _test_font_style(prefmod.primary_font_style)
if _style is not None:
globals.prefs['DIM_PRIMARY_FONT_STYLE'] = _style
def _set_primary_font_color(prefmod):
_color = _test_color(prefmod.primary_font_color)
if _color is not None:
_r, _g, _b = _color.getColors()
_color = color.get_color(_r, _g, _b)
globals.prefs['DIM_PRIMARY_FONT_COLOR'] = _color
_colors = globals.prefs['COLORS']
if _color not in _colors:
_colors.append(_color)
def _set_primary_prefix(prefmod):
_prefix = _test_unicode(prefmod.primary_prefix)
if _prefix is not None:
globals.prefs['DIM_PRIMARY_PREFIX'] = _prefix
def _set_primary_suffix(prefmod):
_suffix = _test_unicode(prefmod.primary_suffix)
if _suffix is not None:
globals.prefs['DIM_PRIMARY_SUFFIX'] = _suffix
def _set_primary_precision(prefmod):
_prec = _test_int(prefmod.primary_precision)
if _prec is not None:
if _prec < 0 or _prec > 15:
sys.stderr.write("Invalid primary dimension precision: %d\n" % _prec)
else:
globals.prefs['DIM_PRIMARY_PRECISION'] = _prec
def _set_primary_units(prefmod):
_unit = _test_units(prefmod.primary_units)
if _unit is not None:
globals.prefs['DIM_PRIMARY_UNITS'] = _unit
def _set_primary_print_zero(prefmod):
_flag = _test_boolean(prefmod.primary_print_zero)
if _flag is not None:
globals.prefs['DIM_PRIMARY_LEADING_ZERO'] = _flag
def _set_primary_trail_decimal(prefmod):
_flag = _test_boolean(prefmod.primary_trailing_decimal)
if _flag is not None:
globals.prefs['DIM_PRIMARY_TRAILING_DECIMAL'] = _flag
def _set_secondary_font_family(prefmod):
_family = prefmod.secondary_font_family
globals.prefs['DIM_SECONDARY_FONT_FAMILY'] = _family # needs a test ...
def _set_secondary_font_size(prefmod):
_size = _test_int(prefmod.secondary_font_size)
if _size is not None:
if _size > 0:
globals.prefs['DIM_SECONDARY_FONT_SIZE'] = _size
globals.prefs['DIM_SECONDARY_TEXT_SIZE'] = float(_size)
else:
sys.stderr.write("Invalid secondary text size: %d\n" % _size)
def _set_secondary_text_size(prefmod):
_size = _test_float(prefmod.secondary_text_size)
if _size is not None:
if _size > 0.0:
globals.prefs['DIM_SECONDARY_TEXT_SIZE'] = _size
else:
sys.stderr.write("Invalid secondary text size: %g\n" % _size)
def _set_secondary_font_weight(prefmod):
_weight = _test_font_weight(prefmod.secondary_font_weight)
if _weight is not None:
globals.prefs['DIM_SECONDARY_FONT_WEIGHT'] = _weight
def _set_secondary_font_style(prefmod):
_style = _test_font_style(prefmod.secondary_font_style)
if _style is not None:
globals.prefs['DIM_SECONDARY_FONT_STYLE'] = _style
def _set_secondary_font_color(prefmod):
_color = _test_color(prefmod.secondary_font_color)
if _color is not None:
_r, _g, _b = _color.getColors()
_color = color.get_color(_r, _g, _b)
globals.prefs['DIM_SECONDARY_FONT_COLOR'] = _color
_colors = globals.prefs['COLORS']
if _color not in _colors:
_colors.append(_color)
def _set_secondary_prefix(prefmod):
_prefix = _test_unicode(prefmod.secondary_prefix)
if _prefix is not None:
globals.prefs['DIM_SECONDARY_PREFIX'] = _prefix
def _set_secondary_suffix(prefmod):
_suffix = _test_unicode(prefmod.secondary_suffix)
if _suffix is not None:
globals.prefs['DIM_SECONDARY_SUFFIX'] = _suffix
def _set_secondary_precision(prefmod):
_prec = _test_int(prefmod.secondary_precision)
if _prec is not None:
if _prec < 0 or _prec > 15:
sys.stderr.write("Invalid secondary dimension precision: %d\n" % _prec)
else:
globals.prefs['DIM_SECONDARY_PRECISION'] = _prec
def _set_secondary_units(prefmod):
_unit = _test_units(prefmod.secondary_units)
if _unit is not None:
globals.prefs['DIM_SECONDARY_UNITS'] = _unit
def _set_secondary_print_zero(prefmod):
_flag = _test_boolean(prefmod.secondary_print_zero)
if _flag is not None:
globals.prefs['DIM_SECONDARY_LEADING_ZERO'] = _flag
def _set_secondary_trail_decimal(prefmod):
_flag = _test_boolean(prefmod.secondary_trailing_decimal)
if _flag is not None:
globals.prefs['DIM_SECONDARY_TRAILING_DECIMAL'] = _flag
def _set_dim_offset(prefmod):
_offset = _test_float(prefmod.dim_offset)
if _offset is not None:
if _offset < 0.0:
sys.stderr.write("Invalid dimension offset: %g\n" % _offset)
else:
globals.prefs['DIM_OFFSET'] = _offset
def _set_dim_extension(prefmod):
_ext = _test_float(prefmod.dim_extension)
if _ext is not None:
if _ext < 0.0:
sys.stderr.write("Invalid dimension extension: %g\n" % _ext)
else:
globals.prefs['DIM_EXTENSION'] = _ext
def _set_dim_color(prefmod):
_color = _test_color(prefmod.dim_color)
if _color is not None:
_r, _g, _b = _color.getColors()
_color = color.get_color(_r, _g, _b)
globals.prefs['DIM_COLOR'] = _color
_colors = globals.prefs['COLORS']
if _color not in _colors:
_colors.append(_color)
def _set_dim_thickness(prefmod):
_t = _test_float(prefmod.dim_thickness)
if _t is not None:
if _t < 0.0:
sys.stderr.write("Invalid dimension thickness %g\n" % _t)
else:
globals.prefs['DIM_THICKNESS'] = _ext
def _set_dim_position(prefmod):
_pos = _test_dim_position(prefmod.dim_position)
if _pos is not None:
globals.prefs['DIM_POSITION'] = _pos
def _set_dim_endpoint(prefmod):
_ept = _test_dim_endpoint(prefmod.dim_endpoint)
if _ept is not None:
globals.prefs['DIM_ENDPOINT'] = _ept
def _set_dim_endpoint_size(prefmod):
_epsize = _test_float(prefmod.dim_endpoint_size)
if _epsize is not None:
if _epsize < 0.0:
sys.stderr.write("Invalid endpoint size: %g\n" % _epsize)
else:
globals.prefs['DIM_ENDPOINT_SIZE'] = _epsize
def _set_dim_dual_mode(prefmod):
_flag = _test_boolean(prefmod.dim_dual_mode)
if _flag is not None:
globals.prefs['DIM_DUAL_MODE'] = _flag
def _set_radial_dim_primary_prefix(prefmod):
_prefix = _test_unicode(prefmod.radial_dim_primary_prefix)
if _prefix is not None:
globals.prefs['RADIAL_DIM_PRIMARY_PREFIX'] = _prefix
def _set_radial_dim_primary_suffix(prefmod):
_suffix = _test_unicode(prefmod.radial_dim_primary_suffix)
if _suffix is not None:
globals.prefs['RADIAL_DIM_PRIMARY_SUFFIX'] = _suffix
def _set_radial_dim_secondary_prefix(prefmod):
_prefix = _test_unicode(prefmod.radial_dim_secondary_prefix)
if _prefix is not None:
globals.prefs['RADIAL_DIM_SECONDARY_PREFIX'] = _prefix
def _set_radial_dim_secondary_suffix(prefmod):
_suffix = _test_unicode(prefmod.radial_dim_secondary_suffix)
if _suffix is not None:
globals.prefs['RADIAL_DIM_SECONDARY_SUFFIX'] = _suffix
def _set_radial_dim_dia_mode(prefmod):
_flag = _test_boolean(prefmod.radial_dim_dia_mode)
if _flag is not None:
globals.prefs['RADIAL_DIM_DIA_MODE'] = _flag
def _set_angular_dim_primary_prefix(prefmod):
_prefix = _test_unicode(prefmod.angular_dim_primary_prefix)
if _prefix is not None:
globals.prefs['ANGULAR_DIM_PRIMARY_PREFIX'] = _prefix
def _set_angular_dim_primary_suffix(prefmod):
_suffix = _test_unicode(prefmod.angular_dim_primary_suffix)
if _suffix is not None:
globals.prefs['ANGULAR_DIM_PRIMARY_SUFFIX'] = _suffix
def _set_angular_dim_secondary_prefix(prefmod):
_prefix = _test_unicode(prefmod.angular_dim_secondary_prefix)
if _prefix is not None:
globals.prefs['ANGULAR_DIM_SECONDARY_PREFIX'] = _prefix
def _set_angular_dim_secondary_suffix(prefmod):
_suffix = _test_unicode(prefmod.angular_dim_secondary_suffix)
if _suffix is not None:
globals.prefs['ANGULAR_DIM_SECONDARY_SUFFIX'] = _suffix
def _set_font_family(prefmod):
_family = prefmod.font_family
globals.prefs['FONT_FAMILY'] = _family
def _set_font_style(prefmod):
_style = _test_font_style(prefmod.font_style)
if _style is not None:
globals.prefs['FONT_STYLE'] = _style
def _set_font_weight(prefmod):
_weight = _test_font_weight(prefmod.font_weight)
if _weight is not None:
globals.prefs['FONT_WEIGHT'] = _weight
def _set_font_size(prefmod):
_size = _test_int(prefmod.font_size)
if _size is not None:
if _size > 0:
globals.prefs['FONT_SIZE'] = _size
globals.prefs['TEXT_SIZE'] = float(_size)
else:
sys.stderr.write("Invalid font size: %d\n" % _size)
def _set_text_size(prefmod):
_size = _test_float(prefmod.text_size)
if _size is not None:
if _size > 0.0:
globals.prefs['TEXT_SIZE'] = _size
else:
sys.stderr.write("Invalid text size: %g\n" % _size)
def _set_font_color(prefmod):
_color = _test_color(prefmod.font_color)
if _color is not None:
_r, _g, _b = _color.getColors()
_color = color.get_color(_r, _g, _b)
globals.prefs['FONT_COLOR'] = _color
_colors = globals.prefs['COLORS']
if _color not in _colors:
_colors.append(_color)
def _set_chamfer_length(prefmod):
_length = _test_float(prefmod.chamfer_length)
if _length is not None:
if _length < 0.0:
sys.stderr.write("Invalid chamfer length: %g\n" % _length)
else:
globals.prefs['CHAMFER_LENGTH'] = _length
def _set_fillet_radius(prefmod):
_radius = _test_float(prefmod.fillet_radius)
if _radius is not None:
if _radius < 0.0:
sys.stderr.write("Invalid fillet radius: %g\n" % _radius)
else:
globals.prefs['FILLET_RADIUS'] = _radius
def _set_line_color(prefmod):
_color = _test_color(prefmod.line_color)
if _color is not None:
_r, _g, _b = _color.getColors()
_color = color.get_color(_r, _g, _b)
globals.prefs['LINE_COLOR'] = _color
_colors = globals.prefs['COLORS']
if _color not in _colors:
_colors.append(_color)
def _set_line_thickness(prefmod):
_t = _test_float(prefmod.line_thickness)
if _t is not None:
if _t < 0.0:
sys.stderr.write("Invalid line thickness: %g\n" % _thickness)
else:
globals.prefs['LINE_THICKNESS'] = _t
def _set_highlight_points(prefmod):
_flag = _test_boolean(prefmod.highlight_points)
if _flag is not None:
globals.prefs['HIGHLIGHT_POINTS'] = _flag
def _set_inactive_layer_color(prefmod):
_color = _test_color(prefmod.inactive_layer_color)
if _color is not None:
_r, _g, _b = _color.getColors()
_color = color.get_color(_r, _g, _b)
globals.prefs['INACTIVE_LAYER_COLOR'] = _color
def _set_background_color(prefmod):
_color = _test_color(prefmod.background_color)
if _color is not None:
_r, _g, _b = _color.getColors()
_color = color.get_color(_r, _g, _b)
globals.prefs['BACKGROUND_COLOR'] = _color
def _set_single_point_color(prefmod):
_color = _test_color(prefmod.single_point_color)
if _color is not None:
_r, _g, _b = _color.getColors()
_color = color.get_color(_r, _g, _b)
globals.prefs['SINGLE_POINT_COLOR'] = _color
def _set_multi_point_color(prefmod):
_color = _test_color(prefmod.multi_point_color)
if _color is not None:
_r, _g, _b = _color.getColors()
_color = color.get_color(_r, _g, _b)
globals.prefs['MULTI_POINT_COLOR'] = _color
def _set_autosplit(prefmod):
_flag = _test_boolean(prefmod.autosplit)
if _flag is not None:
globals.prefs['AUTOSPLIT'] = _flag
def _set_leader_arrow_size(prefmod):
_size = _test_float(prefmod.leader_arrow_size)
if _size is not None:
if _size < 0.0:
sys.stderr.write("Invalid leader arrow size: %g\n" % _size)
else:
globals.prefs['LEADER_ARROW_SIZE'] = _size
def _set_linetypes(prefmod):
_ltlist = prefmod.linetypes
_linetypes = globals.prefs['LINETYPES']
if isinstance(_ltlist, list):
for _obj in _ltlist:
if not isinstance(_obj, tuple):
sys.stderr.write("Invalid linetype tuple: '" + str(_obj) + "'\n")
continue
if len(_obj) != 2:
sys.stderr.write("Invalid linetype tuple: '" + str(_obj) + "'\n")
continue
_name, _dlist = _obj
try:
_linetype = linetype.Linetype(_name, _dlist)
if _linetype not in _linetypes:
_linetypes.append(_linetype)
except:
sys.stderr.write("Invalid linetype: '" + str(_obj) + "'\n")
else:
sys.stderr.write("Invalid line type list: '" + str(_ltlist) + "'\n")
def _set_colors(prefmod):
_clist = prefmod.colors
if isinstance(_clist, list):
for _obj in _clist:
_color = None
if isinstance(_obj, str):
_color = _test_color(_obj)
elif isinstance(_obj, tuple):
if len(_obj) != 3:
sys.stderr.write("Invalid color: '" + str(_obj) + "'\n")
continue
_r, _g, _b = _obj
try:
_color = color.Color(_r, _g, _b)
except:
sys.stderr.write("Invalid color: '" + str(_obj) + "'\n")
continue
if _color is not None and _color not in globals.colors:
globals.colors[_color] = _color
else:
sys.stderr.write("Invalid color list: '" + str(_clist) + "'\n")
def _set_styles(prefmod):
_slist = prefmod.styles
_styles = globals.prefs['STYLES']
_linetypes = globals.prefs['LINETYPES']
if isinstance(_slist, list):
for _obj in _slist:
if not isinstance(_obj, tuple):
sys.stderr.write("Invalid style entry: '" + str(_obj) + "'\n")
continue
if len(_obj) != 4:
sys.stderr.write("Invalid style entry: '" + str(_obj) + "'\n")
continue
_name, _lt, _col, _th = _obj
if not isinstance(_name, types.StringTypes):
sys.stderr.write("Invalid style name: '" + str(_name) + "'\n")
continue
if not isinstance(_name, unicode):
_name = unicode(_obj[0])
if not isinstance(_lt, tuple):
sys.stderr.write("Invalid style linetype: '" + str(_lt) + "'\n")
continue
if len(_lt) != 2:
sys.stderr.write("Invalid style linetype: '" + str(_lt) + "'\n")
continue
try:
_name, _dlist = _lt
_linetype = linetype.Linetype(_name, _dlist)
except:
sys.stderr.write("Invalid style linetype: '" + str(_lt) + "'\n")
continue
_color = _test_color(_col)
if _color is None:
continue
_r, _g, _b = _color.getColors()
_color = color.get_color(_r, _g, _b)
_thickness = _test_float(_th)
if _thickness is None:
continue
try:
_style = style.Style(_name, _linetype, _color, _thickness)
if _style not in _styles:
_styles.append(_style)
if _linetype not in _linetypes:
_linetypes.append(_linetype)
except:
sys.stderr.write("Invalid style: '" + str(_obj) + "'\n")
else:
sys.stderr.write("Invalid style list: '" + str(_slist) + "'\n")
def _test_dimstyle_opt(key, value):
_val = None
if (key == 'DIM_COLOR' or
key == 'DIM_PRIMARY_FONT_COLOR' or
key == 'DIM_SECONDARY_FONT_COLOR'):
_color = None
if isinstance(value, str):
try:
_color = color.Color(value)
except:
sys.stderr.write("Invalid dimension style color: '" + value + "'\n")
elif isinstance(value, tuple):
if len(value) == 3:
_r, _g, _b = value
try:
_color = color.Color(_r, _g, _b)
except:
sys.stderr.write("Invalid dimension style color: '" + value + "'\n")
else:
sys.stderr.write("Invalid dimension style color: '" + value + "'\n")
else:
sys.stderr.write("Invalid dimension style color: '" + value + "'\n")
if _color is not None:
_r, _g, _b = _color.getColors()
_val = color.get_color(_r, _g, _b)
elif (key == 'DIM_PRIMARY_FONT_FAMILY' or
key == 'DIM_SECONDARY_FONT_FAMILY'):
if isinstance(value, str):
_val = value
else:
sys.stderr.write("Invalid dimension style font family: '" + str(value) + "'\n")
elif (key == 'DIM_OFFSET' or
key == 'DIM_ENDPOINT_SIZE' or
key == 'DIM_PRIMARY_FONT_SIZE' or
key == 'DIM_PRIMARY_TEXT_SIZE' or
key == 'DIM_SECONDARY_FONT_SIZE' or
key == 'DIM_SECONDARY_TEXT_SIZE' or
key == 'DIM_EXTENSION'):
_fval = _test_float(value)
if _fval is not None:
if _fval < 0.0:
sys.stderr.write("Invalid dimension style float value: %g\n" % _fval)
else:
_val = _fval
if key == 'DIM_PRIMARY_FONT_SIZE':
sys.stderr.write("Key 'DIM_PRIMARY_FONT_SIZE' is deprecated: use 'DIM_PRIMARY_TEXT_SIZE' instead.\n")
if key == 'DIM_SECONDARY_FONT_SIZE':
sys.stderr.write("Key 'DIM_SECONDARY_FONT_SIZE' is deprecated: use 'DIM_SECONDARY_TEXT_SIZE' instead.\n")
elif (key == 'DIM_PRIMARY_PRECISION' or
key == 'DIM_SECONDARY_PRECISION'):
_ival = _test_int(value)
if _ival is not None:
if _ival < 0 or _ival > 15:
sys.stderr.write("Invalid dimension style integer value: %d\n" % _ival)
else:
_val = _ival
elif (key == 'DIM_PRIMARY_FONT_WEIGHT' or
key == 'DIM_SECONDARY_FONT_WEIGHT'):
_weight = _test_font_weight(value)
if _weight is not None:
_val = _weight
elif (key == 'DIM_PRIMARY_FONT_STYLE' or
key == 'DIM_SECONDARY_FONT_STYLE'):
_style = _test_font_style(value)
if _style is not None:
_val = _style
elif (key == 'DIM_PRIMARY_UNITS' or
key == 'DIM_SECONDARY_UNITS'):
_unit = _test_units(value)
if _unit is not None:
_val = _unit
elif key == 'DIM_POSITION':
_pos = _test_dim_position(value)
if _pos is not None:
_val = _pos
elif key == 'DIM_ENDPOINT':
_ept = _test_dim_endpoint(value)
if _ept is not None:
_val = _ept
elif (key == 'DIM_PRIMARY_LEADING_ZERO' or
key == 'DIM_SECONDARY_LEADING_ZERO' or
key == 'DIM_PRIMARY_TRAILING_DECIMAL' or
key == 'DIM_SECONDARY_TRAILING_DECIMAL' or
key == 'DIM_DUAL_MODE' or
key == 'RADIAL_DIM_DIA_MODE'):
_flag = _test_boolean(value)
if _flag is not None:
_val = _flag
elif (key == 'DIM_PRIMARY_PREFIX' or
key == 'DIM_PRIMARY_SUFFIX' or
key == 'DIM_SECONDARY_PREFIX' or
key == 'DIM_SECONDARY_SUFFIX' or
key == 'RADIAL_DIM_PRIMARY_PREFIX' or
key == 'RADIAL_DIM_PRIMARY_SUFFIX' or
key == 'RADIAL_DIM_SECONDARY_PREFIX' or
key == 'RADIAL_DIM_SECONDARY_SUFFIX' or
key == 'ANGULAR_DIM_PRIMARY_PREFIX' or
key == 'ANGULAR_DIM_PRIMARY_SUFFIX' or
key == 'ANGULAR_DIM_SECONDARY_PREFIX' or
key == 'ANGULAR_DIM_SECONDARY_SUFFIX'):
if isinstance(value, types.StringTypes):
_string = value
if isinstance(_string, str):
_string = unicode(value)
_val = _string
else:
sys.stderr.write("Invalid prefix/suffix: '%s'\n" % str(value))
else:
sys.stderr.write("Unhandled dimension style key: '%s'" % key)
return _val
def _set_dimstyles(prefmod):
_dslist = prefmod.dimstyles
_dimstyles = globals.prefs['DIMSTYLES']
_dsdefs = dimension.dimstyle_defaults
if isinstance(_dslist, list):
for _obj in _dslist:
if not isinstance(_obj, tuple):
sys.stderr.write("Invalid dimension style entry: '" + str(_obj) + "'\n")
continue
if len(_obj) != 2:
sys.stderr.write("Invalid dimension style entry: '" + str(_obj) + "'\n")
continue
_name, _dsdict = _obj
if not isinstance(_name, types.StringTypes):
sys.stderr.write("Invalid dimension style name: '" + str(_name) + "'\n")
continue
if not isinstance(_dsdict, dict):
sys.stderr.write("Invalid dimension style dictionary: '" + str(_dsdict) + "'\n")
continue
_defs = {}
for _opt in _dsdefs:
_val = _dsdefs[_opt]
if (_opt == 'DIM_COLOR' or
_opt == 'DIM_PRIMARY_FONT_COLOR' or
_opt == 'DIM_SECONDARY_FONT_COLOR'):
_val = _val.clone()
_defs[_opt] = _val
_valid = True
for _opt in _dsdict:
if not isinstance(_opt, str):
sys.stderr.write("Invalid dimension style entry: '" + str(_opt) + "'\n")
_valid = False
break
_uopt = string.upper(_opt)
if _uopt not in _dsdefs:
sys.stderr.write("Invalid dimension style entry: '" + _opt + "'\n")
_valid = False
break
_optval = _dsdict[_opt]
_val = _test_dimstyle_opt(_uopt, _optval)
if _val is None:
sys.stderr.write("Invalid dimension style entry: '" + _opt + "'\n")
_valid = False
break
_defs[_uopt] = _val
if _valid:
try:
_dimstyle = dimension.DimStyle(_name, _defs)
if _dimstyle not in _dimstyles:
_dimstyles.append(_dimstyle)
except:
sys.stderr.write("Invalid dimension style: " + str(_obj) + "'\n")
else:
sys.stderr.write("Invalid dimension style list: '" + str(_dslist) + "'\n")
def _set_textstyles(prefmod):
_tslist = prefmod.textstyles
_textstyles = globals.prefs['TEXTSTYLES']
if isinstance(_tslist, list):
for _obj in _tslist:
if not isinstance(_obj, tuple):
sys.stderr.write("Invalid text style entry: '" + str(_obj) + "'\n")
continue
if len(_obj) != 6:
sys.stderr.write("Invalid text style entry: '" + str(_obj) + "'\n")
continue
_name = _test_unicode(_obj[0])
if _name is None:
continue
_family = _obj[1]
if not isinstance(_family, types.StringTypes):
sys.stderr.write("Invalid text style family: '" + str(_family) + "'\n")
continue
_size = _test_int(_obj[2])
if _size is None:
continue
if _size < 0:
sys.stderr.write("Invalid text style size: %d\n" % _size)
continue
_style = _test_font_style(_obj[3])
if _style is None:
continue
_weight = _test_font_weight(_obj[4])
if _weight is None:
continue
_color = _test_color(_obj[5])
if _color is None:
continue
_r, _g, _b = _color.getColors()
_color = color.get_color(_r, _g, _b)
_textstyle = text.TextStyle(_name, _family, _size, _style, _weight, _color)
if _textstyle not in _textstyles:
_textstyles.append(_textstyle)
else:
sys.stderr.write("Invalid text style list: '" + str(_dslist) + "'\n")
#
# this dict lists the attributes in the module and the function
# used to validate the value given by the user
#
_prefkeys = {
'units' : _set_units,
'user_prefs' : _set_user_prefs,
'primary_font_family' : _set_primary_font_family,
'primary_font_size' : _set_primary_font_size,
'primary_text_size' : _set_primary_text_size,
'primary_font_weight' : _set_primary_font_weight,
'primary_font_style' : _set_primary_font_style,
'primary_font_color' : _set_primary_font_color,
'primary_prefix' : _set_primary_prefix,
'primary_suffix' : _set_primary_suffix,
'primary_precision' : _set_primary_precision,
'primary_units' : _set_primary_units,
'primary_leading_zero' : _set_primary_print_zero,
'primary_trailing_decimal': _set_primary_trail_decimal,
'secondary_font_family' : _set_secondary_font_family,
'secondary_font_size' : _set_secondary_font_size,
'secondary_text_size' : _set_secondary_text_size,
'secondary_font_weight' : _set_secondary_font_weight,
'secondary_font_style' : _set_secondary_font_style,
'secondary_font_color' : _set_secondary_font_color,
'secondary_prefix' : _set_secondary_prefix,
'secondary_suffix' : _set_secondary_suffix,
'secondary_precision' : _set_secondary_precision,
'secondary_units' : _set_secondary_units,
'secondary_leading_zero' : _set_secondary_print_zero,
'secondary_trailing_decimal': _set_secondary_trail_decimal,
'dim_offset' : _set_dim_offset,
'dim_extension': _set_dim_extension,
'dim_color' : _set_dim_color,
'dim_thickness' : _set_dim_thickness,
'dim_position': _set_dim_position,
'dim_endpoint': _set_dim_endpoint,
'dim_endpoint_size' : _set_dim_endpoint_size,
'dim_dual_mode' : _set_dim_dual_mode,
'radial_dim_primary_prefix' : _set_radial_dim_primary_prefix,
'radial_dim_primary_suffix' : _set_radial_dim_primary_suffix,
'radial_dim_secondary_prefix' : _set_radial_dim_secondary_prefix,
'radial_dim_secondary_suffix' : _set_radial_dim_secondary_suffix,
'radial_dim_dia_mode' : _set_radial_dim_dia_mode,
'angular_dim_primary_prefix' : _set_angular_dim_primary_prefix,
'angular_dim_primary_suffix' : _set_angular_dim_primary_suffix,
'angular_dim_secondary_prefix' : _set_angular_dim_secondary_prefix,
'angular_dim_secondary_suffix' : _set_angular_dim_secondary_suffix,
'font_family' : _set_font_family,
'font_style' : _set_font_style,
'font_weight' : _set_font_weight,
'font_size' : _set_font_size,
'text_size' : _set_text_size,
'font_color' : _set_font_color,
'chamfer_length' : _set_chamfer_length,
'fillet_radius' : _set_fillet_radius,
'line_color' : _set_line_color,
'linetypes' : _set_linetypes,
'colors' : _set_colors,
'styles' : _set_styles,
'dimstyles' : _set_dimstyles,
'textstyles' : _set_textstyles,
'line_thickness': _set_line_thickness,
'highlight_points' : _set_highlight_points,
'inactive_layer_color' : _set_inactive_layer_color,
'background_color' : _set_background_color,
'single_point_color' : _set_single_point_color,
'multi_point_color' : _set_multi_point_color,
'autosplit' : _set_autosplit,
'leader_arrow_size' : _set_leader_arrow_size,
}
def _set_defaults(prefmod):
if hasattr(prefmod, 'default_dimstyle'):
_dsname = prefmod.default_dimstyle
if isinstance(_dsname, types.StringTypes):
_set = False
for _dimstyle in globals.prefs['DIMSTYLES']:
_name = _dimstyle.getName()
if _name == _dsname:
globals.prefs['DEFAULT_DIMSTYLE'] = _dimstyle
_set = True
break
if not _set:
sys.stderr.write("No DimStyle found with name '%s'\n" % _dsname)
elif _dsname is None:
pass # accept default
else:
sys.stderr.write("Invalid default dimension style: '%s'\n" % str(_dsname))
if hasattr(prefmod, 'default_textstyle'):
_tsname = prefmod.default_textstyle
if isinstance(_tsname, types.StringTypes):
_set = False
for _textstyle in globals.prefs['TEXTSTYLES']:
_name = _textstyle.getName()
if _name == _tsname:
globals.prefs['DEFAULT_TEXTSTYLE'] = _textstyle
_set = True
break
if not _set:
sys.stderr.write("No TextStyle found with name '%s'\n" % _tsname)
elif _tsname is None:
pass # accept default
else:
sys.stderr.write("Invalid default TextStyle: '%s'\n" % str(_tsname))
if hasattr(prefmod, 'default_style'):
_sname = prefmod.default_style
if isinstance(_sname, types.StringTypes):
_set = False
for _style in globals.prefs['STYLES']:
_name = _style.getName()
if _name == _sname:
globals.prefs['DEFAULT_STYLE'] = _style
_set = True
break
if not _set:
sys.stderr.write("No Style found with name '%s'\n" % _sname)
elif _sname is None:
pass # accept default
else:
sys.stderr.write("Invalid default Style: '%s'\n" % str(_sname))
def load_global_prefs():
"""Try and load the preferences stored in the global preferences file
load_global_prefs()
If the preferences file '/etc/pythoncad/prefs.py' exists and can
be read without errors, the global preferences will be set to
the values read from the file.
"""
if os.path.isfile(pref_file):
_f, _p, _d = imp.find_module('prefs', ['/etc/pythoncad'])
if _f is not None:
try:
try:
_mod = imp.load_module('prefs', _f, _p, _d)
finally:
_f.close()
for _key in _prefkeys:
if hasattr(_mod, _key):
_prefkeys[_key](_mod)
_set_defaults(_mod)
except StandardError, e: # should print the error out
sys.stderr.write("Syntax error in %s: %s\n" % (pref_file, e))
def load_user_prefs():
"""Try and load the preferences stored in the user's preference file
load_global_prefs()
If the file '$HOME/.pythoncad/prefs.py' exists and can be read without
errors, the preferences given in that file will be used to set the
global preferences. Reading this file is conditioned upon the 'USER_PREFS'
variable being set to True.
"""
_flag = True
if globals.prefs.has_key('USER_PREFS'):
_flag = globals.prefs['USER_PREFS']
_home = None
if os.environ.has_key('HOME'):
_home = os.environ['HOME']
if _flag and _home is not None:
_pdir = os.path.join(_home, '.pythoncad')
_user_prefs = os.path.join(_pdir, 'prefs.py')
if os.path.exists(_user_prefs):
_f, _p, _d = imp.find_module('prefs', [_pdir])
if _f is not None:
try:
try:
_mod = imp.load_module('prefs', _f, _p, _d)
finally:
_f.close()
for _key in _prefkeys:
if hasattr(_mod, _key):
_prefkeys[_key](_mod)
_set_defaults(_mod)
except: # should print the error out
sys.stderr.write("Syntax error in %s\n" % _user_prefs)
|