jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/502163 )
Change subject: [IMPR] Enable create_categories.py for other sites ......................................................................
[IMPR] Enable create_categories.py for other sites
- create the category in init_page method - skip if the generator's page is not a category or the category to be created already exists - improve option handling and also skip the script if parent or basename options are empty - use slice for missing list - update doc string
Bug: T220262 Change-Id: Ib81810b0ed9c134ff824f0c8fecc211742a859cb --- M scripts/create_categories.py 1 file changed, 42 insertions(+), 40 deletions(-)
Approvals: Huji: Looks good to me, approved jenkins-bot: Verified
diff --git a/scripts/create_categories.py b/scripts/create_categories.py index b42cb8e..17c30cb 100755 --- a/scripts/create_categories.py +++ b/scripts/create_categories.py @@ -3,8 +3,8 @@ r""" Program to batch create categories.
-The program expects a generator of page titles to be used as -suffix for creating new categories with a different base. +The program expects a generator of category titles to be used +as suffix for creating new categories with a different base.
The following command line parameters are supported:
@@ -27,7 +27,7 @@ -basename:"Cultural heritage monuments in"
The page 'User:Multichill/Wallonia' on commons contains -page links like [[Category:Hensies]], causing this script +category links like [[Category:Hensies]], causing this script to create [[Category:Cultural heritage monuments in Hensies]].
""" @@ -43,6 +43,8 @@ import pywikibot from pywikibot.bot import AutomaticTWSummaryBot, SingleSiteBot from pywikibot import pagegenerators +from pywikibot.site import Namespace +from pywikibot.tools import UnicodeType
class CreateCategoriesBot(SingleSiteBot, AutomaticTWSummaryBot): @@ -51,38 +53,47 @@
summary_key = 'create_categories-create'
- def __init__(self, generator, parent, basename, overwrite, **kwargs): + def __init__(self, **kwargs): """Initializer.""" + self.availableOptions.update({ + 'basename': None, + 'parent': None, + 'overwrite': False, + }) super(CreateCategoriesBot, self).__init__(**kwargs) - self.generator = generator - self.parent = parent - self.basename = basename - self.overwrite = overwrite
def init_page(self, item): """Create a category to be processed with the given page title.""" page = super(CreateCategoriesBot, self).init_page(item) title = page.title(with_ns=False) + if page.namespace() != Namespace.CATEGORY: + # return the page title to be skipped later within skip_page + return title
- newpage = pywikibot.Category(pywikibot.Site('commons', 'commons'), - '{} {}'.format(self.basename, title)) - newtext = ('[[Category:%(parent)s|%(title)s]]\n' - '[[Category:%(title)s]]\n' - % {'parent': self.parent, 'title': title}) - newpage.text = newtext - return newpage + category = pywikibot.Category( + page.site, '{} {}'.format(self.getOption('basename'), title))
- def treat(self, page): - """Create category in commons for that page.""" - newtext = page.text - self.current_page = page + text = ('[[{namespace}:{parent}|{title}]]\n{category}\n' + .format(namespace=page.site.namespace(Namespace.CATEGORY), + parent=self.getOption('parent'), + title=title, + category=page.title(as_link=True))) + category.text = text + return category + + def treat_page(self): + """Create category in local site for that page.""" + newtext = self.current_page.text self.current_page.text = '' self.put_current(newtext, ignore_server_errors=True)
def skip_page(self, page): """Skip page if it is not overwritten.""" - if page.exists() and not self.overwrite: - pywikibot.output('{} already exists, skipping'.format(page)) + if isinstance(page, UnicodeType): + pywikibot.warning(page + ' is not a category, skipping') + return True + if page.exists() and not self.getOption('overwrite'): + pywikibot.warning('{} already exists, skipping'.format(page)) return True return super(CreateCategoriesBot, self).skip_page(page)
@@ -96,9 +107,6 @@ @param args: command line arguments @type args: str """ - parent = None - basename = None - overwrite = False options = {}
# Process global args and prepare generator args parser @@ -106,29 +114,23 @@ gen_factory = pagegenerators.GeneratorFactory()
for arg in local_args: - if arg == '-always': - options['always'] = True - elif arg == '-overwrite': - overwrite = True - elif arg.startswith('-parent:'): - parent = arg[len('-parent:'):].strip() - elif arg.startswith('-basename'): - basename = arg[len('-basename:'):].strip() + option, _, value = arg.partition(':') + opt = option[1:] + if arg in ('-always', '-overwrite'): + options[opt] = True + elif option in ('-parent', '-basename'): + if value: + options[opt] = value else: gen_factory.handleArg(arg)
- missing = set() - if not parent: - missing.add('-parent') - if not basename: - missing.add('-basename') + missing = ['-' + arg for arg in ('basename', 'parent') + if arg not in options]
generator = gen_factory.getCombinedGenerator() if generator and not missing: - bot = CreateCategoriesBot(generator, parent, - basename, overwrite, **options) + bot = CreateCategoriesBot(generator=generator, **options) bot.run() - pywikibot.output('All done') return True else: pywikibot.bot.suggest_help(missing_parameters=missing,
pywikibot-commits@lists.wikimedia.org