From 18122fafe80f04c63cc790dab670c350c1134eea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?GOLDERWEB=20=E2=80=93=20Jonathan=20Golder?= Date: Fri, 11 Dec 2015 12:41:23 +0100 Subject: [PATCH] New feature force parsing of countrylists regardless if needed with param "-force-reload" --- chartsbot.py | 22 ++++++++++++++++---- summarypage.py | 54 ++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 61 insertions(+), 15 deletions(-) diff --git a/chartsbot.py b/chartsbot.py index 869b74a..ea5e3af 100644 --- a/chartsbot.py +++ b/chartsbot.py @@ -40,6 +40,8 @@ The following parameters are supported: -always If given, request for confirmation of edit is short circuited Use for unattended run +-force-reload If given, countrylists will be always parsed regardless if + needed or not """ @@ -61,10 +63,10 @@ class ChartsBot( ): """ Bot which automatically updates a ChartsSummaryPage like [[Portal:Charts_und_Popmusik/Aktuelle_Nummer-eins-Hits]] by reading linked - CountryListsAn incomplete sample bot. + CountryLists """ - def __init__( self, generator, always ): + def __init__( self, generator, always, force_reload ): """ Constructor. @@ -74,11 +76,17 @@ class ChartsBot( ): @param always: if True, request for confirmation of edit is short circuited. Use for unattended run @type always: bool + @param force-reload: If given, countrylists will be always parsed + regardless if needed or not + @type force-reload: bool """ self.generator = generator self.always = always + # Force parsing of countrylist + self.force_reload = force_reload + # Set the edit summary message self.site = pywikibot.Site() self.summary = "Bot: Aktualisiere Übersichtsseite Nummer-eins-Hits" @@ -102,7 +110,7 @@ class ChartsBot( ): ################################################################ # Initialise and treat SummaryPageWorker - sumpage = SummaryPage( text ) + sumpage = SummaryPage( text, self.force_reload ) sumpage.treat() # Check if editing is needed and if so get new text @@ -187,10 +195,16 @@ def main(*args): # If always is True, bot won't ask for confirmation of edit (automode) always = False + # If force_reload is True, bot will always parse Countrylist regardless of + # parsing is needed or not + force_reload = False + # Parse command line arguments for arg in local_args: if arg.startswith("-always"): always = True + elif arg.startswith("-force-reload"): + force_reload = True else: genFactory.handleArg(arg) @@ -200,7 +214,7 @@ def main(*args): # The preloading generator is responsible for downloading multiple # pages from the wiki simultaneously. gen = pagegenerators.PreloadingGenerator(gen) - bot = ChartsBot(gen, always) + bot = ChartsBot(gen, always, force_reload) bot.run() else: pywikibot.showHelp() diff --git a/summarypage.py b/summarypage.py index a52fc4f..d859062 100644 --- a/summarypage.py +++ b/summarypage.py @@ -38,14 +38,24 @@ class SummaryPage(): Handles summary page related actions """ - def __init__( self, text ): + def __init__( self, text, force_reload=False ): """ Create Instance + + @param text: Page Text of summarypage + @type text: str + @param force-reload: If given, countrylists will be always parsed + regardless if needed or not + @type force-reload: bool + """ # Parse Text with mwparser self.wikicode = mwparser.parse( text ) + # Force parsing of countrylist + self.force_reload = force_reload + def treat( self ): """ Handles parsing/editing of text @@ -55,7 +65,9 @@ class SummaryPage(): for entry in self.wikicode.filter_templates( matches="/Eintrag" ): # Instantiate SummaryPageEntry-object - summarypageentry = SummaryPageEntry( entry ) + summarypageentry = SummaryPageEntry(entry, + force_reload=self.force_reload) + # Treat SummaryPageEntry-object summarypageentry.treat() @@ -85,13 +97,22 @@ class SummaryPageEntry(): write_needed = False - def __init__( self, entry ): + def __init__( self, entry, force_reload=False ): """ Constructor + + @param entry: Entry template of summarypage entry + @type text: mwparser.template + @param force-reload: If given, countrylists will be always parsed + regardless if needed or not + @type force-reload: bool """ self.old_entry = SummaryPageEntryTemplate( entry ) self.new_entry = SummaryPageEntryTemplate( ) + # Force parsing of countrylist + self.force_reload = force_reload + def treat( self ): """ Controls parsing/update-sequence of entry @@ -134,9 +155,7 @@ class SummaryPageEntry(): try: self.countrylist = CountryList( self.countrylist_wikilink ) - if( self.countrylist and - self.countrylist.is_parsing_needed( self.countrylist_revid )): - self.countrylist.parse() + self.maybe_parse_countrylist() # Maybe fallback to last years list except CountryListError: @@ -144,13 +163,26 @@ class SummaryPageEntry(): self.countrylist_wikilink.title = link_title self.countrylist = CountryList( self.countrylist_wikilink ) - if( self.countrylist and - self.countrylist.is_parsing_needed( self.countrylist_revid )): - self.countrylist.parse() + self.maybe_parse_countrylist() if not self.countrylist: raise SummaryPageEntryError( "CountryList does not exists!" ) + def maybe_parse_countrylist( self ): + """ + Parse countrylist if page-object exists and if parsing is needed or + param -force-reload is set + """ + + # Fast return if no countrylist-object + if not self.countrylist: + return + + # Parse if needed or forced + if( self.countrylist.is_parsing_needed( self.countrylist_revid ) or + self.force_reload ): + self.countrylist.parse() + def get_countrylist_wikilink( self ): """ Load wikilink to related countrylist @@ -250,8 +282,8 @@ class SummaryPageEntryTemplate(): Creates Instance of Class for given mwparser.template object of SummmaryPageEntry Template. If no object was given create empty one. - @param template_obj mw.parser.template Object of - SummmaryPageEntry Template + @param template_obj Object of SummmaryPageEntry Template + @type template_obj: mwparser.template """ # Check if object was given