This file is indexed.

/usr/share/pythoncad/PythonCAD/Generic/preferences.py is in pythoncad 0.1.37.0-3.

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
#
# Copyright (c) 2003, 2004, 2006 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
from PythonCAD.Generic import graphicobject

#
# global variables
#

pref_file = '/etc/pythoncad/prefs.py'

#

def _test_font_weight(val):
    _weight = None
    try:
        _weight = text.TextStyle.getWeightFromString(val)
    except:
        sys.stderr.write("invalid font weight: '" + str(val) + "'\n")
    return _weight

def _test_font_style(val):
    _style = None
    try:
        _style = text.TextStyle.getStyleFromString(val)
    except:
        sys.stderr.write("Invalid font style: '" + str(val) + "'\n")
    return _style

def _test_color(val):
    _color = None
    try:
        _c = color.Color(val)
        if _c in globals.colors:
            _color = globals.colors[_c]
        else:
            globals.colors[_c] = _c            
        if _color is None:
            _color = _c
    except:
        sys.stderr.write("Invalid color: '%s'\n" % val)
    return _color

def _test_dim_position(val):
    _pos = None
    try:
        _pos = dimension.Dimension.getPositionFromString(val)
    except:
        sys.stderr.write("Invalid dimension position: '" + str(val) + "'\n")
    return _pos

def _test_dim_endpoint(val):
    _ept = None
    try:
        _ept = dimension.Dimension.getEndpointTypeFromString(val)
    except:
        sys.stderr.write("Invalid dimension endpoint: '" + str(val) + "'\n")
    return _ept

def _test_units(val):
    _unit = None
    try:
        _unit = units.Unit.getUnitFromString(val)
    except:
        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
    #
    globals.prefs['DIMSTYLES'] = [dimension.Dimension.getDefaultDimStyle()]
    globals.prefs['DEFAULT_DIMSTYLE'] = None
    #
    # drawing and text parameters
    #
    _textstyles = [text.TextBlock.getDefaultTextStyle()]
    _textstyles.append(dimension.DimString.getDefaultTextStyle())
    globals.prefs['TEXTSTYLES'] = _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 = []
    _dst = graphicobject.GraphicObject.getDefaultStyle()
    _styles.append(_dst)
    globals.prefs['STANDARD_STYLE'] = _dst
    _st = style.Style('Solid White Line', _solid, _white, 1.0)    
    _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'] = _dst

#
# 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_dim_style(prefmod):
    _obj = prefmod.dim_style
    if isinstance(_obj, dict):
        try:
            _dstyle = _parse_dimstyle(_obj)
            globals.prefs['DIM_STYLE'] = _dstyle
            for _key in _dstyle.getOptions():
                _value = _dstyle.getOption(_key)
                globals.prefs[_key] = _value
            _color = _dstyle.getOption('DIM_COLOR')
            _colors = globals.prefs['COLORS']
            if _color not in _colors:
                _colors.append(_color)
        except StandardError, _e:
            sys.stderr.write("Invalid DimStyle: " + _e + "\n")
    else:
        sys.stderr.write("Invalid DimStyle: " + str(_obj) +"\n")

def _set_primary_font_family(prefmod):
    _family = prefmod.dim_primary_font_family
    if isinstance(_family, types.StringTypes) and _family != "":
        globals.prefs['DIM_PRIMARY_FONT_FAMILY'] = _family
    else:
        sys.stderr.write("Invalid primary font family: " + str(_family) + "\n")

def _set_primary_font_size(prefmod):
    sys.stderr.write("Variable 'dim_primary_font_size' is obsolete - use 'dim_primary_text_size'\n")

def _set_primary_text_size(prefmod):
    _size = _test_float(prefmod.dim_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.dim_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.dim_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.dim_primary_font_color)
    if _color is not None:
        globals.prefs['DIM_PRIMARY_FONT_COLOR'] = _color
        _colors = globals.prefs['COLORS']
        if _color not in _colors:
            _colors.append(_color)

def _set_primary_text_angle(prefmod):
    _angle = _test_float(prefmod.dim_primary_text_angle)
    if _angle is not None:
        globals.prefs['DIM_PRIMARY_TEXT_ANGLE'] = _angle
    else:
        sys.stderr.write("Invalid primary text angle: %g\n" % _angle)

def _set_primary_text_alignment(prefmod):
    _pta = prefmod.dim_primary_text_alignment
    try:
        _align = text.TextStyle.getAlignmentFromString(_pta)
        globals.prefs['DIM_PRIMARY_TEXT_ALIGNMENT'] = _align
    except:
        sys.stederr.write("Invalid primary text alignment: " + str(_pta))

def _set_primary_prefix(prefmod):
    _prefix = _test_unicode(prefmod.dim_primary_prefix)
    if _prefix is not None:
        globals.prefs['DIM_PRIMARY_PREFIX'] = _prefix

def _set_primary_suffix(prefmod):
    _suffix = _test_unicode(prefmod.dim_primary_suffix)
    if _suffix is not None:
        globals.prefs['DIM_PRIMARY_SUFFIX'] = _suffix

def _set_primary_precision(prefmod):
    _prec = _test_int(prefmod.dim_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.dim_primary_units)
    if _unit is not None:
        globals.prefs['DIM_PRIMARY_UNITS'] = _unit

def _set_primary_print_zero(prefmod):
    _flag = _test_boolean(prefmod.dim_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.dim_primary_trailing_decimal)
    if _flag is not None:
        globals.prefs['DIM_PRIMARY_TRAILING_DECIMAL'] = _flag

def _set_secondary_font_family(prefmod):
    _family = prefmod.dim_secondary_font_family
    if isinstance(_family, types.StringTypes) and _family != "":
        globals.prefs['DIM_SECONDARY_FONT_FAMILY'] = _family
    else:
        sys.stderr.write("Invalid secondary font family: " + str(_family) + "\n")

def _set_secondary_font_size(prefmod):
    sys.stderr.write("Variable 'dim_secondary_font_size' is obsolete - use 'dim_secondary_text_size'\n")

def _set_secondary_text_size(prefmod):
    _size = _test_float(prefmod.dim_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.dim_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.dim_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.dim_secondary_font_color)
    if _color is not None:
        globals.prefs['DIM_SECONDARY_FONT_COLOR'] = _color
        _colors = globals.prefs['COLORS']
        if _color not in _colors:
            _colors.append(_color)

def _set_secondary_text_angle(prefmod):
    _angle = _test_float(prefmod.dim_secondary_text_angle)
    if _angle is not None:
        globals.prefs['DIM_SECONDARY_TEXT_ANGLE'] = _angle
    else:
        sys.stderr.write("Invalid secondary text angle: %g\n" % _angle)

def _set_secondary_text_alignment(prefmod):
    _sta = prefmod.dim_secondary_text_alignment
    try:
        _align = text.TextStyle.getAlignmentFromString(_sta)
        globals.prefs['DIM_SECONDARY_TEXT_ALIGNMENT'] = _align
    except:
        sys.stederr.write("Invalid secondary text alignment: " + str(_sta))

def _set_secondary_prefix(prefmod):
    _prefix = _test_unicode(prefmod.dim_secondary_prefix)
    if _prefix is not None:
        globals.prefs['DIM_SECONDARY_PREFIX'] = _prefix

def _set_secondary_suffix(prefmod):
    _suffix = _test_unicode(prefmod.dim_secondary_suffix)
    if _suffix is not None:
        globals.prefs['DIM_SECONDARY_SUFFIX'] = _suffix

def _set_secondary_precision(prefmod):
    _prec = _test_int(prefmod.dim_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.dim_secondary_units)
    if _unit is not None:
        globals.prefs['DIM_SECONDARY_UNITS'] = _unit

def _set_secondary_print_zero(prefmod):
    _flag = _test_boolean(prefmod.dim_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.dim_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:
        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'] = _t

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_position_offset(prefmod):
    _offset = _test_float(prefmod.dim_position_offset)
    if _offset is not None:
        globals.prefs['DIM_POSITION_OFFSET'] = _offset
    else:
        sys.stderr.write("Invalid dim position offset: '%s'\n" % str(prefmod.dim_position_offset))

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_dim_dual_mode_offset(prefmod):
    _offset = _test_float(prefmod.dim_dual_mode_offset)
    if _offset is not None:
        globals.prefs['DIM_DUAL_MODE_OFFSET'] = _offset
    else:
        sys.stderr.write("Invalid dim dual mode offset: '%s'\n" % str(prefmod.dim_dual_mode_offset))

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_text_style(prefmod):
    _obj = prefmod.text_style
    if isinstance(_obj, dict):
        try:
            _tstyle = _parse_textstyle(_obj)
            globals.prefs['TEXT_STYLE'] = _tstyle
            globals.prefs['FONT_WEIGHT'] = _tstyle.getWeight()
            globals.prefs['FONT_STYLE'] = _tstyle.getStyle()
            globals.prefs['FONT_FAMILY'] = _tstyle.getFamily()
            globals.prefs['TEXT_SIZE'] = _tstyle.getSize()
            globals.prefs['TEXT_ANGLE'] = _tstyle.getAngle()
            globals.prefs['TEXT_ALIGNMENT'] = _tstyle.getAlignment()
            _color = _tstyle.getColor()
            _colors = globals.prefs['COLORS']
            if _color not in _colors:
                _colors.append(_color)
            globals.prefs['FONT_COLOR'] = _color
        except StandardError, _e:
            sys.stderr.write("Invalid TextStyle: " + _e + "\n")
    else:
        sys.stderr.write("Invalid TextStyle: " + str(_obj) +"\n")

def _set_font_family(prefmod):
    _family = prefmod.font_family
    if isinstance(_family, types.StringTypes) and _family != "":
        globals.prefs['FONT_FAMILY'] = _family
    else:
        sys.stderr.write("Invalid font family: " + str(_family) + "\n")

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):
    sys.stderr.write("Variable 'font_size' is obsolete - use 'text_size'\n")

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:
        globals.prefs['FONT_COLOR'] = _color
        _colors = globals.prefs['COLORS']
        if _color not in _colors:
            _colors.append(_color)

def _set_text_angle(prefmod):
    _angle = _test_float(prefmod.text_size)
    if _angle is not None:
        globals.prefs['TEXT_ANGLE'] = _angle
    else:
        sys.stderr.write("Invalid text angle: %g\n" % _angle)

def _set_text_alignment(prefmod):
    _ta = prefmod.text_alignment
    try:
        _align = text.TextStyle.getAlignmentFromString(_ta)
        globals.prefs['TEXT_ALIGNMENT'] = _align
    except:
        sys.stederr.write("Invalid text alignment: " + str(_ta))

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_style(prefmod):
    _obj = prefmod.line_style
    if isinstance(_obj, dict):
        try:
            _style = _parse_style(_obj)
            globals.prefs['LINE_STYLE'] = _style
            _lt = _style.getLinetype()
            globals.prefs['LINE_TYPE'] = _lt
            _lts = globals.prefs['LINETYPES']
            if _lt not in _lts:
                _lts.append(_linetype)
            _color = _style.getColor()
            globals.prefs['LINE_COLOR'] = _color
            _colors = globals.prefs['COLORS']
            if _color not in _colors:
                _colors.append(_color)
            globals.prefs['LINE_THICKNESS'] = _style.getThickness()
        except StandardError, _e:
            sys.stderr.write("Invalid Style: " + _e + "\n")
    else:
        sys.stderr.write("Invalid Style: " + str(_obj) +"\n")
        
def _set_line_type(prefmod):
    _obj = prefmod.line_type
    if isinstance(_obj, tuple) and len(_obj) == 2:
        _name, _dlist = _obj
        try:
            _lt = linetype.Linetype(_name, _dlist)
            globals.prefs['LINE_TYPE'] = _lt
            _lts = globals.prefs['LINETYPES']
            if _lt not in _lt:
                _lts.append(_linetype)
        except:
            sys.stderr.write("Invalid linetype: '" + str(_obj) + "'\n")
    else:
        sys.stderr.write("Invalid linetype tuple: '" + str(_obj) + "'\n")

def _set_line_color(prefmod):
    _color = _test_color(prefmod.line_color)
    if _color is not None:
        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" % _t)
        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:
        globals.prefs['INACTIVE_LAYER_COLOR'] = _color

def _set_background_color(prefmod):
    _color = _test_color(prefmod.background_color)
    if _color is not None:
        globals.prefs['BACKGROUND_COLOR'] = _color

def _set_single_point_color(prefmod):
    _color = _test_color(prefmod.single_point_color)
    if _color is not None:
        globals.prefs['SINGLE_POINT_COLOR'] = _color

def _set_multi_point_color(prefmod):
    _color = _test_color(prefmod.multi_point_color)
    if _color is not None:
        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) or 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) and len(_obj) == 3:
                _r, _g, _b = _obj
                try:
                    _color = color.Color(_r, _g, _b)
                except:
                    sys.stderr.write("Invalid color: '" + str(_obj) + "'\n")
            else:
                sys.stderr.write("Invalid color: '" + str(_obj) + "'\n")
            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 _parse_dimstyle(sd):
    if not isinstance(sd, dict):
        raise TypeError, "Invalid style dictionary: " + `type(sd)`
    _name = None
    _ds = {}
    for _key in sd.keys():
        _v = sd[_key]
        _k = _key.lower()
        if _k == 'name':
            if not isinstance(_v, types.StringTypes):
                raise TypeError, "Invalid DimStyle name type: " + `type(_v)`
            _name = _v
        elif (_k == 'dim_color' or
              _k == 'dim_primary_font_color' or
              _k == 'dim_secondary_font_color'):
            _col = _test_color(_v)
            if _col is None:
                raise ValueError, "Invalid value '%s' for DimStyle key '%s'" % (str(_v), _k)
            _v = _col
        elif (_k == 'dim_primary_font_family' or
              _k == 'dim_secondary_font_family'):
            if not isinstance(_v, types.StringTypes):
                raise TypeError, "Invalid type '%s' for DimStyle key '%s'\n " % (`type(_v)`, _k)
            if _v == "":
                raise ValueError, "Font family for '%s' cannot be empty" % _k
        elif (_k == 'dim_offset' or
              _k == 'dim_dual_mode_offset' or
              _k == 'dim_position_offset' or
              _k == 'dim_thickness' or
              _k == 'dim_endpoint_size' or
              _k == 'dim_primary_text_size' or
              _k == 'dim_secondary_text_size' or
              _k == 'dim_extension'):
            if not isinstance(_v, float):
                raise TypeError, "Invalid type '%s' for DimStyle key '%s'\n " % (`type(_v)`, _k)
            if _v < 0.0:
                raise ValueError, "Invalid value %f for DimStyle key '%s'\n " % (_v, _k)
        elif (_k == 'dim_primary_precision' or
              _k == 'dim_secondary_precision'):
            if not isinstance(_v, int):
                raise TypeError, "Invalid type '%s' for DimStyle key '%s'\n " % (`type(_v)`, _k)
            if _v < 0 or _v > 15:
                raise ValueError, "Invalid value %d for DimStyle key '%s'\n " % (_v, _k)
        elif (_k == 'dim_primary_font_weight' or
              _k == 'dim_secondary_font_weight'):
            try:
                _weight = text.TextStyle.getWeightFromString(_v)
            except:
                raise ValueError, "Invalid value '%s' for DimStyle key '%s'\n " % (_v, _k)
            _v = _weight
        elif (_k == 'dim_primary_text_alignment' or
              _k == 'dim_secondary_text_alignment'):
            try:
                _align = text.TextStyle.getAlignmentFromString(_v)
            except:
                raise ValueError, "Invalid value '%s' for DimStyle key '%s'\n " % (_v, _k)
            _v = _align
        elif (_k == 'dim_primary_font_style' or
              _k == 'dim_secondary_font_style'):
            try:
                _style = text.TextStyle.getStyleFromString(_v)
            except:
                raise ValueError, "Invalid value '%s' for DimStyle key '%s'\n " % (_v, _k)
            _v = _style
        elif (_k == 'dim_primary_text_angle' or
              _k == 'dim_secondary_text_angle'):
            _angle = _test_float(_v)
            if _angle is None:
                raise ValueError, "Invalid value %d for DimStyle key '%s'\n " % (_v, _k)
            _v = _angle
        elif (_k == 'dim_primary_units' or
              _k == 'dim_secondary_units'):
            try:
                _unit = units.Unit.getUnitFromString(_v)
            except:
                raise ValueError, "Invalid value '%s' for DimStyle key '%s'\n " % (_v, _k)
            _v = _unit
        elif _k == 'dim_position':
            try:
                _pos = dimension.Dimension.getPositionFromString(_v)
            except:
                raise ValueError, "Invalid value '%s' for DimStyle key '%s'\n " % (_v, _k)
            _v = _pos
        elif _k == 'dim_endpoint':
            try:
                _ept = dimension.Dimension.getEndpointTypeFromString(_v)
            except:
                raise ValueError, "Invalid value '%s' for DimStyle key '%s'\n " % (_v, _k)
            _v = _ept
        elif (_k == 'dim_primary_leading_zero' or
              _k == 'dim_secondary_leading_zero' or
              _k == 'dim_primary_trailing_decimal' or
              _k == 'dim_secondary_trailing_decimal' or
              _k == 'dim_dual_mode' or
              _k == 'radial_dim_dia_mode'):
            _flag = _test_boolean(_v)
            if _flag is None:
                raise ValueError, "Invalid value '%s' for DimStyle key '%s'\n " % (str(_v), _k)
            _v = _flag
        elif (_k == 'dim_primary_prefix' or
              _k == 'dim_primary_suffix' or
              _k == 'dim_secondary_prefix' or
              _k == 'dim_secondary_suffix' or
              _k == 'radial_dim_primary_prefix' or
              _k == 'radial_dim_primary_suffix' or
              _k == 'radial_dim_secondary_prefix' or
              _k == 'radial_dim_secondary_suffix' or
              _k == 'angular_dim_primary_prefix' or
              _k == 'angular_dim_primary_suffix' or
              _k == 'angular_dim_secondary_prefix' or
              _k == 'angular_dim_secondary_suffix'):
            if not isinstance(_v, types.StringTypes):
                raise TypeError, "Invalid type '%s' for DimStyle key '%s'\n " % (`type(_v)`, _k)
        elif _k == 'dim_primary_font_size':
            sys.stderr.write("Key 'dim_primary_font_size' obsoleted by 'dim_primary_text_size'\n")
            continue
        elif _k == 'dim_secondary_font_size':
            sys.stderr.write("Key 'dim_secondary_font_size' obsoleted by 'dim_secondary_text_size'\n")
            continue
        else:
            raise ValueError, "Unknown DimStyle option: %s" % _k
        if _k != 'name':
            _ds[_key.upper()] = _v
    if _name is None:
        raise ValueError, "DimStyle missing 'name' field: " + str(sd)
    return dimension.DimStyle(_name, _ds)
    
def _parse_textstyle(sd):
    if not isinstance(sd, dict):
        raise TypeError, "Invalid style dictionary: " + `type(sd)`
    _name = sd.get('name')
    if _name is None:
        raise ValueError, "TextStyle missing 'name' field: " + str(sd)
    if not isinstance(_name, types.StringTypes):
        raise TypeError, "Invalid textstyle name type: " + `type(_name)`
    _family = sd.get('family')
    if _family is None:
        raise ValueError, "TextStyle missing 'family' field: " + str(sd)
    if not isinstance(_family, types.StringTypes):
        raise TypeError, "Invalid textstyle family type: " + `type(_family)`
    _val = sd.get('style')
    if _val is None:
        raise TypeError, "TextStyle missing 'style' field: " + str(sd)
    _style = text.TextStyle.getStyleFromString(_val)
    _val = sd.get('weight')
    if _val is None:
        raise TypeError, "TextStyle missing 'weight' field: " + str(sd)
    _weight = text.TextStyle.getWeightFromString(_val)
    _val = sd.get('color')
    if _val is None:
        raise ValueError, "Style missing 'color' field: " + str(sd)
    _color = _test_color(_val)
    if _color is None:
        raise ValueError, "Invalid TextStyle color: " + str(_val)
    _size = sd.get('size')
    if _size is None:
        raise ValueError, "TextStyle missing 'size' field: " + str(sd)
    _angle = sd.get('angle')
    if _angle is None:
        raise ValueError, "TextSytle missing 'angle' field: " + str(sd)
    _val = sd.get('alignment')
    if _val is None:
        raise TypeError, "TextStyle missing 'alignment' field: " + str(sd)
    _align = text.TextStyle.getAlignmentFromString(_val)
    return text.TextStyle(_name, family=_family, style=_style,
                          weight=_weight, color=_color, size=_size,
                          angle=_angle, align=_align)

def _parse_style(sd):
    if not isinstance(sd, dict):
        raise TypeError, "Invalid style dictionary: " + `type(sd)`
    _n = sd.get('name')
    if _n is None:
        raise ValueError, "Style missing 'name' field: " + str(sd)
    if not isinstance(_n, types.StringTypes):
        raise TypeError, "Invalid style name type: " + `type(_n)`
    _l =  sd.get('linetype')
    if _l is None:
        raise ValueError, "Style missing 'linetype' field: " + str(sd)
    if not isinstance(_l, tuple):
        raise TypeError, "Invalid linetype tuple: " + `type(_l)`
    if len(_l) != 2:
        raise ValueError, "Invalid linetype tuple length: " + str(_l)
    _ln = _l[0]
    if not isinstance(_ln, types.StringTypes):
        raise TypeError, "Invalid style linetype name type: " + `type(_ln)`
    _ld = _l[1]
    if _ld is not None and not isinstance(_ld, list):
        raise TypeError, "Invalid style linetype dashlist type: " + `type(_ld)`
    _lt = linetype.Linetype(_ln, _ld)
    _c = sd.get('color')
    if _c is None:
        raise ValueError, "Style missing 'color' field: " + str(sd)
    _col = _test_color(_c)
    if _col is None:
        raise ValueError, "Invalid Style color: " + str(_c)
    _t = sd.get('thickness')
    if _t is None:
        raise ValueError, "Style missing 'thickness' field: " + str(sd)
    return style.Style(_n, _lt, _col, _t)
    
def _set_styles(prefmod):
    _slist = prefmod.styles
    _styles = globals.prefs['STYLES']
    _linetypes = globals.prefs['LINETYPES']
    if isinstance(_slist, list):
        for _obj in _slist:
            _style = None
            if isinstance(_obj, dict):
                try:
                    _style = _parse_style(_obj)
                except:
                    sys.stderr.write("Invalid style: %s\n" % str(_obj))
            elif isinstance(_obj, tuple):
                sys.stderr.write("Tuple based Style definition is deprecated: '" + str(_obj) + "'\n")
                if len(_obj) != 4:
                    sys.stderr.write("Invalid tuple-based style entry: '" + str(_obj) + "'\n")
                    continue
                _sd =  {
                    'name' : _obj[0],
                    'linetype' : _obj[1],
                    'color' : _obj[2],
                    'thickness' : _obj[3]
                    }
                try:
                    _style = _parse_style(_sd)
                except StandardError, _e:
                    sys.stderr.write("Invalid style: %s, %e\n" % (str(_obj), _e))
            else:
                sys.stderr.write("Unexpected style type: %s" % `type(_obj)`)
            if _style is None:
                continue
            if _style not in _styles:
                _styles.append(_style)
            _linetype = _style.getLinetype()
            if _linetype not in _linetypes:
                _linetypes.append(_linetype)
    else:
        sys.stderr.write("Invalid style list: '" + str(_slist) + "'\n")

def _set_dimstyles(prefmod):
    _dslist = prefmod.dimstyles
    _dimstyles = globals.prefs['DIMSTYLES']
    if isinstance(_dslist, list):
        for _obj in _dslist:
            _dimstyle = None
            if not isinstance(_obj, tuple) or len(_obj) != 2:
                sys.stderr.write("Invalid DimStyle entry: '" + str(_obj) + "'\n")
                continue
            _name, _dsdict = _obj
            if not isinstance(_name, types.StringTypes):
                sys.stderr.write("Invalid DimStyle name: '" + str(_name) + "'\n")
                continue
            if not isinstance(_dsdict, dict):
                sys.stderr.write("Invalid DimStyle dictionary: '" + str(_dsdict) + "'\n")
                continue
            _dsdict['name'] = _name
            try:
                _dimstyle = _parse_dimstyle(_dsdict)
            except StandardError, _e:
                sys.stderr.write("Invalid DimStyle: " + _e)
            if _dimstyle is not None and _dimstyle not in _dimstyles:
                _dimstyles.append(_dimstyle)
    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:
            _textstyle = None
            if isinstance(_obj, dict):
                try:
                    _textstyle = _parse_textstyle(_obj)
                except StandardError, _e:
                    sys.stderr.write("Invalid TextStyle: " + _e)
            elif isinstance(_obj, tuple):
                sys.stderr.write("Tuple based TextStyle definition is deprecated: '" + str(_obj) + "'\n")                
                if len(_obj) != 6:
                    sys.stderr.write("Invalid tuple based TextStyle entry: '" + str(_obj) + "'\n")
                    continue
                #
                # tuple-based definition lacked angle and alignment fields,
                # so we add them with reasonable values
                #
                _td = {
                    'name' : _obj[0],
                    'family' : _obj[1],
                    'size' : _obj[2],
                    'style' : _obj[3],
                    'weight' : _obj[4],
                    'color' : _obj[5],
                    'angle' : 0.0, 
                    'alignment' : 'left'
                    }
                try:
                    _textstyle = _parse_textstyle(_td)
                except StandardError, _e:
                    sys.stderr.write("Invalid TextSyle: %s, %e\n" % (str(_obj), _e))
            if _textstyle is not None and _textstyle not in _textstyles:
                _textstyles.append(_textstyle)
    else:
        sys.stderr.write("Invalid text style list: '" + str(_tslist) + "'\n")

#
# this list of tuples stores the tested attributes in the 'prefs.py' module(s)
# and the function used to validate the value given by the user
#

_preflist = [
    #
    # booleans
    #
    ('user_prefs', _set_user_prefs),
    ('autosplit', _set_autosplit),
    ('highlight_points', _set_highlight_points),
    #
    # floats
    #
    ('leader_arrow_size', _set_leader_arrow_size),
    ('chamfer_length', _set_chamfer_length),
    ('fillet_radius', _set_fillet_radius),
    #
    # display colors
    #
    ('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),
    #
    # units
    #
    ('units', _set_units),
    #
    # GraphicObject entity parameters
    #
    ('line_style', _set_line_style),
    ('line_type', _set_line_type),
    ('line_color', _set_line_color),
    ('line_thickness', _set_line_thickness),
    #
    # TextBlock parameters
    #
    ('text_style', _set_text_style),
    ('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),
    ('text_angle', _set_text_angle),
    ('text_alignment', _set_text_alignment),
    #
    # Dimension paramters
    #
    ('dim_style', _set_dim_style),
    ('dim_primary_font_family', _set_primary_font_family),
    ('dim_primary_font_size', _set_primary_font_size),
    ('dim_primary_text_size', _set_primary_text_size),
    ('dim_primary_font_weight', _set_primary_font_weight),
    ('dim_primary_font_style', _set_primary_font_style),
    ('dim_primary_font_color', _set_primary_font_color),
    ('dim_primary_text_angle', _set_primary_text_angle),
    ('dim_primary_text_alignment',_set_primary_text_alignment),
    ('dim_primary_prefix', _set_primary_prefix),
    ('dim_primary_suffix', _set_primary_suffix),
    ('dim_primary_precision', _set_primary_precision),
    ('dim_primary_units', _set_primary_units),
    ('dim_primary_leading_zero', _set_primary_print_zero),
    ('dim_primary_trailing_decimal', _set_primary_trail_decimal),
    ('dim_secondary_font_family', _set_secondary_font_family),
    ('dim_secondary_font_size', _set_secondary_font_size),
    ('dim_secondary_text_size', _set_secondary_text_size),
    ('dim_secondary_font_weight', _set_secondary_font_weight),
    ('dim_secondary_font_style', _set_secondary_font_style),
    ('dim_secondary_font_color', _set_secondary_font_color),
    ('dim_secondary_text_angle', _set_secondary_text_angle),
    ('dim_secondary_text_alignment', _set_secondary_text_alignment),
    ('dim_secondary_prefix', _set_secondary_prefix),
    ('dim_secondary_suffix', _set_secondary_suffix),
    ('dim_secondary_precision', _set_secondary_precision),
    ('dim_secondary_units', _set_secondary_units),
    ('dim_secondary_leading_zero', _set_secondary_print_zero),
    ('dim_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_position_offset', _set_dim_position_offset),
    ('dim_endpoint', _set_dim_endpoint),
    ('dim_endpoint_size', _set_dim_endpoint_size),
    ('dim_dual_mode', _set_dim_dual_mode),
    ('dim_dual_mode_offset', _set_dim_dual_mode_offset),
    ('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),
    #
    # lists of object types to be loaded in at startup
    #
    ('colors', _set_colors),
    ('linetypes', _set_linetypes),
    ('styles', _set_styles),
    ('textstyles', _set_textstyles),
    ('dimstyles', _set_dimstyles)
    ]

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']:
                if _style.getName() == _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.
    """
    try:
        _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 _attr, _func in _preflist:
                    if hasattr(_mod, _attr):
                        _func(_mod)
                _set_defaults(_mod)
                del sys.modules['prefs']
            except StandardError, _e: # should print the error out
                sys.stderr.write("Syntax error in %s: %s\n" % (_p, _e))
    except ImportError:
        pass
    except StandardError, _e:
        sys.stderr.write("Error loading global preferences: %s" % _e)

def load_user_prefs():
    """Try and load the preferences stored in the user's preference file

load_user_prefs()

If the file '$HOME/.pythoncad/prefs.py' ('$APPDATA/PythonCAD/prefs.py' on
Windows) 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']
    _pdir = None
    if sys.platform == 'win32' and os.environ.has_key('APPDATA'):
        _pdir = os.path.join(os.environ.get('APPDATA'), 'PythonCAD')
    else:
        if os.environ.has_key('HOME'):
            _pdir = os.path.join(os.environ['HOME'], '.pythoncad')
    if _flag and _pdir is not None:
        try:
            _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 _attr, _func in _preflist:
                        if hasattr(_mod, _attr):
                            _func(_mod)
                    del sys.modules['prefs']
                except StandardError, _e: # should print the error out
                    sys.stderr.write("Syntax error in %s: %s\n" % (_p, _e))
        except ImportError:
            pass
        except StandardError, _e:
            sys.stderr.write("Error loading user preferences: %s" % _e)

def _save_dimstyle_values(f):
    f.write("#\n# Standard Dimension parameters\n#\n")
    _gp = globals.prefs
    _tc = text.TextStyle
    _dim = dimension.Dimension
    _uc = units.Unit
    _ds = _gp['DIM_STYLE']
    f.write("dim_style = {\n")
    f.write("    'name' : '%s',\n" % _ds.getName())
    _val = _ds.getOption('DIM_PRIMARY_FONT_FAMILY')
    f.write("    'dim_primary_font_family' : '%s',\n" % _val)
    _val = _ds.getOption('DIM_PRIMARY_TEXT_SIZE')
    f.write("    'dim_primary_text_size' : %s,\n" % repr(_val))
    _val = _ds.getOption('DIM_PRIMARY_FONT_WEIGHT')
    f.write("    'dim_primary_font_weight': '%s',\n" % _tc.getWeightAsString(_val))
    _val = _ds.getOption('DIM_PRIMARY_FONT_STYLE')
    f.write("    'dim_primary_font_style' : '%s',\n" % _tc.getStyleAsString(_val))
    _val = _ds.getOption('DIM_PRIMARY_FONT_COLOR')
    f.write("    'dim_primary_font_color' : '%s',\n" % str(_val))
    _val = _ds.getOption('DIM_PRIMARY_TEXT_ANGLE')
    f.write("    'dim_primary_text_angle' : %s,\n" % repr(_val))
    _val = _ds.getOption('DIM_PRIMARY_TEXT_ALIGNMENT')
    f.write("    'dim_primary_text_alignment' : '%s',\n" % _tc.getAlignmentAsString(_val))
    _val = _ds.getOption('DIM_PRIMARY_PREFIX')
    f.write("    'dim_primary_prefix' : %s,\n" % repr(_val))
    _val = _ds.getOption('DIM_PRIMARY_SUFFIX')
    f.write("    'dim_primary_suffix' : %s,\n" % repr(_val))
    _val = _ds.getOption('DIM_PRIMARY_PRECISION')
    f.write("    'dim_primary_precision' : %d,\n" % _val)
    _val = _ds.getOption('DIM_PRIMARY_UNITS')
    f.write("    'dim_primary_units' : '%s',\n" % _uc.getUnitAsString(_val))
    _val = _ds.getOption('DIM_PRIMARY_LEADING_ZERO')
    f.write("    'dim_primary_leading_zero' : %s,\n" % repr(_val))
    _val = _ds.getOption('DIM_PRIMARY_TRAILING_DECIMAL')
    f.write("    'dim_primary_trailing_decimal' : %s,\n" % repr(_val))
    _val = _ds.getOption('DIM_SECONDARY_FONT_FAMILY')
    f.write("    'dim_secondary_font_family' : '%s',\n" % _val)
    _val = _ds.getOption('DIM_SECONDARY_TEXT_SIZE')
    f.write("    'dim_secondary_text_size' : %s,\n" % repr(_val))
    _val = _ds.getOption('DIM_SECONDARY_FONT_WEIGHT')
    f.write("    'dim_secondary_font_weight' : '%s',\n" % _tc.getWeightAsString(_val))
    _val = _ds.getOption('DIM_SECONDARY_FONT_STYLE')
    f.write("    'dim_secondary_font_style' : '%s',\n" % _tc.getStyleAsString(_val))
    _val = _ds.getOption('DIM_SECONDARY_FONT_COLOR')
    f.write("    'dim_secondary_font_color' : '%s',\n" % str(_val))
    _val = _ds.getOption('DIM_SECONDARY_TEXT_ANGLE')
    f.write("    'dim_secondary_text_angle' : %s,\n" % repr(_val))
    _val = _ds.getOption('DIM_SECONDARY_TEXT_ALIGNMENT')
    f.write("    'dim_secondary_text_alignment' : '%s',\n" % _tc.getAlignmentAsString(_val))
    _val = _ds.getOption('DIM_SECONDARY_PREFIX')
    f.write("    'dim_secondary_prefix' : %s,\n" % repr(_val))
    _val = _ds.getOption('DIM_SECONDARY_SUFFIX')
    f.write("    'dim_secondary_suffix' : %s,\n" % repr(_val))
    _val = _ds.getOption('DIM_SECONDARY_PRECISION')
    f.write("    'dim_secondary_precision' : %d,\n" % _val)
    _val = _ds.getOption('DIM_SECONDARY_UNITS')
    f.write("    'dim_secondary_units' : '%s',\n" % _uc.getUnitAsString(_val))
    _val = _ds.getOption('DIM_SECONDARY_LEADING_ZERO')
    f.write("    'dim_secondary_leading_zero' : %s,\n" % repr(_val))
    _val = _ds.getOption('DIM_SECONDARY_TRAILING_DECIMAL')
    f.write("    'dim_secondary_trailing_decimal' : %s,\n" % repr(_val))
    _val = _ds.getOption('DIM_OFFSET')
    f.write("    'dim_offset' : %s,\n" % repr(_val))
    _val = _ds.getOption('DIM_EXTENSION')
    f.write("    'dim_extension' : %s,\n" % repr(_val))
    _val = _ds.getOption('DIM_COLOR')
    f.write("    'dim_color' : '%s',\n" % str(_val))
    _val = _ds.getOption('DIM_THICKNESS')
    f.write("    'dim_thickness' : %s,\n" % repr(_val))
    _val = _ds.getOption('DIM_POSITION')
    f.write("    'dim_position' : '%s',\n" % _dim.getPositionAsString(_val))
    _val = _ds.getOption('DIM_ENDPOINT')
    f.write("    'dim_endpoint' : '%s',\n" % _dim.getEndpointTypeAsString(_val))
    _val = _ds.getOption('DIM_ENDPOINT_SIZE')
    f.write("    'dim_endpoint_size' : %s,\n" % repr(_val))
    _val = _ds.getOption('DIM_DUAL_MODE')
    f.write("    'dim_dual_mode' : %s,\n" % repr(_val))
    _val = _ds.getOption('DIM_POSITION_OFFSET')
    f.write("    'dim_position_offset' : %s,\n" % repr(_val))
    _val = _ds.getOption('DIM_DUAL_MODE_OFFSET')
    f.write("    'dim_dual_mode_offset' : %s,\n" % repr(_val))
    _val = _ds.getOption('RADIAL_DIM_PRIMARY_PREFIX')
    f.write("    'radial_dim_primary_prefix' : %s,\n" % repr(_val))
    _val = _ds.getOption('RADIAL_DIM_PRIMARY_SUFFIX')
    f.write("    'radial_dim_primary_suffix' : %s,\n" % repr(_val))
    _val = _ds.getOption('RADIAL_DIM_SECONDARY_PREFIX')
    f.write("    'radial_dim_secondary_prefix' : %s,\n" % repr(_val))
    _val = _ds.getOption('RADIAL_DIM_SECONDARY_SUFFIX')
    f.write("    'radial_dim_secondary_suffix' : %s,\n" % repr(_val))
    _val = _ds.getOption('RADIAL_DIM_DIA_MODE')
    f.write("    'radial_dim_dia_mode' : %s,\n" % repr(_val))
    _val = _ds.getOption('ANGULAR_DIM_PRIMARY_PREFIX')
    f.write("    'angular_dim_primary_prefix' : %s,\n" % repr(_val))
    _val = _ds.getOption('ANGULAR_DIM_PRIMARY_SUFFIX')
    f.write("    'angular_dim_primary_suffix' : %s,\n" % repr(_val))
    _val = _ds.getOption('ANGULAR_DIM_SECONDARY_PREFIX')
    f.write("    'angular_dim_secondary_prefix' : %s,\n" % repr(_val))
    _val = _ds.getOption('ANGULAR_DIM_SECONDARY_SUFFIX')
    f.write("    'angular_dim_secondary_suffix' : %s\n" % repr(_val))
    f.write("}\n")
    #
    # save overriden values
    #
    _val = _gp['DIM_PRIMARY_FONT_FAMILY']
    if _val != _ds.getOption('DIM_PRIMARY_FONT_FAMILY'):
        f.write("dim_primary_font_family = '%s'\n" % _val)
    _val = _gp['DIM_PRIMARY_TEXT_SIZE']
    if abs(_val - _ds.getOption('DIM_PRIMARY_TEXT_SIZE')) > 1e-10:
        f.write("dim_primary_text_size = %s\n" % repr(_val))
    _val = _gp['DIM_PRIMARY_FONT_WEIGHT']
    if _val != _ds.getOption('DIM_PRIMARY_FONT_WEIGHT'):
        f.write("dim_primary_font_weight = '%s'\n" % _tc.getWeightAsString(_val))
    _val = _gp['DIM_PRIMARY_FONT_STYLE']
    if _val != _ds.getOption('DIM_PRIMARY_FONT_STYLE'):
        f.write("dim_primary_font_style = '%s'\n" % _tc.getStyleAsString(_val))
    _val = _gp['DIM_PRIMARY_FONT_COLOR']
    if _val != _ds.getOption('DIM_PRIMARY_FONT_COLOR'):
        f.write("dim_primary_font_color = '%s'\n" % str(_val))
    _val = _gp['DIM_PRIMARY_TEXT_ANGLE']
    if abs(_val - _ds.getOption('DIM_PRIMARY_TEXT_ANGLE')) > 1e-10:
        f.write("dim_primary_text_angle = %s\n" % repr(_val))
    _val = _gp['DIM_PRIMARY_TEXT_ALIGNMENT']
    if _val != _ds.getOption('DIM_PRIMARY_TEXT_ALIGNMENT'):
        f.write("dim_primary_text_alignment = '%s'\n" % _tc.getAlignmentAsString(_val))
    _val = _gp['DIM_PRIMARY_PREFIX']
    if _val != _ds.getOption('DIM_PRIMARY_PREFIX'):
        f.write("dim_primary_prefix = %s\n" % repr(_val))
    _val = _gp['DIM_PRIMARY_SUFFIX']
    if _val != _ds.getOption('DIM_PRIMARY_SUFFIX'):
        f.write("dim_primary_suffix = %s\n" % repr(_val))
    _val = _gp['DIM_PRIMARY_PRECISION']
    if _val != _ds.getOption('DIM_PRIMARY_PRECISION'):
        f.write("dim_primary_precision = %d\n" % _val)
    _val = _gp['DIM_PRIMARY_UNITS']
    if _val != _ds.getOption('DIM_PRIMARY_UNITS'):
        f.write("dim_primary_units = '%s'\n" % _uc.getUnitAsString(_val))
    _val = _gp['DIM_PRIMARY_LEADING_ZERO']
    if _val is not _ds.getOption('DIM_PRIMARY_LEADING_ZERO'):
        f.write("dim_primary_leading_zero = %s\n" % repr(_val))
    _val = _gp['DIM_PRIMARY_TRAILING_DECIMAL']
    if _val is not _ds.getOption('DIM_PRIMARY_TRAILING_DECIMAL'):
        f.write("dim_primary_trailing_decimal = %s\n" % repr(_val))
    _val = _gp['DIM_SECONDARY_FONT_FAMILY']
    if _val != _ds.getOption('DIM_SECONDARY_FONT_FAMILY'):
        f.write("dim_secondary_font_family = '%s'\n" % _val)
    _val = _gp['DIM_SECONDARY_TEXT_SIZE']
    if abs(_val - _ds.getOption('DIM_SECONDARY_TEXT_SIZE')) > 1e-10:
        f.write("dim_secondary_text_size = %s\n" % repr(_val))
    _val = _gp['DIM_SECONDARY_FONT_WEIGHT']
    if _val != _ds.getOption('DIM_SECONDARY_FONT_WEIGHT'):
        f.write("dim_secondary_font_weight = '%s'\n" % _tc.getWeightAsString(_val))
    _val = _gp['DIM_SECONDARY_FONT_STYLE']
    if _val != _ds.getOption('DIM_SECONDARY_FONT_STYLE'):
        f.write("dim_secondary_font_style = '%s'\n" % _tc.getStyleAsString(_val))
    _val = _gp['DIM_SECONDARY_FONT_COLOR']
    if _val != _ds.getOption('DIM_SECONDARY_FONT_COLOR'):
        f.write("dim_secondary_font_color = '%s'\n" % str(_val))
    _val = _gp['DIM_SECONDARY_TEXT_ANGLE']
    if abs(_val - _ds.getOption('DIM_SECONDARY_TEXT_ANGLE')) > 1e-10:
        f.write("dim_secondary_text_angle = %s\n" % repr(_val))
    _val = _gp['DIM_SECONDARY_TEXT_ALIGNMENT']
    if _val != _ds.getOption('DIM_SECONDARY_TEXT_ALIGNMENT'):
        f.write("dim_secondary_text_alignment = '%s'\n" % _tc.getAlignmentAsString(_val))
    _val = _gp['DIM_SECONDARY_PREFIX']
    if _val != _ds.getOption('DIM_SECONDARY_PREFIX'):
        f.write("dim_secondary_prefix = %s\n" % repr(_val))
    _val = _gp['DIM_SECONDARY_SUFFIX']
    if _val != _ds.getOption('DIM_SECONDARY_SUFFIX'):
        f.write("dim_secondary_suffix = %s\n" % repr(_val))
    _val = _gp['DIM_SECONDARY_PRECISION']
    if _val != _ds.getOption('DIM_SECONDARY_PRECISION'):
        f.write("dim_secondary_precision = %d\n" % _val)
    _val = _gp['DIM_SECONDARY_UNITS']
    if _val != _ds.getOption('DIM_SECONDARY_UNITS'):
        f.write("dim_secondary_units = '%s'\n" % _uc.getUnitAsString(_val))
    _val = _gp['DIM_SECONDARY_LEADING_ZERO']
    if _val is not _ds.getOption('DIM_SECONDARY_LEADING_ZERO'):
        f.write("dim_secondary_leading_zero = %s\n" % repr(_val))
    _val = _gp['DIM_SECONDARY_TRAILING_DECIMAL']
    if _val is not _ds.getOption('DIM_SECONDARY_TRAILING_DECIMAL'):
        f.write("dim_secondary_trailing_decimal = %s\n" % repr(_val))
    _val = _gp['DIM_OFFSET']
    if abs(_val - _ds.getOption('DIM_OFFSET')) > 1e-10:
        f.write("dim_offset = %s\n" % repr(_val))
    _val = _gp['DIM_EXTENSION']
    if abs(_val - _ds.getOption('DIM_EXTENSION')) > 1e-10:
        f.write("dim_extension = %s\n" % repr(_val))
    _val = _gp['DIM_COLOR']
    if _val != _ds.getOption('DIM_COLOR'):
        f.write("dim_color = '%s'\n" % str(_val))
    _val = _gp['DIM_THICKNESS']
    if abs(_val - _ds.getOption('DIM_THICKNESS')) > 1e-10:
        f.write("dim_thickness = %s\n" % repr(_val))
    _val = _gp['DIM_POSITION']
    if _val != _ds.getOption('DIM_POSITION'):
        f.write("dim_position = '%s'\n" % _dim.getPositionAsString(_val))
    _val = _gp['DIM_ENDPOINT']
    if _val != _ds.getOption('DIM_ENDPOINT'):
        f.write("dim_endpoint = '%s'\n" % _dim.getEndpointTypeAsString(_val))
    _val = _gp['DIM_ENDPOINT_SIZE']
    if abs(_val - _ds.getOption('DIM_ENDPOINT_SIZE')) > 1e-10:
        f.write("dim_endpoint_size = %s\n" % repr(_val))
    _val = _gp['DIM_DUAL_MODE']
    if _val is not _ds.getOption('DIM_DUAL_MODE'):
        f.write("dim_dual_mode = %s\n" % repr(_val))
    _val = _gp['DIM_POSITION_OFFSET']
    if abs(_val - _ds.getOption('DIM_POSITION_OFFSET')) > 1e-10:
        f.write("dim_position_offset = %s\n" % repr(_val))
    _val = _gp['DIM_DUAL_MODE_OFFSET']
    if abs(_val - _ds.getOption('DIM_DUAL_MODE_OFFSET')) > 1e-10:
        f.write("dim_dual_mode_offset = %s\n" % repr(_val))
    _val = _gp['RADIAL_DIM_PRIMARY_PREFIX']
    if _val != _ds.getOption('RADIAL_DIM_PRIMARY_PREFIX'):
        f.write("radial_dim_primary_prefix = %s\n" % repr(_val))
    _val = _gp['RADIAL_DIM_PRIMARY_SUFFIX']
    if _val != _ds.getOption('RADIAL_DIM_PRIMARY_SUFFIX'):
        f.write("radial_dim_primary_suffix = %s\n" % repr(_val))
    _val = _gp['RADIAL_DIM_SECONDARY_PREFIX']
    if _val != _ds.getOption('RADIAL_DIM_SECONDARY_PREFIX'):
        f.write("radial_dim_secondary_prefix = %s\n" % repr(_val))
    _val = _gp['RADIAL_DIM_SECONDARY_SUFFIX']
    if _val != _ds.getOption('RADIAL_DIM_SECONDARY_SUFFIX'):
        f.write("radial_dim_secondary_suffix = %s\n" % repr(_val))
    _val = _gp['RADIAL_DIM_DIA_MODE']
    if _val is not _ds.getOption('RADIAL_DIM_DIA_MODE'):
        f.write("radial_dim_dia_mode = %s\n" % repr(_val))
    _val = _gp['ANGULAR_DIM_PRIMARY_PREFIX']
    if _val != _ds.getOption('ANGULAR_DIM_PRIMARY_PREFIX'):
        f.write("angular_dim_primary_prefix = %s\n" % repr(_val))
    _val = _gp['ANGULAR_DIM_PRIMARY_SUFFIX']
    if _val != _ds.getOption('ANGULAR_DIM_PRIMARY_SUFFIX'):
        f.write("angular_dim_primary_suffix = %s\n" % repr(_val))
    _val = _gp['ANGULAR_DIM_SECONDARY_PREFIX']
    if _val != _ds.getOption('ANGULAR_DIM_SECONDARY_PREFIX'):
        f.write("angular_dim_secondary_prefix = %s\n" % repr(_val))
    _val = _gp['ANGULAR_DIM_SECONDARY_SUFFIX']
    if _val != _ds.getOption('ANGULAR_DIM_SECONDARY_SUFFIX'):
        f.write("angular_dim_secondary_suffix = %s\n" % repr(_val))
    
def _save_textstyle_values(f):
    f.write("#\n# Standard TextBlock parameters\n#\n")
    _gp = globals.prefs
    _ts = _gp['TEXT_STYLE']
    _tsn = _ts.getName()
    _tsf = _ts.getFamily()
    _tsz = _ts.getSize()
    _tss = _ts.getStyle()
    _tsw = _ts.getWeight()
    _tsc = _ts.getColor()
    _tsa = _ts.getAngle()
    _tsl = _ts.getAlignment()
    _tc = text.TextStyle
    f.write("text_style = {\n")
    f.write("    'name' : '%s',\n" % _tsn)
    f.write("    'family' : '%s',\n" % _tsf)
    f.write("    'size' : %s,\n" % repr(_tsz))
    f.write("    'angle' : %s,\n" % repr(_tsa))
    f.write("    'color' : '%s',\n" % str(_tsc))
    f.write("    'weight' : '%s',\n" % _tc.getWeightAsString(_tsw))
    f.write("    'style' : '%s',\n" % _tc.getStyleAsString(_tss))
    f.write("    'alignment' : '%s'\n" % _tc.getAlignmentAsString(_tsl))
    f.write("}\n")
    #
    # save overriden values
    #
    _val = _gp['FONT_FAMILY']
    if _val != _tsf:
        f.write("font_family = '%s'\n" % _val)
    _val = _gp['TEXT_SIZE']
    if abs(_val - _tsz) > 1e-10:
        f.write("text_size = %s\n" % repr(_val))
    _val = _gp['TEXT_ALIGNMENT']
    if _val != _tsl:
        f.write("text_alignment = '%s'\n" % _tc.getAlignmentAsString(_val))
    _val = _gp['FONT_COLOR']
    if _val != _tsc:
        f.write("font_color = '%s'\n" % str(_val))
    _val = _gp['FONT_STYLE']
    if _val != _tss:
        f.write("font_style = '%s'\n" % _tc.getStyleAsString(_val))
    _val = _gp['FONT_WEIGHT']
    if _val != _tsw:
        f.write("font_weight = '%s'\n" % _tc.getWeightAsString(_val))
    _val = _gp['TEXT_ANGLE']
    if abs(_val - _tsa) > 1e-10:
        f.write("text_angle = %s\n" % repr(_val))
    
def _save_style_values(f):
    f.write("#\n# Standard GraphicObject parameters\n#\n")
    _gp = globals.prefs
    _s = _gp['LINE_STYLE']
    _sn = _s.getName()
    _sl = _s.getLinetype()
    _ln = _sl.getName()
    _ll = _sl.getList()
    _sc = _s.getColor()
    _st = _s.getThickness()
    f.write("line_style = {\n")
    f.write("    'name' : '%s',\n" % _sn)
    f.write("    'linetype' : ('%s', %s),\n" % (_ln, repr(_ll)))
    f.write("    'color' : '%s',\n" % str(_sc))
    f.write("    'thickness' : %s\n" % repr(_st))
    f.write("}\n")
    _lt = _gp['LINE_TYPE']
    if _lt != _sl:
        _n = _v.getName()
        _l = _v.getList()
        f.write("line_type = ('%s', %s)\n" % (_n, repr(_l)))
    _c = _gp['LINE_COLOR']
    if _c != _sc:
        f.write("line_color = '%s'\n" % str(_c))
    _t = _gp['LINE_THICKNESS']
    if abs(_t - _st) > 1e-10:
        f.write("line_thickness = %s\n" % repr(_t))
    
def save_user_prefs():
    """Store the current user settings in $HOME/.pythoncad/prefs.py on
    Unix machines, $APPDATA/PythonCAD/prefs.py on Windows.

save_user_prefs()
    """
    if sys.platform == 'win32':
        _home = os.environ.get('APPDATA')
        if _home is None:
            raise ValueError, "Unable to determine APPDATA directory."
        _pdir = os.path.join(_home, 'PythonCAD')
    else:
        _home = os.environ.get('HOME')
        if _home is None:
            raise ValueError, "Unable to determine HOME directory"
        _pdir = os.path.join(_home, '.pythoncad')
    if not os.path.isdir(_pdir):
        os.mkdir(_pdir, 0755)
    _pfile = os.path.join(_pdir, 'prefs.py')
    _f = open(_pfile, "w")
    try:
        _gp = globals.prefs
        _top = """#
# PythonCAD user preferences
#
# If you edit this file manually, be careful!
#
"""
        _f.write("%s" % _top)
        _f.write("#\n# Boolean variables\n#\n")
        _v = _gp['AUTOSPLIT']
        _f.write("autosplit = %s\n" % repr(_v))
        _v = _gp['HIGHLIGHT_POINTS']
        _f.write("highlight_points = %s\n" % repr(_v))
        #
        _f.write("#\n# Size variables (float values)\n#\n")
        _v = _gp['CHAMFER_LENGTH']
        _f.write("chamfer_length = %s\n" %  repr(_v))
        _v = _gp['FILLET_RADIUS']
        _f.write("fillet_radius = %s\n" % repr(_v))
        _v = _gp['LEADER_ARROW_SIZE']
        _f.write("leader_arrow_size = %s\n" % repr(_v))
        #
        _f.write("#\n# Display color settings\n#\n")
        _v = _gp['INACTIVE_LAYER_COLOR']
        _f.write("inactive_layer_color = '%s'\n" % str(_v))
        _v = _gp['BACKGROUND_COLOR']
        _f.write("background_color = '%s'\n" % str(_v))
        _v = _gp['SINGLE_POINT_COLOR']
        _f.write("single_point_color = '%s'\n" % str(_v))
        _v = _gp['MULTI_POINT_COLOR']
        _f.write("multi_point_color = '%s'\n" % str(_v))
        #
        _f.write("#\n# Units\n#\n")
        _v = _gp['UNITS']
        _f.write("units = '%s'\n" % units.Unit.getUnitAsString(_v))
        #
        _save_style_values(_f)
        _save_textstyle_values(_f)
        _save_dimstyle_values(_f)
    finally:
        _f.close()