Revision: 8105
Author: alexsh
Date: 2010-04-18 12:16:23 +0000 (Sun, 18 Apr 2010)
Log Message:
-----------
cleanup old API detect to Site().has_api()
Modified Paths:
--------------
trunk/pywikipedia/catlib.py
trunk/pywikipedia/upload.py
trunk/pywikipedia/watchlist.py
Modified: trunk/pywikipedia/catlib.py
===================================================================
--- trunk/pywikipedia/catlib.py 2010-04-16 19:12:44 UTC (rev 8104)
+++ trunk/pywikipedia/catlib.py 2010-04-18 12:16:23 UTC (rev 8105)
@@ -179,13 +179,7 @@
This should not be used outside of this module.
"""
- try:
- if wikipedia.config.use_api and self.site().versionnumber() >= 11:
- api_url = self.site().api_address()
- del api_url
- else:
- raise NotImplementedError # version not support
- except NotImplementedError:
+ if not self.site().has_api() or self.site().versionnumber() < 11:
for tag, page in self._oldParseCategory(purge, startFrom):
yield tag, page
return
Modified: trunk/pywikipedia/upload.py
===================================================================
--- trunk/pywikipedia/upload.py 2010-04-16 19:12:44 UTC (rev 8104)
+++ trunk/pywikipedia/upload.py 2010-04-18 12:16:23 UTC (rev 8105)
@@ -220,13 +220,7 @@
If the upload fails, the user is asked whether to try again or not.
If the user chooses not to retry, returns null.
"""
- try:
- if config.use_api and self.targetSite.versionnumber() >= 16:
- x = self.targetSite.api_address()
- del x
- else:
- raise NotImplementedError
- except NotImplementedError:
+ if not self.targetSite.has_api() or self.targetSite.versionnumber() < 16:
return self._uploadImageOld(debug)
if not hasattr(self,'_contents'):
Modified: trunk/pywikipedia/watchlist.py
===================================================================
--- trunk/pywikipedia/watchlist.py 2010-04-16 19:12:44 UTC (rev 8104)
+++ trunk/pywikipedia/watchlist.py 2010-04-18 12:16:23 UTC (rev 8105)
@@ -59,13 +59,7 @@
return pageName in watchlist
def refresh(site, sysop=False):
- try:
- if wikipedia.config.use_api and site.versionnumber() >= 10:
- x = site.api_address()
- del x
- else:
- raise NotImplementedError
- except NotImplementedError:
+ if not site.has_api() or site.versionnumber() < 10:
_refreshOld(site)
# get watchlist special page's URL
@@ -79,7 +73,7 @@
'wlprop': 'title',
}
- wikipedia.output(u'Retrieving watchlist for %s' % repr(site))
+ wikipedia.output(u'Retrieving watchlist for %s via API.' % repr(site))
#wikipedia.put_throttle() # It actually is a get, but a heavy one.
watchlist = []
while True:
Revision: 8104
Author: russblau
Date: 2010-04-16 19:12:44 +0000 (Fri, 16 Apr 2010)
Log Message:
-----------
Updated for use with rewrite branch.
Modified Paths:
--------------
branches/rewrite/generate_user_files.py
Modified: branches/rewrite/generate_user_files.py
===================================================================
--- branches/rewrite/generate_user_files.py 2010-04-16 13:43:18 UTC (rev 8103)
+++ branches/rewrite/generate_user_files.py 2010-04-16 19:12:44 UTC (rev 8104)
@@ -2,9 +2,51 @@
""" Script to create user files (user-config.py, user-fixes.py) """
__version__ = '$Id$'
-import os, sys, codecs, re
+import codecs, os, platform, re, sys
-base_dir = ''
+def get_base_dir():
+ """Return 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 PYWIKIBOT2_DIR environment variable, use the value
+ of it
+ 3. Use (and if necessary create) a 'pywikibot' folder (Windows) or
+ '.pywikibot' directory (Unix and similar) under the user's home
+ directory.
+
+ """
+ # copied from config2.py, without the lines that check whether the
+ # directory already contains a user-config.py file
+ # this code duplication is nasty, should fix
+ NAME = "pywikibot"
+ for arg in sys.argv[1:]:
+ if arg.startswith("-dir:"):
+ base_dir = arg[5:]
+ sys.argv.remove(arg)
+ break
+ else:
+ if "PYWIKIBOT2_DIR" in os.environ:
+ base_dir = os.environ["PYWIKIBOT2_DIR"]
+ else:
+ is_windows = sys.platform == 'win32'
+ home = os.path.expanduser("~")
+ if is_windows:
+ _win_version = int(platform.version()[0])
+ if _win_version == 5:
+ base_dir = os.path.join(home, "Application Data", NAME)
+ elif _win_version == 6:
+ base_dir = os.path.join(home, "AppData\\Roaming", NAME)
+ else:
+ base_dir = os.path.join(home, "."+NAME)
+ if not os.path.isdir(base_dir):
+ os.makedirs(base_dir, mode=0700)
+ if not os.path.isabs(base_dir):
+ base_dir = os.path.normpath(os.path.join(os.getcwd(), base_dir))
+ return base_dir
+
+base_dir = get_base_dir()
console_encoding = sys.stdout.encoding
if console_encoding is None or sys.platform == 'cygwin':
@@ -35,6 +77,47 @@
print("Invalid response")
return response
+def change_base_dir():
+ """Create a new user directory."""
+ global base_dir
+ while True:
+ new_base = raw_input("New user directory? ")
+ new_base = os.path.abspath(new_base)
+ if os.path.exists(new_base):
+ if os.path.isfile(new_base):
+ print("ERROR: there is an existing file with that name.")
+ continue
+ # make sure user can read and write this directory
+ if not os.access(new_base, os.R_OK|os.W_OK):
+ print("ERROR: directory access restricted")
+ continue
+ print("OK: using existing directory")
+ break
+ else:
+ try:
+ os.mkdir(new_base, 0700)
+ except Exception:
+ print("ERROR: directory creation failed")
+ continue
+ print("OK: Created new directory.")
+ break
+
+ from textwrap import wrap
+ msg = wrap("""WARNING: Your user files will be created in the directory
+'%(new_base)s' you have chosen. To access these files, you will either have
+to use the argument "-dir:%(new_base)s" every time you run the bot, or set
+the environment variable "PYWIKIBOT2_DIR" equal to this directory name in
+your operating system. See your operating system documentation for how to
+set environment variables.""" % locals(), width=76)
+ for line in msg:
+ print line
+ ok = raw_input("Is this OK? [y/N] ")
+ if ok in ["Yy"]:
+ base_dir = new_base
+ return True
+ print "Aborting changes."
+ return False
+
def file_exists(filename):
if os.path.exists(filename):
print("'%s' already exists." % filename)
@@ -44,19 +127,32 @@
def create_user_config():
_fnc = os.path.join(base_dir, "user-config.py")
if not file_exists(_fnc):
- know_families = re.findall(r'(.+)_family.py\b', '\n'.join(os.listdir(os.path.join(base_dir, "families"))))
- fam = listchoice(know_families, "Select family of sites we are working on", default = 'wikipedia')
- mylang = raw_input("The language code of the site we're working on (default: 'en'): ") or 'en'
- username = raw_input("Username (%s %s): " % (mylang, fam)) or 'UnnamedBot'
+ known_families = re.findall(r'(.+)_family.py\b',
+ '\n'.join(os.listdir(os.path.join(base_dir, "families"))))
+ fam = listchoice(known_families,
+ "Select family of sites we are working on",
+ default='wikipedia')
+ mylang = raw_input(
+"The language code of the site we're working on (default: 'en'): ") or 'en'
+ username = raw_input("Username (%s %s): " % (mylang, fam)
+ ) or 'UnnamedBot'
username = unicode(username, console_encoding)
while True:
- choice = raw_input("Which variant of user_config.py:\n[S]mall or [E]xtended (with further informations)? ").upper()
- if choice in ['S','E']:
+ choice = raw_input(
+"Which variant of user_config.py:\n[S]mall or [E]xtended (with further information)? "
+ ).upper()
+ if choice in "SE":
break
#
# I don't like this solution. Temporary for me.
- f = codecs.open("config.py", "r", "utf-8") ; cpy = f.read() ; f.close()
+ #
+ # determine what directory this script (generate_user_files.py) lives in
+ install = os.path.dirname(os.path.abspath(sys.argv[0]))
+ # config2.py will be in the pywikibot/ directory
+ f = codecs.open(os.path.join(install, "pywikibot", "config2.py"),
+ "r", "utf-8")
+ cpy = f.read() ; f.close()
res = re.findall("^(############## (?:LOGFILE|"
"INTERWIKI|"
@@ -67,14 +163,16 @@
"DATABASE|"
"SEARCH ENGINE|"
"COPYRIGHT|"
- "FURTHER) SETTINGS .*?)^(?=#####|# =====)", cpy, re.MULTILINE | re.DOTALL)
+ "FURTHER) SETTINGS .*?)^(?=#####|# =====)",
+ cpy, re.MULTILINE | re.DOTALL)
config_text = '\n'.join(res)
f = codecs.open(_fnc, "w", "utf-8")
if choice == 'E':
f.write("""# -*- coding: utf-8 -*-
-# This is an automatically generated file. You can find more configuration parameters in 'config.py' file.
+# This is an automatically generated file. You can find more configuration
+# parameters in 'config.py' file.
# The family of sites we are working on. wikipedia.py will import
# families/xxx_family.py so if you want to change this variable,
@@ -124,16 +222,51 @@
print("'%s' written." % _fnf)
if __name__ == "__main__":
- print("1: Create user_config.py file")
- print("2: Create user_fixes.py file")
- print("3: The two files")
- choice = raw_input("What do you do? ")
- if choice == "1":
- create_user_config()
- if choice == "2":
- create_user_fixes()
- if choice == "3":
- create_user_config()
- create_user_fixes()
- if not choice in ["1", "2", "3"]:
- print("Nothing to do")
+ while True:
+ print("Your default user directory is '%s'" % base_dir)
+ ok = raw_input("[K]eep/Change? ").upper().strip()
+ if (not ok) or "KEEP".startswith(ok):
+ break
+ if "CHANGE".startswith(ok):
+ if change_base_dir():
+ break
+ while True:
+ if os.path.exists(os.path.join(base_dir, "user-config.py")):
+ break
+ do_copy = raw_input(
+ "Do you want to copy user files from an existing pywikipedia installation? "
+ ).upper().strip()
+ if do_copy and "YES".startswith(do_copy):
+ oldpath = raw_input("Path to existing wikipedia.py? ")
+ if not os.path.exists(oldpath):
+ print("ERROR: Not a valid path")
+ continue
+ if os.path.isfile(oldpath):
+ # User probably typed /wikipedia.py at the end, so strip it
+ oldpath = os.path.dirname(oldpath)
+ if not os.path.isfile(os.path.join(oldpath, "user-config.py")):
+ print("ERROR: no user_config.py found in that directory")
+ continue
+ newf = file(os.path.join(base_dir, "user-config.py"), "wb")
+ oldf = file(os.path.join(oldpath, "user-config.py"), "rb")
+ newf.write(oldf.read())
+ newf.close() ; oldf.close()
+ if os.path.isfile(os.path.join(oldpath, "user-fixes.py")):
+ newfix = file(os.path.join(base_dir, "user-fixes.py"), "wb")
+ oldfix = file(os.path.join(oldpath, "user-fixes.py"), "rb")
+ newfix.write(oldfix.read())
+ newfix.close() ; oldfix.close()
+ elif do_copy and "NO".startswith(do_copy):
+ break
+ if not os.path.isfile(os.path.join(base_dir, "user-config.py")):
+ a = raw_input("Create user-config.py file? [y/N] ")
+ if a[:1] in ["Y", "y"]:
+ create_user_config()
+ else:
+ print("NOTE: user-config.py already exists in the directory")
+ if not os.path.isfile(os.path.join(base_dir, "user-fixes.py")):
+ a = raw_input("Create user-fixes.py file? [y/N] ")
+ if a[:1] in ["Y", "y"]:
+ create_user_fixes()
+ else:
+ print("NOTE: user-fixes.py already exists in the directory")
Revision: 8101
Author: xqt
Date: 2010-04-16 09:08:26 +0000 (Fri, 16 Apr 2010)
Log Message:
-----------
use unicode strings, minor changes
Modified Paths:
--------------
branches/rewrite/scripts/interwiki.py
trunk/pywikipedia/interwiki.py
Modified: branches/rewrite/scripts/interwiki.py
===================================================================
--- branches/rewrite/scripts/interwiki.py 2010-04-16 08:47:13 UTC (rev 8100)
+++ branches/rewrite/scripts/interwiki.py 2010-04-16 09:08:26 UTC (rev 8101)
@@ -1645,7 +1645,7 @@
except pywikibot.NoPage:
pywikibot.output(u"BUG>>> %s no longer exists?" % new[site])
continue
- mods, adding, removing, modifying = compareLanguages(old, new, insite = lclSite)
+ mods, mcomment, adding, removing, modifying = compareLanguages(old, new, insite = lclSite)
if (len(removing) > 0 and not globalvar.autonomous) or (len(modifying) > 0 and self.problemfound) or len(old) == 0 or (globalvar.needlimit and len(adding) + len(modifying) >= globalvar.needlimit +1):
try:
if self.replaceLinks(new[site], new, bot):
@@ -1781,8 +1781,8 @@
pywikibot.output(u"Changes to be made: %s" % mods)
oldtext = page.get()
template = (page.namespace() == 10)
- newtext = pywikibot.replaceLanguageLinks(oldtext, new,
- site = page.site(),
+ newtext = pywikibot.replaceLanguageLinks(oldtext, new,
+ site = page.site(),
template = template)
# This is for now. Later there should be different funktions for each kind
if not botMayEdit(page):
@@ -1845,7 +1845,7 @@
timeout=60
while 1:
try:
- status, reason, data = page.put(newtext, comment = pywikibot.translate(page.site().lang, msg)[0] + mods)
+ status, reason, data = page.put(newtext, comment = mcomment)
except pywikibot.LockedPage:
pywikibot.output(u'Page %s is locked. Skipping.' % (page,))
raise SaveError(u'Locked')
Modified: trunk/pywikipedia/interwiki.py
===================================================================
--- trunk/pywikipedia/interwiki.py 2010-04-16 08:47:13 UTC (rev 8100)
+++ trunk/pywikipedia/interwiki.py 2010-04-16 09:08:26 UTC (rev 8101)
@@ -1156,14 +1156,14 @@
pywikibot.output(u"NOTE: Ignoring non-disambiguation page %s for %s because disambiguation page %s has already been found." % (page.aslink(True), self.originPage.aslink(True), disambig.aslink(True)))
return (True, None)
else:
- choice = pywikibot.inputChoice('WARNING: %s is a disambiguation page, but %s doesn\'t seem to be one. Follow it anyway?' % (self.originPage.aslink(True), page.aslink(True)), ['Yes', 'No', 'Add an alternative', 'Give up'], ['y', 'n', 'a', 'g'])
+ choice = pywikibot.inputChoice(u'WARNING: %s is a disambiguation page, but %s doesn\'t seem to be one. Follow it anyway?' % (self.originPage.aslink(True), page.aslink(True)), ['Yes', 'No', 'Add an alternative', 'Give up'], ['y', 'n', 'a', 'g'])
elif not self.originPage.isDisambig() and page.isDisambig():
nondisambig = self.getFoundNonDisambig(page.site())
if nondisambig:
pywikibot.output(u"NOTE: Ignoring disambiguation page %s for %s because non-disambiguation page %s has already been found." % (page.aslink(True), self.originPage.aslink(True), nondisambig.aslink(True)))
return (True, None)
else:
- choice = pywikibot.inputChoice('WARNING: %s doesn\'t seem to be a disambiguation page, but %s is one. Follow it anyway?' % (self.originPage.aslink(True), page.aslink(True)), ['Yes', 'No', 'Add an alternative', 'Give up'], ['y', 'n', 'a', 'g'])
+ choice = pywikibot.inputChoice(u'WARNING: %s doesn\'t seem to be a disambiguation page, but %s is one. Follow it anyway?' % (self.originPage.aslink(True), page.aslink(True)), ['Yes', 'No', 'Add an alternative', 'Give up'], ['y', 'n', 'a', 'g'])
if choice == 'n':
return (True, None)
elif choice == 'a':
@@ -1191,7 +1191,7 @@
if config.without_interwiki:
f = codecs.open(
pywikibot.config.datafilepath('without_interwiki.txt'), 'a', 'utf-8')
- f.write("# %s \n" % page.aslink(forceInterwiki=True))
+ f.write(u"# %s \n" % page.aslink(forceInterwiki=True))
f.close()
def askForHints(self, counter):
@@ -1317,7 +1317,7 @@
pywikibot.output(u"%s: %s gives new %sredirect %s"
% (self.originPage.aslink(), page.aslink(True), redir, redirectTargetPage.aslink(True)))
continue
-
+
# must be behind the page.isRedirectPage() part
# otherwise a redirect error would be raised
if page.isEmpty() and not page.isCategory():
@@ -1381,10 +1381,10 @@
f = codecs.open(
pywikibot.config.datafilepath('autonomous_problems.dat'),
'a', 'utf-8')
- f.write("* %s {Found more than one link for %s}" % (self.originPage.aslink(True), page.site()))
+ f.write(u"* %s {Found more than one link for %s}" % (self.originPage.aslink(True), page.site()))
if config.interwiki_graph and config.interwiki_graph_url:
filename = interwiki_graph.getFilename(self.originPage, extension = config.interwiki_graph_formats[0])
- f.write(" [%s%s graph]" % (config.interwiki_graph_url, filename))
+ f.write(u" [%s%s graph]" % (config.interwiki_graph_url, filename))
f.write("\n")
f.close()
# FIXME: What errors are we catching here?
@@ -1460,7 +1460,7 @@
site = page.site()
if site == self.originPage.site():
if page != self.originPage:
- self.problem("Found link to %s" % page.aslink(True) )
+ self.problem(u"Found link to %s" % page.aslink(True) )
self.whereReport(page)
errorCount += 1
else:
@@ -1473,7 +1473,7 @@
for site, pages in new.iteritems():
if len(pages) > 1:
errorCount += 1
- self.problem("Found more than one link for %s" % site)
+ self.problem(u"Found more than one link for %s" % site)
if not errorCount and not globalvar.select:
# no errors, so all lists have only one item
@@ -1755,7 +1755,9 @@
pywikibot.output(u"Changes to be made: %s" % mods)
oldtext = page.get()
template = (page.namespace() == 10)
- newtext = pywikibot.replaceLanguageLinks(oldtext, new, site = page.site(), template = template)
+ newtext = pywikibot.replaceLanguageLinks(oldtext, new,
+ site = page.site(),
+ template = template)
# This is for now. Later there should be different funktions for each kind
if not botMayEdit(page):
if template:
@@ -1771,7 +1773,7 @@
# Determine whether we need permission to submit
ask = False
if removing and removing != [page.site()]: # Allow for special case of a self-pointing interwiki link
- self.problem('Found incorrect link to %s in %s'% (",".join([x.lang for x in removing]), page.aslink(True)), createneed = False)
+ self.problem(u'Found incorrect link to %s in %s'% (",".join([x.lang for x in removing]), page.aslink(True)), createneed = False)
ask = True
if globalvar.force:
ask = False
@@ -1791,7 +1793,7 @@
page.site().hostname(),
page.site().nice_get_address(page.title())
))
- pywikibot.input("Press Enter when finished in browser.")
+ pywikibot.input(u"Press Enter when finished in browser.")
return True
elif answer == 'a':
# don't ask for the rest of this subject
@@ -1854,7 +1856,7 @@
elif answer == 'g':
raise GiveUpOnPage
else:
- raise LinkMustBeRemoved('Found incorrect link to %s in %s'% (",".join([x.lang for x in removing]), page.aslink(True)))
+ raise LinkMustBeRemoved(u'Found incorrect link to %s in %s'% (",".join([x.lang for x in removing]), page.aslink(True)))
def reportBacklinks(self, new, updatedSites):
"""