Revision: 5788 Author: alnokta Date: 2008-08-06 16:29:47 +0000 (Wed, 06 Aug 2008)
Log Message: ----------- Add -exceptinterwiki command line option so that it ignores (don't perform replacements) the interwiki links.
Credits to Betacommand for the code.
Modified Paths: -------------- trunk/pywikipedia/replace.py
Modified: trunk/pywikipedia/replace.py =================================================================== --- trunk/pywikipedia/replace.py 2008-08-06 09:35:24 UTC (rev 5787) +++ trunk/pywikipedia/replace.py 2008-08-06 16:29:47 UTC (rev 5788) @@ -251,7 +251,7 @@ """ def __init__(self, generator, replacements, exceptions={}, acceptall=False, allowoverlap=False, recursive=False, - addedCat=None, sleep=None): + addedCat=None, sleep=None, exceptinterwiki=False): """ Arguments: * generator - A generator that yields Page objects. @@ -292,6 +292,7 @@ self.acceptall = acceptall self.allowoverlap = allowoverlap self.recursive = recursive + self.exceptinterwiki = exceptinterwiki if addedCat: site = wikipedia.getSite() cat_ns = site.category_namespaces()[0] @@ -338,8 +339,13 @@ for old, new in self.replacements: if self.sleep != None: time.sleep(self.sleep) + if self.exceptinterwiki: + interwikis = wikipedia.getLanguageLinks(new_text) + new_text = wikipedia.removeLanguageLinks(new_text) new_text = wikipedia.replaceExcept(new_text, old, new, exceptions, allowoverlap=self.allowoverlap) + if self.exceptinterwiki: + new_text = wikipedia.replaceLanguageLinks(new_text,interwikis) return new_text
def run(self): @@ -492,6 +498,8 @@ allowoverlap = False # Do not recurse replacement recursive = False + #add flag to ignore interwiki links + exceptinterwiki = False # This factory is responsible for processing command line arguments # that are also used by other scripts and that determine on which pages # to work on. @@ -543,6 +551,8 @@ sleep = float(arg[7:]) elif arg == '-always': acceptall = True + elif arg == '-exceptinterwiki': + exceptinterwiki = True elif arg == '-recursive': recursive = True elif arg == '-nocase': @@ -701,7 +711,7 @@ pageNumber=20, lookahead=100) else: preloadingGen = pagegenerators.PreloadingGenerator(gen, pageNumber=60) - bot = ReplaceRobot(preloadingGen, replacements, exceptions, acceptall, allowoverlap, recursive, add_cat, sleep) + bot = ReplaceRobot(preloadingGen, replacements, exceptions, acceptall, allowoverlap, recursive, add_cat, sleep, exceptinterwiki) bot.run()
if __name__ == "__main__":