<s> from __future__ import unicode_literals <EOL> import time <EOL> import curses <EOL> from . import docs <EOL> from . content import SubredditContent <EOL> from . page import Page , PageController , logged_in <EOL> from . objects import Navigator , Color , Command <EOL> from . submission import SubmissionPage <EOL> from . subscription import SubscriptionPage <EOL> class SubredditController ( PageController ) : <EOL> character_map = { } <EOL> class SubredditPage ( Page ) : <EOL> def __init__ ( self , reddit , term , config , oauth , name ) : <EOL> """<STR_LIT>""" <EOL> super ( SubredditPage , self ) . __init__ ( reddit , term , config , oauth ) <EOL> self . controller = SubredditController ( self , keymap = config . keymap ) <EOL> self . content = SubredditContent . from_name ( reddit , name , term . loader ) <EOL> self . nav = Navigator ( self . content . get ) <EOL> @ SubredditController . register ( Command ( '<STR_LIT>' ) ) <EOL> def refresh_content ( self , order = None , name = None ) : <EOL> "<STR_LIT>" <EOL> order = order or self . content . order <EOL> name = name or self . content . name <EOL> if order == '<STR_LIT:ignore>' : <EOL> order = None <EOL> with self . term . loader ( '<STR_LIT>' ) : <EOL> self . content = SubredditContent . from_name ( <EOL> self . reddit , name , self . term . loader , order = order ) <EOL> if not self . term . loader . exception : <EOL> self . nav = Navigator ( self . content . get ) <EOL> @ SubredditController . register ( Command ( '<STR_LIT>' ) ) <EOL> def search_subreddit ( self , name = None ) : <EOL> "<STR_LIT>" <EOL> name = name or self . content . name <EOL> query = self . term . prompt_input ( '<STR_LIT>' . format ( name ) ) <EOL> if not query : <EOL> return <EOL> with self . term . loader ( '<STR_LIT>' ) : <EOL> self . content = SubredditContent . from_name ( <EOL> self . reddit , name , self . term . loader , query = query ) <EOL> if not self . term . loader . exception : <EOL> self . nav = Navigator ( self . content . get ) <EOL> @ SubredditController . register ( Command ( '<STR_LIT>' ) ) <EOL> def prompt_subreddit ( self ) : <EOL> "<STR_LIT>" <EOL> name = self . term . prompt_input ( '<STR_LIT>' ) <EOL> if name is not None : <EOL> self . refresh_content ( order = '<STR_LIT:ignore>' , name = name ) <EOL> @ SubredditController . register ( Command ( '<STR_LIT>' ) ) <EOL> def open_submission ( self , url = None ) : <EOL> "<STR_LIT>" <EOL> data = { } <EOL> if url is None : <EOL> data = self . content . get ( self . nav . absolute_index ) <EOL> url = data [ '<STR_LIT>' ] <EOL> with self . term . loader ( '<STR_LIT>' ) : <EOL> page = SubmissionPage ( <EOL> self . reddit , self . term , self . config , self . oauth , url = url ) <EOL> if self . term . loader . exception : <EOL> return <EOL> page . loop ( ) <EOL> if data . get ( '<STR_LIT>' ) == '<STR_LIT>' : <EOL> self . config . history . add ( data [ '<STR_LIT>' ] ) <EOL> @ SubredditController . register ( Command ( '<STR_LIT>' ) ) <EOL> def open_link ( self ) : <EOL> "<STR_LIT>" <EOL> data = self . content . get ( self . nav . absolute_index ) <EOL> if data [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> self . open_submission ( ) <EOL> elif data [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> self . open_submission ( url = data [ '<STR_LIT>' ] ) <EOL> self . config . history . add ( data [ '<STR_LIT>' ] ) <EOL> else : <EOL> self . term . open_browser ( data [ '<STR_LIT>' ] ) <EOL> self . config . history . add ( data [ '<STR_LIT>' ] ) <EOL> @ SubredditController . register ( Command ( '<STR_LIT>' ) ) <EOL> @ logged_in <EOL> def post_submission ( self ) : <EOL> "<STR_LIT>" <EOL> name = self . content . name <EOL> if '<STR_LIT:+>' in name or name in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> self . term . show_notification ( "<STR_LIT>" . format ( name ) ) <EOL> return <EOL> submission_info = docs . SUBMISSION_FILE . format ( name = name ) <EOL> text = self . term . open_editor ( submission_info ) <EOL> if not text or '<STR_LIT:\n>' not in text : <EOL> self . term . show_notification ( '<STR_LIT>' ) <EOL> return <EOL> title , content = text . split ( '<STR_LIT:\n>' , <NUM_LIT:1> ) <EOL> with self . term . loader ( '<STR_LIT>' , delay = <NUM_LIT:0> ) : <EOL> submission = self . reddit . submit ( name , title , text = content , <EOL> raise_captcha_exception = True ) <EOL> time . sleep ( <NUM_LIT> ) <EOL> if self . term . loader . exception : <EOL> return <EOL> with self . term . loader ( '<STR_LIT>' ) : <EOL> page = SubmissionPage ( <EOL> self . reddit , self . term , self . config , self . oauth , <EOL> submission = submission ) <EOL> if self . term . loader . exception : <EOL> return <EOL> page . loop ( ) <EOL> self . refresh_content ( ) <EOL> @ SubredditController . register ( Command ( '<STR_LIT>' ) ) <EOL> @ logged_in <EOL> def open_subscriptions ( self ) : <EOL> "<STR_LIT>" <EOL> with self . term . loader ( '<STR_LIT>' ) : <EOL> page = SubscriptionPage ( <EOL> self . reddit , self . term , self . config , self . oauth ) <EOL> if self . term . loader . exception : <EOL> return <EOL> page . loop ( ) <EOL> if page . subreddit_data is not None : <EOL> self . refresh_content ( order = '<STR_LIT:ignore>' , <EOL> name = page . subreddit_data [ '<STR_LIT:name>' ] ) <EOL> def _draw_item ( self , win , data , inverted ) : <EOL> n_rows , n_cols = win . getmaxyx ( ) <EOL> n_cols -= <NUM_LIT:1> <EOL> valid_rows = range ( <NUM_LIT:0> , n_rows ) <EOL> offset = <NUM_LIT:0> if not inverted else - ( data [ '<STR_LIT>' ] - n_rows ) <EOL> n_title = len ( data [ '<STR_LIT>' ] ) <EOL> for row , text in enumerate ( data [ '<STR_LIT>' ] , start = offset ) : <EOL> if row in valid_rows : <EOL> self . term . add_line ( win , text , row , <NUM_LIT:1> , curses . A_BOLD ) <EOL> row = n_title + offset <EOL> if row in valid_rows : <EOL> seen = ( data [ '<STR_LIT>' ] in self . config . history ) <EOL> link_color = Color . MAGENTA if seen else Color . BLUE <EOL> attr = curses . A_UNDERLINE | link_color <EOL> self . term . add_line ( win , '<STR_LIT>' . format ( ** data ) , row , <NUM_LIT:1> , attr ) <EOL> row = n_title + offset + <NUM_LIT:1> <EOL> if row in valid_rows : <EOL> self . term . add_line ( win , '<STR_LIT>' . format ( ** data ) , row , <NUM_LIT:1> ) <EOL> text , attr = self . term . get_arrow ( data [ '<STR_LIT>' ] ) <EOL> self . term . add_line ( win , text , attr = attr ) <EOL> self . term . add_line ( win , '<STR_LIT>' . format ( ** data ) ) <EOL> text , attr = self . term . timestamp_sep <EOL> self . term . add_line ( win , text , attr = attr ) <EOL> self . term . add_line ( win , '<STR_LIT>' . format ( ** data ) ) <EOL> if data [ '<STR_LIT>' ] : <EOL> text , attr = self . term . stickied <EOL> self . term . add_line ( win , text , attr = attr ) <EOL> if data [ '<STR_LIT>' ] : <EOL> text , attr = self . term . guilded <EOL> self . term . add_line ( win , text , attr = attr ) <EOL> if data [ '<STR_LIT>' ] : <EOL> text , attr = '<STR_LIT>' , ( curses . A_BOLD | Color . RED ) <EOL> self . term . add_line ( win , text , attr = attr ) <EOL> row = n_title + offset + <NUM_LIT:2> <EOL> if row in valid_rows : <EOL> text = '<STR_LIT>' . format ( ** data ) <EOL> self . term . add_line ( win , text , row , <NUM_LIT:1> , curses . A_BOLD ) <EOL> text = '<STR_LIT>' . format ( ** data ) <EOL> self . term . add_line ( win , text , attr = Color . YELLOW ) <EOL> if data [ '<STR_LIT>' ] : <EOL> text = '<STR_LIT>' . format ( ** data ) <EOL> self . term . add_line ( win , text , attr = Color . RED ) </s>
