http://www.mediawiki.org/wiki/Special:Code/pywikipedia/10878
Revision: 10878 Author: drtrigon Date: 2013-01-10 16:33:29 +0000 (Thu, 10 Jan 2013) Log Message: ----------- improvement; implemented the magicwords part of DRTRIGON-131 (and removed magicwords by replacing it with the new method)
Modified Paths: -------------- trunk/pywikipedia/subster.py trunk/pywikipedia/subster_irc.py
Modified: trunk/pywikipedia/subster.py =================================================================== --- trunk/pywikipedia/subster.py 2013-01-07 10:21:35 UTC (rev 10877) +++ trunk/pywikipedia/subster.py 2013-01-10 16:33:29 UTC (rev 10878) @@ -74,6 +74,7 @@ import crontab import logging import ast +import shelve, pprint
import pagegenerators, basic # Splitting the bot into library parts @@ -109,7 +110,6 @@ #'postproc': '("","")', 'postproc': '('', '')', 'wiki': 'False', # may be change to url='wiki://' - 'magicwords_only': 'False', 'beautifulsoup': 'False', # DRTRIGON-88 'expandtemplates': 'False', # DRTRIGON-93 (only with 'wiki') 'simple': '', # DRTRIGON-85 @@ -128,10 +128,6 @@ 'EditFlags': {'minorEdit': True, 'botflag': True}, }
-## used/defined magic words, look also at bot_control -# use, for example: '<!--SUBSTER-BOTerror--><!--SUBSTER-BOTerror-->' -magic_words = {} # no magic word substitution (for empty dict) - # debug tools # (look at 'bot_control.py' for more info) debug = [] @@ -265,22 +261,6 @@ substed_content = content substed_tags = [] # DRTRIGON-73
- # 0.) subst (internal) magic words - try: - (substed_content, tags) = self.subBotMagicWords(substed_content) - substed_tags += tags - except: - exc_info = sys.exc_info() - result = u''.join(traceback.format_exception(exc_info[0], exc_info[1], exc_info[2])) - substed_content += ast.literal_eval(self._param_default['error']) %\ - {'error': bot_config['ErrorTemplate'] %\ - ( pywikibot.Timestamp.now().isoformat(' '), - u' ' + result.replace(u'\n', u'\n ').rstrip() ) } - substed_tags.append( u'>error:BotMagicWords<' ) - - if (len(params) == 1) and ast.literal_eval(params[0]['magicwords_only']): - return (substed_content, substed_tags) - for item in params: # 1.) - 5.) subst templates try: @@ -298,28 +278,6 @@
return (substed_content, substed_tags)
- def subBotMagicWords(self, content): - """Substitute the DrTrigonBot Magic Word (tag)s in content. - - @param content: Content with tags to substitute. - @type content: string - - Returns a tuple containig the new content with tags - substituted and a list of those tags. - """ - - substed_tags = [] # DRTRIGON-73 - - # 0.) subst (internal) magic words - for subitem in magic_words.keys(): - prev_content = content - content = self.get_var_regex(subitem).sub( (self._var_regex_str%{'var':subitem,'cont':magic_words[subitem]}), - content, 1) # subst. once - if (content != prev_content): - substed_tags.append(subitem) - - return (content, substed_tags) - def subTemplate(self, content, param): """Substitute the template tags in content according to param.
@@ -359,7 +317,7 @@ # (security: check url not to point to a local file on the server, # e.g. 'file://' - same as used in xsalt.py) secure = False - for item in [u'http://', u'https://', u'mail://']: + for item in [u'http://', u'https://', u'mail://', u'local://']: secure = secure or (param['url'][:len(item)] == item) param['wiki'] = ast.literal_eval(param['wiki']) param['zip'] = ast.literal_eval(param['zip']) @@ -370,11 +328,18 @@ external_buffer = pywikibot.Page(self.site, param['url']).get(expandtemplates=True) else: external_buffer = self.load( pywikibot.Page(self.site, param['url']) ) - elif (param['url'][:7] == u'mail://'): # DRTRIGON-101 + elif (param['url'][:7] == u'mail://'): # DRTRIGON-101 param['url'] = param['url'].replace(u'{{@}}', u'@') # e.g. nlwiki mbox = SubsterMailbox(pywikibot.config.datafilepath(bot_config['data_path'], bot_config['mbox_file'], '')) external_buffer = mbox.find_data(param['url']) mbox.close() + elif (param['url'][:8] == u'local://'): # DRTRIGON-131 + if (param['url'][8:] == u'cache/state_bots'): + d = shelve.open('cache/state_bots') # filename hard-coded + external_buffer = pprint.pformat(d) + d.close() + else: + external_buffer = u'n/a' elif param['zip']: external_buffer = urllib.urlopen(param['url']).read() # issue with r355: http://de.wikipedia.org/w/index.php?title=Vorlage:Infobox_Kreditinstitut/Dat...
Modified: trunk/pywikipedia/subster_irc.py =================================================================== --- trunk/pywikipedia/subster_irc.py 2013-01-07 10:21:35 UTC (rev 10877) +++ trunk/pywikipedia/subster_irc.py 2013-01-10 16:33:29 UTC (rev 10878) @@ -42,10 +42,6 @@ 'ConfCSSconfig': u'User:DrTrigon/DrTrigonBot/subster-config.css', }
-## used/defined magic words, look also at bot_control -# use, for example: '<!--SUBSTER-BOTerror--><!--SUBSTER-BOTerror-->' -magic_words = {} # no magic word substitution (for empty dict) - # debug tools # (look at 'bot_control.py' and 'subster.py' for more info) debug = [] @@ -151,8 +147,7 @@ del bot
def main(): - subster.magic_words = magic_words - subster.debug = debug + subster.debug = debug site = pywikibot.getSite() site.forceLogin() chan = '#' + site.language() + '.' + site.family.name