<s> import os <EOL> import sublime <EOL> import sublime_plugin <EOL> from . my_sublime_lib import WindowAndTextCommand <EOL> from . my_sublime_lib . path import file_path_tuple <EOL> from . my_sublime_lib . view import OutputPanel , get_text , set_text <EOL> from . tinycsscheme . parser import CSSchemeParser , strvalue <EOL> from . tinycsscheme . dumper import CSSchemeDumper , DumpError <EOL> from . import converters <EOL> from . converters import tmtheme <EOL> PACKAGE = __package__ <EOL> DEBUG = False <EOL> def settings ( ) : <EOL> """<STR_LIT>""" <EOL> return sublime . load_settings ( "<STR_LIT>" ) <EOL> def status ( msg , printonly = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> sublime . status_message ( "<STR_LIT>" % ( PACKAGE , msg ) ) <EOL> if printonly : <EOL> msg = msg + '<STR_LIT:\n>' + printonly <EOL> print ( "<STR_LIT>" % ( PACKAGE , msg ) ) <EOL> class convert_csscheme ( WindowAndTextCommand ) : <EOL> """<STR_LIT>""" <EOL> def is_enabled ( self ) : <EOL> path = self . view . file_name ( ) <EOL> return bool ( path ) and any ( conv . valid_file ( path ) for conv in converters . all ) <EOL> def run ( self , edit = None ) : <EOL> if self . view . is_dirty ( ) : <EOL> return status ( "<STR_LIT>" ) <EOL> self . preview_opened = False <EOL> in_file = self . view . file_name ( ) <EOL> in_tuple = file_path_tuple ( in_file ) <EOL> ext = '<STR_LIT>' <EOL> with OutputPanel ( self . view . window ( ) , "<STR_LIT>" ) as out : <EOL> conv = tuple ( c for c in converters . all if c . valid_file ( in_file ) ) <EOL> if len ( conv ) > <NUM_LIT:1> : <EOL> out . write_line ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return <EOL> elif not conv : <EOL> out . write_line ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % '<STR_LIT:U+002CU+0020>' . join ( "<STR_LIT:.>" + c . ext for c in converters . all ) ) <EOL> return <EOL> conv = conv [ <NUM_LIT:0> ] <EOL> out . set_path ( in_tuple . path ) <EOL> executables = settings ( ) . get ( "<STR_LIT>" , { } ) <EOL> text = conv . convert ( out , in_file , executables ) <EOL> if not text : <EOL> return <EOL> self . previewed = not settings ( ) . get ( '<STR_LIT>' ) <EOL> def preview_compiled_css ( ) : <EOL> if not self . previewed : <EOL> self . preview_compiled_css ( text , conv , in_tuple . base_name ) <EOL> self . previewed = True <EOL> stylesheet = CSSchemeParser ( ) . parse_stylesheet ( text ) <EOL> if stylesheet . errors : <EOL> conv . report_parse_errors ( out , in_file , text , stylesheet . errors ) <EOL> preview_compiled_css ( ) <EOL> return <EOL> elif not stylesheet . rules : <EOL> out . write_line ( "<STR_LIT>" ) <EOL> return <EOL> for i , r in enumerate ( stylesheet . rules ) : <EOL> if not r . at_keyword or r . at_keyword . strip ( '<STR_LIT:@>' ) != '<STR_LIT>' : <EOL> continue <EOL> if strvalue ( r . value ) == '<STR_LIT:true>' : <EOL> ext = '<STR_LIT>' <EOL> del stylesheet . rules [ i ] <EOL> break <EOL> else : <EOL> e = DumpError ( r , "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> conv . report_dump_error ( out , in_file , text , e ) <EOL> preview_compiled_css ( ) <EOL> return <EOL> out_file = in_tuple . no_ext + ext <EOL> try : <EOL> CSSchemeDumper ( ) . dump_stylesheet_file ( out_file , stylesheet ) <EOL> except DumpError as e : <EOL> conv . report_dump_error ( out , in_file , text , e ) <EOL> if DEBUG : <EOL> import traceback <EOL> traceback . print_exc ( ) <EOL> preview_compiled_css ( ) <EOL> return <EOL> status ( "<STR_LIT>" ) <EOL> if settings ( ) . get ( '<STR_LIT>' ) : <EOL> self . view . window ( ) . open_file ( out_file ) <EOL> def preview_compiled_css ( self , text , conv , base_name ) : <EOL> if conv . ext == '<STR_LIT>' : <EOL> return <EOL> v = self . view . window ( ) . new_file ( ) <EOL> v . set_scratch ( True ) <EOL> v . set_syntax_file ( "<STR_LIT>" % PACKAGE ) <EOL> v . set_name ( "<STR_LIT>" % base_name ) <EOL> set_text ( v , text ) <EOL> class convert_tmtheme ( sublime_plugin . TextCommand ) : <EOL> """<STR_LIT>""" <EOL> def is_enabled ( self ) : <EOL> path = self . view . file_name ( ) <EOL> return bool ( path ) and path . endswith ( "<STR_LIT>" ) <EOL> def run ( self , edit , overwrite = False , skip_names = False ) : <EOL> path = self . view . file_name ( ) <EOL> new_path = os . path . splitext ( path ) [ <NUM_LIT:0> ] + '<STR_LIT>' <EOL> if not overwrite and os . path . exists ( new_path ) : <EOL> if not sublime . ok_cancel_dialog ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % new_path ) : <EOL> return <EOL> with OutputPanel ( self . view . window ( ) , "<STR_LIT>" ) as out : <EOL> data = tmtheme . load ( get_text ( self . view ) , path , out ) <EOL> if not data : <EOL> return <EOL> csscheme = tmtheme . to_csscheme ( data , out , skip_names ) <EOL> if not csscheme : <EOL> return <EOL> v = self . view . window ( ) . open_file ( new_path ) <EOL> def continue_operation ( ) : <EOL> v . settings ( ) . set ( '<STR_LIT>' , False ) <EOL> set_text ( v , csscheme ) <EOL> v . settings ( ) . erase ( '<STR_LIT>' ) <EOL> v . run_command ( '<STR_LIT>' ) <EOL> sublime . set_timeout ( continue_operation , <NUM_LIT:0> ) </s>
