Skip to content
This repository was archived by the owner on Feb 20, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions rtv/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ def main():
# Load the browsing history from previous sessions
config.load_history()

# Load the tagging from previous sessions
config.load_res()

# Load any previously saved auth session token
config.load_refresh_token()
if config['clear_auth']:
Expand Down Expand Up @@ -244,6 +247,8 @@ def main():
finally:
# Try to save the browsing history
config.save_history()
# Try to save the res data
# config.save_res()
# Ensure sockets are closed to prevent a ResourceWarning
if 'reddit' in locals():
reddit.handler.http.close()
Expand Down
21 changes: 20 additions & 1 deletion rtv/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

import os
import codecs
import json
import shutil
import time
import argparse
from functools import partial

Expand All @@ -26,6 +28,7 @@
TOKEN = os.path.join(XDG_DATA_HOME, 'rtv', 'refresh-token')
HISTORY = os.path.join(XDG_DATA_HOME, 'rtv', 'history.log')
THEMES = os.path.join(XDG_CONFIG_HOME, 'rtv', 'themes')
RESBACKUP = os.path.join(XDG_CONFIG_HOME, 'rtv', 'resbackup')


def build_parser():
Expand Down Expand Up @@ -151,10 +154,12 @@ class Config(object):
This class manages the loading and saving of configs and other files.
"""

def __init__(self, history_file=HISTORY, token_file=TOKEN, **kwargs):
def __init__(self, history_file=HISTORY, token_file=TOKEN,
resbackup_file=RESBACKUP, **kwargs):

self.history_file = history_file
self.token_file = token_file
self.resbackup_file = resbackup_file
self.config = kwargs

default, bindings = self.get_file(DEFAULT_CONFIG)
Expand All @@ -165,6 +170,7 @@ def __init__(self, history_file=HISTORY, token_file=TOKEN, **kwargs):
# so they are treated differently from the rest of the config options.
self.refresh_token = None
self.history = OrderedSet()
self.res_data = {}

def __getitem__(self, item):
if item in self.config:
Expand Down Expand Up @@ -215,6 +221,19 @@ def delete_history(self):
os.remove(self.history_file)
self.history = OrderedSet()

def load_res(self):
if os.path.exists(self.resbackup_file):
with codecs.open(self.resbackup_file, encoding='utf-8') as fp:
self.res_data = json.load(fp)['data']
else:
self.res_data = {}

def save_res(self):
self.res_data['backup.lastModified,file'] = int(time.time() * 1000)
self._ensure_filepath(self.resbackup_file)
with codecs.open(self.resbackup_file, 'w+', encoding='utf-8') as fp:
json.dump({'SCHEMA_VERSION': 2, 'data': self.res_data}, fp)

@staticmethod
def get_args():
"""
Expand Down
19 changes: 19 additions & 0 deletions rtv/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,25 @@ def _draw_footer(self):
self.term.add_line(window, text, 0, 0)
self._row += 1

def _draw_user_tag(self, win, data):
user_tag = self.config.res_data.get(
'tag.{}'.format(data['author']).lower())
if not user_tag:
return

try:
attr = self.term.attr(user_tag['color'])
except KeyError:
attr = self.term.attr('none')

self.term.add_space(win)
self.term.add_line(win, '{text}'.format(**user_tag), attr=attr)

if 'votesUp' in user_tag and 'votesDown' in user_tag:
votesNet = '[%+1.f]' % (user_tag['votesUp'] - user_tag['votesDown'])
self.term.add_line(win, ' {}'.format(votesNet), attr=attr)


def _move_cursor(self, direction):
# Note: ACS_VLINE doesn't like changing the attribute, so disregard the
# redraw flag and opt to always redraw
Expand Down
4 changes: 4 additions & 0 deletions rtv/submission_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,8 @@ def _draw_comment(self, win, data, inverted):
self.term.add_space(win)
self.term.add_line(win, '{flair}'.format(**data), attr=attr)

self._draw_user_tag(win, data)

arrow, attr = self.term.get_arrow(data['likes'])
self.term.add_space(win)
self.term.add_line(win, arrow, attr=attr)
Expand Down Expand Up @@ -403,6 +405,8 @@ def _draw_submission(self, win, data):
self.term.add_space(win)
self.term.add_line(win, '{flair}'.format(**data), attr=attr)

self._draw_user_tag(win, data)

attr = self.term.attr('SubmissionSubreddit')
self.term.add_space(win)
self.term.add_line(win, '/r/{subreddit}'.format(**data), attr=attr)
Expand Down
2 changes: 2 additions & 0 deletions rtv/subreddit_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,8 @@ def _draw_item(self, win, data, inverted):
self.term.add_space(win)
self.term.add_line(win, '{flair}'.format(**data), attr=attr)

self._draw_user_tag(win, data)

attr = self.term.attr('CursorBlock')
for y in range(n_rows):
self.term.addch(win, y, 0, str(' '), attr)
1 change: 1 addition & 0 deletions rtv/templates/resbackup
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"SCHEMA_VERSION":2,"data":{"backup.lastModified.file":1536371213026,"tag.spez":{"color":"red","ignored":true,"link":"https://old.reddit.com/r/announcements/comments/5frg1n/tifu_by_editing_some_comments_and_creating_an/","text":"reddit CEO","votesDown":2,"votesUp":5},"tag.civilization_phaze_3":{"color":"green","link":"https://old.reddit.com/r/Python/comments/2xmo63/a_python_terminal_viewer_for_browsing_reddit/","text":"rtv"}}}
23 changes: 23 additions & 0 deletions rtv/theme.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,24 @@ class Theme(object):
'Link': (curses.COLOR_BLUE, None, curses.A_UNDERLINE),
'LinkSeen': (curses.COLOR_MAGENTA, None, curses.A_UNDERLINE),
'UserFlair': (curses.COLOR_YELLOW, None, curses.A_BOLD)
},
# tagging
'tagging': {
'black': (curses.COLOR_BLACK, None, curses.A_REVERSE),
'red': (curses.COLOR_RED, None, curses.A_REVERSE),
'green': (curses.COLOR_GREEN, None, curses.A_REVERSE),
'yellow': (curses.COLOR_YELLOW, None, curses.A_REVERSE),
'blue': (curses.COLOR_BLUE, None, curses.A_REVERSE),
'magenta': (curses.COLOR_MAGENTA, None, curses.A_REVERSE),
'cyan': (curses.COLOR_CYAN, None, curses.A_REVERSE),
'silver': (curses.COLOR_WHITE, None, curses.A_REVERSE),
'none': (None, None, curses.A_REVERSE),
'gray': (8, None, curses.A_REVERSE),
'orange': (9, None, curses.A_REVERSE),
'lime': (10, None, curses.A_REVERSE),
'cornflowerblue': (12, None, curses.A_REVERSE),
'pink': (13, None, curses.A_REVERSE),
'white': (15, None, curses.A_REVERSE),
}
}

Expand Down Expand Up @@ -170,6 +188,9 @@ def __init__(self, name=None, source=None, elements=None, use_color=True):
# Create the "Selected" versions of elements, which are prefixed with
# the @ symbol. For example, "@CommentText" represents how comment
# text is formatted when it is highlighted by the cursor.
for key in self.DEFAULT_THEME['tagging']:
dest = '@{0}'.format(key)
self._set_fallback(elements, key, 'Selected', dest)
for key in self.DEFAULT_THEME['normal']:
dest = '@{0}'.format(key)
self._set_fallback(elements, key, 'Selected', dest)
Expand All @@ -178,6 +199,8 @@ def __init__(self, name=None, source=None, elements=None, use_color=True):
self._set_fallback(elements, key, 'SelectedCursor', dest)

# Fill in the ``None`` values for all of the elements with normal text
for key in self.DEFAULT_THEME['tagging']:
self._set_fallback(elements, key, 'Normal')
for key in self.DEFAULT_THEME['normal']:
self._set_fallback(elements, key, 'Normal')
for key in self.DEFAULT_THEME['cursor']:
Expand Down
27 changes: 27 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,30 @@ def test_config_history():
config.delete_history()
assert len(config.history) == 0
assert not os.path.exists(fp.name)


def test_config_resbackup():
"Ensure that the resbackup data can be loaded and saved"

# Should still be able to load if the file doesn't exist
config = Config(resbackup_file='/fake_path/fake_file')
config.load_res()
assert len(config.res_data) == 0

with NamedTemporaryFile(delete=False) as fp:
config = Config(resbackup_file=fp.name, history_size=3)

config.res_data['tag.#testuser'] = {'color': 'red',
'ignored': False,
'link': 'https://reddit.com',
'text': 'very funny person',
'votesDown': 2,
'votesUp': 15}
assert len(config.res_data) == 1

# should save last modified time
config.save_res()
config.load_res()
assert len(config.res_data) == 2
assert 'tag.#testuser' in config.res_data
assert 'backup.lastModified.file' in config.res_data