Revision: 4292 Author: russblau Date: 2007-09-14 19:04:11 +0000 (Fri, 14 Sep 2007)
Log Message: ----------- Provide more options for users to specify configuration directories; save base path in wikipedia namespace after determining it the first time.
Modified Paths: -------------- trunk/pywikipedia/config.py trunk/pywikipedia/interwiki.py trunk/pywikipedia/version.py trunk/pywikipedia/welcome.py trunk/pywikipedia/wikipedia.py trunk/pywikipedia/wikipediatools.py
Modified: trunk/pywikipedia/config.py =================================================================== --- trunk/pywikipedia/config.py 2007-09-14 18:57:50 UTC (rev 4291) +++ trunk/pywikipedia/config.py 2007-09-14 19:04:11 UTC (rev 4292) @@ -66,9 +66,10 @@
# Get the names of all known families, and initialize # with empty dictionaries +import wikipediatools as _wt +_base_dir = _wt.get_base_dir() _RfamilyFile = re.compile('(?P<name>.+)_family.py$') -import wikipediatools as _wt -for _filename in os.listdir(_wt.absoluteFilename('families')): +for _filename in os.listdir(os.path.join(_base_dir, 'families')): _m = _RfamilyFile.match(_filename) if _m: familyName = _m.group('name') @@ -402,7 +403,7 @@
# Get the user files _thislevel=0 -_fns=[_wt.absoluteFilename("user-config.py")] +_fns=[os.path.join(_base_dir, "user-config.py")] for _filename in _fns: _thislevel += 1 if os.path.exists(_filename):
Modified: trunk/pywikipedia/interwiki.py =================================================================== --- trunk/pywikipedia/interwiki.py 2007-09-14 18:57:50 UTC (rev 4291) +++ trunk/pywikipedia/interwiki.py 2007-09-14 19:04:11 UTC (rev 4292) @@ -208,6 +208,7 @@ #
import sys, copy, re +import os.path import time import codecs import socket @@ -1164,8 +1165,10 @@
def dump(self): site = wikipedia.getSite() - import wikipediatools as _wt - dumpfn = _wt.absoluteFilename('interwiki-dumps', 'interwikidump-%s-%s.txt' % (site.family.name, site.lang)) + dumpfn = os.path.join(wikipedia.base_dir, + 'interwiki-dumps', + 'interwikidump-%s-%s.txt' + % (site.family.name, site.lang)) f = codecs.open(dumpfn, 'w', 'utf-8') for subj in self.subjects: f.write(subj.originPage.aslink(None)+'\n') @@ -1517,8 +1520,10 @@
if optRestore or optContinue: site = wikipedia.getSite() - import wikipediatools as _wt - dumpFileName = _wt.absoluteFilename('interwiki-dumps', u'interwikidump-%s-%s.txt' % (site.family.name, site.lang)) + dumpFileName = os.path.join(wikipedia.base_dir, + 'interwiki-dumps', + u'interwikidump-%s-%s.txt' + % (site.family.name, site.lang)) hintlessPageGen = pagegenerators.TextfilePageGenerator(dumpFileName) if optContinue: # We waste this generator to find out the last page's title
Modified: trunk/pywikipedia/version.py =================================================================== --- trunk/pywikipedia/version.py 2007-09-14 18:57:50 UTC (rev 4291) +++ trunk/pywikipedia/version.py 2007-09-14 19:04:11 UTC (rev 4292) @@ -6,7 +6,7 @@ # __version__ = '$Id$'
-import wikipediatools +import os.path import time import sys
@@ -28,11 +28,11 @@ tag = '' date = time.strptime('T'.join(d[3:5]), '%Y-%m-%dT%H:%M:%SZ') rev = d[2] + ' (wikipedia.py)' - datestring = time.strftime('%b %d %Y, %H:%M:%S', date) - return {'tag': tag, 'rev': rev, 'date': datestring} + return {'tag': tag, 'rev': rev, 'date': datestring} + def getversion_svn(): - entries = open(wikipediatools.absoluteFilename('.svn/entries')) + entries = open(os.path.join(wikipedia.base_dir, '.svn/entries')) for i in range(4): entries.readline() tag = entries.readline().strip() @@ -48,7 +48,7 @@ return (tag, rev, date)
def getversion_nightly(): - data = open(wikipediatools.absoluteFilename('version')) + data = open(os.path.join(wikipedia.base_dir, 'version')) tag = data.readline().strip() date = time.strptime(data.readline()[:19],'%Y-%m-%dT%H:%M:%S') rev = data.readline().strip()
Modified: trunk/pywikipedia/welcome.py =================================================================== --- trunk/pywikipedia/welcome.py 2007-09-14 18:57:50 UTC (rev 4291) +++ trunk/pywikipedia/welcome.py 2007-09-14 19:04:11 UTC (rev 4292) @@ -171,7 +171,7 @@
import wikipedia, config, string, locale import time, re, cPickle, os, urllib -import codecs, wikipediatools +import codecs
locale.setlocale(locale.LC_ALL, '')
@@ -443,9 +443,11 @@ wikipedia.output(u'Error! - No fileName!') raise FilenameNotSet("No signature filename specified.") try: - f = codecs.open(wikipediatools.absoluteFilename(fileSignName), 'r', encoding = config.console_encoding) + f = codecs.open(os.path.join(wikipedia.base_dir, fileSignName), 'r', + encoding = config.console_encoding) except: - f = codecs.open(wikipediatools.absoluteFilename(fileSignName), 'r', encoding = 'utf-8') + f = codecs.open(os.path.join(wikipedia.base_dir, fileSignName), 'r', + encoding = 'utf-8') signText = f.read() f.close()
@@ -621,7 +623,8 @@ welcomer = u'{{subst:Benvenuto}} %s'
welcomed_users = list() - if savedata == True and os.path.exists(wikipediatools.absoluteFilename(filename)): + if savedata == True and os.path.exists(os.path.join(wikipedia.base_dir, + filename)): f = file(filename) number_user = cPickle.load(f) yield number_user
Modified: trunk/pywikipedia/wikipedia.py =================================================================== --- trunk/pywikipedia/wikipedia.py 2007-09-14 18:57:50 UTC (rev 4291) +++ trunk/pywikipedia/wikipedia.py 2007-09-14 19:04:11 UTC (rev 4292) @@ -107,7 +107,7 @@ """ from __future__ import generators # -# (C) Pywikipedia bot team, 2003-2006 +# (C) Pywikipedia bot team, 2003-2007 # # Distributed under the terms of the MIT license. # @@ -123,17 +123,22 @@ import htmlentitydefs import warnings import unicodedata - -import config, login import xmlreader from BeautifulSoup import * import simplejson
-# we'll set the locale to system default. This will ensure correct string +# Set the locale to system default. This will ensure correct string # handling for non-latin characters on Python 2.3.x. For Python 2.4.x it's no # longer needed. locale.setlocale(locale.LC_ALL, '')
+# Before importing config, determine the user's base directory +import wikipediatools +base_dir = wikipediatools.get_base_dir() +del wikipediatools + +import config, login + try: set # introduced in Python2.4: faster and future except NameError: @@ -2428,8 +2433,7 @@ self.setDelay(mindelay)
def logfn(self): - import wikipediatools as _wt - return _wt.absoluteFilename('throttle.log') + return os.path.join(base_dir, 'throttle.log')
def checkMultiplicity(self): self.lock.acquire() @@ -3168,8 +3172,7 @@ fam = config.family try: # search for family module in the 'families' subdirectory - import wikipediatools as _wt - sys.path.append(_wt.absoluteFilename('families')) + sys.path.append(os.path.join(base_dir, 'families')) exec "import %s_family as myfamily" % fam except ImportError: if fatal: @@ -3378,9 +3381,9 @@ self._cookies = None self.loginStatusKnown = True else: - import wikipediatools as _wt - tmp = '%s-%s-%s-login.data' % (self.family.name, self.lang, username) - fn = _wt.absoluteFilename('login-data', tmp) + tmp = '%s-%s-%s-login.data' % ( + self.family.name, self.lang, username) + fn = os.path.join(base_dir, 'login-data', tmp) if not os.path.exists(fn): self._cookies = None self.loginStatusKnown = True @@ -4363,7 +4366,7 @@ that are not global. This makes sure that global arguments are applied first, regardless of the order in which the arguments were given. ''' - global default_code, default_family, verbose + global user_directory, default_code, default_family, verbose # get commandline arguments args = sys.argv # get the name of the module calling this function. This is @@ -4374,8 +4377,8 @@ nonGlobalArgs = [] for arg in args[1:]: if sys.platform=='win32': - # stupid Windows gives parameters encoded as windows-1252, but input - # encoded as cp850 + # Windows gives parameters encoded as windows-1252, + # regardless of console encoding arg = unicode(arg, 'windows-1252') else: # Linux uses the same encoding for both @@ -4383,11 +4386,14 @@ if arg == '-help': showHelp(moduleName) sys.exit(0) + if arg.startswith('-dir:'): + user_directory = arg[5:] + if not os.path.isdir(user_directory): + raise RuntimeError("Directory '%s' does not exist." + % user_directory) elif arg.startswith('-family:'): - global default_family default_family = arg[8:] elif arg.startswith('-lang:'): - global default_code default_code = arg[6:] elif arg.startswith('-putthrottle:'): put_throttle.setDelay(int(arg[13:]), absolute = True) @@ -4415,8 +4421,7 @@ #########################
# search for user interface module in the 'userinterfaces' subdirectory -import wikipediatools as _wt -sys.path.append(_wt.absoluteFilename('userinterfaces')) +sys.path.append(os.path.join(base_dir, 'userinterfaces')) exec "import %s_interface as uiModule" % config.userinterface ui = uiModule.UI() verbose = 0 @@ -4653,8 +4658,7 @@ if enabled: if not logname: logname = '%s.log' % calledModuleName() - import wikipediatools as _wt - logfn = _wt.absoluteFilename('logs', logname) + logfn = os.path.join(base_dir, 'logs', logname) try: logfile = codecs.open(logfn, 'a', 'utf-8') except IOError: @@ -4928,8 +4932,7 @@ # Special opener in case we are using a site with authentication if config.authenticate: import urllib2, cookielib - import wikipediatools as _wt - COOKIEFILE = _wt.absoluteFilename('login-data', 'cookies.lwp') + COOKIEFILE = os.path.join(base_dir, 'login-data', 'cookies.lwp') cj = cookielib.LWPCookieJar() if os.path.isfile(COOKIEFILE): cj.load(COOKIEFILE)
Modified: trunk/pywikipedia/wikipediatools.py =================================================================== --- trunk/pywikipedia/wikipediatools.py 2007-09-14 18:57:50 UTC (rev 4291) +++ trunk/pywikipedia/wikipediatools.py 2007-09-14 19:04:11 UTC (rev 4292) @@ -1,28 +1,40 @@ __version__ = '$Id$' import os, sys
-def absoluteFilename(*f): - """Return an absolute path to the filename given as argument; - optionally a directory may be given as the first argument and - filename as the second. - The path is based on the directory from which the script is being - run, if it contains a 'user-config.py' file; otherwise on the directory - from which this module was loaded. +def get_base_dir(): + """ Determine the directory in which user-specific information is stored. + This is determined in the following order - + 1. If the script was called with a -dir: argument, use the directory provided + in this argument + 2. If the user has a PYWIKIBOT_DIR environment variable, use the value of it + 3. If the script was started from a directory that contains a user-config.py + file, use this directory as the base + 4. If all else fails, use the directory from which this module was loaded """ - if os.path.exists('user-config.py'): - #There's config in the current directory, so assume login-data etc will be here as well - location = "." + for arg in sys.argv[1:]: + if arg.startswith("-dir:"): + base_dir = arg[5:] + sys.argv.remove(arg) + break else: - try: - mod = sys.modules['wikipediatools'] - except KeyError: - print sys.modules - location = None + if os.environ.has_key("PYWIKIBOT_DIR"): + base_dir = os.environ["PYWIKIBOT_DIR"] else: - path = mod.__file__ - location = os.path.split(path)[0] - if not location: - location='.' - if not os.path.isabs(location): - location = os.path.normpath(os.path.join(os.getcwd(), location)) - return os.path.join(location,*f) + if os.path.exists('user-config.py'): + base_dir = '.' + else: + try: + base_dir = os.path.split( + sys.modules['wikipediatools'].__file__)[0] + except KeyError: + print sys.modules + base_dir = '.' + if not os.path.isabs(base_dir): + base_dir = os.path.normpath(os.path.join(os.getcwd(), base_dir)) + # make sure this path is valid and that it contains user-config file + if not os.path.isdir(base_dir): + raise RuntimeError("Directory '%s' does not exist." % base_dir) + if not os.path.exists(os.path.join(base_dir, "user-config.py")): + raise RuntimeError("No user-config.py found in directory '%s'." + % base_dir) + return base_dir
pywikipedia-l@lists.wikimedia.org