<s> import sublime , sublime_plugin <EOL> import os <EOL> import json <EOL> import threading <EOL> from . gist . api import GistApi <EOL> from . gist . api import GIST_BASE_URL as base_url <EOL> from . gist . lib import util <EOL> from . gist . lib import callback <EOL> from . gist . lib . progress import ThreadProgress <EOL> from . gist . lib . panel import Printer <EOL> class BaseGistView ( object ) : <EOL> """<STR_LIT>""" <EOL> def is_enabled ( self ) : <EOL> if not self . view or not self . view . file_name ( ) : <EOL> return False <EOL> self . fileFullName = self . view . file_name ( ) <EOL> self . settings = util . get_settings ( ) <EOL> self . options = self . view . settings ( ) . get ( "<STR_LIT>" ) <EOL> if not self . options : return False <EOL> self . fileName = self . options [ "<STR_LIT>" ] <EOL> self . fileProperty = self . options [ "<STR_LIT>" ] <EOL> self . gist = self . options [ "<STR_LIT>" ] <EOL> self . gist_id = self . gist [ "<STR_LIT:id>" ] <EOL> self . html_url = self . gist [ "<STR_LIT>" ] <EOL> self . gist_url = self . gist [ "<STR_LIT:url>" ] <EOL> self . raw_url = self . fileProperty [ "<STR_LIT>" ] <EOL> return True <EOL> class ChooseGist ( sublime_plugin . WindowCommand ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( ChooseGist , self ) . __init__ ( * args , ** kwargs ) <EOL> def run ( self , read_cache = True , include_files = True , callback_command = None ) : <EOL> self . include_files = include_files <EOL> self . callback_command = callback_command <EOL> self . settings = util . get_settings ( ) <EOL> if not self . settings [ "<STR_LIT>" ] : <EOL> message = "<STR_LIT>" + "<STR_LIT>" <EOL> return Printer . get ( "<STR_LIT>" ) . write ( message ) <EOL> if read_cache : <EOL> _gists = util . get_gists_cache ( self . settings ) <EOL> if _gists : return self . choose_gist ( _gists ) <EOL> api = GistApi ( self . settings [ "<STR_LIT>" ] ) <EOL> thread = threading . Thread ( target = api . list ) <EOL> thread . start ( ) <EOL> ThreadProgress ( api , thread , '<STR_LIT>' , <EOL> self . choose_gist , _callback_options = { } <EOL> ) <EOL> def choose_gist ( self , res , options = { } ) : <EOL> if not isinstance ( res , list ) : <EOL> _gists = res . json ( ) <EOL> else : <EOL> _gists = res <EOL> _gists = sorted ( _gists , key = lambda k : k [ '<STR_LIT>' ] , reverse = True ) <EOL> util . add_gists_to_cache ( _gists ) <EOL> if self . callback_command : <EOL> self . items = [ ] <EOL> self . items_property = { } <EOL> for _gist in _gists : <EOL> if not self . include_files : <EOL> description = _gist [ "<STR_LIT:description>" ] <EOL> if not description : <EOL> description = "<STR_LIT>" % _gist [ "<STR_LIT:id>" ] <EOL> self . items . append ( description ) <EOL> self . items_property [ description ] = { <EOL> "<STR_LIT>" : _gist <EOL> } <EOL> else : <EOL> description = _gist [ "<STR_LIT:description>" ] <EOL> if not description : <EOL> description = "<STR_LIT>" % _gist [ "<STR_LIT:id>" ] <EOL> files_number = len ( _gist [ "<STR_LIT>" ] ) <EOL> if files_number > <NUM_LIT:1> : <EOL> self . items . append ( description ) <EOL> gist_items_property = [ ] <EOL> for key , value in _gist [ "<STR_LIT>" ] . items ( ) : <EOL> if files_number > <NUM_LIT:1> : <EOL> key = "<STR_LIT>" % ( "<STR_LIT:U+0020>" * <NUM_LIT:4> , key ) <EOL> self . items . append ( key ) <EOL> self . items_property [ key ] = [ { <EOL> "<STR_LIT>" : key . strip ( ) , <EOL> "<STR_LIT>" : value , <EOL> "<STR_LIT>" : _gist <EOL> } ] <EOL> gist_items_property . extend ( self . items_property [ key ] ) <EOL> self . items_property [ description ] = gist_items_property <EOL> self . window . show_quick_panel ( self . items , self . on_done , <EOL> sublime . MONOSPACE_FONT ) <EOL> def on_done ( self , index ) : <EOL> if index == - <NUM_LIT:1> : return <EOL> for item in self . items_property [ self . items [ index ] ] : <EOL> self . window . run_command ( self . callback_command , { <EOL> "<STR_LIT>" : item <EOL> } ) <EOL> class OpenGist ( sublime_plugin . WindowCommand ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( OpenGist , self ) . __init__ ( * args , ** kwargs ) <EOL> def run ( self , options = { } ) : <EOL> if "<STR_LIT>" not in options : <EOL> return <EOL> filename = options [ "<STR_LIT>" ] <EOL> fileProperty = options [ "<STR_LIT>" ] <EOL> _gist = options [ "<STR_LIT>" ] <EOL> settings = util . get_settings ( ) <EOL> api = GistApi ( settings [ "<STR_LIT>" ] ) <EOL> thread = threading . Thread ( target = api . retrieve , args = ( fileProperty [ "<STR_LIT>" ] , ) ) <EOL> thread . start ( ) <EOL> ThreadProgress ( api , thread , '<STR_LIT>' % filename . strip ( ) , <EOL> callback . open_gist , _callback_options = options <EOL> ) <EOL> class DeleteExistGist ( sublime_plugin . WindowCommand ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( DeleteExistGist , self ) . __init__ ( * args , ** kwargs ) <EOL> def run ( self , options = { } ) : <EOL> if "<STR_LIT>" not in options : <EOL> return <EOL> filename = options [ "<STR_LIT>" ] <EOL> _gist = options [ "<STR_LIT>" ] <EOL> settings = util . get_settings ( ) <EOL> api = GistApi ( settings [ "<STR_LIT>" ] ) <EOL> thread = threading . Thread ( target = api . delete , args = ( _gist [ "<STR_LIT:url>" ] , ) ) <EOL> thread . start ( ) <EOL> ThreadProgress ( api , thread , '<STR_LIT>' % filename , <EOL> callback . delete_gist , _callback_options = { <EOL> "<STR_LIT>" : filename <EOL> } <EOL> ) <EOL> class OpenGistInBrowser ( sublime_plugin . WindowCommand ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( OpenGistInBrowser , self ) . __init__ ( * args , ** kwargs ) <EOL> def run ( self , options = { } ) : <EOL> if "<STR_LIT>" not in options : <EOL> return <EOL> _gist = options [ "<STR_LIT>" ] <EOL> util . open_with_browser ( _gist [ "<STR_LIT>" ] ) <EOL> class UpdateContentToGist ( sublime_plugin . TextCommand ) : <EOL> def run ( self , edit , options ) : <EOL> if "<STR_LIT>" not in options : <EOL> return <EOL> filename = options [ "<STR_LIT>" ] <EOL> _gist = options [ "<STR_LIT>" ] <EOL> data = { <EOL> "<STR_LIT>" : { <EOL> filename : { <EOL> "<STR_LIT:content>" : self . selection <EOL> } <EOL> } <EOL> } <EOL> settings = util . get_settings ( ) <EOL> api = GistApi ( settings [ "<STR_LIT>" ] ) <EOL> thread = threading . Thread ( target = api . patch , args = ( _gist [ "<STR_LIT:url>" ] , data , ) ) <EOL> thread . start ( ) <EOL> ThreadProgress ( api , thread , '<STR_LIT>' % filename , <EOL> callback . update_to_gist , _callback_options = { <EOL> "<STR_LIT>" : filename <EOL> } <EOL> ) <EOL> def is_enabled ( self ) : <EOL> self . selection = self . view . substr ( self . view . sel ( ) [ <NUM_LIT:0> ] ) <EOL> if not self . selection : <EOL> self . selection = self . view . substr ( sublime . Region ( <NUM_LIT:0> , self . view . size ( ) ) ) <EOL> if not self . selection : <EOL> return False <EOL> return True <EOL> class AddFileToGist ( sublime_plugin . TextCommand ) : <EOL> def run ( self , edit , options = { } ) : <EOL> self . gist = options [ "<STR_LIT>" ] <EOL> gist_description = self . gist [ "<STR_LIT:description>" ] <EOL> if not gist_description : <EOL> gist_description = "<STR_LIT>" % self . gist [ "<STR_LIT:id>" ] <EOL> message = "<STR_LIT>" % gist_description <EOL> sublime . active_window ( ) . show_input_panel ( message , <EOL> "<STR_LIT>" , self . on_input_name , None , None ) <EOL> def on_input_name ( self , filename ) : <EOL> if not filename : <EOL> message = "<STR_LIT>" <EOL> if not sublime . ok_cancel_dialog ( message , "<STR_LIT>" ) : return <EOL> sublime . active_window ( ) . show_input_panel ( "<STR_LIT>" , <EOL> '<STR_LIT>' , self . on_input_name , None , None ) <EOL> return <EOL> data = { <EOL> "<STR_LIT>" : { <EOL> filename : { <EOL> "<STR_LIT:content>" : self . selection <EOL> } <EOL> } <EOL> } <EOL> settings = util . get_settings ( ) <EOL> api = GistApi ( settings [ "<STR_LIT>" ] ) <EOL> thread = threading . Thread ( target = api . patch , args = ( self . gist [ "<STR_LIT:url>" ] , data , ) ) <EOL> thread . start ( ) <EOL> description = self . gist [ "<STR_LIT:description>" ] <EOL> if not description : <EOL> description = "<STR_LIT>" % self . gist [ "<STR_LIT:id>" ] <EOL> ThreadProgress ( api , thread , '<STR_LIT>' % ( filename , description ) , <EOL> callback . add_file_to_gist , _callback_options = { <EOL> "<STR_LIT>" : filename , <EOL> "<STR_LIT>" : description <EOL> } <EOL> ) <EOL> def is_enabled ( self ) : <EOL> self . selection = self . view . substr ( self . view . sel ( ) [ <NUM_LIT:0> ] ) <EOL> if not self . selection : <EOL> self . selection = self . view . substr ( sublime . Region ( <NUM_LIT:0> , self . view . size ( ) ) ) <EOL> if not self . selection : <EOL> return False <EOL> return True <EOL> class DeleteFileFromGist ( sublime_plugin . WindowCommand ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( DeleteFileFromGist , self ) . __init__ ( * args , ** kwargs ) <EOL> def run ( self , options = { } ) : <EOL> if "<STR_LIT>" not in options : <EOL> return <EOL> self . options = options <EOL> self . gist = options [ "<STR_LIT>" ] <EOL> self . fileNames = self . gist [ "<STR_LIT>" ] <EOL> self . items = [ ] <EOL> for key , value in self . fileNames . items ( ) : <EOL> self . items . append ( key ) <EOL> self . window . show_quick_panel ( self . items , self . on_done ) <EOL> def on_done ( self , index ) : <EOL> if index == - <NUM_LIT:1> : return <EOL> self . fileName = self . items [ index ] <EOL> data = { <EOL> "<STR_LIT>" : { <EOL> self . fileName : None <EOL> } <EOL> } <EOL> settings = util . get_settings ( ) <EOL> api = GistApi ( settings [ "<STR_LIT>" ] ) <EOL> thread = threading . Thread ( target = api . patch , args = ( self . gist [ "<STR_LIT:url>" ] , data , ) ) <EOL> thread . start ( ) <EOL> description = self . gist [ "<STR_LIT:description>" ] <EOL> if not description : <EOL> description = "<STR_LIT>" % self . gist [ "<STR_LIT:id>" ] <EOL> ThreadProgress ( api , thread , '<STR_LIT>' % ( self . fileName , description ) , <EOL> callback . delete_file_from_gist , _callback_options = { <EOL> "<STR_LIT>" : self . fileName , <EOL> "<STR_LIT>" : description <EOL> } <EOL> ) <EOL> class CreateGist ( sublime_plugin . TextCommand ) : <EOL> def run ( self , edit , public = False ) : <EOL> self . public = public <EOL> self . settings = util . get_settings ( ) <EOL> sublime . active_window ( ) . show_input_panel ( "<STR_LIT>" , <EOL> '<STR_LIT>' , self . on_input_name , None , None ) <EOL> def on_input_name ( self , filename ) : <EOL> if not filename : <EOL> message = "<STR_LIT>" <EOL> if not sublime . ok_cancel_dialog ( message , "<STR_LIT>" ) : return <EOL> sublime . active_window ( ) . show_input_panel ( "<STR_LIT>" , <EOL> '<STR_LIT>' , self . on_input_name , None , None ) <EOL> return <EOL> self . fileName = filename <EOL> sublime . active_window ( ) . show_input_panel ( '<STR_LIT>' , <EOL> filename . split ( "<STR_LIT:.>" ) [ <NUM_LIT:0> ] , self . on_input_descrition , None , None ) <EOL> def on_input_descrition ( self , desc ) : <EOL> post_url = base_url % "<STR_LIT>" <EOL> data = { <EOL> "<STR_LIT:description>" : desc , <EOL> "<STR_LIT>" : self . public , <EOL> "<STR_LIT>" : { <EOL> self . fileName : { <EOL> "<STR_LIT:content>" : self . selection <EOL> } <EOL> } <EOL> } <EOL> api = GistApi ( self . settings [ "<STR_LIT>" ] ) <EOL> thread = threading . Thread ( target = api . post , args = ( post_url , data , ) ) <EOL> thread . start ( ) <EOL> ThreadProgress ( api , thread , '<STR_LIT>' % self . fileName , <EOL> callback . create_gist , _callback_options = { <EOL> "<STR_LIT>" : self . fileName , <EOL> "<STR_LIT:content>" : self . selection <EOL> } <EOL> ) <EOL> def is_enabled ( self ) : <EOL> self . selection = self . view . substr ( self . view . sel ( ) [ <NUM_LIT:0> ] ) <EOL> if not self . selection : <EOL> self . selection = self . view . substr ( sublime . Region ( <NUM_LIT:0> , self . view . size ( ) ) ) <EOL> if not self . selection : <EOL> return False <EOL> return True <EOL> class RenameGist ( BaseGistView , sublime_plugin . TextCommand ) : <EOL> def run ( self , edit ) : <EOL> self . old_filename = self . fileName <EOL> sublime . active_window ( ) . show_input_panel ( '<STR_LIT>' , <EOL> self . old_filename , self . on_input_name , None , None ) <EOL> def on_input_name ( self , input ) : <EOL> if not input : <EOL> sublime . error_message ( "<STR_LIT>" ) <EOL> return <EOL> self . new_filename = input <EOL> body = open ( self . view . file_name ( ) , encoding = "<STR_LIT:utf-8>" ) . read ( ) <EOL> data = { <EOL> "<STR_LIT>" : { <EOL> self . old_filename : { <EOL> "<STR_LIT:filename>" : self . new_filename , <EOL> "<STR_LIT:content>" : body <EOL> } <EOL> } <EOL> } <EOL> api = GistApi ( self . settings [ "<STR_LIT>" ] ) <EOL> thread = threading . Thread ( target = api . patch , args = ( self . gist_url , data , ) ) <EOL> thread . start ( ) <EOL> ThreadProgress ( api , thread , '<STR_LIT>' % ( <EOL> self . old_filename , <EOL> self . new_filename <EOL> ) , <EOL> callback . rename_gist , _callback_options = { <EOL> "<STR_LIT>" : self . old_filename , <EOL> "<STR_LIT>" : self . new_filename , <EOL> "<STR_LIT>" : self . fileFullName , <EOL> "<STR_LIT>" : self . options <EOL> } <EOL> ) <EOL> class UpdateGistDescription ( BaseGistView , sublime_plugin . TextCommand ) : <EOL> def run ( self , edit ) : <EOL> sublime . active_window ( ) . show_input_panel ( '<STR_LIT>' , <EOL> "<STR_LIT>" , self . on_input_name , None , None ) <EOL> def on_input_name ( self , input ) : <EOL> if not input : <EOL> sublime . error_message ( "<STR_LIT>" ) <EOL> return <EOL> self . desc = input <EOL> body = open ( self . view . file_name ( ) , encoding = "<STR_LIT:utf-8>" ) . read ( ) <EOL> data = { <EOL> "<STR_LIT:description>" : self . desc , <EOL> "<STR_LIT>" : { <EOL> self . fileName : { <EOL> "<STR_LIT:content>" : body <EOL> } <EOL> } <EOL> } <EOL> api = GistApi ( self . settings [ "<STR_LIT>" ] ) <EOL> thread = threading . Thread ( target = api . patch , args = ( self . gist_url , data , ) ) <EOL> thread . start ( ) <EOL> ThreadProgress ( api , thread , '<STR_LIT>' , <EOL> callback . update_description , _callback_options = { <EOL> "<STR_LIT>" : self . fileFullName , <EOL> "<STR_LIT>" : self . desc <EOL> } <EOL> ) <EOL> class UpdateGist ( BaseGistView , sublime_plugin . TextCommand ) : <EOL> def run ( self , edit ) : <EOL> if self . fileName in globals ( ) : <EOL> thread = globals ( ) [ self . fileName ] <EOL> if thread and thread . is_alive ( ) : <EOL> return <EOL> body = open ( self . view . file_name ( ) , encoding = "<STR_LIT:utf-8>" ) . read ( ) <EOL> data = { <EOL> "<STR_LIT>" : { <EOL> self . fileName : { <EOL> "<STR_LIT:content>" : body <EOL> } <EOL> } <EOL> } <EOL> api = GistApi ( self . settings [ "<STR_LIT>" ] ) <EOL> thread = threading . Thread ( target = api . patch , args = ( self . gist_url , data , ) ) <EOL> thread . start ( ) <EOL> ThreadProgress ( api , thread , '<STR_LIT>' % self . fileName , <EOL> callback . update_gist , _callback_options = { <EOL> "<STR_LIT>" : self . fileFullName <EOL> } <EOL> ) <EOL> globals ( ) [ self . fileName ] = thread <EOL> class RefreshGist ( BaseGistView , sublime_plugin . TextCommand ) : <EOL> def run ( self , edit ) : <EOL> api = GistApi ( self . settings [ "<STR_LIT>" ] ) <EOL> thread = threading . Thread ( target = api . retrieve , args = ( self . raw_url , ) ) <EOL> thread . start ( ) <EOL> ThreadProgress ( api , thread , '<STR_LIT>' % self . fileName , <EOL> callback . refresh_gist , _callback_options = { <EOL> "<STR_LIT>" : self . fileFullName <EOL> } <EOL> ) <EOL> class DeleteGist ( BaseGistView , sublime_plugin . TextCommand ) : <EOL> def run ( self , edit ) : <EOL> api = GistApi ( self . settings [ "<STR_LIT>" ] ) <EOL> thread = threading . Thread ( target = api . delete , args = ( self . gist_url , ) ) <EOL> thread . start ( ) <EOL> ThreadProgress ( api , thread , '<STR_LIT>' % self . fileName , <EOL> callback . delete_gist , _callback_options = { <EOL> "<STR_LIT>" : self . fileFullName <EOL> } <EOL> ) <EOL> class ClearGistCache ( sublime_plugin . WindowCommand ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( ClearGistCache , self ) . __init__ ( * args , ** kwargs ) <EOL> def run ( self ) : <EOL> settings = util . get_settings ( ) <EOL> cachedir = os . path . join ( settings [ "<STR_LIT>" ] , "<STR_LIT>" , "<STR_LIT>" ) <EOL> try : <EOL> os . remove ( cachedir ) <EOL> Printer . get ( "<STR_LIT>" ) . write ( "<STR_LIT>" ) <EOL> except : <EOL> Printer . get ( "<STR_LIT>" ) . write ( "<STR_LIT>" ) <EOL> class OpenCurrentGistInBrowser ( BaseGistView , sublime_plugin . TextCommand ) : <EOL> def run ( self , edit ) : <EOL> util . open_with_browser ( self . html_url ) <EOL> class ReleaseNote ( sublime_plugin . WindowCommand ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( ReleaseNote , self ) . __init__ ( * args , ** kwargs ) <EOL> def run ( self ) : <EOL> util . open_with_browser ( "<STR_LIT>" ) <EOL> class AboutHaoGist ( sublime_plugin . WindowCommand ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( AboutHaoGist , self ) . __init__ ( * args , ** kwargs ) <EOL> def run ( self ) : <EOL> util . open_with_browser ( "<STR_LIT>" ) </s>
