2005-01-07 00:56:54 +05:30
|
|
|
#
|
|
|
|
# Gramps - a GTK+/GNOME based genealogy program
|
|
|
|
#
|
2006-05-13 11:24:07 +05:30
|
|
|
# Copyright (C) 2005-2006 Donald N. Allingham
|
2005-01-07 00:56:54 +05:30
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
#
|
|
|
|
|
2005-01-11 22:26:51 +05:30
|
|
|
# $Id$
|
2005-01-07 00:56:54 +05:30
|
|
|
|
|
|
|
"""
|
|
|
|
Low-level handling of .ini keys.
|
|
|
|
"""
|
|
|
|
import os
|
|
|
|
import time
|
|
|
|
import ConfigParser
|
2006-03-21 08:31:50 +05:30
|
|
|
import errno
|
2006-03-21 12:03:59 +05:30
|
|
|
import const
|
2008-02-19 01:37:09 +05:30
|
|
|
from Config import DATE_FORMAT, NAME_FORMAT, default_value
|
2005-01-07 00:56:54 +05:30
|
|
|
|
|
|
|
NL = "\n" # FIX: newlines on Mac/Windows, if different?
|
|
|
|
|
2007-09-08 11:24:02 +05:30
|
|
|
INIFILE = os.path.join(const.HOME_DIR,"keys.ini")
|
2007-07-23 02:33:52 +05:30
|
|
|
|
2005-01-07 00:56:54 +05:30
|
|
|
def make_bool(val):
|
|
|
|
""" Function to turn strings into booleans. """
|
|
|
|
# these are the possible strings that should be considered False
|
|
|
|
if val.lower() in ["0", "false", "none", ""]:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
2009-05-21 22:49:50 +05:30
|
|
|
class IniKeyClient(object):
|
2005-01-07 00:56:54 +05:30
|
|
|
""" Class to emulate gconf's client """
|
|
|
|
def __init__(self, filename = None):
|
|
|
|
""" Constructor takes an optional filename """
|
|
|
|
self.data = {}
|
|
|
|
self.callbacks = {}
|
|
|
|
self.filename = filename
|
|
|
|
if self.filename and os.path.exists(filename):
|
|
|
|
self.data = self.load_ini(self.filename)
|
2006-04-25 02:34:01 +05:30
|
|
|
|
2005-01-07 00:56:54 +05:30
|
|
|
def notify_add(self, path, func):
|
|
|
|
"""
|
|
|
|
I think that these are callbacks that get called when the keys
|
|
|
|
are set.
|
|
|
|
"""
|
|
|
|
parts = path.split("/") # FIX: make path-sep independent
|
|
|
|
# /apps/gramps/section/key
|
|
|
|
section = parts[-2]
|
|
|
|
key = parts[-1]
|
|
|
|
if section not in self.callbacks:
|
|
|
|
self.callbacks[section] = {}
|
2006-08-09 08:42:15 +05:30
|
|
|
if key not in self.callbacks[section]:
|
|
|
|
self.callbacks[section][key] = []
|
|
|
|
if func not in self.callbacks[section][key]:
|
|
|
|
self.callbacks[section][key].append(func)
|
2006-04-25 02:34:01 +05:30
|
|
|
|
2005-01-07 00:56:54 +05:30
|
|
|
def load_ini(self, filename):
|
|
|
|
""" Load .ini into dict of dicts, which it returns """
|
|
|
|
cp = ConfigParser.ConfigParser()
|
|
|
|
cp.read(filename)
|
|
|
|
data = {}
|
|
|
|
for sec in cp.sections():
|
|
|
|
name = sec.lower()
|
2008-07-17 23:40:32 +05:30
|
|
|
if name not in data:
|
2005-01-07 00:56:54 +05:30
|
|
|
data[name] = {}
|
|
|
|
for opt in cp.options(sec):
|
|
|
|
data[name][opt.lower()] = cp.get(sec, opt).strip()
|
|
|
|
return data
|
2006-04-25 02:34:01 +05:30
|
|
|
|
2005-01-07 00:56:54 +05:30
|
|
|
def save_ini(self, filename = None):
|
|
|
|
"""
|
|
|
|
Saves the current section/keys to a .ini file. Optional filename
|
|
|
|
will override the default filename, if one.
|
|
|
|
"""
|
|
|
|
if not filename:
|
|
|
|
filename = self.filename
|
|
|
|
if filename:
|
2006-03-21 08:31:50 +05:30
|
|
|
try:
|
|
|
|
head, tail = os.path.split( filename )
|
|
|
|
os.makedirs( head )
|
|
|
|
except OSError, e:
|
|
|
|
if e.errno != errno.EEXIST:
|
|
|
|
raise
|
2005-01-07 00:56:54 +05:30
|
|
|
fp = open(filename, "w")
|
|
|
|
fp.write(";; Gramps key file" + NL)
|
|
|
|
fp.write((";; Automatically created at %s" % time.strftime("%Y/%m/%d %H:%M:%S")) + NL + NL)
|
|
|
|
sections = self.data.keys()
|
|
|
|
sections.sort()
|
|
|
|
for section in sections:
|
|
|
|
fp.write(("[%s]" + NL) % section)
|
|
|
|
keys = self.data[section].keys()
|
|
|
|
keys.sort()
|
|
|
|
for key in keys:
|
|
|
|
fp.write(("%s=%s" + NL)% (key, self.data[section][key]))
|
|
|
|
fp.write(NL)
|
|
|
|
fp.close()
|
|
|
|
# else, no filename given
|
2006-04-25 02:34:01 +05:30
|
|
|
|
|
|
|
def get_bool(self, key):
|
2005-01-07 00:56:54 +05:30
|
|
|
""" Emulates gconf's client method """
|
2006-04-25 02:34:01 +05:30
|
|
|
return make_bool(self.data[key[0]][key[1]])
|
|
|
|
|
|
|
|
def get_string(self, key):
|
2005-01-07 00:56:54 +05:30
|
|
|
""" Emulates gconf's client method """
|
2006-04-25 02:34:01 +05:30
|
|
|
return self.data[key[0]][key[1]]
|
|
|
|
|
|
|
|
def get_int(self, key):
|
2005-01-07 00:56:54 +05:30
|
|
|
""" Emulates gconf's client method """
|
2006-07-20 01:26:15 +05:30
|
|
|
try:
|
|
|
|
val = int(self.data[key[0]][key[1]])
|
|
|
|
return val
|
|
|
|
except ValueError:
|
|
|
|
if self.data[key[0]][key[1]].lower() in ["true"]:
|
|
|
|
return 1
|
|
|
|
else:
|
|
|
|
return 0
|
2006-04-25 02:34:01 +05:30
|
|
|
|
|
|
|
def set_bool(self, key, val):
|
2005-01-07 00:56:54 +05:30
|
|
|
""" Emulates gconf's client method """
|
2006-04-25 02:34:01 +05:30
|
|
|
if key[0] not in self.data:
|
|
|
|
self.data[key[0]] = {}
|
|
|
|
self.data[key[0]][key[1]] = str(val)
|
|
|
|
if key[0] in self.callbacks and key[1] in self.callbacks[key[0]]:
|
2006-08-09 08:42:15 +05:30
|
|
|
for func in self.callbacks[key[0]][key[1]]:
|
|
|
|
func(self,0,self.data[key[0]][key[1]],None)
|
2006-04-25 02:34:01 +05:30
|
|
|
|
|
|
|
def set_string(self, key, val):
|
2005-01-07 00:56:54 +05:30
|
|
|
""" Emulates gconf's client method """
|
2006-04-25 02:34:01 +05:30
|
|
|
if key[0] not in self.data:
|
|
|
|
self.data[key[0]] = {}
|
|
|
|
self.data[key[0]][key[1]] = val
|
|
|
|
if key[0] in self.callbacks and key[1] in self.callbacks[key[0]]:
|
2006-08-09 08:42:15 +05:30
|
|
|
for func in self.callbacks[key[0]][key[1]]:
|
|
|
|
func(self,0,self.data[key[0]][key[1]],None)
|
2006-04-25 02:34:01 +05:30
|
|
|
|
|
|
|
def set_int(self, key, val):
|
2005-01-07 00:56:54 +05:30
|
|
|
""" Emulates gconf's client method """
|
2006-04-25 02:34:01 +05:30
|
|
|
if key[0] not in self.data:
|
|
|
|
self.data[key[0]] = {}
|
|
|
|
self.data[key[0]][key[1]] = str(val)
|
|
|
|
if key[0] in self.callbacks and key[1] in self.callbacks[key[0]]:
|
2006-08-09 08:42:15 +05:30
|
|
|
for func in self.callbacks[key[0]][key[1]]:
|
|
|
|
func(self,0,self.data[key[0]][key[1]],None)
|
2006-04-25 02:34:01 +05:30
|
|
|
|
2005-01-07 00:56:54 +05:30
|
|
|
def suggest_sync(self):
|
|
|
|
self.save_ini() # save back to default file, if named
|
|
|
|
|
2007-07-23 02:33:52 +05:30
|
|
|
client = IniKeyClient(INIFILE)
|
2005-01-07 00:56:54 +05:30
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Functions to obtain values from .ini keys
|
|
|
|
# and store values into .ini keys
|
|
|
|
#
|
|
|
|
# All gramps keys should be accessed through these functions!
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
def get_date_format(_date_format_list=[]):
|
2006-04-25 03:16:54 +05:30
|
|
|
return get_int(DATE_FORMAT,
|
2006-04-25 02:34:01 +05:30
|
|
|
range(len(_date_format_list)))
|
2005-01-07 00:56:54 +05:30
|
|
|
|
|
|
|
def save_date_format(val,_date_format_list=[]):
|
2006-04-25 03:16:54 +05:30
|
|
|
set_int(DATE_FORMAT, val,
|
2006-04-25 02:34:01 +05:30
|
|
|
range(len(_date_format_list)))
|
2005-01-07 00:56:54 +05:30
|
|
|
|
|
|
|
def get_name_format(_name_format_list):
|
2006-04-25 03:16:54 +05:30
|
|
|
return get_int(NAME_FORMAT,
|
2006-04-25 02:34:01 +05:30
|
|
|
range(len(_name_format_list)))
|
2005-01-07 00:56:54 +05:30
|
|
|
|
|
|
|
def save_name_format(val,_name_format_list):
|
2006-04-25 03:16:54 +05:30
|
|
|
set_int(NAME_FORMAT, val,
|
2006-04-25 02:34:01 +05:30
|
|
|
range(len(_name_format_list)))
|
2005-01-07 00:56:54 +05:30
|
|
|
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Low-level grabbing and saving keys with error checking.
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2006-04-25 02:34:01 +05:30
|
|
|
|
|
|
|
def set(key, value):
|
|
|
|
if key[2] == 0:
|
|
|
|
set_bool(key, value)
|
|
|
|
elif key[2] == 1:
|
|
|
|
set_int(key, value)
|
|
|
|
else:
|
|
|
|
set_string(key, value)
|
|
|
|
|
|
|
|
def get(key):
|
|
|
|
if key[2] == 0:
|
|
|
|
val = get_bool(key)
|
|
|
|
elif key[2] == 1:
|
|
|
|
val = get_int(key)
|
|
|
|
else:
|
|
|
|
val = get_string(key)
|
2008-06-16 20:31:46 +05:30
|
|
|
if val is None or val == "":
|
2006-04-25 02:34:01 +05:30
|
|
|
val = default_value[key]
|
|
|
|
return val
|
|
|
|
|
|
|
|
def get_bool(key):
|
2005-01-07 00:56:54 +05:30
|
|
|
try:
|
2006-04-25 02:34:01 +05:30
|
|
|
val = client.get_bool(key)
|
2005-01-07 00:56:54 +05:30
|
|
|
except KeyError:
|
|
|
|
val = None
|
2007-08-25 21:04:13 +05:30
|
|
|
if val in (True, False):
|
2005-01-07 00:56:54 +05:30
|
|
|
return val
|
2006-08-25 09:50:16 +05:30
|
|
|
elif key in default_value:
|
2006-04-25 02:34:01 +05:30
|
|
|
return default_value[key]
|
2006-08-25 09:50:16 +05:30
|
|
|
else:
|
|
|
|
print "No default value for %s" % key
|
|
|
|
return False
|
2005-01-07 00:56:54 +05:30
|
|
|
|
2006-04-25 02:34:01 +05:30
|
|
|
def set_bool(key, val):
|
2005-01-07 00:56:54 +05:30
|
|
|
if val in (True,False):
|
2006-04-25 02:34:01 +05:30
|
|
|
client.set_bool(key,val)
|
2005-01-07 00:56:54 +05:30
|
|
|
|
2006-04-25 02:34:01 +05:30
|
|
|
def get_int(key, correct_tuple=None):
|
2005-01-07 00:56:54 +05:30
|
|
|
try:
|
2006-04-25 02:34:01 +05:30
|
|
|
return client.get_int(key)
|
2005-01-07 00:56:54 +05:30
|
|
|
except KeyError:
|
2006-04-25 02:34:01 +05:30
|
|
|
return default_value[key]
|
2005-01-07 00:56:54 +05:30
|
|
|
|
2006-04-25 02:34:01 +05:30
|
|
|
def set_int(key, val, correct_tuple=None):
|
2005-01-07 00:56:54 +05:30
|
|
|
if not correct_tuple or val in correct_tuple:
|
2006-04-25 02:34:01 +05:30
|
|
|
client.set_int(key, val)
|
2005-01-07 00:56:54 +05:30
|
|
|
|
2006-04-25 02:34:01 +05:30
|
|
|
def get_string(key, test_func=None):
|
2005-01-07 00:56:54 +05:30
|
|
|
try:
|
2006-04-25 02:34:01 +05:30
|
|
|
val = client.get_string(key)
|
2005-01-07 00:56:54 +05:30
|
|
|
except KeyError:
|
|
|
|
val = ""
|
|
|
|
if not test_func or test_func(val):
|
|
|
|
return val
|
|
|
|
else:
|
2006-04-25 02:34:01 +05:30
|
|
|
return default_value[key]
|
2005-01-07 00:56:54 +05:30
|
|
|
|
2006-04-25 02:34:01 +05:30
|
|
|
def set_string(key, val, test_func=None):
|
2005-01-07 00:56:54 +05:30
|
|
|
if not test_func or test_func(val):
|
2006-04-25 02:34:01 +05:30
|
|
|
client.set_string(key, val)
|
2005-01-07 00:56:54 +05:30
|
|
|
|
|
|
|
def sync():
|
|
|
|
client.suggest_sync()
|
|
|
|
|
2006-05-13 11:24:07 +05:30
|
|
|
def get_default(key,sample=''):
|
2006-05-13 10:42:19 +05:30
|
|
|
return default_value[key]
|