<s> import os <EOL> import sys <EOL> import os . path <EOL> sys . path . insert ( <NUM_LIT:0> , os . path . dirname ( os . path . abspath ( __file__ ) ) ) <EOL> import re <EOL> import sublime <EOL> import sublime_plugin <EOL> import webbrowser <EOL> import plistlib <EOL> from github import GitHubApi <EOL> import logging as logger <EOL> try : <EOL> import xml . parsers . expat as expat <EOL> except ImportError : <EOL> expat = None <EOL> VERSION = "<STR_LIT>" <EOL> try : <EOL> sys . path . append ( os . path . join ( sublime . packages_path ( ) , '<STR_LIT>' ) ) <EOL> import git <EOL> sys . path . remove ( os . path . join ( sublime . packages_path ( ) , '<STR_LIT>' ) ) <EOL> except ImportError : <EOL> git = None <EOL> logger . basicConfig ( format = '<STR_LIT>' ) <EOL> class BaseGitHubCommand ( sublime_plugin . TextCommand ) : <EOL> """<STR_LIT>""" <EOL> MSG_USERNAME = "<STR_LIT>" <EOL> MSG_PASSWORD = "<STR_LIT>" <EOL> MSG_ONE_TIME_PASSWORD = "<STR_LIT>" <EOL> MSG_TOKEN_SUCCESS = "<STR_LIT>" <EOL> ERR_NO_USER_TOKEN = "<STR_LIT>" "<STR_LIT>" "<STR_LIT>" <EOL> ERR_UNAUTHORIZED = "<STR_LIT>" "<STR_LIT>" <EOL> ERR_UNAUTHORIZED_TOKEN = "<STR_LIT>" "<STR_LIT>" <EOL> def run ( self , edit ) : <EOL> self . settings = sublime . load_settings ( "<STR_LIT>" ) <EOL> self . github_user = None <EOL> self . github_password = None <EOL> self . github_one_time_password = None <EOL> self . accounts = self . settings . get ( "<STR_LIT>" ) <EOL> self . active_account = self . settings . get ( "<STR_LIT>" ) <EOL> if not self . active_account : <EOL> self . active_account = list ( self . accounts . keys ( ) ) [ <NUM_LIT:0> ] <EOL> self . github_token = self . accounts [ self . active_account ] [ "<STR_LIT>" ] <EOL> if not self . github_token : <EOL> self . github_token = self . settings . get ( "<STR_LIT>" ) <EOL> if self . github_token : <EOL> self . settings . set ( "<STR_LIT>" , { "<STR_LIT>" : { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : self . github_token } } ) <EOL> self . settings . set ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . active_account = self . settings . get ( "<STR_LIT>" ) <EOL> self . settings . erase ( "<STR_LIT>" ) <EOL> sublime . save_settings ( "<STR_LIT>" ) <EOL> self . base_uri = self . accounts [ self . active_account ] [ "<STR_LIT>" ] <EOL> self . debug = self . settings . get ( '<STR_LIT>' ) <EOL> self . proxies = { '<STR_LIT>' : self . accounts [ self . active_account ] . get ( "<STR_LIT>" , None ) } <EOL> self . force_curl = self . accounts [ self . active_account ] . get ( "<STR_LIT>" , False ) <EOL> self . gistapi = GitHubApi ( self . base_uri , self . github_token , debug = self . debug , <EOL> proxies = self . proxies , force_curl = self . force_curl ) <EOL> def get_token ( self ) : <EOL> sublime . error_message ( self . ERR_NO_USER_TOKEN ) <EOL> self . get_username ( ) <EOL> def get_username ( self ) : <EOL> self . view . window ( ) . show_input_panel ( self . MSG_USERNAME , self . github_user or "<STR_LIT>" , self . on_done_username , None , None ) <EOL> def get_password ( self ) : <EOL> self . view . window ( ) . show_input_panel ( self . MSG_PASSWORD , "<STR_LIT>" , self . on_done_password , None , None ) <EOL> def get_one_time_password ( self ) : <EOL> self . view . window ( ) . show_input_panel ( self . MSG_ONE_TIME_PASSWORD , "<STR_LIT>" , self . on_done_one_time_password , None , None ) <EOL> def on_done_username ( self , value ) : <EOL> "<STR_LIT>" <EOL> self . github_user = value <EOL> sublime . set_timeout ( self . get_password , <NUM_LIT:50> ) <EOL> def on_done_one_time_password ( self , value ) : <EOL> "<STR_LIT>" <EOL> self . github_one_time_password = value <EOL> self . on_done_password ( self . github_password ) <EOL> def on_done_password ( self , value ) : <EOL> "<STR_LIT>" <EOL> self . github_password = value <EOL> try : <EOL> api = GitHubApi ( self . base_uri , debug = self . debug ) <EOL> self . github_token = api . get_token ( self . github_user , <EOL> self . github_password , <EOL> self . github_one_time_password ) <EOL> self . github_password = self . github_one_time_password = None <EOL> self . accounts [ self . active_account ] [ "<STR_LIT>" ] = self . github_token <EOL> self . settings . set ( "<STR_LIT>" , self . accounts ) <EOL> sublime . save_settings ( "<STR_LIT>" ) <EOL> self . gistapi = GitHubApi ( self . base_uri , self . github_token , debug = self . debug ) <EOL> try : <EOL> if self . callback : <EOL> sublime . error_message ( self . MSG_TOKEN_SUCCESS ) <EOL> callback = self . callback <EOL> self . callback = None <EOL> sublime . set_timeout ( callback , <NUM_LIT:50> ) <EOL> except AttributeError : <EOL> pass <EOL> except GitHubApi . OTPNeededException : <EOL> sublime . set_timeout ( self . get_one_time_password , <NUM_LIT:50> ) <EOL> except GitHubApi . UnauthorizedException : <EOL> sublime . error_message ( self . ERR_UNAUTHORIZED ) <EOL> sublime . set_timeout ( self . get_username , <NUM_LIT:50> ) <EOL> except GitHubApi . UnknownException as e : <EOL> sublime . error_message ( e . message ) <EOL> class InsertTextCommand ( sublime_plugin . TextCommand ) : <EOL> """<STR_LIT>""" <EOL> def run ( self , edit , ** args ) : <EOL> self . view . insert ( edit , <NUM_LIT:0> , args [ '<STR_LIT:text>' ] ) <EOL> class OpenGistCommand ( BaseGitHubCommand ) : <EOL> """<STR_LIT>""" <EOL> MSG_SUCCESS = "<STR_LIT>" <EOL> starred = False <EOL> open_in_editor = False <EOL> syntax_file_map = None <EOL> copy_gist_id = False <EOL> def run ( self , edit ) : <EOL> super ( OpenGistCommand , self ) . run ( edit ) <EOL> if self . github_token : <EOL> self . get_gists ( ) <EOL> else : <EOL> self . callback = self . get_gists <EOL> self . get_token ( ) <EOL> def get_gists ( self ) : <EOL> try : <EOL> self . gists = self . gistapi . list_gists ( starred = self . starred ) <EOL> format = self . settings . get ( "<STR_LIT>" ) <EOL> packed_gists = [ ] <EOL> for idx , gist in enumerate ( self . gists ) : <EOL> attribs = { "<STR_LIT:index>" : idx + <NUM_LIT:1> , <EOL> "<STR_LIT:filename>" : list ( gist [ "<STR_LIT>" ] . keys ( ) ) [ <NUM_LIT:0> ] , <EOL> "<STR_LIT:description>" : gist [ "<STR_LIT:description>" ] or '<STR_LIT>' } <EOL> if isinstance ( format , list ) : <EOL> item = [ ( format_str % attribs ) for format_str in format ] <EOL> else : <EOL> item = format % attribs <EOL> packed_gists . append ( item ) <EOL> args = [ packed_gists , self . on_done ] <EOL> if self . settings . get ( "<STR_LIT>" ) : <EOL> args . append ( sublime . MONOSPACE_FONT ) <EOL> self . view . window ( ) . show_quick_panel ( * args ) <EOL> except GitHubApi . UnauthorizedException : <EOL> sublime . error_message ( self . ERR_UNAUTHORIZED_TOKEN ) <EOL> sublime . set_timeout ( self . get_username , <NUM_LIT:50> ) <EOL> except GitHubApi . UnknownException as e : <EOL> sublime . error_message ( e . message ) <EOL> def on_done ( self , idx ) : <EOL> if idx == - <NUM_LIT:1> : <EOL> return <EOL> gist = self . gists [ idx ] <EOL> filename = list ( gist [ "<STR_LIT>" ] . keys ( ) ) [ <NUM_LIT:0> ] <EOL> filedata = gist [ "<STR_LIT>" ] [ filename ] <EOL> content = self . gistapi . get_gist ( gist ) <EOL> if self . open_in_editor : <EOL> new_view = self . view . window ( ) . new_file ( ) <EOL> if expat : <EOL> if not self . syntax_file_map : <EOL> self . syntax_file_map = self . _generate_syntax_file_map ( ) <EOL> try : <EOL> extension = os . path . splitext ( filename ) [ <NUM_LIT:1> ] [ <NUM_LIT:1> : ] . lower ( ) <EOL> syntax_file = self . syntax_file_map [ extension ] <EOL> new_view . set_syntax_file ( syntax_file ) <EOL> except KeyError : <EOL> logger . warn ( "<STR_LIT>" % extension ) <EOL> pass <EOL> new_view . run_command ( "<STR_LIT>" , { '<STR_LIT:text>' : content } ) <EOL> new_view . set_name ( filename ) <EOL> new_view . settings ( ) . set ( '<STR_LIT>' , gist ) <EOL> elif self . copy_gist_id : <EOL> sublime . set_clipboard ( gist [ "<STR_LIT>" ] ) <EOL> else : <EOL> sublime . set_clipboard ( content ) <EOL> sublime . status_message ( self . MSG_SUCCESS % filename ) <EOL> @ staticmethod <EOL> def _generate_syntax_file_map ( ) : <EOL> """<STR_LIT>""" <EOL> syntax_file_map = { } <EOL> packages_path = sublime . packages_path ( ) <EOL> packages = [ f for f in os . listdir ( packages_path ) if os . path . isdir ( os . path . join ( packages_path , f ) ) ] <EOL> for package in packages : <EOL> package_dir = os . path . join ( packages_path , package ) <EOL> syntax_files = [ os . path . join ( package_dir , f ) for f in os . listdir ( package_dir ) if f . endswith ( "<STR_LIT>" ) ] <EOL> for syntax_file in syntax_files : <EOL> try : <EOL> plist = plistlib . readPlist ( syntax_file ) <EOL> if plist : <EOL> for file_type in plist [ '<STR_LIT>' ] : <EOL> syntax_file_map [ file_type . lower ( ) ] = syntax_file <EOL> except expat . ExpatError : <EOL> logger . warn ( "<STR_LIT>" % syntax_file ) <EOL> except KeyError : <EOL> pass <EOL> if hasattr ( sublime , "<STR_LIT>" ) : <EOL> syntax_files = sublime . find_resources ( "<STR_LIT>" ) <EOL> for syntax_file in syntax_files : <EOL> try : <EOL> plist = plistlib . readPlistFromBytes ( bytearray ( sublime . load_resource ( syntax_file ) , "<STR_LIT:utf-8>" ) ) <EOL> if plist : <EOL> for file_type in plist [ '<STR_LIT>' ] : <EOL> syntax_file_map [ file_type . lower ( ) ] = syntax_file <EOL> except expat . ExpatError : <EOL> logger . warn ( "<STR_LIT>" % syntax_file ) <EOL> except KeyError : <EOL> pass <EOL> return syntax_file_map <EOL> class OpenStarredGistCommand ( OpenGistCommand ) : <EOL> """<STR_LIT>""" <EOL> starred = True <EOL> class OpenGistInEditorCommand ( OpenGistCommand ) : <EOL> """<STR_LIT>""" <EOL> open_in_editor = True <EOL> class OpenGistUrlCommand ( OpenGistCommand ) : <EOL> """<STR_LIT>""" <EOL> copy_gist_id = True <EOL> class OpenStarredGistInEditorCommand ( OpenGistCommand ) : <EOL> """<STR_LIT>""" <EOL> starred = True <EOL> open_in_editor = True <EOL> class OpenGistInBrowserCommand ( OpenGistCommand ) : <EOL> """<STR_LIT>""" <EOL> def on_done ( self , idx ) : <EOL> if idx == - <NUM_LIT:1> : <EOL> return <EOL> gist = self . gists [ idx ] <EOL> webbrowser . open ( gist [ "<STR_LIT>" ] ) <EOL> class OpenStarredGistInBrowserCommand ( OpenGistInBrowserCommand ) : <EOL> """<STR_LIT>""" <EOL> starred = True <EOL> class GistFromSelectionCommand ( BaseGitHubCommand ) : <EOL> """<STR_LIT>""" <EOL> MSG_DESCRIPTION = "<STR_LIT>" <EOL> MSG_FILENAME = "<STR_LIT>" <EOL> MSG_SUCCESS = "<STR_LIT>" <EOL> def run ( self , edit ) : <EOL> self . description = None <EOL> self . filename = None <EOL> super ( GistFromSelectionCommand , self ) . run ( edit ) <EOL> if self . github_token : <EOL> self . get_description ( ) <EOL> else : <EOL> self . callback = self . get_description <EOL> self . get_token ( ) <EOL> def get_description ( self ) : <EOL> self . view . window ( ) . show_input_panel ( self . MSG_DESCRIPTION , "<STR_LIT>" , self . on_done_description , None , None ) <EOL> def get_filename ( self ) : <EOL> current_filename = self . view . file_name ( ) or "<STR_LIT>" <EOL> filename = os . path . basename ( current_filename ) <EOL> self . view . window ( ) . show_input_panel ( self . MSG_FILENAME , filename , self . on_done_filename , None , None ) <EOL> def on_done_description ( self , value ) : <EOL> "<STR_LIT>" <EOL> self . description = value <EOL> sublime . set_timeout ( self . get_filename , <NUM_LIT:50> ) <EOL> def on_done_filename ( self , value ) : <EOL> self . filename = value <EOL> if all ( [ region . empty ( ) for region in self . view . sel ( ) ] ) : <EOL> text = self . view . substr ( sublime . Region ( <NUM_LIT:0> , self . view . size ( ) ) ) <EOL> else : <EOL> text = "<STR_LIT:\n>" . join ( [ self . view . substr ( region ) for region in self . view . sel ( ) ] ) <EOL> try : <EOL> gist = self . gistapi . create_gist ( description = self . description , <EOL> filename = self . filename , <EOL> content = text , <EOL> public = self . public ) <EOL> self . view . settings ( ) . set ( '<STR_LIT>' , gist ) <EOL> sublime . set_clipboard ( gist [ "<STR_LIT>" ] ) <EOL> sublime . status_message ( self . MSG_SUCCESS ) <EOL> except GitHubApi . UnauthorizedException : <EOL> self . settings . set ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> sublime . save_settings ( "<STR_LIT>" ) <EOL> sublime . error_message ( self . ERR_UNAUTHORIZED_TOKEN ) <EOL> sublime . set_timeout ( self . get_username , <NUM_LIT:50> ) <EOL> except GitHubApi . UnknownException as e : <EOL> sublime . error_message ( e . message ) <EOL> except GitHubApi . ConnectionException as e : <EOL> sublime . error_message ( e . message ) <EOL> class PrivateGistFromSelectionCommand ( GistFromSelectionCommand ) : <EOL> """<STR_LIT>""" <EOL> public = False <EOL> class PublicGistFromSelectionCommand ( GistFromSelectionCommand ) : <EOL> """<STR_LIT>""" <EOL> public = True <EOL> class UpdateGistCommand ( BaseGitHubCommand ) : <EOL> MSG_SUCCESS = "<STR_LIT>" <EOL> def run ( self , edit ) : <EOL> super ( UpdateGistCommand , self ) . run ( edit ) <EOL> self . gist = self . view . settings ( ) . get ( '<STR_LIT>' ) <EOL> if not self . gist : <EOL> sublime . error_message ( "<STR_LIT>" ) <EOL> return <EOL> if self . github_token : <EOL> self . update ( ) <EOL> else : <EOL> self . callback = self . update <EOL> self . get_token ( ) <EOL> def update ( self ) : <EOL> text = self . view . substr ( sublime . Region ( <NUM_LIT:0> , self . view . size ( ) ) ) <EOL> try : <EOL> updated_gist = self . gistapi . update_gist ( self . gist , text ) <EOL> sublime . set_clipboard ( updated_gist [ "<STR_LIT>" ] ) <EOL> sublime . status_message ( self . MSG_SUCCESS ) <EOL> except GitHubApi . UnauthorizedException : <EOL> self . settings . set ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> sublime . save_settings ( "<STR_LIT>" ) <EOL> sublime . error_message ( self . ERR_UNAUTHORIZED_TOKEN ) <EOL> sublime . set_timeout ( self . get_username , <NUM_LIT:50> ) <EOL> except GitHubApi . UnknownException as e : <EOL> sublime . error_message ( e . message ) <EOL> class SwitchAccountsCommand ( BaseGitHubCommand ) : <EOL> def run ( self , edit ) : <EOL> super ( SwitchAccountsCommand , self ) . run ( edit ) <EOL> accounts = list ( self . accounts . keys ( ) ) <EOL> self . view . window ( ) . show_quick_panel ( accounts , self . account_selected ) <EOL> def account_selected ( self , index ) : <EOL> if index == - <NUM_LIT:1> : <EOL> return <EOL> else : <EOL> self . active_account = list ( self . accounts . keys ( ) ) [ index ] <EOL> self . settings . set ( "<STR_LIT>" , self . active_account ) <EOL> sublime . save_settings ( "<STR_LIT>" ) <EOL> self . base_uri = self . accounts [ self . active_account ] [ "<STR_LIT>" ] <EOL> self . github_token = self . accounts [ self . active_account ] [ "<STR_LIT>" ] <EOL> if git : <EOL> class RemoteUrlCommand ( git . GitTextCommand ) : <EOL> url_type = '<STR_LIT>' <EOL> allows_line_highlights = False <EOL> master = False <EOL> def run ( self , edit ) : <EOL> if self . master : <EOL> branch = "<STR_LIT>" <EOL> else : <EOL> branch = "<STR_LIT>" <EOL> command = "<STR_LIT>" % branch <EOL> self . run_command ( command . split ( ) , self . done_rev_parse ) <EOL> def done_rev_parse ( self , result ) : <EOL> if "<STR_LIT>" in result : <EOL> sublime . error_message ( result ) <EOL> return <EOL> remote , self . remote_branch = result . strip ( ) . split ( "<STR_LIT:/>" , <NUM_LIT:1> ) <EOL> self . settings = sublime . load_settings ( "<STR_LIT>" ) <EOL> self . active_account = self . settings . get ( "<STR_LIT>" ) <EOL> self . accounts = self . settings . get ( "<STR_LIT>" ) <EOL> if not self . active_account : <EOL> self . active_account = list ( self . accounts . keys ( ) ) [ <NUM_LIT:0> ] <EOL> self . protocol = self . accounts [ self . active_account ] . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> remote = self . accounts [ self . active_account ] . get ( "<STR_LIT>" , remote ) <EOL> command = "<STR_LIT>" + remote <EOL> self . run_command ( command . split ( ) , self . done_remote ) <EOL> def done_remote ( self , result ) : <EOL> remote_loc = result . split ( ) [ <NUM_LIT:0> ] <EOL> repo_url = re . sub ( '<STR_LIT>' , self . protocol + '<STR_LIT>' , remote_loc ) <EOL> repo_url = re . sub ( r'<STR_LIT>' , r'<STR_LIT>' , repo_url ) <EOL> repo_url = re . sub ( '<STR_LIT>' , '<STR_LIT>' , repo_url ) <EOL> self . repo_url = repo_url <EOL> self . run_command ( "<STR_LIT>" . split ( ) , self . done_toplevel ) <EOL> def done_toplevel ( self , result ) : <EOL> self . toplevel_path = result . strip ( ) <EOL> absolute_path = self . view . file_name ( ) <EOL> absolute_path = absolute_path . replace ( '<STR_LIT:\\>' , '<STR_LIT:/>' ) <EOL> relative_path = re . split ( re . escape ( self . toplevel_path ) , absolute_path , re . IGNORECASE ) . pop ( ) <EOL> line_nums = "<STR_LIT>" <EOL> if self . allows_line_highlights : <EOL> non_empty_regions = [ region for region in self . view . sel ( ) if not region . empty ( ) ] <EOL> if non_empty_regions : <EOL> selection = non_empty_regions [ <NUM_LIT:0> ] <EOL> ( start_row , _ ) = self . view . rowcol ( selection . begin ( ) ) <EOL> ( end_row , end_col ) = self . view . rowcol ( selection . end ( ) ) <EOL> if end_col == <NUM_LIT:0> : <EOL> end_row -= <NUM_LIT:1> <EOL> line_nums = "<STR_LIT>" % ( start_row + <NUM_LIT:1> ) <EOL> if end_row > start_row : <EOL> line_nums += "<STR_LIT>" % ( end_row + <NUM_LIT:1> ) <EOL> elif self . settings . get ( "<STR_LIT>" ) : <EOL> ( current_row , _ ) = self . view . rowcol ( self . view . sel ( ) [ <NUM_LIT:0> ] . begin ( ) ) <EOL> line_nums = "<STR_LIT>" % ( current_row + <NUM_LIT:1> ) <EOL> self . url = "<STR_LIT>" % ( self . repo_url , self . url_type , self . remote_branch , relative_path , line_nums ) <EOL> self . on_done ( ) <EOL> else : <EOL> class RemoteUrlCommand ( sublime_plugin . TextCommand ) : <EOL> def run ( self , edit ) : <EOL> sublime . error_message ( "<STR_LIT>" ) <EOL> class OpenRemoteUrlCommand ( RemoteUrlCommand ) : <EOL> allows_line_highlights = True <EOL> def run ( self , edit ) : <EOL> super ( OpenRemoteUrlCommand , self ) . run ( edit ) <EOL> def on_done ( self ) : <EOL> webbrowser . open ( self . url ) <EOL> class OpenRemoteUrlMasterCommand ( OpenRemoteUrlCommand ) : <EOL> master = True <EOL> class CopyRemoteUrlCommand ( RemoteUrlCommand ) : <EOL> allows_line_highlights = True <EOL> def run ( self , edit ) : <EOL> super ( CopyRemoteUrlCommand , self ) . run ( edit ) <EOL> def on_done ( self ) : <EOL> sublime . set_clipboard ( self . url ) <EOL> sublime . status_message ( "<STR_LIT>" ) <EOL> class CopyRemoteUrlMasterCommand ( CopyRemoteUrlCommand ) : <EOL> master = True <EOL> class BlameCommand ( OpenRemoteUrlCommand ) : <EOL> url_type = '<STR_LIT>' <EOL> class BlameMasterCommand ( BlameCommand ) : <EOL> master = True <EOL> class HistoryCommand ( OpenRemoteUrlCommand ) : <EOL> url_type = '<STR_LIT>' <EOL> allows_line_highlights = False <EOL> class HistoryMasterCommand ( HistoryCommand ) : <EOL> master = True <EOL> class EditCommand ( OpenRemoteUrlCommand ) : <EOL> url_type = '<STR_LIT>' <EOL> allows_line_highlights = False <EOL> class EditMasterCommand ( EditCommand ) : <EOL> master = True </s>
