jenkins-bot submitted this change.

View Change

Approvals: Inductiveload: Looks good to me, but someone else must approve Xqt: Looks good to me, approved jenkins-bot: Verified
[IMPR] Provide -create and -createonly options with add_text

https://www.mediawiki.org/wiki/Topic:Wgdnu8bl29esui34

Also fix type hints

Bug: T291354
Change-Id: Ib77d7ab94285ae74c37da000d63b72e8a92d3d3f
---
M pywikibot/bot.py
M scripts/add_text.py
2 files changed, 32 insertions(+), 14 deletions(-)

diff --git a/pywikibot/bot.py b/pywikibot/bot.py
index 3197e60..c50d36c 100644
--- a/pywikibot/bot.py
+++ b/pywikibot/bot.py
@@ -1895,7 +1895,8 @@
parameters for the translated message.
"""

- summary_key = None # must be defined in subclasses
+ #: Must be defined in subclasses.
+ summary_key = None # type: Optional[str]

@property
def summary_parameters(self) -> Dict[str, str]:
diff --git a/scripts/add_text.py b/scripts/add_text.py
index 63cdfd7..d664adb 100755
--- a/scripts/add_text.py
+++ b/scripts/add_text.py
@@ -18,6 +18,11 @@

-up If used, put the text at the top of the page

+-create Create the page if necessary. Note that talk pages are
+ created already without of this option.
+
+-createonly Only create the page but do not edit existing ones
+
-always If used, the bot won't ask if it should add the specified
text

@@ -88,6 +93,8 @@
'textfile': '',
'summary': '',
'up': False,
+ 'create': False,
+ 'createonly': False,
'always': False,
'minor': True,
'talk_page': False,
@@ -109,7 +116,7 @@
@deprecated('Page.text, NoRedirectPageBot class and BaseBot.skip_page() '
'(see add_text.AddTextBot for example)', since='6.4.0')
def get_text(page: pywikibot.page.BasePage, old: Optional[str],
- create: bool) -> str:
+ create: bool) -> Optional[str]:
"""
Get text on page. If old is not None, return old.

@@ -336,20 +343,30 @@

def skip_page(self, page):
"""Skip if -exceptUrl matches or page does not exists."""
- if page.exists() and self.opt.regex_skip_url:
- url = page.full_url()
- result = re.findall(self.opt.regex_skip_url, page.site.getUrl(url))
-
- if result:
- pywikibot.warning(
- 'Skipping {page} because -excepturl matches {result}.'
- .format(page=page, result=result))
+ if page.exists():
+ if self.opt.createonly:
+ pywikibot.warning('Skipping because {page} already exists'
+ .format(page=page))
return True

- if page.isTalkPage() and not page.exists():
+ if self.opt.regex_skip_url:
+ url = page.full_url()
+ result = re.findall(self.opt.regex_skip_url,
+ page.site.getUrl(url))
+
+ if result:
+ pywikibot.warning(
+ 'Skipping {page} because -excepturl matches {result}.'
+ .format(page=page, result=result))
+ return True
+
+ elif page.isTalkPage():
pywikibot.output("{} doesn't exist, creating it!".format(page))
return False

+ elif self.opt.create:
+ return False
+
return super().skip_page(page)

def treat_page(self):
@@ -367,7 +384,7 @@
self.put_current(text, summary=self.opt.summary, minor=self.opt.minor)


-def main(*argv: Tuple[str, ...]) -> None:
+def main(*argv: str) -> None:
"""
Process command line arguments and invoke bot.

@@ -393,7 +410,7 @@

def parse(argv: Tuple[str, ...],
generator_factory: pagegenerators.GeneratorFactory
- ) -> Dict[str, str]:
+ ) -> Dict[str, Union[bool, str]]:
"""
Parses our arguments and provide a named tuple with their values.

@@ -414,7 +431,7 @@

if option in ('-text', '-textfile', '-summary'):
args[option[1:]] = value
- elif option in ('-up', '-always'):
+ elif option in ('-up', '-always', '-create', 'createonly'):
args[option[1:]] = True
elif option in ('-talk', '-talkpage'):
args['talk_page'] = True

To view, visit change 721297. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: Ib77d7ab94285ae74c37da000d63b72e8a92d3d3f
Gerrit-Change-Number: 721297
Gerrit-PatchSet: 5
Gerrit-Owner: Xqt <info@gno.de>
Gerrit-Reviewer: D3r1ck01 <xsavitar.wiki@aol.com>
Gerrit-Reviewer: Inductiveload <inductiveload@gmail.com>
Gerrit-Reviewer: Xqt <info@gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged