Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_deque_preloading(self):
"""Test pages being added to a DequePreloadingGenerator."""
mainpage = self.get_mainpage()
pages = pywikibot.tools.DequeGenerator([mainpage])
gen = pagegenerators.DequePreloadingGenerator(pages)
pages_out = []
for page in gen:
pages_out.append(page)
# Add a page to the generator
if not page.isTalkPage():
pages.extend([page.toggleTalkPage()])
self.assertTrue(all(isinstance(page,
pywikibot.Page) for page in pages_out))
self.assertIn(mainpage, pages_out)
self.assertIn(mainpage.toggleTalkPage(), pages_out)
self.assertEqual(len(pages_out), 2)
self.assertTrue(pages_out[1].isTalkPage())
def test_brackets(self):
"""Test TextfilePageGenerator with brackets."""
filename = join_data_path('pagelist-brackets.txt')
site = self.get_site()
titles = list(pagegenerators.TextfilePageGenerator(filename, site))
self.assertEqual(len(titles), len(self.expected_titles))
expected_titles = [
expected_title[self.title_columns[site.namespaces[page.namespace()]
.case]]
for expected_title, page in zip(self.expected_titles, titles)]
self.assertPageTitlesEqual(titles, expected_titles)
for arg in local_args:
if iwconf.readOptions(arg):
continue
elif arg.startswith('-warnfile:'):
warnfile = arg[10:]
elif arg.startswith('-years'):
# Look if user gave a specific year at which to start
# Must be a natural number or negative integer.
if len(arg) > 7 and (arg[7:].isdigit() or
(arg[7] == '-' and arg[8:].isdigit())):
startyear = int(arg[7:])
else:
startyear = 1
# avoid problems where year pages link to centuries etc.
iwconf.followredirect = False
hintlessPageGen = pagegenerators.YearPageGenerator(startyear)
elif arg.startswith('-days'):
if len(arg) > 6 and arg[5] == ':' and arg[6:].isdigit():
# Looks as if the user gave a specific month at which to start
# Must be a natural number.
startMonth = int(arg[6:])
else:
startMonth = 1
hintlessPageGen = pagegenerators.DayPageGenerator(startMonth)
elif arg.startswith('-new'):
if len(arg) > 5 and arg[4] == ':' and arg[5:].isdigit():
# Looks as if the user gave a specific number of pages
newPages = int(arg[5:])
else:
newPages = 100
elif arg.startswith('-restore'):
iwconf.restoreAll = arg[9:].lower() == 'all'
alternatives.append(page.title())
else:
alternatives.append(arg[5:])
elif arg == '-just':
getAlternatives = False
elif arg == '-dnskip':
dnSkip = True
elif arg == '-main':
main_only = True
elif arg == '-first':
first_only = True
elif arg.startswith('-min:'):
minimum = int(arg[5:])
elif arg.startswith('-start'):
try:
generator = pagegenerators.CategorizedPageGenerator(
pywikibot.Site().disambcategory(),
start=arg[7:], namespaces=[0])
except pywikibot.NoPage:
pywikibot.output(
'Disambiguation category for your wiki is not known.')
raise
else:
generator_factory.handleArg(arg)
site = pywikibot.Site()
generator = generator_factory.getCombinedGenerator(generator)
if not generator:
pywikibot.bot.suggest_help(missing_generator=True)
return False
'anyway?' % page.title(),
default=False, automatic_quit=False):
alternatives.append(page.title())
else:
alternatives.append(arg[5:])
elif arg == '-just':
getAlternatives = False
elif arg == '-dnskip':
dnSkip = True
elif arg == '-main':
main_only = True
elif arg.startswith('-min:'):
minimum = int(arg[5:])
elif arg.startswith('-start'):
try:
generator = pagegenerators.CategorizedPageGenerator(
pywikibot.Site().disambcategory(),
start=arg[7:], namespaces=[0])
except pywikibot.NoPage:
pywikibot.output(
'Disambiguation category for your wiki is not known.')
raise
else:
generator_factory.handleArg(arg)
site = pywikibot.Site()
generator = generator_factory.getCombinedGenerator(generator)
if not generator:
pywikibot.bot.suggest_help(missing_generator=True)
return False
def main(*args):
"""
Process command line arguments and invoke bot.
If args is an empty list, sys.argv is used.
@param args: command line arguments
@type args: list of unicode
"""
options = {}
local_args = pywikibot.handle_args(args)
genFactory = pagegenerators.GeneratorFactory()
for arg in local_args:
if genFactory.handleArg(arg):
continue
if arg.startswith('-summary:'):
options['summary'] = arg[9:]
elif arg.startswith('-tmp:'):
options['tmp'] = arg[5:]
elif arg == '-force':
options['force'] = True
elif arg == '-always':
options['always'] = True
gen = genFactory.getCombinedGenerator()
if gen:
bot = BadgesBot(generator=gen, **options)
def main(*args):
"""Create and run a PiperBot instance from the given command arguments."""
local_args = pywikibot.handle_args(args)
# 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.
gen_factory = pagegenerators.GeneratorFactory()
# The program to pipe stuff through
filters = []
options = {}
# Parse command line arguments
for arg in local_args:
option, sep, value = arg.partition(':')
if option == '-filter':
filters.append(value)
elif option == '-always':
options['always'] = True
else:
# check if a standard argument like
# -start:XYZ or -ref:Asdf was given.
gen_factory.handleArg(arg)
SELECT page_namespace, page_title
FROM page
JOIN text ON (page_id = old_id)
%s
%s
LIMIT 200""" % (whereClause, exceptClause)
gen = pagegenerators.MySQLPageGenerator(query)
gen = genFactory.getCombinedGenerator(gen)
if not gen:
# syntax error, show help text from the top of this file
pywikibot.showHelp('replace')
return
preloadingGen = pagegenerators.PreloadingGenerator(gen)
bot = ReplaceRobot(preloadingGen, replacements, exceptions, acceptall,
allowoverlap, recursive, add_cat, sleep, edit_summary,
site)
site.login()
bot.run()
# Explicitly call pywikibot.stopme().
# It will make sure the callback is triggered before replace.py is unloaded.
pywikibot.stopme()
pywikibot.output(u'\n%s pages changed.' % bot.changed_pages)
else:
xml_filename = arg[5:]
else:
gen_factory.handleArg(arg)
if xml_filename:
generator = XmlDumpPageGenerator(xml_filename, xml_start,
gen_factory.namespaces)
if not generator:
generator = gen_factory.getCombinedGenerator()
if not generator:
pywikibot.bot.suggest_help(missing_generator=True)
return False
if not gen_factory.nopreload:
generator = pagegenerators.PreloadingGenerator(generator)
generator = pagegenerators.RedirectFilterPageGenerator(generator)
bot = ReferencesRobot(generator, **options)
bot.run()
return True
commonsImagePage.title(with_ns=False)))
bot = ImageBot(
pg.FileLinksGenerator(localImagePage),
localImagePage.title(with_ns=False),
commonsImagePage.title(with_ns=False),
'', self.getOption('replacealways'),
self.getOption('replaceloose'))
bot.run()
# If the image is used with the urlname the
# previous function won't work
is_used = bool(list(pywikibot.FilePage(
self.site,
page.title()).usingPages(total=1)))
if is_used and self.getOption('replaceloose'):
bot = ImageBot(
pg.FileLinksGenerator(
localImagePage),
localImagePage.title(
with_ns=False, as_url=True),
commonsImagePage.title(with_ns=False),
'', self.getOption('replacealways'),
self.getOption('replaceloose'))
bot.run()
# refresh because we want the updated list
usingPages = len(list(pywikibot.FilePage(
self.site, page.title()).usingPages()))
else:
pywikibot.output('Please change them manually.')
continue
else:
pywikibot.output(color_format(