http://www.mediawiki.org/wiki/Special:Code/pywikipedia/10282
Revision: 10282
Author: xqt
Date: 2012-06-03 11:13:46 +0000 (Sun, 03 Jun 2012)
Log Message:
-----------
fix some wiki syntax before cleanup links.
translateMagicWords implemented but not freed yet,
minor changes due PEP 8
Modified Paths:
--------------
trunk/pywikipedia/cosmetic_changes.py
Modified: trunk/pywikipedia/cosmetic_changes.py
===================================================================
--- trunk/pywikipedia/cosmetic_changes.py 2012-06-03 10:47:51 UTC (rev 10281)
+++ trunk/pywikipedia/cosmetic_changes.py 2012-06-03 11:13:46 UTC (rev 10282)
@@ -135,6 +135,7 @@
(u'Belege', u'Belege fehlen\g<parameters>'),
(u'Quelle', u'Belege fehlen\g<parameters>'),
(u'Quellen', u'Belege fehlen\g<parameters>'),
+ (u'Quellen fehlen', u'Belege fehlen\g<parameters>'),
],
}
}
@@ -159,16 +160,18 @@
text = self.commonsfiledesc(text)
text = self.fixSelfInterwiki(text)
text = self.standardizePageFooter(text)
+ text = self.fixSyntaxSave(text)
text = self.cleanUpLinks(text)
text = self.cleanUpSectionHeaders(text)
text = self.putSpacesInLists(text)
text = self.translateAndCapitalizeNamespaces(text)
+## text = self.translateMagicWords(text)
text = self.replaceDeprecatedTemplates(text)
text = self.resolveHtmlEntities(text)
text = self.validXhtml(text)
text = self.removeUselessSpaces(text)
text = self.removeNonBreakingSpaceBeforePercent(text)
- text = self.fixSyntaxSave(text)
+
text = self.fixHtml(text)
text = self.fixReferences(text)
text = self.fixStyle(text)
@@ -245,7 +248,9 @@
# German Wikipedia. See
# http://de.wikipedia.org/wiki/Hilfe_Diskussion:Personendaten/Archiv/1#Positi…
# ignoring nn-wiki of cause of the comment line above iw section
- if not self.template and not '{{Personendaten' in text:
+ if not self.template and not '{{Personendaten' in text and \
+ not '{{SORTIERUNG' in text and not '{{DEFAULTSORT' in text and \
+ not self.site.lang in ('et', 'it', 'bg', 'ru'):
categories = pywikibot.getCategoryLinks(text, site = self.site)
if not self.talkpage:# and pywikibot.calledModuleName() <> 'interwiki':
@@ -366,6 +371,23 @@
':\g<nameAndLabel>]]', exceptions)
return text
+ def translateMagicWords(self, text):
+ """
+ Makes sure that localized namespace names are used.
+ """
+ # not wanted at ru
+ # arz uses english stylish codes
+ if self.site.lang not in ['arz', 'ru']:
+ exceptions = ['nowiki', 'comment', 'math', 'pre']
+ for magicWord in ['img_thumbnail', 'img_left', 'img_center', 'img_right', 'img_none',
+ 'img_framed', 'img_frameless', 'img_border', 'img_upright',]:
+ aliases = self.site.siteinfo('magicwords').get(magicWord)
+ if not aliases: continue
+ text = pywikibot.replaceExcept(text, r'\[\[(?P<left>.+?:.+?\..+?\|) *(' + '|'.join(aliases) +') *(?P<right>(\|.*?)?\]\])',
+ r'[[\g<left>' + aliases[0] + '\g<right>',
+ exceptions)
+ return text
+
def cleanUpLinks(self, text):
# helper function which works on one link and either returns it
# unmodified, or returns a replacement.
@@ -590,19 +612,35 @@
#from fixes.py
def fixSyntaxSave(self, text):
- exceptions = ['nowiki', 'comment', 'math', 'pre', 'source', 'startspace']
+ exceptions = ['nowiki', 'comment', 'math', 'pre', 'source',
+ 'startspace']
+ # link to the wiki working on
+ ## TODO: disable this for difflinks and titled links
+ ## http://de.wikipedia.org/w/index.php?title=Wikipedia%3aVandalismusmeldung&di…
+## text = pywikibot.replaceExcept(text,
+## r'\[https?://%s\.%s\.org/wiki/(?P<link>\S+)\s+(?P<title>.+?)\s?\]'
+## % (self.site.lang, self.site.family.name),
+## r'[[\g<link>|\g<title>]]', exceptions)
# external link in double brackets
- text = pywikibot.replaceExcept(text, r'\[\[(?P<url>https?://[^\]]+?)\]\]', r'[\g<url>]', exceptions)
+ text = pywikibot.replaceExcept(text,
+ r'\[\[(?P<url>https?://[^\]]+?)\]\]',
+ r'[\g<url>]', exceptions)
# external link starting with double bracket
- text = pywikibot.replaceExcept(text, r'\[\[(?P<url>https?://.+?)\]', r'[\g<url>]', exceptions)
+ text = pywikibot.replaceExcept(text,
+ r'\[\[(?P<url>https?://.+?)\]',
+ r'[\g<url>]', exceptions)
# external link and description separated by a dash, with
# whitespace in front of the dash, so that it is clear that
# the dash is not a legitimate part of the URL.
- text = pywikibot.replaceExcept(text, r'\[(?P<url>https?://[^\|\] \r\n]+?) +\| *(?P<label>[^\|\]]+?)\]', r'[\g<url> \g<label>]', exceptions)
+ text = pywikibot.replaceExcept(text,
+ r'\[(?P<url>https?://[^\|\] \r\n]+?) +\| *(?P<label>[^\|\]]+?)\]',
+ r'[\g<url> \g<label>]', exceptions)
# dash in external link, where the correct end of the URL can
# be detected from the file extension. It is very unlikely that
# this will cause mistakes.
- text = pywikibot.replaceExcept(text, r'\[(?P<url>https?://[^\|\] ]+?(\.pdf|\.html|\.htm|\.php|\.asp|\.aspx|\.jsp)) *\| *(?P<label>[^\|\]]+?)\]', r'[\g<url> \g<label>]', exceptions)
+ text = pywikibot.replaceExcept(text,
+ r'\[(?P<url>https?://[^\|\] ]+?(\.pdf|\.html|\.htm|\.php|\.asp|\.aspx|\.jsp)) *\| *(?P<label>[^\|\]]+?)\]',
+ r'[\g<url> \g<label>]', exceptions)
return text
def fixHtml(self, text):
http://www.mediawiki.org/wiki/Special:Code/pywikipedia/10278
Revision: 10278
Author: xqt
Date: 2012-06-03 10:25:52 +0000 (Sun, 03 Jun 2012)
Log Message:
-----------
use login librays from pywikibot, remove code duplications, solve r8155
Modified Paths:
--------------
branches/rewrite/scripts/login.py
Modified: branches/rewrite/scripts/login.py
===================================================================
--- branches/rewrite/scripts/login.py 2012-06-03 09:02:44 UTC (rev 10277)
+++ branches/rewrite/scripts/login.py 2012-06-03 10:25:52 UTC (rev 10278)
@@ -40,7 +40,7 @@
"""
#
# (C) Rob W.W. Hooft, 2003
-# (C) Pywikipedia bot team, 2003-2010
+# (C) Pywikipedia bot team, 2003-2012
#
# Distributed under the terms of the MIT license.
#
@@ -50,183 +50,10 @@
import pywikibot
from pywikibot import config, deprecate_arg
from pywikibot.exceptions import NoSuchSite, NoUsername
+from pywikibot.login import LoginManager
_logger = "wiki.login"
-
-# On some wikis you are only allowed to run a bot if there is a link to
-# the bot's user page in a specific list.
-# If bots are listed in a template, the templates name must be given as
-# second parameter, otherwise it must be None
-botList = {
- 'wikipedia': {
- 'en': [u'Wikipedia:Bots/Status', 'BotS'],
- 'simple': [u'Wikipedia:Bots', '/links']
- },
- 'gentoo': {
- 'en': [u'Help:Bots', None],
- }
-}
-
-
-class LoginManager:
- @deprecate_arg("username", "user")
- @deprecate_arg("verbose", None)
- def __init__(self, password=None, sysop=False, site=None, user=None):
- if site is not None:
- self.site = site
- else:
- self.site = pywikibot.Site()
- if user:
- self.username = user
- elif sysop:
- try:
- self.username = config.sysopnames\
- [self.site.family.name][self.site.code]
- except KeyError:
- raise NoUsername(
-u"""ERROR: Sysop username for %(fam_name)s:%(wiki_code)s is undefined.
-If you have a sysop account for that site, please add a line to user-config.py:
-
-sysopnames['%(fam_name)s']['%(wiki_code)s'] = 'myUsername'"""
- % {'fam_name': self.site.family.name,
- 'wiki_code': self.site.code})
- else:
- try:
- self.username = config.usernames\
- [self.site.family.name][self.site.code]
- except:
- raise NoUsername(
-u"""ERROR: Username for %(fam_name)s:%(wiki_code)s is undefined.
-If you have an account for that site, please add a line to user-config.py:
-
-usernames['%(fam_name)s']['%(wiki_code)s'] = 'myUsername'"""
- % {'fam_name': self.site.family.name,
- 'wiki_code': self.site.code})
- self.password = password
- if getattr(config, 'password_file', ''):
- self.readPassword()
-
- def botAllowed(self):
- """
- Checks whether the bot is listed on a specific page to comply with
- the policy on the respective wiki.
- """
- if self.site.family.name in botList \
- and self.site.code in botList[self.site.family.name]:
- botListPageTitle, botTemplate = botList[self.site.family.name][self.site.code]
- botListPage = pywikibot.Page(self.site, botListPageTitle)
- if botTemplate:
- for template in botListPage.templatesWithParams():
- if template[0] == botTemplate \
- and template[1][0] == self.username:
- return True
- else:
- for linkedPage in botListPage.linkedPages():
- if linkedPage.title(withNamespace=False) == self.username:
- return True
- return False
- else:
- # No bot policies on other sites
- return True
-
- def getCookie(self, remember=True, captcha = None):
- """
- Login to the site.
-
- remember Remember login (default: True)
- captchaId A dictionary containing the captcha id and answer, if any
-
- Returns cookie data if succesful, None otherwise.
- """
- # NOT IMPLEMENTED - see data/api.py for implementation
-
- def storecookiedata(self, data):
- """
- Store cookie data.
-
- The argument data is the raw data, as returned by getCookie().
-
- Returns nothing.
- """
- # THIS IS OVERRIDDEN IN data/api.py
- filename = config.datafilepath('pywikibot.lwp')
- pywikibot.debug(u"Storing cookies to %s" % filename,
- _logger)
- f = open(filename, 'w')
- f.write(data)
- f.close()
-
- def readPassword(self):
- """
- Read passwords from a file.
-
- DO NOT FORGET TO REMOVE READ ACCESS FOR OTHER USERS!!!
- Use chmod 600 password-file.
- All lines below should be valid Python tuples in the form
- (code, family, username, password) or (username, password)
- to set a default password for an username. Default usernames
- should occur above specific usernames.
-
- Example:
-
- ("my_username", "my_default_password")
- ("my_sysop_user", "my_sysop_password")
- ("en", "wikipedia", "my_en_user", "my_en_pass")
- """
- password_f = open(config.password_file)
- for line in password_f:
- if not line.strip(): continue
- entry = eval(line)
- if len(entry) == 2: #for default userinfo
- if entry[0] == self.username: self.password = entry[1]
- elif len(entry) == 4: #for userinfo included code and family
- if entry[0] == self.site.code and \
- entry[1] == self.site.family.name and \
- entry[2] == self.username:
- self.password = entry[3]
- password_f.close()
-
- def login(self, retry = False):
- if not self.password:
- # As we don't want the password to appear on the screen, we set
- # password = True
- self.password = pywikibot.input(
- u'Password for user %(name)s on %(site)s:'
- % {'name': self.username, 'site': self.site},
- password = True)
-
-# self.password = self.password.encode(self.site.encoding())
-
- pywikibot.output(u"Logging in to %(site)s as %(name)s"
- % {'name': self.username, 'site': self.site})
- try:
- cookiedata = self.getCookie()
- except pywikibot.data.api.APIError, e:
- pywikibot.error(u"Login failed (%s)." % e.code)
- if retry:
- self.password = None
- return self.login(retry = True)
- else:
- return False
- self.storecookiedata(cookiedata)
- pywikibot.log(u"Should be logged in now")
-## # Show a warning according to the local bot policy
-## FIXME: disabled due to recursion; need to move this to the Site object after
-## login
-## if not self.botAllowed():
-## logger.error(
-## u"Username '%(name)s' is not listed on [[%(page)s]]."
-## % {'name': self.username,
-## 'page': botList[self.site.family.name][self.site.code]})
-## logger.error(
-##"Please make sure you are allowed to use the robot before actually using it!")
-## return False
- return True
-
- def showCaptchaWindow(self, url):
- pass
-
def main(*args):
password = None
sysop = False
@@ -253,27 +80,23 @@
namedict = config.sysopnames
else:
namedict = config.usernames
- for familyName in namedict:
- for lang in namedict[familyName]:
- try:
- site = pywikibot.getSite(code=lang, fam=familyName)
- if site.logged_in(sysop) \
- and site.user() == site.username(sysop):
- pywikibot.output(u"Login successful on %(site)s." % locals())
- else:
- pywikibot.output(u"Not logged in on %(site)s." % locals())
- except NoSuchSite:
- pywikibot.output(
- lang + u'.' + familyName +
+ else:
+ site = pywikibot.getSite()
+ namedict = {site.family.name: {site.lang: None}}
+ for familyName in namedict:
+ for lang in namedict[familyName]:
+ try:
+ site = pywikibot.getSite(code=lang, fam=familyName)
+ if site.logged_in(sysop) \
+ and site.user() == site.username(sysop):
+ pywikibot.output(u"Login successful on %(site)s." % locals())
+ else:
+ pywikibot.output(u"Not logged in on %(site)s." % locals())
+ except NoSuchSite:
+ pywikibot.output(
+ lang + u'.' + familyName +
u' is not a valid site, please remove it from your config')
- else:
- site = pywikibot.Site(sysop=sysop)
- if site.logged_in(sysop) and site.user() == site.username(sysop):
- pywikibot.output(u"Login successful on %(site)s." % locals())
- else:
- pywikibot.output(u"Not logged in on %(site)s." % locals())
-
if __name__ == "__main__":
try:
main()
http://www.mediawiki.org/wiki/Special:Code/pywikipedia/10277
Revision: 10277
Author: xqt
Date: 2012-06-03 09:02:44 +0000 (Sun, 03 Jun 2012)
Log Message:
-----------
login library
Modified Paths:
--------------
branches/rewrite/pywikibot/login.py
Modified: branches/rewrite/pywikibot/login.py
===================================================================
--- branches/rewrite/pywikibot/login.py 2012-06-03 08:56:51 UTC (rev 10276)
+++ branches/rewrite/pywikibot/login.py 2012-06-03 09:02:44 UTC (rev 10277)
@@ -1,46 +1,11 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
-Script to log the robot in to a wiki account.
-
-Suggestion is to make a special account to use for robot use only. Make
-sure this robot account is well known on your home wiki before using.
-
-Parameters:
-
- -all Try to log in on all sites where a username is defined in
- user-config.py.
-
-
- -force Ignores if the user is already logged in, and tries to log in.
-
- -pass Useful in combination with -all when you have accounts for
- several sites and use the same password for all of them.
- Asks you for the password, then logs in on all given sites.
-
- -pass:XXXX Uses XXXX as password. Be careful if you use this
- parameter because your password will be shown on your
- screen, and will probably be saved in your command line
- history. This is NOT RECOMMENDED for use on computers
- where others have either physical or remote access.
- Use -pass instead.
-
- -sysop Log in with your sysop account.
-
-If not given as parameter, the script will ask for your username and
-password (password entry will be hidden), log in to your home wiki using
-this combination, and store the resulting cookies (containing your password
-hash, so keep it secured!) in a file in the data subdirectory.
-
-All scripts in this library will be looking for this cookie file and will
-use the login information if it is present.
-
-To log out, throw away the *.lwp file that is created in the data
-subdirectory.
+Library to log the robot in to a wiki account.
"""
#
# (C) Rob W.W. Hooft, 2003
-# (C) Pywikipedia bot team, 2003-2010
+# (C) Pywikipedia bot team, 2003-2012
#
# Distributed under the terms of the MIT license.
#
@@ -226,56 +191,3 @@
def showCaptchaWindow(self, url):
pass
-
-def main(*args):
- password = None
- sysop = False
- logall = False
- forceLogin = False
- for arg in pywikibot.handleArgs(*args):
- if arg.startswith("-pass"):
- if len(arg) == 5:
- password = pywikibot.input(u'Password for all accounts:',
- password = True)
- else:
- password = arg[6:]
- elif arg == "-sysop":
- sysop = True
- elif arg == "-all":
- logall = True
- elif arg == "-force":
- forceLogin = True
- else:
- pywikibot.showHelp('login')
- return
- if logall:
- if sysop:
- namedict = config.sysopnames
- else:
- namedict = config.usernames
- for familyName in namedict:
- for lang in namedict[familyName]:
- try:
- site = pywikibot.getSite(code=lang, fam=familyName)
- if site.logged_in(sysop) \
- and site.user() == site.username(sysop):
- pywikibot.output(u"Login successful on %(site)s." % locals())
- else:
- pywikibot.output(u"Not logged in on %(site)s." % locals())
- except NoSuchSite:
- pywikibot.output(
- lang + u'.' + familyName +
-u' is not a valid site, please remove it from your config')
-
- else:
- site = pywikibot.Site(sysop=sysop)
- if site.logged_in(sysop) and site.user() == site.username(sysop):
- pywikibot.output(u"Login successful on %(site)s." % locals())
- else:
- pywikibot.output(u"Not logged in on %(site)s." % locals())
-
-if __name__ == "__main__":
- try:
- main()
- finally:
- pywikibot.stopme()
http://www.mediawiki.org/wiki/Special:Code/pywikipedia/10276
Revision: 10276
Author: xqt
Date: 2012-06-03 08:56:51 +0000 (Sun, 03 Jun 2012)
Log Message:
-----------
copy script part of the login script and keep the library on pwb folder
Added Paths:
-----------
branches/rewrite/scripts/login.py
Copied: branches/rewrite/scripts/login.py (from rev 10274, branches/rewrite/pywikibot/login.py)
===================================================================
--- branches/rewrite/scripts/login.py (rev 0)
+++ branches/rewrite/scripts/login.py 2012-06-03 08:56:51 UTC (rev 10276)
@@ -0,0 +1,281 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+"""
+Script to log the robot in to a wiki account.
+
+Suggestion is to make a special account to use for robot use only. Make
+sure this robot account is well known on your home wiki before using.
+
+Parameters:
+
+ -all Try to log in on all sites where a username is defined in
+ user-config.py.
+
+
+ -force Ignores if the user is already logged in, and tries to log in.
+
+ -pass Useful in combination with -all when you have accounts for
+ several sites and use the same password for all of them.
+ Asks you for the password, then logs in on all given sites.
+
+ -pass:XXXX Uses XXXX as password. Be careful if you use this
+ parameter because your password will be shown on your
+ screen, and will probably be saved in your command line
+ history. This is NOT RECOMMENDED for use on computers
+ where others have either physical or remote access.
+ Use -pass instead.
+
+ -sysop Log in with your sysop account.
+
+If not given as parameter, the script will ask for your username and
+password (password entry will be hidden), log in to your home wiki using
+this combination, and store the resulting cookies (containing your password
+hash, so keep it secured!) in a file in the data subdirectory.
+
+All scripts in this library will be looking for this cookie file and will
+use the login information if it is present.
+
+To log out, throw away the *.lwp file that is created in the data
+subdirectory.
+"""
+#
+# (C) Rob W.W. Hooft, 2003
+# (C) Pywikipedia bot team, 2003-2010
+#
+# Distributed under the terms of the MIT license.
+#
+__version__ = '$Id$'
+
+import logging
+import pywikibot
+from pywikibot import config, deprecate_arg
+from pywikibot.exceptions import NoSuchSite, NoUsername
+
+_logger = "wiki.login"
+
+
+# On some wikis you are only allowed to run a bot if there is a link to
+# the bot's user page in a specific list.
+# If bots are listed in a template, the templates name must be given as
+# second parameter, otherwise it must be None
+botList = {
+ 'wikipedia': {
+ 'en': [u'Wikipedia:Bots/Status', 'BotS'],
+ 'simple': [u'Wikipedia:Bots', '/links']
+ },
+ 'gentoo': {
+ 'en': [u'Help:Bots', None],
+ }
+}
+
+
+class LoginManager:
+ @deprecate_arg("username", "user")
+ @deprecate_arg("verbose", None)
+ def __init__(self, password=None, sysop=False, site=None, user=None):
+ if site is not None:
+ self.site = site
+ else:
+ self.site = pywikibot.Site()
+ if user:
+ self.username = user
+ elif sysop:
+ try:
+ self.username = config.sysopnames\
+ [self.site.family.name][self.site.code]
+ except KeyError:
+ raise NoUsername(
+u"""ERROR: Sysop username for %(fam_name)s:%(wiki_code)s is undefined.
+If you have a sysop account for that site, please add a line to user-config.py:
+
+sysopnames['%(fam_name)s']['%(wiki_code)s'] = 'myUsername'"""
+ % {'fam_name': self.site.family.name,
+ 'wiki_code': self.site.code})
+ else:
+ try:
+ self.username = config.usernames\
+ [self.site.family.name][self.site.code]
+ except:
+ raise NoUsername(
+u"""ERROR: Username for %(fam_name)s:%(wiki_code)s is undefined.
+If you have an account for that site, please add a line to user-config.py:
+
+usernames['%(fam_name)s']['%(wiki_code)s'] = 'myUsername'"""
+ % {'fam_name': self.site.family.name,
+ 'wiki_code': self.site.code})
+ self.password = password
+ if getattr(config, 'password_file', ''):
+ self.readPassword()
+
+ def botAllowed(self):
+ """
+ Checks whether the bot is listed on a specific page to comply with
+ the policy on the respective wiki.
+ """
+ if self.site.family.name in botList \
+ and self.site.code in botList[self.site.family.name]:
+ botListPageTitle, botTemplate = botList[self.site.family.name][self.site.code]
+ botListPage = pywikibot.Page(self.site, botListPageTitle)
+ if botTemplate:
+ for template in botListPage.templatesWithParams():
+ if template[0] == botTemplate \
+ and template[1][0] == self.username:
+ return True
+ else:
+ for linkedPage in botListPage.linkedPages():
+ if linkedPage.title(withNamespace=False) == self.username:
+ return True
+ return False
+ else:
+ # No bot policies on other sites
+ return True
+
+ def getCookie(self, remember=True, captcha = None):
+ """
+ Login to the site.
+
+ remember Remember login (default: True)
+ captchaId A dictionary containing the captcha id and answer, if any
+
+ Returns cookie data if succesful, None otherwise.
+ """
+ # NOT IMPLEMENTED - see data/api.py for implementation
+
+ def storecookiedata(self, data):
+ """
+ Store cookie data.
+
+ The argument data is the raw data, as returned by getCookie().
+
+ Returns nothing.
+ """
+ # THIS IS OVERRIDDEN IN data/api.py
+ filename = config.datafilepath('pywikibot.lwp')
+ pywikibot.debug(u"Storing cookies to %s" % filename,
+ _logger)
+ f = open(filename, 'w')
+ f.write(data)
+ f.close()
+
+ def readPassword(self):
+ """
+ Read passwords from a file.
+
+ DO NOT FORGET TO REMOVE READ ACCESS FOR OTHER USERS!!!
+ Use chmod 600 password-file.
+ All lines below should be valid Python tuples in the form
+ (code, family, username, password) or (username, password)
+ to set a default password for an username. Default usernames
+ should occur above specific usernames.
+
+ Example:
+
+ ("my_username", "my_default_password")
+ ("my_sysop_user", "my_sysop_password")
+ ("en", "wikipedia", "my_en_user", "my_en_pass")
+ """
+ password_f = open(config.password_file)
+ for line in password_f:
+ if not line.strip(): continue
+ entry = eval(line)
+ if len(entry) == 2: #for default userinfo
+ if entry[0] == self.username: self.password = entry[1]
+ elif len(entry) == 4: #for userinfo included code and family
+ if entry[0] == self.site.code and \
+ entry[1] == self.site.family.name and \
+ entry[2] == self.username:
+ self.password = entry[3]
+ password_f.close()
+
+ def login(self, retry = False):
+ if not self.password:
+ # As we don't want the password to appear on the screen, we set
+ # password = True
+ self.password = pywikibot.input(
+ u'Password for user %(name)s on %(site)s:'
+ % {'name': self.username, 'site': self.site},
+ password = True)
+
+# self.password = self.password.encode(self.site.encoding())
+
+ pywikibot.output(u"Logging in to %(site)s as %(name)s"
+ % {'name': self.username, 'site': self.site})
+ try:
+ cookiedata = self.getCookie()
+ except pywikibot.data.api.APIError, e:
+ pywikibot.error(u"Login failed (%s)." % e.code)
+ if retry:
+ self.password = None
+ return self.login(retry = True)
+ else:
+ return False
+ self.storecookiedata(cookiedata)
+ pywikibot.log(u"Should be logged in now")
+## # Show a warning according to the local bot policy
+## FIXME: disabled due to recursion; need to move this to the Site object after
+## login
+## if not self.botAllowed():
+## logger.error(
+## u"Username '%(name)s' is not listed on [[%(page)s]]."
+## % {'name': self.username,
+## 'page': botList[self.site.family.name][self.site.code]})
+## logger.error(
+##"Please make sure you are allowed to use the robot before actually using it!")
+## return False
+ return True
+
+ def showCaptchaWindow(self, url):
+ pass
+
+def main(*args):
+ password = None
+ sysop = False
+ logall = False
+ forceLogin = False
+ for arg in pywikibot.handleArgs(*args):
+ if arg.startswith("-pass"):
+ if len(arg) == 5:
+ password = pywikibot.input(u'Password for all accounts:',
+ password = True)
+ else:
+ password = arg[6:]
+ elif arg == "-sysop":
+ sysop = True
+ elif arg == "-all":
+ logall = True
+ elif arg == "-force":
+ forceLogin = True
+ else:
+ pywikibot.showHelp('login')
+ return
+ if logall:
+ if sysop:
+ namedict = config.sysopnames
+ else:
+ namedict = config.usernames
+ for familyName in namedict:
+ for lang in namedict[familyName]:
+ try:
+ site = pywikibot.getSite(code=lang, fam=familyName)
+ if site.logged_in(sysop) \
+ and site.user() == site.username(sysop):
+ pywikibot.output(u"Login successful on %(site)s." % locals())
+ else:
+ pywikibot.output(u"Not logged in on %(site)s." % locals())
+ except NoSuchSite:
+ pywikibot.output(
+ lang + u'.' + familyName +
+u' is not a valid site, please remove it from your config')
+
+ else:
+ site = pywikibot.Site(sysop=sysop)
+ if site.logged_in(sysop) and site.user() == site.username(sysop):
+ pywikibot.output(u"Login successful on %(site)s." % locals())
+ else:
+ pywikibot.output(u"Not logged in on %(site)s." % locals())
+
+if __name__ == "__main__":
+ try:
+ main()
+ finally:
+ pywikibot.stopme()
http://www.mediawiki.org/wiki/Special:Code/pywikipedia/10273
Revision: 10273
Author: valhallasw
Date: 2012-06-03 08:17:36 +0000 (Sun, 03 Jun 2012)
Log Message:
-----------
+ wrapper script to use rewrite in 'directory' mode - run scripts using
python pwb.py <name_of_script> <options>
and it will use the package directory to store all user files, will fix up
search paths so the package does not need to be installed, etc.
Added Paths:
-----------
branches/rewrite/pwb.py
Added: branches/rewrite/pwb.py
===================================================================
--- branches/rewrite/pwb.py (rev 0)
+++ branches/rewrite/pwb.py 2012-06-03 08:17:36 UTC (rev 10273)
@@ -0,0 +1,24 @@
+import sys,os
+sys.path.append('.')
+sys.path.append('externals')
+sys.path.append('pywikibot/compat')
+
+if "PYWIKIBOT2_DIR" not in os.environ:
+ os.environ["PYWIKIBOT2_DIR"] = os.path.split(__file__)[0]
+
+sys.argv.pop(0)
+if len(sys.argv) > 0:
+ if not os.path.exists(sys.argv[0]):
+ testpath = os.path.join(os.path.split(__file__)[0], 'scripts', sys.argv[0])
+ if os.path.exists(testpath):
+ sys.argv[0] = testpath
+ else:
+ testpath = testpath + '.py'
+ if os.path.exists(testpath):
+ sys.argv[0] = testpath
+ else:
+ raise Exception("%s not found!" % sys.argv[0])
+ sys.path.append(os.path.split(sys.argv[0])[0])
+ execfile(sys.argv[0])
+else:
+ sys.argv.append('')