http://www.mediawiki.org/wiki/Special:Code/pywikipedia/11467
Revision: 11467 Author: xqt Date: 2013-04-27 07:34:04 +0000 (Sat, 27 Apr 2013) Log Message: ----------- POP8 changes, reorder imports
Modified Paths: -------------- trunk/pywikipedia/maintenance/preferences.py
Modified: trunk/pywikipedia/maintenance/preferences.py =================================================================== --- trunk/pywikipedia/maintenance/preferences.py 2013-04-27 07:06:02 UTC (rev 11466) +++ trunk/pywikipedia/maintenance/preferences.py 2013-04-27 07:34:04 UTC (rev 11467) @@ -1,61 +1,86 @@ # -*- coding: utf-8 -*- -""" This module contains a read-write class that represents the user preferences. """ +""" This module contains a read-write class that represents the user +preferences . + +""" # -# (C) Pywikipedia bot team, ?-2013 +# (C) Pywikipedia bot team, 2007-2013 # # Distributed under the terms of the MIT license. # __version__ = '$Id$' #
+import sys +import time from HTMLParser import HTMLParser from htmlentitydefs import name2codepoint
+sys.path.insert(1, '..') +import wikipedia as pywikibot +import config + + class Checkbox(object): + def __init__(self, value, state): self.value = value self.state = state + def set(self): self.state = True + def unset(self): self.state = False + def __bool__(self): return self.state + def __str__(self): return str(self.state)
+ class Select(list): + def __init__(self): list.__init__(self, ()) self.value = u'' + def set(self, value): self.value = value + def __bool__(self): return bool(self.value) + def __str__(self): return str(self.value) + def __unicode__(self): return unicode(self.value)
+ class Preferences(HTMLParser, dict): - def __init__(self, site = None): + + def __init__(self, site=None): HTMLParser.__init__(self) dict.__init__(self, ()) self.in_form = False self.select = None + if site: + self.load(site)
- if site: self.load(site) - def handle_entityref(self, name): if name in name2codepoint: self.handle_data(unichr(name2codepoint[name])) else: self.handle_data(u'&%s;' % name) + def handle_charref(self, name): try: self.handle_data(unichr(int(name))) except ValueError: self.handle_data(u'&#$s;' % name) + def handle_starttag(self, tag, attrs): if tag == 'form': self.in_form = ('method', 'post') in attrs @@ -67,7 +92,7 @@ self[attrs['name']] = attrs['value'] elif attrs.get('type') == 'checkbox': self[attrs['name']] = Checkbox(attrs['value'], - 'checked' in attrs) + 'checked' in attrs) elif attrs.get('type') == 'radio': if attrs['name'] not in self: self[attrs['name']] = Select() @@ -89,7 +114,6 @@ if self.select and tag == 'select': self.select = None
- def load(self, site): site.forceLogin() data = site.getUrl(site.get_address("Special:Preferences")) @@ -109,11 +133,12 @@ else: predata[key] = value self.site.postForm(self.site.path(), predata) + def set(self, key, value): if key in self: if type(self[key]) is Select: return self.key.set(value) - elif type(self[key]) is Checkbox: + if type(self[key]) is Checkbox: if value: return self[key].set() else: @@ -124,13 +149,13 @@ def table_cell(value, length): s = u'| ' + unicode(value) tabs = length - (len(s) / 8) - if tabs < 0: tabs = 0 + if tabs < 0: + tabs = 0 s = s + '\t' * tabs return s
-def set_all(keys, values, verbose = False): - import wikipedia, config, time
+def set_all(keys, values, verbose=False): log = open('preferences.txt', 'a') log.write('PREFERENCES\t%s\n' % time.gmtime()) log.write('KEYS\t%s\n' % keys) @@ -143,20 +168,22 @@ except (SystemExit, KeyboardInterrupt): return except Exception, e: - wikipedia.output(u'Warning! An exception occured! %s: %s' % (e.__class__.__name__, str(e))) + pywikibot.output(u'Warning! An exception occured! %s: %s' + % (e.__class__.__name__, str(e))) log.write('FAILED\t%s\t%s\n' % (family, lang)) else: log.write('SUCCESS\t%s\t%s\n' % (family, lang)) log.close()
-def set_for(lang, family, keys, values, verbose = False): - import wikipedia - site = wikipedia.getSite(lang, family) + +def set_for(lang, family, keys, values, verbose=False): + site = pywikibot.getSite(lang, family) prefs = Preferences(site) for key, value in zip(keys, values): prev = unicode(prefs.get(key, '')) - if verbose: wikipedia.output(u"Setting '%s' on %s from '%s' to '%s'." % \ - (key, site, prev, value)) + if verbose: + pywikibot.output(u"Setting '%s' on %s from '%s' to '%s'." + % (key, site, prev, value)) prefs.set(key, value) prefs.save() try: @@ -166,55 +193,63 @@
def main(): - import wikipedia, config
- wikipedia.output(u'Warning! This script will set preferences on all configured accounts!') - wikipedia.output(u'You have %s accounts configured.' % \ - sum([len(family) for family in config.usernames.itervalues()])) + pywikibot.output(u'Warning! This script will set preferences on all ' + u'configured accounts!') + pywikibot.output(u'You have %s accounts configured.' + % sum([len(family) + for family in config.usernames.itervalues()]))
- if wikipedia.inputChoice(u'Do you wish to continue?', ['no', 'yes'], ['n', 'y'], 'n') == 'n': return + if pywikibot.inputChoice(u'Do you wish to continue?', + ['no', 'yes'], ['n', 'y'], 'n') == 'n': + return
- if wikipedia.inputChoice(u'Do you already know which preference you wish to set?', - ['no', 'yes'], ['n', 'y'], 'y') == 'n': - site = wikipedia.getSite() - wikipedia.output(u'Getting list of available preferences from %s.' % site) + if pywikibot.inputChoice(u'Do you already know which preference you wish ' + u'to set?', ['no', 'yes'], ['n', 'y'], 'y') == 'n': + site = pywikibot.getSite() + pywikibot.output(u'Getting list of available preferences from %s.' + % site) prefs = Preferences(site)
- wikipedia.output(u'-------------------------------------------------------------------------') - wikipedia.output(u'| Name | Value |') - wikipedia.output(u'-------------------------------------------------------------------------') + pywikibot.output(u'-' * 73) + pywikibot.output(u'| Name | Value |') + pywikibot.output(u'-' * 73) pref_data = prefs.items() pref_data.sort() for key, value in pref_data: - wikipedia.output(table_cell(key, 4) + table_cell(value, 5) + '|') - wikipedia.output(u'-------------------------------------------------------------------------') - wikipedia.output(u'') - wikipedia.output(u'(For checkboxes: An empty string evaluates to False; all others to True)') - wikipedia.output(u'') + pywikibot.output(table_cell(key, 4) + table_cell(value, 5) + '|') + pywikibot.output(u'-' * 73) + pywikibot.output(u'') + pywikibot.output(u'(For checkboxes: An empty string evaluates to False; ' + u'all others to True)') + pywikibot.output(u'')
while True: keys, values = [], [] while True: try: - keys.append(wikipedia.input(u'Which preference do you wish to set?')) + keys.append(pywikibot.input( + u'Which preference do you wish to set?')) except KeyboardInterrupt: return - values.append(wikipedia.input(u"To what value do you wish to set '%s'?" % keys[-1])) - if wikipedia.inputChoice(u"Set more preferences?", - ['no', 'yes'], ['n', 'y'], 'n') == 'n': break + values.append(pywikibot.input( + u"To what value do you wish to set '%s'?" % keys[-1])) + if pywikibot.inputChoice(u"Set more preferences?", + ['no', 'yes'], ['n', 'y'], 'n') == 'n': + break
- if wikipedia.inputChoice(u"Set %s?" % u', '.join((u'%s:%s' % (key, value) - for key, value in zip(keys, values))), + if pywikibot.inputChoice( + u"Set %s?" + % u', '.join(u'%s:%s' % (key, value) + for key, value in zip(keys, values)), ['yes', 'no'], ['y', 'n'], 'n') == 'y': - set_all(keys, values, verbose = True) - wikipedia.output(u"Preferences have been set on all wikis.") + set_all(keys, values, verbose=True) + pywikibot.output(u"Preferences have been set on all wikis.")
+ if __name__ == '__main__': - import sys - sys.path.insert(1, '..') - import wikipedia try: - wikipedia.handleArgs() + pywikibot.handleArgs() main() finally: - wikipedia.stopme() + pywikibot.stopme()
pywikipedia-svn@lists.wikimedia.org