##############################################################################
# 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 and/or ThanCad drawings customisation.
This module gets/saves options to configuration files.
"""
import ConfigParser
from p_ggen import path
import thanlayer, thanvar
#############################################################################
#############################################################################
class ThanConfigParser(ConfigParser.SafeConfigParser):
def getintvar(self, sect, opt, intvar):
try: i = self.getint(sect, opt)
except: pass
else: intvar.set(i)
#############################################################################
#############################################################################
class ThanOptions:
def __init__(self, config):
self.zoomWhenConf = Tkinter.IntVar()
self.regenWhenConf = Tkinter.IntVar()
self.regenWhenZoom = Tkinter.IntVar()
self.regenWhenZoomall = Tkinter.IntVar()
self.centerWhenZoom = Tkinter.IntVar()
self.zoomFact = 1.2
self.showMenubar = Tkinter.IntVar()
self.showToolbar = Tkinter.IntVar()
self.thanOptFactory()
if thanvar.Win32:
windir = os.path.expandvars("$windir")
if windir != "$windir": f = os.path.join(windir, config+".ini")
else: f = os.path.join(config+".ini")
else:
f = os.path.expanduser(os.path.join("~", "."+config))
self.thanOptFilini = f
self.thanOptGet()
def thanOptFactory(self):
"Factory set default values."
self.zoomWhenConf.set( True)
self.regenWhenConf.set( True)
self.regenWhenZoom.set( False)
self.regenWhenZoomall.set( True)
self.centerWhenZoom.set( False)
self.zoomFact = 1.2
self.showMenubar.set( False)
self.showToolbar.set( True)
def thanOptGet(self):
"Read options from configuration file."
c = ThanConfigParser()
c.read(self.thanOptFilini)
c.getintvar("draw", "zoom when config", self.zoomWhenConf)
c.getintvar("draw", "regen when config", self.regenWhenConf)
c.getintvar("draw", "regen when zoom", self.regenWhenZoom)
c.getintvar("draw", "regen when zoom all", self.regenWhenZoomall)
c.getintvar("draw", "center when zoom", self.centerWhenZoom)
c.getintvar("draw", "show menu bar", self.showMenubar)
c.getintvar("draw", "show tool bar", self.showToolbar)
try: self.zoomFact = c.getfloat("draw", "zoom factor")
except: pass
def thanOptSave(self):
"Write options into configuration file."
c = ThanConfigParser()
c.read(self.thanOptFilini)
if not c.has_section("draw"): c.add_section("draw")
c.set("draw", "zoom when config", self.zoomWhenConf.get())
c.set("draw", "regen when config", self.regenWhenConf.get())
c.set("draw", "regen when zoom", self.regenWhenZoom.get())
c.set("draw", "regen when zoom all", self.regenWhenZoomall.get())
c.set("draw", "center when zoom", self.centerWhenZoom.get())
c.set("draw", "show menu bar", self.showMenubar.get())
c.set("draw", "show tool bar", self.showToolbar.get())
c.set("draw", "zoom factor", self.zoomFact)
try:
f = file(self.thanOptFilini, "w")
c.write(f)
except IOError: pass
def thanOptColorsGet():
"Read colors from configuration file."
rc = []
fc, terr = thanvar.thanConfigFile("thancad.conf")
if terr != "": return rc, terr
c = ConfigParser.SafeConfigParser()
c.read(fc)
try: rc1 = c.get("colors", "user defined")
except: return rc, ""
rc = []
for thc in rc1.split(";"):
thc = thanlayer.thanAttCol(thc)
if thc == None: continue
if str(thc) not in rc: rc.append(str(thc))
return rc, ""
def thanOptColorsSave(rc1):
"Write colors into configuration file."
fc, terr = thanvar.thanConfigFile("thancad.conf")
if terr != "": return terr
c = ConfigParser.SafeConfigParser()
c.read(fc)
if not c.has_section("colors"): c.add_section("colors")
rc = []
for thc in rc1:
thc = thanlayer.thanAttCol(thc)
if thc != None: rc.append("%d %d %d" % thc.thanVal)
c.set("colors", "user defined", ";".join(rc))
try:
f = file(fc, "w")
c.write(f)
except IOError: pass
return ""
#############################################################################
#############################################################################
if __name__ == "__main__":
rc, terr = thanOptColorsGet()
print rc, terr
rc.append("111 111 111")
terr = thanOptColorsSave(rc)
print terr
|