<s> """<STR_LIT>""" <EOL> import json <EOL> import logging <EOL> import os <EOL> import os . path <EOL> import re <EOL> import sys <EOL> sys . path . append ( os . path . dirname ( __file__ ) ) <EOL> try : <EOL> import Stumblr . tumblor as tumblor <EOL> except ImportError : <EOL> import tumblor <EOL> from lib . thread_progress import ThreadProgress <EOL> import sublime <EOL> import sublime_plugin <EOL> import tempfile <EOL> import threading <EOL> try : <EOL> from urlparse import parse_qs , urlparse <EOL> except ImportError : <EOL> from urllib . parse import parse_qs , urlparse <EOL> import webbrowser <EOL> logging . basicConfig ( level = logging . DEBUG , <EOL> format = '<STR_LIT>' ) <EOL> PREFSFILE = '<STR_LIT>' <EOL> class TumblrUtility ( object ) : <EOL> def __init__ ( self ) : <EOL> self . prefsfile = '<STR_LIT>' <EOL> self . prefs = sublime . load_settings ( self . prefsfile ) <EOL> self . t = self . get_tumblr ( ) <EOL> @ property <EOL> def application_tokens ( self ) : <EOL> return ( self . prefs . get ( '<STR_LIT>' , '<STR_LIT>' ) and <EOL> self . prefs . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> @ property <EOL> def oauth_tokens ( self ) : <EOL> """<STR_LIT>""" <EOL> prefs = self . prefs <EOL> if ( not prefs . get ( '<STR_LIT>' ) or <EOL> not prefs . get ( '<STR_LIT>' ) ) : <EOL> return None <EOL> return { <EOL> '<STR_LIT>' : prefs . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : prefs . get ( '<STR_LIT>' ) <EOL> } <EOL> def get_tumblr ( self ) : <EOL> """<STR_LIT>""" <EOL> creds = self . oauth_tokens <EOL> prefs = self . prefs <EOL> if not creds : <EOL> logging . debug ( '<STR_LIT>' ) <EOL> return None <EOL> logging . debug ( '<STR_LIT>' ) <EOL> return tumblor . Tumblor ( prefs . get ( '<STR_LIT>' ) , <EOL> prefs . get ( '<STR_LIT>' ) , credentials = creds , <EOL> base_hostname = prefs . get ( '<STR_LIT>' ) ) <EOL> def get_new_tokens ( self ) : <EOL> prefs = self . prefs <EOL> sublime . status_message ( '<STR_LIT>' ) <EOL> t = tumblor . Tumblor ( prefs . get ( '<STR_LIT>' ) , prefs . get ( '<STR_LIT>' ) , <EOL> credentials = None , base_hostname = prefs . get ( '<STR_LIT>' ) ) <EOL> tokens = t . serialize_credentials ( ) <EOL> prefs . set ( '<STR_LIT>' , tokens [ '<STR_LIT>' ] ) <EOL> prefs . set ( '<STR_LIT>' , tokens [ '<STR_LIT>' ] ) <EOL> sublime . save_settings ( PREFSFILE ) <EOL> return tokens <EOL> class CheckCredentialsCommand ( sublime_plugin . WindowCommand ) : <EOL> """<STR_LIT>""" <EOL> def run ( self ) : <EOL> tum = TumblrUtility ( ) <EOL> if not tum . application_tokens : <EOL> sublime . status_message ( '<STR_LIT>' % PREFSFILE ) <EOL> return <EOL> if tum . oauth_tokens : <EOL> sublime . status_message ( '<STR_LIT>' ) <EOL> return <EOL> sublime . status_message ( '<STR_LIT>' ) <EOL> return <EOL> class TumblrListDraftsCommand ( sublime_plugin . WindowCommand ) : <EOL> """<STR_LIT>""" <EOL> def run ( self ) : <EOL> draft_thread = ListDraftsThreaded ( self . window ) <EOL> draft_thread . start ( ) <EOL> ThreadProgress ( draft_thread , '<STR_LIT>' , '<STR_LIT>' ) <EOL> class ListDraftsThreaded ( threading . Thread , TumblrUtility ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , window ) : <EOL> self . window = window <EOL> self . drafts = None <EOL> self . strip_tags_re = re . compile ( r'<STR_LIT>' ) <EOL> threading . Thread . __init__ ( self ) <EOL> TumblrUtility . __init__ ( self ) <EOL> def run ( self ) : <EOL> posts = self . get_draft_list ( ) <EOL> logging . info ( "<STR_LIT>" % len ( posts ) ) <EOL> def show_panel ( ) : <EOL> self . window . show_quick_panel ( self . draft_list , self . on_done ) <EOL> if len ( posts ) : <EOL> sublime . set_timeout ( show_panel , <NUM_LIT:10> ) <EOL> sublime . status_message ( '<STR_LIT>' ) <EOL> def get_draft_list ( self ) : <EOL> drafts = self . t . call_api ( '<STR_LIT>' , params = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> if drafts [ '<STR_LIT>' ] [ '<STR_LIT:status>' ] == <NUM_LIT:200> : <EOL> self . drafts = drafts [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> if len ( self . drafts ) : <EOL> self . draft_list = [ self . snippet ( p ) for p in self . drafts ] <EOL> return self . drafts <EOL> logging . info ( '<STR_LIT>' ) <EOL> return [ ] <EOL> logging . error ( '<STR_LIT>' ) <EOL> logging . error ( drafts ) <EOL> return [ ] <EOL> def snippet ( self , post ) : <EOL> """<STR_LIT>""" <EOL> if post [ '<STR_LIT:type>' ] == '<STR_LIT:text>' : <EOL> return [ post [ '<STR_LIT:title>' ] [ : <NUM_LIT> ] or post [ '<STR_LIT>' ] [ : <NUM_LIT> ] , <EOL> post [ '<STR_LIT:date>' ] , <EOL> self . strip_tags ( post [ '<STR_LIT:body>' ] ) [ : <NUM_LIT> ] <EOL> ] <EOL> if post [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> source = self . strip_tags ( post . get ( '<STR_LIT:source>' , '<STR_LIT>' ) ) <EOL> if ( post . get ( '<STR_LIT>' ) ) : <EOL> source = urlparse ( post . get ( '<STR_LIT>' , '<STR_LIT>' ) ) . path <EOL> source_title = post . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> return [ source [ - <NUM_LIT> : ] , <EOL> "<STR_LIT>" % ( post [ '<STR_LIT:date>' ] , source_title ) , <EOL> self . strip_tags ( post [ '<STR_LIT:text>' ] ) [ : <NUM_LIT> ] <EOL> ] <EOL> if post [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> source = urlparse ( post [ '<STR_LIT:url>' ] ) . netloc <EOL> return [ post [ '<STR_LIT:title>' ] [ : <NUM_LIT> ] , <EOL> "<STR_LIT>" % ( post [ '<STR_LIT:date>' ] , source ) , <EOL> self . strip_tags ( post [ '<STR_LIT:description>' ] ) [ : <NUM_LIT> ] <EOL> ] <EOL> if post [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> return [ post [ '<STR_LIT>' ] [ : <NUM_LIT> ] , <EOL> post [ '<STR_LIT:date>' ] , <EOL> post [ '<STR_LIT>' ] [ : <NUM_LIT> ] <EOL> ] <EOL> else : <EOL> return [ post [ '<STR_LIT>' ] , post [ '<STR_LIT:date>' ] , "<STR_LIT>" ] <EOL> def strip_tags ( self , markup ) : <EOL> if markup : <EOL> return self . strip_tags_re . sub ( '<STR_LIT>' , markup ) <EOL> return "<STR_LIT>" <EOL> def on_done ( self , index ) : <EOL> if index == - <NUM_LIT:1> : <EOL> logging . info ( "<STR_LIT>" ) <EOL> else : <EOL> logging . info ( "<STR_LIT>" % self . drafts [ index ] ) <EOL> self . new_buffer_from_post ( self . drafts [ index ] ) <EOL> return <EOL> def new_buffer_from_post ( self , post ) : <EOL> mapping = { <EOL> '<STR_LIT:text>' : [ '<STR_LIT:title>' , '<STR_LIT:body>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT:source>' , '<STR_LIT:text>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT:title>' , '<STR_LIT:description>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT:title>' , '<STR_LIT>' ] <EOL> } <EOL> fields = mapping [ post [ '<STR_LIT:type>' ] ] <EOL> for field in fields : <EOL> self . new_view_with_text ( post [ field ] , { <EOL> '<STR_LIT:id>' : str ( post [ '<STR_LIT:id>' ] ) , <EOL> '<STR_LIT>' : field <EOL> } ) <EOL> def new_view_with_text ( self , text , settings = { } ) : <EOL> """<STR_LIT>""" <EOL> temp , tempname = tempfile . mkstemp ( prefix = '<STR_LIT>' % settings [ '<STR_LIT>' ] ) <EOL> logging . debug ( '<STR_LIT>' % ( temp , tempname ) ) <EOL> temp = os . fdopen ( temp , '<STR_LIT:wb>' ) <EOL> temp . write ( text . encode ( '<STR_LIT:utf-8>' ) ) <EOL> temp . close ( ) <EOL> new_view = self . window . open_file ( tempname ) <EOL> new_view . settings ( ) . set ( '<STR_LIT>' , True ) <EOL> for k in settings : <EOL> logging . debug ( '<STR_LIT>' % ( k , settings [ k ] ) ) <EOL> new_view . settings ( ) . set ( '<STR_LIT>' % k , settings [ k ] ) <EOL> self . set_syntax ( new_view ) <EOL> pass <EOL> def set_syntax ( self , view ) : <EOL> """<STR_LIT>""" <EOL> possible_syntaxes = sublime . find_resources ( "<STR_LIT>" ) <EOL> if possible_syntaxes : <EOL> md_syntax = possible_syntaxes [ <NUM_LIT:0> ] <EOL> logging . debug ( '<STR_LIT>' , md_syntax ) <EOL> view . set_syntax_file ( md_syntax ) <EOL> else : <EOL> logging . debug ( '<STR_LIT>' ) <EOL> return None <EOL> class StumblrEvents ( sublime_plugin . EventListener ) : <EOL> """<STR_LIT>""" <EOL> def on_close ( self , view ) : <EOL> view_prefs = view . settings ( ) <EOL> if view_prefs . get ( '<STR_LIT>' ) : <EOL> original_name = view . file_name ( ) <EOL> if original_name and os . path . exists ( original_name ) : <EOL> logging . info ( "<STR_LIT>" % original_name ) <EOL> os . remove ( original_name ) <EOL> pass <EOL> return <EOL> def on_pre_save ( self , view ) : <EOL> """<STR_LIT>""" <EOL> master_prefs = sublime . load_settings ( PREFSFILE ) <EOL> if not master_prefs . get ( '<STR_LIT>' ) : <EOL> return <EOL> view_prefs = view . settings ( ) <EOL> if view_prefs . get ( '<STR_LIT>' ) and view . is_dirty ( ) : <EOL> logging . debug ( '<STR_LIT>' ) <EOL> postup = UpdateDraftThreaded ( view ) <EOL> postup . start ( ) <EOL> ThreadProgress ( postup , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> return <EOL> return <EOL> class TumblrUpdateDraftCommand ( sublime_plugin . TextCommand ) : <EOL> """<STR_LIT>""" <EOL> def run ( self , edit ) : <EOL> view_prefs = self . view . settings ( ) <EOL> if view_prefs . get ( '<STR_LIT>' ) : <EOL> postup = UpdateDraftThreaded ( self . view ) <EOL> postup . start ( ) <EOL> ThreadProgress ( postup , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> return <EOL> def is_enabled ( self ) : <EOL> if self . view . settings ( ) . get ( '<STR_LIT>' ) : <EOL> return True <EOL> return False <EOL> class UpdateDraftThreaded ( threading . Thread , TumblrUtility ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , view ) : <EOL> self . view = view <EOL> view_prefs = view . settings ( ) <EOL> self . post_id = int ( view_prefs . get ( '<STR_LIT>' ) ) <EOL> self . post_field = view_prefs . get ( '<STR_LIT>' ) <EOL> threading . Thread . __init__ ( self ) <EOL> TumblrUtility . __init__ ( self ) <EOL> def run ( self ) : <EOL> sublime . set_timeout ( self . post_view , <NUM_LIT:10> ) <EOL> def post_view ( self ) : <EOL> body = self . view . substr ( sublime . Region ( <NUM_LIT:0> , self . view . size ( ) ) ) <EOL> logging . info ( "<STR_LIT>" % ( self . post_id , self . post_field ) ) <EOL> params = { <EOL> '<STR_LIT:id>' : self . post_id , <EOL> self . post_field : body <EOL> } <EOL> logging . info ( params ) <EOL> result = self . t . call_api ( '<STR_LIT>' , verb = '<STR_LIT>' , params = params ) <EOL> logging . info ( result ) <EOL> if result [ '<STR_LIT>' ] [ '<STR_LIT:status>' ] == <NUM_LIT:200> : <EOL> sublime . status_message ( '<STR_LIT>' ) <EOL> else : <EOL> logging . error ( "<STR_LIT>" % result ) <EOL> sublime . status_message ( '<STR_LIT>' ) <EOL> return <EOL> class TumblrLoginCommand ( sublime_plugin . WindowCommand ) : <EOL> """<STR_LIT>""" <EOL> def run ( self ) : <EOL> TumblrThreadedLogin ( self . window ) . start ( ) <EOL> class TumblrThreadedLogin ( threading . Thread , TumblrUtility ) : <EOL> def __init__ ( self , window ) : <EOL> self . window = window <EOL> threading . Thread . __init__ ( self ) <EOL> TumblrUtility . __init__ ( self ) <EOL> def run ( self ) : <EOL> self . get_new_tokens ( ) <EOL> def show_status ( ) : <EOL> if self . oauth_tokens : <EOL> sublime . status_message ( '<STR_LIT>' ) <EOL> else : <EOL> sublime . status_message ( '<STR_LIT>' ) <EOL> return <EOL> sublime . set_timeout ( show_status , <NUM_LIT:10> ) <EOL> class TumblrPublishDraftCommand ( sublime_plugin . TextCommand ) : <EOL> def run ( self , edit ) : <EOL> TumblrThreadedPublishDraft ( self . view ) . start ( ) <EOL> def is_enabled ( self ) : <EOL> if self . view . settings ( ) . get ( '<STR_LIT>' ) : <EOL> return True <EOL> return False <EOL> class TumblrThreadedPublishDraft ( threading . Thread , TumblrUtility ) : <EOL> def __init__ ( self , view ) : <EOL> self . view = view <EOL> self . view_settings = view . settings ( ) <EOL> self . post_id = int ( self . view_settings . get ( '<STR_LIT>' ) ) <EOL> threading . Thread . __init__ ( self ) <EOL> TumblrUtility . __init__ ( self ) <EOL> def run ( self ) : <EOL> def publish_post ( ) : <EOL> info = self . t . call_api ( method = '<STR_LIT>' , params = { <EOL> '<STR_LIT:id>' : self . post_id , <EOL> '<STR_LIT:state>' : '<STR_LIT>' , <EOL> } , verb = '<STR_LIT>' ) <EOL> logging . debug ( info ) <EOL> if info [ '<STR_LIT>' ] [ '<STR_LIT:status>' ] == <NUM_LIT:200> : <EOL> published_id = info [ '<STR_LIT>' ] [ '<STR_LIT:id>' ] <EOL> more_info = self . t . call_api ( method = '<STR_LIT>' , params = { <EOL> '<STR_LIT>' : self . prefs . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT:id>' : published_id <EOL> } ) <EOL> logging . debug ( more_info ) <EOL> if more_info [ '<STR_LIT>' ] [ '<STR_LIT:status>' ] == <NUM_LIT:200> : <EOL> post = more_info [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> webbrowser . open ( post [ '<STR_LIT>' ] ) <EOL> sublime . set_timeout ( publish_post , <NUM_LIT:10> ) <EOL> class TumblrPostDraftCommand ( sublime_plugin . TextCommand ) : <EOL> """<STR_LIT>""" <EOL> def run ( self , edit ) : <EOL> TumblrThreadedPostDraft ( self . view ) . start ( ) <EOL> class TumblrThreadedPostDraft ( threading . Thread , TumblrUtility ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , view ) : <EOL> self . view = view <EOL> self . response = None <EOL> threading . Thread . __init__ ( self ) <EOL> TumblrUtility . __init__ ( self ) <EOL> def run ( self ) : <EOL> def show_status ( ) : <EOL> self . post_draft ( ) <EOL> logging . info ( self . response ) <EOL> if self . response [ '<STR_LIT>' ] [ '<STR_LIT:status>' ] == <NUM_LIT> : <EOL> sublime . status_message ( '<STR_LIT>' ) <EOL> self . view . set_name ( str ( self . response [ '<STR_LIT>' ] [ '<STR_LIT:id>' ] ) ) <EOL> prefs = self . view . settings ( ) <EOL> prefs . set ( '<STR_LIT>' , True ) <EOL> prefs . set ( '<STR_LIT>' , str ( self . response [ '<STR_LIT>' ] [ '<STR_LIT:id>' ] ) ) <EOL> prefs . set ( '<STR_LIT>' , '<STR_LIT:body>' ) <EOL> else : <EOL> sublime . status_message ( '<STR_LIT>' ) <EOL> prefs . erase ( '<STR_LIT>' ) <EOL> return <EOL> sublime . set_timeout ( show_status , <NUM_LIT:10> ) <EOL> def post_draft ( self ) : <EOL> if self . t : <EOL> body = self . view . substr ( sublime . Region ( <NUM_LIT:0> , self . view . size ( ) ) ) <EOL> payload = { <EOL> '<STR_LIT:state>' : '<STR_LIT>' , '<STR_LIT:type>' : '<STR_LIT:text>' , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:body>' : body <EOL> } <EOL> self . response = self . t . call_api ( '<STR_LIT>' , params = payload , verb = '<STR_LIT>' ) <EOL> class TumblrDeleteDraftCommand ( sublime_plugin . TextCommand ) : <EOL> """<STR_LIT>""" <EOL> def run ( self , edit ) : <EOL> TumblrThreadedDeleteDraftCommand ( self . view ) . start ( ) <EOL> def is_enabled ( self ) : <EOL> if self . view . settings ( ) . get ( '<STR_LIT>' ) : <EOL> return True <EOL> return False <EOL> class TumblrThreadedDeleteDraftCommand ( threading . Thread , TumblrUtility ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , view ) : <EOL> self . view = view <EOL> self . response = None <EOL> self . view_settings = view . settings ( ) <EOL> self . post_id = int ( self . view_settings . get ( '<STR_LIT>' ) ) <EOL> threading . Thread . __init__ ( self ) <EOL> TumblrUtility . __init__ ( self ) <EOL> def run ( self ) : <EOL> def delete_draft ( ) : <EOL> post = self . t . call_api ( method = '<STR_LIT>' , params = { <EOL> '<STR_LIT>' : self . prefs . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT:id>' : self . post_id <EOL> } ) <EOL> logging . debug ( post ) <EOL> if post [ '<STR_LIT>' ] [ '<STR_LIT:status>' ] == <NUM_LIT:200> : <EOL> deleted_post = self . t . call_api ( method = '<STR_LIT>' , params = { <EOL> '<STR_LIT:id>' : self . post_id <EOL> } , verb = '<STR_LIT>' ) <EOL> logging . debug ( deleted_post ) <EOL> if deleted_post [ '<STR_LIT>' ] [ '<STR_LIT:status>' ] == <NUM_LIT:200> : <EOL> sublime . status_message ( '<STR_LIT>' % self . post_id ) <EOL> return <EOL> sublime . status_message ( '<STR_LIT>' % self . post_id ) <EOL> return <EOL> sublime . set_timeout ( delete_draft , <NUM_LIT:10> ) </s>
