Simplify score and comment count data

This commit changes the functionality of the Content class (and its
subclasses) by removing the text from the comments and score fields of
Reddit data dictionaries created by Content.strip*.  data['comments'] is
now an integer, but data['score'] remains a string to preserve using '-'
when the score is hidden.

It is now the responsibility of wherever this data is used to provide
the extra text that these variables used to include, and modifications
have been made to the Submission and Subreddit classes to preserve the
previous way the data was displayed.

Tests modified to expect these changes.
This commit is contained in:
John Helmert
2019-07-29 19:10:26 -05:00
parent 9e43d7ed65
commit 8df2df93f6
4 changed files with 15 additions and 14 deletions

View File

@@ -736,9 +736,9 @@ def test_subreddit_page__create_format(terminal, subreddit_page):
data = {
'index': '1',
'title': 'Without saying what the category is, what are your top five?',
'score': '144655 pts',
'score': '144655',
'likes': True,
'comments': '26584 comments',
'comments': '26584',
'created': '10month',
'edited': '(edit 5month)',
'author': 'reddit_user',
@@ -858,7 +858,8 @@ def test_subreddit_page__create_format(terminal, subreddit_page):
assert format_list[6][0](data) == expected_str['%n'] + ' '
else:
# General case
assert format_list[1][0](data) == expected_str[fmt]
assert format_list[1][0](data) == expected_str[fmt], \
"On specifier {0}".format(fmt)
format_list[1][1](data)
terminal.attr.assert_called_with(expected_attr[fmt])

View File

@@ -152,7 +152,7 @@ class Content(object):
data['body'] = comment.body
data['html'] = comment.body_html
data['created'] = cls.humanize_timestamp(comment.created_utc)
data['score'] = '{0} pts'.format(
data['score'] = '{0}'.format(
'-' if comment.score_hidden else comment.score)
data['author'] = name
data['is_author'] = (name == sub_name)
@@ -187,7 +187,7 @@ class Content(object):
data['nsfw'] = comment.over_18
data['subreddit'] = six.text_type(comment.subreddit)
data['url_type'] = 'selfpost'
data['score'] = '{0} pts'.format(
data['score'] = '{0}'.format(
'-' if comment.score_hidden else comment.score)
data['likes'] = comment.likes
data['created'] = cls.humanize_timestamp(comment.created_utc)
@@ -233,8 +233,8 @@ class Content(object):
data['html'] = sub.selftext_html or ''
data['created'] = cls.humanize_timestamp(sub.created_utc)
data['created_long'] = cls.humanize_timestamp(sub.created_utc, True)
data['comments'] = '{0} comments'.format(sub.num_comments)
data['score'] = '{0} pts'.format('-' if sub.hide_score else sub.score)
data['comments'] = sub.num_comments
data['score'] = '{0}'.format('-' if sub.hide_score else sub.score)
data['author'] = name
data['permalink'] = sub.permalink
data['subreddit'] = six.text_type(sub.subreddit)

View File

@@ -284,7 +284,7 @@ class SubmissionPage(Page):
attr = self.term.attr('Score')
self.term.add_space(win)
self.term.add_line(win, '{score}'.format(**data), attr=attr)
self.term.add_line(win, '{score} pts'.format(**data), attr=attr)
attr = self.term.attr('Created')
self.term.add_space(win)
@@ -385,7 +385,7 @@ class SubmissionPage(Page):
row = len(data['split_title']) + len(split_text) + 3
attr = self.term.attr('Score')
self.term.add_line(win, '{score}'.format(**data), row, 1, attr=attr)
self.term.add_line(win, '{score} pts'.format(**data), row, 1, attr=attr)
arrow, attr = self.term.get_arrow(data['likes'])
self.term.add_space(win)
@@ -393,7 +393,7 @@ class SubmissionPage(Page):
attr = self.term.attr('CommentCount')
self.term.add_space(win)
self.term.add_line(win, '{comments}'.format(**data), attr=attr)
self.term.add_line(win, '{comments} comments'.format(**data), attr=attr)
if data['gold']:
attr = self.term.attr('Gold')

View File

@@ -316,7 +316,7 @@ class SubredditPage(Page):
lambda data: self._submission_attr(data), first))
elif item == "%s":
# Need to cut off the characters that aren't the score number
form.append((lambda data: data['score'][:-4],
form.append((lambda data: str(data['score']),
lambda data: self.term.attr('Score'), first))
elif item == "%v":
# This isn't great, self.term.get_arrow gets called twice
@@ -324,7 +324,7 @@ class SubredditPage(Page):
lambda data: self.term.get_arrow(data['likes'])[1], first))
elif item == "%c":
form.append((
lambda data: '{0}'.format(data['comments'][:-9])
lambda data: data['comments']
if data['comments'] else None, # Don't try to subscript null items
lambda data: self.term.attr('CommentCount'),
first))
@@ -483,7 +483,7 @@ class SubredditPage(Page):
if row in valid_rows:
attr = self.term.attr('Score')
self.term.add_line(win, '{score}'.format(**data), row, 1, attr)
self.term.add_line(win, '{score} pts'.format(**data), row, 1, attr)
self.term.add_space(win)
arrow, attr = self.term.get_arrow(data['likes'])
@@ -500,7 +500,7 @@ class SubredditPage(Page):
attr = self.term.attr('CommentCount')
self.term.add_space(win)
self.term.add_line(win, '{comments}'.format(**data), attr=attr)
self.term.add_line(win, '{comments} comments'.format(**data), attr=attr)
if data['saved']:
attr = self.term.attr('Saved')