<s> import utils <EOL> import config <EOL> import re <EOL> import git <EOL> import copy <EOL> import comment_list <EOL> import webbrowser <EOL> import github <EOL> try : <EOL> import vim <EOL> except ImportError as e : <EOL> vim = False <EOL> i_hash = { } <EOL> class Issue : <EOL> defaults = { <EOL> "<STR_LIT:title>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:state>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ ] , <EOL> "<STR_LIT:body>" : "<STR_LIT>" , <EOL> } <EOL> def __init__ ( self , ** kwargs ) : <EOL> if not Issue . defaults . get ( "<STR_LIT>" ) : <EOL> Issue . defaults [ "<STR_LIT>" ] = utils . github . user ( ) [ "<STR_LIT>" ] <EOL> self . repo = kwargs . get ( "<STR_LIT>" ) <EOL> self . number = kwargs . get ( "<STR_LIT>" ) <EOL> self . issue_uri = "<STR_LIT>" % ( self . repo , self . number ) <EOL> self . comments = comment_list . CommentList ( self . number , self . repo ) <EOL> self . _get_data ( ) <EOL> @ classmethod <EOL> def open ( cls , * args ) : <EOL> i = cls . _issue_from_args ( * args ) <EOL> if not i or not i . repo : <EOL> print "<STR_LIT>" <EOL> return <EOL> i . post_hook ( ) <EOL> @ classmethod <EOL> def browse ( cls , * args ) : <EOL> i = cls . _issue_from_args ( * args ) <EOL> if hasattr ( i , "<STR_LIT:url>" ) : <EOL> webbrowser . open ( i . url ) <EOL> i . map_buffer ( ) <EOL> @ classmethod <EOL> def save ( cls ) : <EOL> i = cls . _issue_from_buffer ( ) <EOL> if not i : <EOL> print "<STR_LIT>" <EOL> return <EOL> i . position = vim . current . window . cursor <EOL> i . parse ( ) <EOL> i . update ( ) <EOL> i . post_hook ( ) <EOL> @ classmethod <EOL> def toggle_state ( cls ) : <EOL> i = cls . _issue_from_buffer ( ) <EOL> i . position = vim . current . window . cursor <EOL> i . parse ( ) <EOL> i . change_state ( ) <EOL> i . update ( ) <EOL> i . post_hook ( ) <EOL> @ classmethod <EOL> def _issue_from_args ( cls , * args , ** kwargs ) : <EOL> kwargs = utils . args_to_kwargs ( args , kwargs ) <EOL> if not kwargs . get ( "<STR_LIT:args>" ) or len ( kwargs . get ( "<STR_LIT:args>" ) ) == <NUM_LIT:0> : <EOL> kwargs [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> else : <EOL> kwargs [ "<STR_LIT>" ] = kwargs . get ( "<STR_LIT:args>" ) [ <NUM_LIT:0> ] <EOL> del kwargs [ "<STR_LIT:args>" ] <EOL> key = "<STR_LIT>" % ( kwargs . get ( "<STR_LIT>" ) , kwargs . get ( "<STR_LIT>" ) ) <EOL> if not i_hash . has_key ( key ) : <EOL> i_hash [ key ] = cls ( ** kwargs ) <EOL> return i_hash . get ( key ) <EOL> @ classmethod <EOL> def _issue_from_buffer ( cls ) : <EOL> bname = vim . current . buffer . name <EOL> mg = re . match ( r"<STR_LIT>" , bname ) <EOL> if not mg : <EOL> return None <EOL> return i_hash . get ( bname ) <EOL> def change_state ( self ) : <EOL> if self . data [ "<STR_LIT:state>" ] == "<STR_LIT>" : <EOL> self . data [ "<STR_LIT:state>" ] = "<STR_LIT>" <EOL> else : <EOL> self . data [ "<STR_LIT:state>" ] = "<STR_LIT>" <EOL> def parse ( self ) : <EOL> self . data [ "<STR_LIT:body>" ] = [ ] <EOL> for index , line in enumerate ( vim . current . buffer [ <NUM_LIT:1> : ] ) : <EOL> mg = re . match ( r"<STR_LIT>" , line ) <EOL> if mg : <EOL> value = mg . group ( "<STR_LIT:value>" ) <EOL> label = mg . group ( "<STR_LIT:label>" ) . lower ( ) <EOL> if label in self . defaults . keys ( ) : <EOL> if type ( self . defaults [ label ] ) == list : <EOL> self . data [ label ] = value . split ( "<STR_LIT:U+002C>" ) <EOL> else : <EOL> self . data [ label ] = value <EOL> elif re . search ( r"<STR_LIT>" % self . number , line ) : <EOL> self . comments . parse ( vim . current . buffer [ index + <NUM_LIT:1> : - <NUM_LIT:1> ] ) <EOL> break <EOL> else : <EOL> self . data [ "<STR_LIT:body>" ] . append ( line ) <EOL> self . data [ "<STR_LIT:body>" ] = utils . trim_lines ( self . data [ "<STR_LIT:body>" ] ) <EOL> def post_hook ( self ) : <EOL> self . draw ( ) <EOL> self . map_buffer ( ) <EOL> if hasattr ( self , "<STR_LIT>" ) : <EOL> vim . command ( str ( self . position [ <NUM_LIT:0> ] ) ) <EOL> def map_buffer ( self ) : <EOL> vim . command ( "<STR_LIT>" ) <EOL> vim . command ( "<STR_LIT>" ) <EOL> vim . command ( "<STR_LIT>" % ( self . repo , self . number ) ) <EOL> def draw ( self ) : <EOL> self . buffer_name = "<STR_LIT>" % ( self . repo , self . number ) <EOL> b = utils . get_buffer ( self . buffer_name ) <EOL> vim . command ( "<STR_LIT>" ) <EOL> vim . command ( "<STR_LIT>" ) <EOL> b . append ( "<STR_LIT>" % ( self . repo , self . number ) ) <EOL> b . append ( "<STR_LIT>" ) <EOL> keys = self . data . keys ( ) <EOL> keys . remove ( "<STR_LIT:body>" ) <EOL> for key in keys : <EOL> value = self . data [ key ] <EOL> if type ( value ) == list : <EOL> value = "<STR_LIT:U+002C>" . join ( value ) <EOL> b . append ( "<STR_LIT>" % ( key . capitalize ( ) , value ) ) <EOL> if self . data . has_key ( "<STR_LIT:body>" ) and self . data [ "<STR_LIT:body>" ] : <EOL> for line in self . data [ "<STR_LIT:body>" ] . splitlines ( ) : <EOL> b . append ( line ) <EOL> self . comments . draw ( b ) <EOL> vim . command ( "<STR_LIT>" ) <EOL> def update ( self ) : <EOL> if self . number == "<STR_LIT>" : <EOL> self . _create_issue ( ) <EOL> else : <EOL> self . _save_issue ( ) <EOL> def _get_data ( self ) : <EOL> self . data = copy . deepcopy ( self . defaults ) <EOL> if not self . number == "<STR_LIT>" : <EOL> data , status = github . request ( github . url ( self . issue_uri ) ) <EOL> if not status : <EOL> utils . log ( data ) <EOL> return <EOL> for key in self . defaults . keys ( ) + [ "<STR_LIT>" , "<STR_LIT:user>" ] : <EOL> if key in ( "<STR_LIT>" , "<STR_LIT:user>" ) and data . get ( key ) : <EOL> self . data [ key ] = data [ key ] [ "<STR_LIT>" ] <EOL> elif key == "<STR_LIT>" : <EOL> self . data [ key ] = [ str ( label [ "<STR_LIT:name>" ] ) for label in data [ key ] ] <EOL> elif key == "<STR_LIT>" and data . get ( "<STR_LIT>" ) : <EOL> self . data [ key ] = data [ key ] [ "<STR_LIT:title>" ] <EOL> elif data . get ( key ) : <EOL> self . data [ key ] = data [ key ] <EOL> self . url = data [ "<STR_LIT>" ] <EOL> def _create_issue ( self ) : <EOL> uri = "<STR_LIT>" % self . repo <EOL> url = github . url ( uri ) <EOL> data = utils . clean_data ( copy . deepcopy ( self . data ) , [ "<STR_LIT:state>" ] ) <EOL> if not data or len ( data . keys ( ) ) == <NUM_LIT:0> : <EOL> utils . log ( "<STR_LIT>" ) <EOL> return <EOL> data , status = github . request ( url , "<STR_LIT>" , data ) <EOL> if not status : <EOL> utils . log ( data ) <EOL> return <EOL> self . number = str ( data [ "<STR_LIT>" ] ) <EOL> self . data [ "<STR_LIT:user>" ] = data [ "<STR_LIT:user>" ] [ "<STR_LIT>" ] <EOL> self . url = data [ "<STR_LIT>" ] <EOL> self . issue_uri = "<STR_LIT>" % ( self . repo , self . number ) <EOL> self . comments . number = self . number <EOL> del i_hash [ "<STR_LIT>" % ( self . repo , "<STR_LIT>" ) ] <EOL> i_hash [ "<STR_LIT>" % ( self . repo , self . number ) ] = self <EOL> vim . command ( "<STR_LIT>" ) <EOL> vim . command ( "<STR_LIT>" % self . buffer_name ) <EOL> def _save_issue ( self ) : <EOL> url = github . url ( self . issue_uri ) <EOL> data = utils . clean_data ( copy . deepcopy ( self . data ) , [ "<STR_LIT>" , "<STR_LIT:user>" , "<STR_LIT>" ] ) <EOL> data , status = github . request ( url , "<STR_LIT>" , data ) </s>
