thancadconf.py :  » Business-Application » ThanCad » thancad-0.0.9 » thanopt » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Business Application » ThanCad 
ThanCad » thancad 0.0.9 » thanopt » thancadconf.py
##############################################################################
# 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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.