From: <umg...@us...> - 2006-08-15 15:12:04
|
Revision: 338 Author: umgangee Date: 2006-08-15 08:11:51 -0700 (Tue, 15 Aug 2006) ViewCVS: http://svn.sourceforge.net/pybridge/?rev=338&view=rev Log Message: ----------- Revised environment class to differentiate between installed paths (/usr/bin/, /usr/share/pybridge/glade/ etc) and source dist paths (pybridge/bin/, pybridge/glade/ etc). Modified Paths: -------------- trunk/pybridge/pybridge/environment.py trunk/pybridge/pybridge/server/database.py trunk/pybridge/pybridge/ui/__init__.py trunk/pybridge/pybridge/ui/utils.py Modified: trunk/pybridge/pybridge/environment.py =================================================================== --- trunk/pybridge/pybridge/environment.py 2006-08-15 15:02:10 UTC (rev 337) +++ trunk/pybridge/pybridge/environment.py 2006-08-15 15:11:51 UTC (rev 338) @@ -16,50 +16,80 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -import os, sys +import os +import sys -HOME = os.path.expanduser('~') -if hasattr(sys, 'frozen'): # For py2exe distribution. - CURRENTDIR = os.path.dirname(sys.executable) - BASEDIR = os.path.abspath(CURRENTDIR) -else: - CURRENTDIR = os.path.dirname(os.path.abspath(sys.argv[0])) - BASEDIR = os.path.abspath(os.path.join(CURRENTDIR, '..')) +class Environment: + """This module provides path location services for PyBridge.""" -if os.path.exists(os.path.join(BASEDIR, 'share', 'pybridge')): - DATADIR = os.path.join(BASEDIR, 'share', 'pybridge') -else: - DATADIR = BASEDIR -CONFIG_DIR = os.path.join(HOME, '.pybridge') -CLIENT_SETTINGS_PATH = os.path.join(CONFIG_DIR, 'client.cfg') -USER_DB = os.path.join(CONFIG_DIR, 'users.db') + def __init__(self): + # Locate base directory. + if hasattr(sys, 'frozen'): # If py2exe distribution. + currentdir = os.path.dirname(sys.executable) + self.basedir = os.path.abspath(currentdir) + else: # Typically /usr/ or root of source distribution. + currentdir = os.path.dirname(os.path.abspath(sys.argv[0])) + self.basedir = os.path.normpath(os.path.join(currentdir, '..')) + + # Locate shared resources directory, typically /usr/share/. + if os.path.exists(os.path.join(self.basedir, 'share')): + self.sharedir = os.path.join(self.basedir, 'share') + else: # Root of source distribution. + self.sharedir = self.basedir + + # Locate config directory. + self.configdir = os.path.join(os.path.expanduser('~'), '.pybridge') + if not os.path.exists(self.configdir): + os.mkdir(self.configdir) # Create directory. -if not os.path.isdir(CONFIG_DIR): - os.makedirs(CONFIG_DIR) -LOCALE_DIR = os.path.join(BASEDIR, 'locale') + def find_configfile(self, name): + """A config file is located in <configdir>/""" + return os.path.join(self.configdir, name) -DOCS_DIR = '.' -GLADE_DIR = 'glade' -PIXMAPS_DIR = 'pixmaps' + def find_doc(self, name): + """A documentation file may be located in: + + <sharedir>/doc/pybridge/ (installed) + <basedir>/ (source) + """ + if self.sharedir == self.basedir: + return os.path.join(self.basedir, name) + else: + return os.path.join(self.sharedir, 'doc', 'pybridge', name) -class Environment: - def find_datafile(self, name): - return os.path.join(CONFIG_DIR, name) + def find_glade(self, name): + """A Glade interface file may be located in: + + <sharedir>/pybridge/glade/ (installed) + <basedir>/glade/ (source) + """ + if self.sharedir == self.basedir: + return os.path.join(self.basedir, 'glade', name) + else: + return os.path.join(self.sharedir, 'pybridge', 'glade', name) - def find_doc(self, name): - return os.path.join(DATADIR, DOCS_DIR, name) - def find_glade(self, name): - return os.path.join(DATADIR, GLADE_DIR, name) - def find_pixmap(self, name): - return os.path.join(DATADIR, PIXMAPS_DIR, name) + """A pixmap file may be located in: + + <sharedir>/pybridge/pixmaps/ (installed) + <basedir>/pixmaps/ (source) + """ + if self.sharedir == self.basedir: + return os.path.join(self.basedir, 'pixmaps', name) + else: + return os.path.join(self.sharedir, 'pybridge', 'pixmaps', name) + def get_localedir(self): + """Returns the path of the locale directory.""" + return os.path.join(self.sharedir, 'locale') + + environment = Environment() Modified: trunk/pybridge/pybridge/server/database.py =================================================================== --- trunk/pybridge/pybridge/server/database.py 2006-08-15 15:02:10 UTC (rev 337) +++ trunk/pybridge/pybridge/server/database.py 2006-08-15 15:11:51 UTC (rev 338) @@ -20,7 +20,7 @@ from twisted.internet import defer from twisted.python import failure -from pybridge.environment import USER_DB +from pybridge.environment import environment class DuplicateError(Exception): @@ -36,7 +36,8 @@ def __init__(self): # Open the database file. - self.accounts = shelve.open(USER_DB, 'c', writeback=True) + dbfile = environment.find_configfile('users.db') + self.accounts = shelve.open(dbfile, 'c', writeback=True) def addUser(self, username, **attrs): Modified: trunk/pybridge/pybridge/ui/__init__.py =================================================================== --- trunk/pybridge/pybridge/ui/__init__.py 2006-08-15 15:02:10 UTC (rev 337) +++ trunk/pybridge/pybridge/ui/__init__.py 2006-08-15 15:11:51 UTC (rev 338) @@ -16,7 +16,7 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -from pybridge.environment import LOCALE_DIR +from pybridge.environment import environment def run(): """""" @@ -29,7 +29,7 @@ import locale import gettext locale.setlocale(locale.LC_ALL, '') - gettext.bindtextdomain('pybridge', LOCALE_DIR) + gettext.bindtextdomain('pybridge', environment.get_localedir()) gettext.textdomain('pybridge') gettext.install('pybridge') Modified: trunk/pybridge/pybridge/ui/utils.py =================================================================== --- trunk/pybridge/pybridge/ui/utils.py 2006-08-15 15:02:10 UTC (rev 337) +++ trunk/pybridge/pybridge/ui/utils.py 2006-08-15 15:11:51 UTC (rev 338) @@ -16,7 +16,7 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -from pybridge.environment import CLIENT_SETTINGS_PATH +from pybridge.environment import environment # Set up client with UI event handler. from pybridge.network.client import client @@ -38,7 +38,7 @@ general = {} - def __init__(self, filename=CLIENT_SETTINGS_PATH): + def __init__(self, filename): self.config = ConfigParser.SafeConfigParser() self.filename = filename self.read() @@ -63,7 +63,7 @@ self.config.write(file(self.filename, 'w')) -settings = Settings() +settings = Settings(environment.find_configfile('client.cfg')) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |