Browse Source

New feature force parsing of countrylists regardless if needed with param "-force-reload"

Jonathan Golder 3 years ago
parent
commit
18122fafe8
2 changed files with 61 additions and 15 deletions
  1. 18
    4
      chartsbot.py
  2. 43
    11
      summarypage.py

+ 18
- 4
chartsbot.py View File

@@ -40,6 +40,8 @@ The following parameters are supported:
40 40
 
41 41
 -always           If given, request for confirmation of edit is short circuited
42 42
                   Use for unattended run
43
+-force-reload     If given, countrylists will be always parsed regardless if
44
+                  needed or not
43 45
 """
44 46
 
45 47
 
@@ -61,10 +63,10 @@ class ChartsBot( ):
61 63
     """
62 64
     Bot which automatically updates a ChartsSummaryPage like
63 65
     [[Portal:Charts_und_Popmusik/Aktuelle_Nummer-eins-Hits]] by reading linked
64
-    CountryListsAn incomplete sample bot.
66
+    CountryLists
65 67
     """
66 68
 
67
-    def __init__( self, generator, always ):
69
+    def __init__( self, generator, always, force_reload ):
68 70
         """
69 71
         Constructor.
70 72
 
@@ -74,11 +76,17 @@ class ChartsBot( ):
74 76
         @param always: if True, request for confirmation of edit is short
75 77
                         circuited. Use for unattended run
76 78
         @type always: bool
79
+        @param force-reload: If given, countrylists will be always parsed
80
+                             regardless if needed or not
81
+        @type force-reload: bool
77 82
         """
78 83
 
79 84
         self.generator = generator
80 85
         self.always = always
81 86
 
87
+        # Force parsing of countrylist
88
+        self.force_reload = force_reload
89
+
82 90
         # Set the edit summary message
83 91
         self.site = pywikibot.Site()
84 92
         self.summary = "Bot: Aktualisiere Übersichtsseite Nummer-eins-Hits"
@@ -102,7 +110,7 @@ class ChartsBot( ):
102 110
         ################################################################
103 111
 
104 112
         # Initialise and treat SummaryPageWorker
105
-        sumpage = SummaryPage( text )
113
+        sumpage = SummaryPage( text, self.force_reload )
106 114
         sumpage.treat()
107 115
 
108 116
         # Check if editing is needed and if so get new text
@@ -187,10 +195,16 @@ def main(*args):
187 195
     # If always is True, bot won't ask for confirmation of edit (automode)
188 196
     always = False
189 197
 
198
+    # If force_reload is True, bot will always parse Countrylist regardless of
199
+    # parsing is needed or not
200
+    force_reload = False
201
+
190 202
     # Parse command line arguments
191 203
     for arg in local_args:
192 204
         if arg.startswith("-always"):
193 205
             always = True
206
+        elif arg.startswith("-force-reload"):
207
+            force_reload = True
194 208
         else:
195 209
             genFactory.handleArg(arg)
196 210
 
@@ -200,7 +214,7 @@ def main(*args):
200 214
         # The preloading generator is responsible for downloading multiple
201 215
         # pages from the wiki simultaneously.
202 216
         gen = pagegenerators.PreloadingGenerator(gen)
203
-        bot = ChartsBot(gen, always)
217
+        bot = ChartsBot(gen, always, force_reload)
204 218
         bot.run()
205 219
     else:
206 220
         pywikibot.showHelp()

+ 43
- 11
summarypage.py View File

@@ -38,14 +38,24 @@ class SummaryPage():
38 38
     Handles summary page related actions
39 39
     """
40 40
 
41
-    def __init__( self, text ):
41
+    def __init__( self, text, force_reload=False ):
42 42
         """
43 43
         Create Instance
44
+
45
+        @param text: Page Text of summarypage
46
+        @type text: str
47
+        @param force-reload: If given, countrylists will be always parsed
48
+                             regardless if needed or not
49
+        @type force-reload: bool
50
+
44 51
         """
45 52
 
46 53
         # Parse Text with mwparser
47 54
         self.wikicode = mwparser.parse( text )
48 55
 
56
+        # Force parsing of countrylist
57
+        self.force_reload = force_reload
58
+
49 59
     def treat( self ):
50 60
         """
51 61
         Handles parsing/editing of text
@@ -55,7 +65,9 @@ class SummaryPage():
55 65
         for entry in self.wikicode.filter_templates( matches="/Eintrag" ):
56 66
 
57 67
             # Instantiate SummaryPageEntry-object
58
-            summarypageentry = SummaryPageEntry( entry )
68
+            summarypageentry = SummaryPageEntry(entry,
69
+                                                force_reload=self.force_reload)
70
+
59 71
             # Treat SummaryPageEntry-object
60 72
             summarypageentry.treat()
61 73
 
@@ -85,13 +97,22 @@ class SummaryPageEntry():
85 97
 
86 98
     write_needed = False
87 99
 
88
-    def __init__( self, entry ):
100
+    def __init__( self, entry, force_reload=False ):
89 101
         """
90 102
         Constructor
103
+
104
+        @param entry: Entry template of summarypage entry
105
+        @type text: mwparser.template
106
+        @param force-reload: If given, countrylists will be always parsed
107
+                             regardless if needed or not
108
+        @type force-reload: bool
91 109
         """
92 110
         self.old_entry = SummaryPageEntryTemplate( entry )
93 111
         self.new_entry = SummaryPageEntryTemplate( )
94 112
 
113
+        # Force parsing of countrylist
114
+        self.force_reload = force_reload
115
+
95 116
     def treat( self ):
96 117
         """
97 118
         Controls parsing/update-sequence of entry
@@ -134,9 +155,7 @@ class SummaryPageEntry():
134 155
         try:
135 156
             self.countrylist = CountryList( self.countrylist_wikilink )
136 157
 
137
-            if( self.countrylist and
138
-                self.countrylist.is_parsing_needed( self.countrylist_revid )):
139
-                    self.countrylist.parse()
158
+            self.maybe_parse_countrylist()
140 159
 
141 160
         # Maybe fallback to last years list
142 161
         except CountryListError:
@@ -144,13 +163,26 @@ class SummaryPageEntry():
144 163
             self.countrylist_wikilink.title = link_title
145 164
             self.countrylist = CountryList( self.countrylist_wikilink )
146 165
 
147
-            if( self.countrylist and
148
-                self.countrylist.is_parsing_needed( self.countrylist_revid )):
149
-                    self.countrylist.parse()
166
+            self.maybe_parse_countrylist()
150 167
 
151 168
             if not self.countrylist:
152 169
                 raise SummaryPageEntryError( "CountryList does not exists!" )
153 170
 
171
+    def maybe_parse_countrylist( self ):
172
+        """
173
+        Parse countrylist if page-object exists and if parsing is needed or
174
+        param -force-reload is set
175
+        """
176
+
177
+        # Fast return if no countrylist-object
178
+        if not self.countrylist:
179
+            return
180
+
181
+        # Parse if needed or forced
182
+        if( self.countrylist.is_parsing_needed( self.countrylist_revid ) or
183
+            self.force_reload ):
184
+                self.countrylist.parse()
185
+
154 186
     def get_countrylist_wikilink( self ):
155 187
         """
156 188
         Load wikilink to related countrylist
@@ -250,8 +282,8 @@ class SummaryPageEntryTemplate():
250 282
         Creates Instance of Class for given mwparser.template object of
251 283
         SummmaryPageEntry Template. If no object was given create empty one.
252 284
 
253
-        @param    template_obj    mw.parser.template   Object of
254
-                                  SummmaryPageEntry Template
285
+        @param template_obj Object of SummmaryPageEntry Template
286
+        @type template_obj: mwparser.template
255 287
         """
256 288
 
257 289
         # Check if object was given