settings.py :  » Network » rwhois.py » releaseforge-0.5 » modules » 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 » Network » rwhois.py 
rwhois.py » releaseforge 0.5 » modules » settings.py
import logging
import os 
import sys
from constants import DEFAULT_DATA_PATH

debug = logging.getLogger("settings").debug
error = logging.getLogger("settings").error

class Settings(dict):
    def __init__(self, filename=None, autocreate=False):
        dict.__init__(self)
        self.filename = filename
        self.autocreate = autocreate
        if filename:
            self.loadSettings()

    def __getitem__(self, k):
        try:
            return dict.__getitem__(self, k)
        except:
            error("Could not locate setting %s", k)
            return ""


    def setFilename(self, filename):
        self.filename = filename


    def loadSettings(self):
        if not self.filename:
            error("Cannot loadSettings")
            return

        if not os.access(self.filename, os.R_OK) and self.autocreate:
            self.createInitialSettings()

        fp = None
        try:
            fp = open(self.filename, "r")
            for line in fp:
                line = line.strip()
                if not line or line[0] == '#': continue
                name, value = line.split(':', 1)
                name = name.strip().lower()
                value = value.strip()
                dict.__setitem__(self, name, value)
            fp.close()
            debug("Loaded settings from: %s", self.filename)
        except Exception, e:
            error(e)
        if fp: fp.close()    

    
    def saveSettings(self):
        fp = None
        try:
            fp = open(self.filename, "w")
            
            for k,v in self.items():
                fp.write("%s: %s\n" % (k,v))
                
        except Exception, e:
            error("Could not saveSettings", e)
        if fp: fp.close()

            
    def createInitialSettings(self):
        try:
            fp = open(self.filename, "w")
        except Exception, e:
            error("Could not write file: %s - %s", self.filename, str(e))
            sys.exit(1)          

        try:
            data_path = os.path.join(os.environ['HOME'], DEFAULT_DATA_PATH)
        except:
            data_path = os.path.join(os.getcwd(), DEFAULT_DATA_PATH)

        debug("Using %s as the data_path in %s", data_path, self.filename)
        fp.write("data_path: %s\n" % data_path)
        fp.close()
                 
        
            
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.