##############################################################################
# ThanCad 0.0.9 "DoesSomething": 2dimensional CAD with raster support for engineers.
#
# Copyright (c) 2001-2009 Thanasis Stamos, August 23, 2009
# URL: http://thancad.sourceforge.net
# e-mail: cyberthanasis@excite.com
#
# This program 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.
#
# This program 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 (www.gnu.org/licenses/gpl.html).
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##############################################################################
"""\
ThanCad 0.0.9 "DoesSomething": 2dimensional CAD with raster support for engineers.
Package which provides for ThanCad customisation.
This module gets/saves options to configuration files. It also keeps
these options as global variables in this module.
"""
import sys, ConfigParser
import p_ggen
import thanvar
from thandefs.thanatt import thanAttCol
def thanOptColorsGet(c):
"Read colors from configuration file."
# rc = []; rootcol = "0 222 255"; selcol = "0 36 140"; colosn = "41"
rc = []; rootcol = "0 222 255"; selcol = "12"; colosn = "41"
if c == None: return rootcol, rc, selcol, colosn
try: rc1 = c.get("colors", "user defined")
except: pass
else:
for thc in rc1.split(";"):
thc = thanAttCol(thc)
if thc == None: continue
if str(thc) not in rc: rc.append(str(thc))
while len(rc) < 14: rc.append(None)
while len(rc) > 14: del rc[-1]
try: rc1 = c.get("colors", "root")
except: pass
else:
rc1 = thanAttCol(rc1)
if rc1 != None: rootcol = rc1
try: rc1 = c.get("colors", "selcol")
except: pass
else:
rc1 = thanAttCol(rc1)
if rc1 != None: selcol = rc1
try: rc1 = c.get("colors", "osnap")
except: pass
else:
rc1 = thanAttCol(rc1)
if rc1 != None: colosn = rc1
return rootcol, rc, selcol, colosn
def thanOptColorsSave(c, rootcol, rc1, selcol, colosn):
"Write colors into configuration file."
if c == None: return
if not c.has_section("colors"): c.add_section("colors")
rc = []
for thc in rc1:
thc = thanAttCol(thc)
if thc != None: rc.append(str(thc))
c.set("colors", "root", str(rootcol))
c.set("colors", "user defined", ";".join(rc))
c.set("colors", "select", str(selcol))
c.set("colors", "osnap", str(colosn))
thanOsnapModesText = \
( ("end", "Endpoint"),
("mid", "Midpoint"),
("cen", "Center" ),
("nod", "Node" ),
("qua", "Quadrant"),
("int", "Intersection"),
("tan", "tangent" ),
("nea", "Nearest" ),
("per", "Perpendicular"),
("ena", "Enabled" ),
)
def thanOptOsnapGet(c):
"Read colors from configuration file."
modes = dict(end=True)
if c == None: return modes
modes = {}
for mode,t in thanOsnapModesText:
try: val = c.get("osnap", t)
except: continue
if val.strip().lower() == "false": continue
if val: modes[mode] = True
return modes
def thanOptOsnapSave(c, modes):
"Read colors from configuration file."
if c == None: return
if not c.has_section("osnap"): c.add_section("osnap")
for mode, t in thanOsnapModesText:
val = mode in modes
c.set("osnap", t, str(val))
def thanOptInterGet(c):
"Read international settings."
if c == None: return p_ggen.thanGetEncoding()
try: val = c.get("international", "encoding")
except: return p_ggen.thanGetEncoding()
return val
def thanOptInterSave(c):
"Read colors from configuration file."
if c == None: return
if not c.has_section("international"): c.add_section("international")
c.set("international", "encoding", p_ggen.thanGetEncoding())
def thanOptFilesGet(c):
"Read recent opened file and recent directories."
filnams = []
fildir = ""
if c == None: return filnams, fildir
try: val = c.get("files", "recent files")
except: pass
else: filnams = val.split(";")
try: val = c.get("files", "recent directory")
except: pass
else: fildir = val
return filnams, fildir
def thanOptFilesSave(c, filnams, fildir):
"Read colors from configuration file."
if c == None: return
if not c.has_section("files"): c.add_section("files")
c.set("files", "recent files", ";".join(filnams))
c.set("files", "recent directory", fildir)
#############################################################################
#############################################################################
def thanOptsGet():
"Reads the attributes from config files and store them as global variables."
fc, terr = thanvar.thanConfigFile("thancad.conf")
if terr != "":
print terr
c = None
else:
c = ConfigParser.SafeConfigParser()
c.read(fc)
global thanColRoot, thanColUser, thanColSel, thanColOsn
thanColRoot, thanColUser, thanColSel, thanColOsn = thanOptColorsGet(c)
global thanOsnapModes
thanOsnapModes = thanOptOsnapGet(c)
enc = thanOptInterGet(c)
try: p_ggen.thanSetEncoding(enc)
except: pass
filnams, fildir = thanOptFilesGet(c)
if fildir.strip() == "": fildir = ""
filnams = [p_ggen.path(filnam) for filnam in filnams if filnam.strip() != ""]
fildir = p_ggen.path(fildir)
for filnam in filnams: thanvar.thanfiles.thanFilAddRecent(filnam)
thanvar.thanfiles.thanFilSetFiledir(fildir)
def thanOptsSave():
"Writes the attributes to config files."
fc, terr = thanvar.thanConfigFile("thancad.conf")
if terr != "":
print terr
c = None
else:
c = ConfigParser.SafeConfigParser()
c.read(fc)
global thanColRoot, thanColUser, thanColSel, thanColOsn
thanOptColorsSave(c, thanColRoot, thanColUser, thanColSel, thanColOsn)
global thanOsnapModes
thanOptOsnapSave(c, thanOsnapModes)
thanOptInterSave(c)
filnams = thanvar.thanfiles.thanFilGetRecent()
fildir = thanvar.thanfiles.thanFilGetFiledir()
thanOptFilesSave(c, filnams, fildir)
if c != None:
try:
f = file(fc, "w")
c.write(f)
except IOError, why: print "Could not save config file:", why; pass
|