http://www.mediawiki.org/wiki/Special:Code/pywikipedia/9050
Revision: 9050 Author: xqt Date: 2011-03-13 12:08:39 +0000 (Sun, 13 Mar 2011) Log Message: ----------- stripped trailing whitespace
Modified Paths: -------------- trunk/pywikipedia/maintenance/family_check.py trunk/pywikipedia/maintenance/preferences.py trunk/pywikipedia/maintenance/readtalk.py trunk/pywikipedia/maintenance/setmail.py trunk/pywikipedia/maintenance/update_namespaces.py
Modified: trunk/pywikipedia/maintenance/family_check.py =================================================================== --- trunk/pywikipedia/maintenance/family_check.py 2011-03-13 11:31:13 UTC (rev 9049) +++ trunk/pywikipedia/maintenance/family_check.py 2011-03-13 12:08:39 UTC (rev 9050) @@ -34,7 +34,7 @@ site = wikipedia.getSite(lang, family) wikipedia.output(u'Checking %s' % site) namespaces = check_namespaces(site) - if namespaces: + if namespaces: for id, name, defined_namespace in namespaces: wikipedia.output(u'Namespace %s for %s is %s, %s is defined in family file.' % \ (id, site, name, defined_namespace))
Modified: trunk/pywikipedia/maintenance/preferences.py =================================================================== --- trunk/pywikipedia/maintenance/preferences.py 2011-03-13 11:31:13 UTC (rev 9049) +++ trunk/pywikipedia/maintenance/preferences.py 2011-03-13 12:08:39 UTC (rev 9050) @@ -1,6 +1,6 @@ """ This module contains a read-write class that represents the user preferences. """ from HTMLParser import HTMLParser -from htmlentitydefs import name2codepoint +from htmlentitydefs import name2codepoint
class Checkbox(object): def __init__(self, value, state): @@ -14,7 +14,7 @@ return self.state def __str__(self): return str(self.state) - + class Select(list): def __init__(self): list.__init__(self, ()) @@ -34,11 +34,11 @@ dict.__init__(self, ()) self.in_form = False self.select = None - + if site: self.load(site) - + def handle_entityref(self, name): - if name in name2codepoint: + if name in name2codepoint: self.handle_data(unichr(name2codepoint[name])) else: self.handle_data(u'&%s;' % name) @@ -48,9 +48,9 @@ except ValueError: self.handle_data(u'&#$s;' % name) def handle_starttag(self, tag, attrs): - if tag == 'form': + if tag == 'form': self.in_form = ('method', 'post') in attrs - + attrs = dict(attrs) if tag == 'input' and self.in_form: if attrs.get('type', 'text') in ('hidden', 'text'): @@ -73,21 +73,21 @@ self.select.append(attrs['value']) if 'selected' in attrs: self.select.set(attrs['value']) - + def handle_endtag(self, tag): - if self.in_form and tag == 'form': + if self.in_form and tag == 'form': self.in_form = False if self.select and tag == 'select': self.select = None - - + + def load(self, site): site.forceLogin() data = site.getUrl(site.get_address("Special:Preferences")) self.feed(data) self.close() self.site = site - + def save(self): predata = {'wpSaveprefs': '1', 'title': 'Special:Preferences'} for key, value in self.iteritems(): @@ -118,15 +118,15 @@ if tabs < 0: tabs = 0 s = s + '\t' * tabs return s - + def set_all(keys, values, verbose = False): import wikipedia, config, time - + log = open('preferences.txt', 'a') log.write('PREFERENCES\t%s\n' % time.gmtime()) log.write('KEYS\t%s\n' % keys) log.write('VALUES\t%s\n' % values) - + for family in config.usernames: for lang in config.usernames[family]: try: @@ -139,7 +139,7 @@ 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, persistent_http = True) @@ -154,23 +154,23 @@ site.conn.close() except AttributeError: pass -
+ 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()])) - + if wikipedia.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?', + + 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) prefs = Preferences(site) - + wikipedia.output(u'-------------------------------------------------------------------------') wikipedia.output(u'| Name | Value |') wikipedia.output(u'-------------------------------------------------------------------------') @@ -182,7 +182,7 @@ wikipedia.output(u'') wikipedia.output(u'(For checkboxes: An empty string evaluates to False; all others to True)') wikipedia.output(u'') - + while True: keys, values = [], [] while True: @@ -191,15 +191,15 @@ 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?", + if wikipedia.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))), + 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.") - + if __name__ == '__main__': import sys sys.path.append('..')
Modified: trunk/pywikipedia/maintenance/readtalk.py =================================================================== --- trunk/pywikipedia/maintenance/readtalk.py 2011-03-13 11:31:13 UTC (rev 9049) +++ trunk/pywikipedia/maintenance/readtalk.py 2011-03-13 12:08:39 UTC (rev 9050) @@ -9,7 +9,7 @@ *Error checking """ import sys, re -sys.path.append(re.sub('/[^/]*$', '', sys.path[0])) +sys.path.append(re.sub('/[^/]*$', '', sys.path[0])) sys.path.append('..') import wikipedia, config, userlib
@@ -38,7 +38,7 @@ def main(): # Get a dictionary of all the usernames all = sysop = False - + for arg in wikipedia.handleArgs(): if arg.startswith('-all'): all = True
Modified: trunk/pywikipedia/maintenance/setmail.py =================================================================== --- trunk/pywikipedia/maintenance/setmail.py 2011-03-13 11:31:13 UTC (rev 9049) +++ trunk/pywikipedia/maintenance/setmail.py 2011-03-13 12:08:39 UTC (rev 9050) @@ -16,7 +16,7 @@
if __name__ == '__main__': r_mail = re.compile(ur'(http://\S*Confirmemail\S*)') - + email = wikipedia.input('Email?') host = wikipedia.input('Host?') port = wikipedia.input('Port (default: 110; ssl: 995)?') @@ -26,21 +26,21 @@ port = 0 if not port: port = 110 - ssl = wikipedia.inputChoice('SSL? ', ['no', 'yes'], + ssl = wikipedia.inputChoice('SSL? ', ['no', 'yes'], ['n', 'y'], (port == 995) and 'y' or 'n') == 'y' username = wikipedia.input('User?') password = wikipedia.input('Password?', True) do_delete = wikipedia.inputChoice('Delete confirmed mails?', ['yes', 'no'], ['y', 'n'], 'y') == 'y' - + if email: preferences.set_all(['wpUserEmail', 'wpEmailFlag', 'wpOpenotifusertalkpages'], [email, True, False], verbose = True) - + if ssl: pop = poplib.POP3_SSL(host, port) else: pop = poplib.POP3(host, port) - + pop.user(username) pop.pass_(password) wikipedia.output(unicode(pop.getwelcome())) @@ -54,10 +54,10 @@ link = r_mail.search(line).group(1) wikipedia.output(u'Confirming %s.' % link) confirm(link) - + if not confirmed: wikipedia.output(u'Unconfirmed mail!') elif do_delete: pop.dele(i) pop.quit() - +
Modified: trunk/pywikipedia/maintenance/update_namespaces.py =================================================================== --- trunk/pywikipedia/maintenance/update_namespaces.py 2011-03-13 11:31:13 UTC (rev 9049) +++ trunk/pywikipedia/maintenance/update_namespaces.py 2011-03-13 12:08:39 UTC (rev 9050) @@ -30,7 +30,7 @@ r_namespace_def = re.compile(r'['"]([a-z_-]*)['"]\s*:\s*((?:%s)|(?:%s))\s*,' % (r_string, r_list)) def update_family(family, changes): global namespace_section_text, namespace_defs, new_defs - + if family: output(u'Updating family %s' % family.name) family_file_name = '../families/%s_family.py' % family.name @@ -44,31 +44,31 @@ family_file = open(family_file_name, 'r') old_family_text = family_text = family_file.read() family_file.close() - + for lang, namespaces in changes.iteritems(): for namespace_id, namespace_name, predefined_namespace in namespaces: output(u'Setting namespace[%s] for %s to %s' % (namespace_id, lang, namespace_name)) - + namespace_section = re.search(r_namespace_section % namespace_id, family_text) #namespace_section2 = re.search(r_namespace_section_once % (namespace_id, lang) ,family_text) if not namespace_section: continue namespace_section_text = namespace_section.group(1) - namespace_defs = dict([(match.group(1), match.group(2)) + namespace_defs = dict([(match.group(1), match.group(2)) for match in r_namespace_def.finditer(namespace_section_text)]) - + if not namespace_defs.get(lang, '').startswith('['): output(u'Updating namespace[%s] to %s' % (namespace_id, namespace_name)) - + namespace_defs[lang] = escape_string(namespace_name.encode('utf-8')) else: output(u'Namespace[%s] definition is a list; not updating.' % namespace_id) - + new_defs = namespace_defs.items() new_defs.sort(key = lambda x: x[0]) new_text = '\n' + ''.join([(base_indent + 4) * ' ' + "'%s': %s,\n" % i for i in new_defs]) + ' ' * base_indent family_text = family_text.replace(namespace_section.group(1), new_text) - + if family_text == old_family_text: output(u'No changes made') elif test_data(family_text): @@ -79,10 +79,10 @@ else: output(u'Warning! Syntax error!') output(family_text.decode('utf-8')) - + def escape_string(string): return "u'%s'" % string.replace('\', '\\').replace("'", "\'") - + def test_data(_test_data): try: exec _test_data @@ -91,7 +91,7 @@ except: return True return True - + def check_and_update(families, update_main = False): for family in families: family = wikipedia.Family(family) @@ -100,7 +100,7 @@ if update_main: # Update also the family.py file update_family(None, result) - + if __name__ == '__main__': try: update_main_family = False