Removed unnecessary Registry classes

Removed data path from config file
Changed various paths:
 - Windows: Application Data
 - Unix/Linux: XDG directories, falling back to ~/.openlp
 - Mac OS X: Application Support/openlp
This commit is contained in:
Raoul Snyman 2009-06-04 18:53:49 +02:00
parent 3561ede5b0
commit 08198dfc01
4 changed files with 80 additions and 276 deletions

View File

@ -2,7 +2,7 @@
# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4 # vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4
""" """
OpenLP - Open Source Lyrics Projection OpenLP - Open Source Lyrics Projection
Copyright (c) 2008 Raoul Snyman Copyright (c) 2008-2009 Raoul Snyman
Portions copyright (c) 2008 Martin Thompson, Tim Bentley Portions copyright (c) 2008 Martin Thompson, Tim Bentley
This program is free software; you can redistribute it and/or modify it under This program is free software; you can redistribute it and/or modify it under
@ -19,26 +19,32 @@ Place, Suite 330, Boston, MA 02111-1307 USA
""" """
import os import os
from openlp.core.utils.registry import Registry
class ConfigHelper(object): class ConfigHelper(object):
""" """
Utility Helper to allow classes to find directories in a standard manner. Utility Helper to allow classes to find directories in a standard manner.
""" """
__registry__ = None
@staticmethod @staticmethod
def get_data_path(): def get_data_path():
if os.name == 'nt': if os.name == u'nt':
# ask OS for path to application data, set on Windows XP and Vista # ask OS for path to application data, set on Windows XP and Vista
appdata = os.getenv('APPDATA') path = os.path.join(os.getenv(u'APPDATA'), u'openlp', u'data')
default = os.path.join(appdata, u'.openlp', u'data') elif os.name == u'mac':
path = os.path.join(os.getenv(u'HOME'), u'Library',
u'Application Support', u'openlp', u'Data')
else: else:
default = os.path.expanduser(u'~/.openlp/data') try:
from xdg import BaseDirectory
path = os.path.join(BaseDirectory.xdg_data_home, u'openlp')
except ImportError:
path = os.path.join(os.getenv(u'HOME'), u'.openlp', u'data')
reg = ConfigHelper.get_registry() reg = ConfigHelper.get_registry()
path = ConfigHelper.get_config('main', 'data path', default) #path = ConfigHelper.get_config('main', 'data path', path)
if not os.path.exists(path): if not os.path.exists(path):
os.makedirs(path) os.makedirs(path)
return path return path
@staticmethod @staticmethod
@ -69,13 +75,18 @@ class ConfigHelper(object):
This static method loads the appropriate registry class based on the This static method loads the appropriate registry class based on the
current operating system, and returns an instantiation of that class. current operating system, and returns an instantiation of that class.
""" """
reg = None if ConfigHelper.__registry__ is None:
if os.name == 'nt': config_path = u''
#from winregistry import WinRegistry if os.name == u'nt':
#reg = WinRegistry(r'\Software\openlp') config_path = os.path.join(os.getenv(u'APPDATA'), u'openlp')
from linregistry import LinRegistry elif os.name == u'mac':
reg = LinRegistry(os.path.join(os.getenv('APPDATA'), '.openlp')) config_path = os.path.join(os.getenv(u'HOME'), u'Library',
else: u'Application Support', u'openlp')
from linregistry import LinRegistry else:
reg = LinRegistry(os.path.join(os.getenv('HOME'), '.openlp')) try:
return reg from xdg import BaseDirectory
config_path = os.path.join(BaseDirectory.xdg_config_home, u'openlp')
except ImportError:
config_path = os.path.join(os.getenv(u'HOME'), u'.openlp')
ConfigHelper.__registry__ = Registry(config_path)
return ConfigHelper.__registry__

View File

@ -1,110 +0,0 @@
# -*- coding: utf-8 -*-
# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4
"""
OpenLP - Open Source Lyrics Projection
Copyright (c) 2008 Raoul Snyman
Portions copyright (c) 2008 Martin Thompson, Tim Bentley
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; version 2 of the License.
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
"""
import os
import sys
from ConfigParser import SafeConfigParser
from openlp.core.utils import Registry
class LinRegistry(Registry):
"""
The LinRegistry class is a high-level class for working with Linux and
Unix configurations.
"""
def __init__(self, dir):
self.config = SafeConfigParser()
self.file_name = os.path.join(dir, 'openlp.conf')
self.config.read(self.file_name)
def has_value(self, section, key):
"""
Check if a value exists.
"""
return self.config.has_option(section, key)
def get_value(self, section, key, default=None):
"""
Get a single value from the registry.
"""
try:
if self.config.get(section, key):
return self.config.get(section, key)
else:
return default
except:
return default
def set_value(self, section, key, value):
"""
Set a single value in the registry.
"""
try :
self.config.set(section, key, str(value))
return self._save()
except:
return False
def delete_value(self, section, key):
"""
Delete a single value from the registry.
"""
try:
self.config.remove_option(section, key)
return self._save()
except:
return False
def has_section(self, section):
"""
Check if a section exists.
"""
return self.config.has_section(section)
def create_section(self, section):
"""
Create a new section in the registry.
"""
try:
self.config.add_section(section)
return self._save()
except:
return False
def delete_section(self, section):
"""
Delete a section (including all values).
"""
try:
self.config.remove_section(section)
return self._save()
except:
return False
def _save(self):
try:
if not os.path.exists(os.path.dirname(self.file_name)):
os.makedirs(os.path.dirname(self.file_name))
file_handle = open(self.file_name, 'w')
self.config.write(file_handle)
close(file_handle)
self.config.read(self.file_name)
return True
except:
return False

View File

@ -17,55 +17,92 @@ 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 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA Place, Suite 330, Boston, MA 02111-1307 USA
""" """
import os
import sys
from ConfigParser import SafeConfigParser
class Registry(object): class Registry(object):
""" """
The Registry class is a generic class for the accessing configurations. The Registry class is a high-level class for working with a configuration
file.
""" """
def __init__(self): def __init__(self, dir):
""" self.config = SafeConfigParser()
Initialise the Registry object. Override this to add custom initialisation. self.file_name = os.path.join(dir, 'openlp.conf')
""" self.config.read(self.file_name)
pass
def has_value(self, section, key): def has_value(self, section, key):
""" """
Check if a value exists. Check if a value exists.
""" """
pass return self.config.has_option(section, key)
def get_value(self, section, key, default=None): def get_value(self, section, key, default=None):
""" """
Get a single value from the registry. Get a single value from the registry.
""" """
pass try:
if self.config.get(section, key):
return self.config.get(section, key)
else:
return default
except:
return default
def set_value(self, section, key, value): def set_value(self, section, key, value):
""" """
Set a single value in the registry. Set a single value in the registry.
""" """
pass try :
self.config.set(section, key, str(value))
return self._save()
except:
return False
def delete_value(self, section, key): def delete_value(self, section, key):
""" """
Delete a single value from the registry. Delete a single value from the registry.
""" """
pass try:
self.config.remove_option(section, key)
return self._save()
except:
return False
def has_section(self, section): def has_section(self, section):
""" """
Check if a section exists. Check if a section exists.
""" """
return False return self.config.has_section(section)
def create_section(self, section): def create_section(self, section):
""" """
Create a new section in the registry. Create a new section in the registry.
""" """
pass try:
self.config.add_section(section)
return self._save()
except:
return False
def delete_section(self, section): def delete_section(self, section):
""" """
Delete a section (including all values). Delete a section (including all values).
""" """
pass try:
self.config.remove_section(section)
return self._save()
except:
return False
def _save(self):
try:
if not os.path.exists(os.path.dirname(self.file_name)):
os.makedirs(os.path.dirname(self.file_name))
file_handle = open(self.file_name, 'w')
self.config.write(file_handle)
close(file_handle)
self.config.read(self.file_name)
return True
except:
return False

View File

@ -1,134 +0,0 @@
# -*- coding: utf-8 -*-
# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4
"""
OpenLP - Open Source Lyrics Projection
Copyright (c) 2008 Raoul Snyman
Portions copyright (c) 2008 Martin Thompson, Tim Bentley
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; version 2 of the License.
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
"""
import _winreg
import types
from openlp.core.utils import Registry
class WinRegistry(Registry):
"""
The WinRegistry class is a high-level wrapper class for the Windows registry
functions in Python.
"""
def __init__(self, base_key):
"""
Connection to the Windows registry, and save the handle.
"""
self.reg_handle = _winreg.ConnectRegistry(None, _winreg.HKEY_CURRENT_USER)
self.base_key = base_key
if not self.base_key.endswith('\\'):
self.base_key = self.base_key + '\\'
def has_value(self, section, key):
"""
Check if a key/value exists.
"""
if not self.has_section(section):
return False
key_handle = _winreg.OpenKey(self.reg_handle, self.base_key + section)
try:
value, reg_type = _winreg.QueryValueEx(key_handle, key)
except EnvironmentError:
return False
finally:
_winreg.CloseKey(key_handle)
if reg_type == _winreg.REG_NONE:
return False
elif reg_type == _winreg.REG_SZ and value == '':
return False
elif reg_type == _winreg.REG_DWORD and value == 0:
return False
else:
return True
def get_value(self, section, key, default=None):
"""
Get a single value from the Windows registry.
"""
if not self.has_value(section, key):
return default
else:
key_handle = _winreg.OpenKey(self.reg_handle, self.base_key + section)
try:
value = _winreg.QueryValueEx(key_handle, key)[0]
except EnvironmentError:
value = default
finally:
_winreg.CloseKey(key_handle)
return value
def set_value(self, section, key, value):
"""
Set a single value in the Windows registry.
"""
reg_type = _winreg.REG_BINARY
if type(value) is types.StringType:
reg_type = _winreg.REG_SZ
elif type(value) is types.IntType:
reg_type = _winreg.REG_DWORD
key_handle = _winreg.OpenKey(self.reg_handle, self.base_key + section)
_winreg.SetValueEx(key_handle, key, 0, reg_type, value)
_winreg.CloseKey(key_handle)
def delete_value(self, section, key):
"""
Delete a value from the Windows registry.
"""
key_handle = _winreg.OpenKey(self.reg_handle, self.base_key + section)
_winreg.DeleteValue(key_handle, key)
_winreg.CloseKey(key_handle)
def has_section(self, section):
"""
Check if a section exists.
"""
key_handle = None
try:
key_handle = _winreg.OpenKey(self.reg_handle, self.base_key + section)
except EnvironmentError:
return False
finally:
if key_handle is None:
return False
_winreg.CloseKey(key_handle)
return True
def create_section(self, section):
"""
Create a new section in the Windows registry.
"""
try:
_winreg.CreateKey(self.reg_handle, self.base_key + section)
return True
except EnvironmentError:
return False
def delete_section(self, section):
key_handle = None
try:
key_handle = _winreg.OpenKey(self.reg_handle, self.base_key)
_winreg.DeleteKey(key_handle, section)
except EnvironmentError:
return False
finally:
if key_handle is None:
return False
_winreg.CloseKey(key_handle)
return True