From d6620dbdbe6eb10f6aec9e265e6a596e861844ff Mon Sep 17 00:00:00 2001 From: Robert Greener Date: Sun, 8 Mar 2015 21:28:36 +0000 Subject: [PATCH 1/3] Added -a for force_ascii, utf-8 by default --- __main__.py | 7 +++++++ rtv/content.py | 23 +++++++++++++++++------ rtv/main.py | 19 ++++++++++++++----- 3 files changed, 38 insertions(+), 11 deletions(-) create mode 100644 __main__.py diff --git a/__main__.py b/__main__.py new file mode 100644 index 0000000..846ccc1 --- /dev/null +++ b/__main__.py @@ -0,0 +1,7 @@ +# Entry point for rtv module +# Run by moving into the top level directory (the one with setup.py) +# and typing "python -m rtv" + +from rtv.main import main + +main() diff --git a/rtv/content.py b/rtv/content.py index 48e321d..404b5f0 100644 --- a/rtv/content.py +++ b/rtv/content.py @@ -8,19 +8,30 @@ import requests from .errors import SubmissionURLError, SubredditNameError +is_utf8 = True + def clean(unicode_string): """ - Convert unicode string into ascii-safe characters. + Checks if -a was given, if it was it + converts unicode string into ascii-safe characters. """ + if not is_utf8: + if six.PY2: + ascii_string = unicode_string.encode('ascii', 'replace') + else: + ascii_string = unicode_string.encode().decode('ascii', 'replace') - if six.PY2: - ascii_string = unicode_string.encode('ascii', 'replace') + ascii_string = ascii_string.replace('\\', '') + return ascii_string else: - ascii_string = unicode_string.encode().decode('ascii', 'replace') + if six.PY2: + utf8_string = unicode_string.encode('utf-8', 'replace') + else: + utf8_string = unicode_string.encode().decode('utf-8', 'replace') - ascii_string = ascii_string.replace('\\', '') - return ascii_string + utf8_string = utf8_string.replace('\\', '') + return utf8_string def split_text(big_text, width): diff --git a/rtv/main.py b/rtv/main.py index b868676..697a69f 100644 --- a/rtv/main.py +++ b/rtv/main.py @@ -8,7 +8,10 @@ from .errors import SubmissionURLError, SubredditNameError from .utils import curses_session, load_config, HELP from .subreddit import SubredditPage from .submission import SubmissionPage +import locale +import rtv.content +is_utf8 = True # if -a is set then this is set to false DESCRIPTION = """ Reddit Terminal Viewer is a lightweight browser for www.reddit.com built into a @@ -19,9 +22,9 @@ EPILOG = """ Controls ----- RTV currently supports browsing both subreddits and individual submissions. -In each mode the controls are slightly different. In subreddit mode you can -browse through the top submissions on either the front page or a specific -subreddit. In submission mode you can view the self text for a submission and +In each mode the controls are slightly different. In subreddit mode you can +browse through the top submissions on either the front page or a specific +subreddit. In submission mode you can view the self text for a submission and browse comments. """ @@ -34,9 +37,11 @@ def main(): formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument('-s', dest='subreddit', help='subreddit name') parser.add_argument('-l', dest='link', help='full link to a submission') + parser.add_argument('-a', dest='force_ascii', + help='forces ascii (disables unicode)', action='store_true') group = parser.add_argument_group( - 'authentication (optional)', + 'authentication (optional)', 'Authenticating allows you to view your customized front page. ' 'If only the username is given, the password will be prompted ' 'securely.') @@ -54,6 +59,10 @@ def main(): if args.subreddit is None: args.subreddit = 'front' + if args.force_ascii: + rtv.content.is_utf8 = False + locale.setlocale(locale.LC_ALL, '') + try: reddit = praw.Reddit(user_agent='desktop:https://github.com/michael-lazar/rtv:(by /u/civilization_phaze_3)') reddit.config.decode_html_entities = True @@ -75,7 +84,7 @@ def main(): page.loop() except InvalidUserPass: - print('Invalid password for username: {}'.format(args.username)) + print('Invalid password for username: {}'.format(args.username)) except KeyboardInterrupt: return From 6e88f339868e8668cc1b6a3accef6c27c5c438d5 Mon Sep 17 00:00:00 2001 From: Robert Greener Date: Sun, 8 Mar 2015 21:55:46 +0000 Subject: [PATCH 2/3] removed unused variable --- rtv/main.py | 1 - 1 file changed, 1 deletion(-) diff --git a/rtv/main.py b/rtv/main.py index 697a69f..8a4b865 100644 --- a/rtv/main.py +++ b/rtv/main.py @@ -11,7 +11,6 @@ from .submission import SubmissionPage import locale import rtv.content -is_utf8 = True # if -a is set then this is set to false DESCRIPTION = """ Reddit Terminal Viewer is a lightweight browser for www.reddit.com built into a From abadbd7037503973ce009b7c5e8c79150b1c375d Mon Sep 17 00:00:00 2001 From: Robert Greener Date: Tue, 10 Mar 2015 17:51:34 +0000 Subject: [PATCH 3/3] Removed copy of __main__.py, changed flag from -a to --force-ascii --- __main__.py | 7 ------- rtv/main.py | 2 +- 2 files changed, 1 insertion(+), 8 deletions(-) delete mode 100644 __main__.py diff --git a/__main__.py b/__main__.py deleted file mode 100644 index 846ccc1..0000000 --- a/__main__.py +++ /dev/null @@ -1,7 +0,0 @@ -# Entry point for rtv module -# Run by moving into the top level directory (the one with setup.py) -# and typing "python -m rtv" - -from rtv.main import main - -main() diff --git a/rtv/main.py b/rtv/main.py index 8a4b865..434ca73 100644 --- a/rtv/main.py +++ b/rtv/main.py @@ -36,7 +36,7 @@ def main(): formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument('-s', dest='subreddit', help='subreddit name') parser.add_argument('-l', dest='link', help='full link to a submission') - parser.add_argument('-a', dest='force_ascii', + parser.add_argument('--force-ascii', dest='force_ascii', help='forces ascii (disables unicode)', action='store_true') group = parser.add_argument_group(