<s> import os <EOL> import sys <EOL> import time <EOL> import sublime <EOL> if sys . version_info < ( <NUM_LIT:3> , ) : <EOL> from sublime_lib import WindowAndTextCommand <EOL> from sublime_lib . path import file_path_tuple <EOL> from sublime_lib . view import OutputPanel , get_text <EOL> from fileconv import dumpers , loaders <EOL> else : <EOL> from . sublime_lib import WindowAndTextCommand <EOL> from . sublime_lib . path import file_path_tuple <EOL> from . sublime_lib . view import OutputPanel , get_text <EOL> from . fileconv import dumpers , loaders <EOL> class ConvertFileCommand ( WindowAndTextCommand ) : <EOL> """<STR_LIT>""" <EOL> target_list = [ dict ( name = fmt . name , <EOL> kwargs = { "<STR_LIT>" : fmt . ext } ) <EOL> for fmt in dumpers . get . values ( ) ] <EOL> for i , itm in enumerate ( target_list ) : <EOL> if itm [ '<STR_LIT:name>' ] == "<STR_LIT>" : <EOL> target_list . insert ( <EOL> i + <NUM_LIT:1> , <EOL> dict ( name = "<STR_LIT>" , <EOL> kwargs = { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : False } ) <EOL> ) <EOL> def run ( self , edit = None , source_format = None , target_format = None , ext = None , <EOL> open_new_file = False , rearrange_yaml_syntax_def = False , _output = None , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if self . view . is_dirty ( ) : <EOL> self . view . run_command ( "<STR_LIT>" ) <EOL> if self . view . is_dirty ( ) : <EOL> return sublime . error_message ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> file_path = self . view . file_name ( ) <EOL> if not file_path : <EOL> return self . status ( "<STR_LIT>" , file_path ) <EOL> if source_format and target_format == source_format : <EOL> return self . status ( "<STR_LIT>" % target_format ) <EOL> if source_format and source_format not in loaders . get : <EOL> return self . status ( "<STR_LIT>" % source_format ) <EOL> if target_format and target_format not in dumpers . get : <EOL> return self . status ( "<STR_LIT>" % target_format ) <EOL> with OutputPanel ( self . window , "<STR_LIT>" ) as output : <EOL> output . show ( ) <EOL> if not source_format : <EOL> output . write ( "<STR_LIT>" ) <EOL> for Loader in loaders . get . values ( ) : <EOL> if Loader . file_is_valid ( self . view ) : <EOL> source_format = Loader . ext <EOL> output . write_line ( '<STR_LIT>' % Loader . name ) <EOL> break <EOL> if not source_format : <EOL> return output . write_line ( "<STR_LIT>" ) <EOL> elif target_format == source_format : <EOL> return output . write_line ( "<STR_LIT>" % Loader . name ) <EOL> Loader = loaders . get [ source_format ] <EOL> opts = Loader . load_options ( self . view ) <EOL> def get_new_ext ( target_format ) : <EOL> if ext : <EOL> return '<STR_LIT:.>' + ext <EOL> if opts and '<STR_LIT>' in opts : <EOL> return '<STR_LIT:.>' + opts [ '<STR_LIT>' ] <EOL> else : <EOL> new_ext , prepend_target_format = Loader . get_new_file_ext ( self . view ) <EOL> if prepend_target_format : <EOL> new_ext = "<STR_LIT>" % ( target_format . upper ( ) , new_ext [ <NUM_LIT:1> : ] ) <EOL> return ( new_ext or '<STR_LIT:.>' + target_format ) <EOL> path_tuple = file_path_tuple ( file_path ) <EOL> if not target_format : <EOL> output . write ( "<STR_LIT>" ) <EOL> if not opts or '<STR_LIT>' not in opts : <EOL> output . write ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> options , items = [ ] , [ ] <EOL> for itm in self . target_list : <EOL> target_format_ = itm [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> if target_format_ != source_format : <EOL> options . append ( [ "<STR_LIT>" % itm [ '<STR_LIT:name>' ] , <EOL> path_tuple . base_name + get_new_ext ( target_format_ ) ] ) <EOL> items . append ( itm ) <EOL> def on_select ( index ) : <EOL> if index < <NUM_LIT:0> or index >= len ( items ) : <EOL> output . write_line ( "<STR_LIT>" ) <EOL> return <EOL> target = items [ index ] <EOL> output . write_line ( '<STR_LIT>' % target [ '<STR_LIT:name>' ] ) <EOL> kwargs . update ( target [ '<STR_LIT>' ] ) <EOL> kwargs . update ( dict ( source_format = source_format , _output = output ) ) <EOL> self . run ( * args , ** kwargs ) <EOL> self . window . show_quick_panel ( options , on_select ) <EOL> return <EOL> target_format = opts [ '<STR_LIT>' ] <EOL> if source_format is not None and target_format == source_format : <EOL> return output . write_line ( "<STR_LIT>" <EOL> % target_format ) <EOL> if target_format not in dumpers . get : <EOL> return output . write_line ( "<STR_LIT>" <EOL> % target_format ) <EOL> output . write_line ( '<STR_LIT>' % dumpers . get [ target_format ] . name ) <EOL> start_time = time . time ( ) <EOL> loader_ = Loader ( self . window , self . view , output = output ) <EOL> try : <EOL> data = loader_ . load ( * args , ** kwargs ) <EOL> except : <EOL> output . write_line ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> raise <EOL> if not data : <EOL> return <EOL> new_file_path = path_tuple . no_ext + get_new_ext ( target_format ) <EOL> new_dir = os . path . dirname ( new_file_path ) <EOL> if not os . path . exists ( new_dir ) : <EOL> try : <EOL> os . makedirs ( new_dir ) <EOL> except OSError : <EOL> output . write_line ( "<STR_LIT>" % new_dir ) <EOL> return <EOL> dumper = dumpers . get [ target_format ] ( self . window , self . view , new_file_path , <EOL> output = output ) <EOL> try : <EOL> dumper . dump ( data , * args , ** kwargs ) <EOL> except : <EOL> output . write_line ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> raise <EOL> self . status ( "<STR_LIT>" <EOL> % ( source_format , target_format ) ) <EOL> output . write_line ( "<STR_LIT>" % ( time . time ( ) - start_time ) ) <EOL> output_text = get_text ( output . view ) <EOL> if open_new_file or rearrange_yaml_syntax_def : <EOL> new_view = self . window . open_file ( new_file_path ) <EOL> if rearrange_yaml_syntax_def : <EOL> new_view . run_command ( "<STR_LIT>" , <EOL> { "<STR_LIT>" : True , "<STR_LIT>" : output_text } ) <EOL> def status ( self , msg , file_path = None ) : <EOL> sublime . status_message ( msg ) <EOL> print ( "<STR_LIT>" + msg + ( "<STR_LIT>" % file_path if file_path is not None else "<STR_LIT>" ) ) </s>
