mirror of
https://github.com/gryf/pygtktalog.git
synced 2025-12-17 11:30:19 +01:00
98 lines
2.5 KiB
Python
98 lines
2.5 KiB
Python
#!/usr/bin/python
|
|
# -*- coding: utf-8 -*-
|
|
"""
|
|
config parser object
|
|
"""
|
|
|
|
#{{{ podstawowe importy i sprawdzenia
|
|
import sys
|
|
import os
|
|
from ConfigParser import ConfigParser
|
|
|
|
class Ini(object):
|
|
def __init__(self):
|
|
self.ini = []
|
|
|
|
def add_section(self, section):
|
|
self.ini.append("[%s]" % section)
|
|
|
|
def add_key(self, key, value):
|
|
self.ini.append("%s=%s" % (key, value))
|
|
|
|
def add_comment(self, comment):
|
|
self.ini.append(";%s" % comment)
|
|
|
|
def add_verb(self, verb):
|
|
self.ini.append(verb)
|
|
|
|
def show(self):
|
|
return "\n".join(self.ini)
|
|
|
|
|
|
class Config:
|
|
ini = Ini()
|
|
|
|
confd = {
|
|
'wx' : 800,
|
|
'wy' : 600,
|
|
'h' : 200,
|
|
'v' : 300,
|
|
'exportxls' : False,
|
|
'cd' : '/cdrom',
|
|
}
|
|
|
|
dictconf = {
|
|
"main window width" : "wx",
|
|
"main window height": "wy",
|
|
"horizontal panes": "h",
|
|
"vertical panes":"v",
|
|
"export xls":"exportxls",
|
|
"cd drive":"cd",
|
|
}
|
|
|
|
dbool = ('exportxls')
|
|
dstring = ('cd')
|
|
|
|
try:
|
|
path = os.environ['HOME']
|
|
except:
|
|
path = "/tmp"
|
|
|
|
def __init__(self):
|
|
self.load()
|
|
|
|
def save(self):
|
|
self.confd
|
|
newIni = Ini()
|
|
newIni.add_section("pyGTKtalog conf")
|
|
for opt in self.dictconf:
|
|
newIni.add_key(opt,self.confd[self.dictconf[opt]])
|
|
try:
|
|
f = open("%s/.pygtktalog" % self.path,"w")
|
|
except:
|
|
print "Cannot open config file %s for writing." % (self.path, "/.pygtktalog")
|
|
f.write(newIni.show())
|
|
f.close()
|
|
|
|
def load(self):
|
|
try:
|
|
# try to read config file
|
|
parser = ConfigParser()
|
|
parser.read("%s/.pygtktalog" % self.path)
|
|
for sec in parser.sections():
|
|
if sec == 'pyGTKtalog conf':
|
|
for opt in parser.options(sec):
|
|
try:
|
|
if self.dictconf[opt] in self.dbool:
|
|
self.confd[self.dictconf[opt]] = parser.getboolean(sec,opt)
|
|
elif self.dictconf[opt] in self.dstring:
|
|
self.confd[self.dictconf[opt]] = parser.get(sec,opt)
|
|
else:
|
|
self.confd[self.dictconf[opt]] = parser.getint(sec,opt)
|
|
except:
|
|
pass
|
|
except:
|
|
print "No config file"
|
|
pass
|
|
|