jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/720472 )
Change subject: [IMPR]: preload pages in pagefromfile.py ......................................................................
[IMPR]: preload pages in pagefromfile.py
Preload pages instead of reading them one by one before putting changes.
Change-Id: I6205fb2cb052ac7b0c103456925fcf75ea5f54ca --- M scripts/pagefromfile.py 1 file changed, 15 insertions(+), 15 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/scripts/pagefromfile.py b/scripts/pagefromfile.py index e54e409..f79297b 100755 --- a/scripts/pagefromfile.py +++ b/scripts/pagefromfile.py @@ -73,6 +73,9 @@ from pywikibot import config, i18n from pywikibot.backports import Tuple from pywikibot.bot import CurrentPageBot, OptionHandler, SingleSiteBot +from pywikibot.pagegenerators import PreloadingGenerator + +CTX_ATTR = '_content_ctx'
class NoTitleError(Exception): @@ -104,21 +107,12 @@ 'showdiff': False, }
- def init_page(self, item) -> pywikibot.Page: - """Get the tuple and return the page object to be processed.""" - title, content = item - page = pywikibot.Page(self.site, title) - page.text = content.strip() - return super().init_page(page) - def treat_page(self) -> None: """Upload page content.""" page = self.current_page title = page.title() # save the content retrieved from generator - contents = page.text - # delete page's text to get it from live wiki - del page.text + contents = getattr(page, CTX_ATTR)
if self.opt.summary: comment = self.opt.summary @@ -195,7 +189,7 @@ 'title': None, }
- def __init__(self, filename, **kwargs) -> None: + def __init__(self, filename, site=None, **kwargs) -> None: """Initializer.
Check if self.file name exists. If not, ask for a new filename. @@ -204,8 +198,9 @@ """ super().__init__(**kwargs) self.filename = filename + self.site = site or pywikibot.Site()
- def __iter__(self) -> Generator[Tuple[str, str], None, None]: + def __iter__(self) -> Generator[pywikibot.Page, None, None]: """Read file and yield a tuple of page title and content.""" pywikibot.output("\n\nReading '{}'...".format(self.filename)) try: @@ -235,7 +230,10 @@ if length == 0: break position += length - yield title, contents + + page = pywikibot.Page(self.site, title) + setattr(page, CTX_ATTR, contents.strip()) + yield page
def findpage(self, text) -> Tuple[int, str, str]: """Find page to work on.""" @@ -320,8 +318,10 @@ if failed_filename: pywikibot.bot.suggest_help(missing_parameters=['-file']) else: - reader = PageFromFileReader(filename, **r_options) - bot = PageFromFileRobot(generator=reader, **options) + site = pywikibot.Site() + reader = PageFromFileReader(filename, site=site, **r_options) + reader = PreloadingGenerator(reader) + bot = PageFromFileRobot(generator=reader, site=site, **options) bot.run()
pywikibot-commits@lists.wikimedia.org