jenkins-bot has submitted this change. (
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/616512 )
Change subject: [IMPR] Cleanup archivebot.py
......................................................................
[IMPR] Cleanup archivebot.py
- rename single-character variables for clarity
- drop redundant casting to float
- make use enumerate's start argument
- and more
This is the first refactoring step to allow for easier
solving current feature requests.
Change-Id: Id2026fa0ae296bb1d6ea7acab8994b7c6bf186a9
---
M scripts/archivebot.py
1 file changed, 25 insertions(+), 25 deletions(-)
Approvals:
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/scripts/archivebot.py b/scripts/archivebot.py
index 9e9adcb..e64c4db 100755
--- a/scripts/archivebot.py
+++ b/scripts/archivebot.py
@@ -233,7 +233,6 @@
@type string: str
@return: key and duration extracted form the string
"""
- key = string[-1]
if string.isdigit():
key = 's'
duration = string
@@ -241,6 +240,7 @@
string + key, 1, UserWarning,
since='20161009')
else:
+ key = string[-1]
duration = string[:-1]
return key, duration
@@ -256,10 +256,10 @@
Returns a tuple (size,unit), where size is an integer and unit is
'B' (bytes) or 'T' (threads).
"""
- r = re.search(r'(\d+) *([BkKMT]?)', string)
- if not r:
+ match = re.search(r'(\d+) *([BkKMT]?)', string)
+ if not match:
raise MalformedConfigError("Couldn't parse size:
{}".format(string))
- val, unit = (int(r.group(1)), r.group(2))
+ val, unit = (int(match.group(1)), match.group(2))
if unit == 'M':
val *= 1024
unit = 'K'
@@ -534,8 +534,8 @@
self.archives = {}
self.archived_threads = 0
self.month_num2orig_names = {}
- for n, (_long, _short) in enumerate(self.site.months_names):
- self.month_num2orig_names[n + 1] = {'long': _long, 'short':
_short}
+ for n, (long, short) in enumerate(self.site.months_names, start=1):
+ self.month_num2orig_names[n] = {'long': long, 'short':
short}
def get_attr(self, attr, default='') -> Any:
"""Get an archiver attribute."""
@@ -612,45 +612,45 @@
self.page.threads = []
whys = set()
pywikibot.output('Processing {} threads'.format(len(oldthreads)))
- for t in oldthreads:
- if len(oldthreads) - self.archived_threads \
- <= int(self.get_attr('minthreadsleft', 5)):
- self.page.threads.append(t)
+ for thread in oldthreads:
+ threads_left = len(oldthreads) - self.archived_threads
+ if threads_left <= int(self.get_attr('minthreadsleft', 5)):
+ self.page.threads.append(thread)
continue # Because there's too little threads left.
# TODO: Make an option so that unstamped (unsigned) posts get
# archived.
- why = t.should_be_archived(self)
+ why = thread.should_be_archived(self)
if why:
archive = self.get_attr('archive')
lang = self.site.lang
+ timestamp = thread.timestamp
params = {
'counter': to_local_digits(arch_counter, lang),
- 'year': to_local_digits(t.timestamp.year, lang),
- 'isoyear': to_local_digits(t.timestamp.isocalendar()[0],
+ 'year': to_local_digits(timestamp.year, lang),
+ 'isoyear': to_local_digits(timestamp.isocalendar()[0],
lang),
- 'isoweek': to_local_digits(t.timestamp.isocalendar()[1],
+ 'isoweek': to_local_digits(timestamp.isocalendar()[1],
lang),
- 'semester': to_local_digits(
- int(ceil(float(t.timestamp.month) / 6)), lang),
- 'quarter': to_local_digits(
- int(ceil(float(t.timestamp.month) / 3)), lang),
- 'month': to_local_digits(t.timestamp.month, lang),
+ 'semester': to_local_digits(int(ceil(timestamp.month / 6)),
+ lang),
+ 'quarter': to_local_digits(int(ceil(timestamp.month / 3)),
+ lang),
+ 'month': to_local_digits(timestamp.month, lang),
'monthname': self.month_num2orig_names[
- t.timestamp.month]['long'],
+ timestamp.month]['long'],
'monthnameshort': self.month_num2orig_names[
- t.timestamp.month]['short'],
+ timestamp.month]['short'],
'week': to_local_digits(
- int(time.strftime('%W',
- t.timestamp.timetuple())), lang),
+ int(time.strftime('%W', timestamp.timetuple())), lang),
}
archive = pywikibot.Page(self.site, archive % params)
- if self.feed_archive(archive, t, max_arch_size, params):
+ if self.feed_archive(archive, thread, max_arch_size, params):
arch_counter += 1
self.set_attr('counter', str(arch_counter))
whys.add(why)
self.archived_threads += 1
else:
- self.page.threads.append(t)
+ self.page.threads.append(thread)
return whys
def run(self) -> None:
--
To view, visit
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/616512
To unsubscribe, or for help writing mail filters, visit
https://gerrit.wikimedia.org/r/settings
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: Id2026fa0ae296bb1d6ea7acab8994b7c6bf186a9
Gerrit-Change-Number: 616512
Gerrit-PatchSet: 2
Gerrit-Owner: Matěj Suchánek <matejsuchanek97(a)gmail.com>
Gerrit-Reviewer: D3r1ck01 <xsavitar.wiki(a)aol.com>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged