2005-01-07 00:56:54 +05:30
|
|
|
#
|
|
|
|
# Gramps - a GTK+/GNOME based genealogy program
|
|
|
|
#
|
2005-01-11 22:26:51 +05:30
|
|
|
# Copyright (C) 2005 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
|
2006-04-25 02:34:01 +05:30
|
|
|
from _GrampsConfigKeys import *
|
2005-01-07 00:56:54 +05:30
|
|
|
|
|
|
|
NL = "\n" # FIX: newlines on Mac/Windows, if different?
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
class IniKeyClient:
|
|
|
|
""" 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] = {}
|
|
|
|
self.callbacks[section][key] = 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()
|
|
|
|
if not data.has_key(name):
|
|
|
|
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-04-25 02:34:01 +05:30
|
|
|
if self.data[key[0]][key[1]].isdigit():
|
|
|
|
return int(self.data[key[0]][key[1]])
|
|
|
|
elif self.data[key[0]][key[1]].lower() in ["true"]:
|
2005-01-07 00:56:54 +05:30
|
|
|
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]]:
|
|
|
|
self.callbacks[key[0]][key[1]](self,0,self.data[key[0]][key[1]],None)
|
|
|
|
|
|
|
|
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]]:
|
|
|
|
self.callbacks[key[0]][key[1]](self,0,self.data[key[0]][key[1]],None)
|
|
|
|
|
|
|
|
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]]:
|
|
|
|
self.callbacks[key[0]][key[1]](self,0,self.data[key[0]][key[1]],None)
|
|
|
|
|
2005-01-07 00:56:54 +05:30
|
|
|
def suggest_sync(self):
|
|
|
|
self.save_ini() # save back to default file, if named
|
|
|
|
|
2006-03-21 12:03:59 +05:30
|
|
|
client = IniKeyClient(os.path.join(const.home_dir,"keys.ini"))
|
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)
|
|
|
|
if not val:
|
|
|
|
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
|
|
|
|
if val in (True,False):
|
|
|
|
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_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 10:42:19 +05:30
|
|
|
def get_default(key):
|
|
|
|
return default_value[key]
|