<s> import os <EOL> import re <EOL> import sublime <EOL> import sublime_plugin <EOL> class QuickCreateFileCreatorBase ( sublime_plugin . WindowCommand ) : <EOL> relative_paths = [ ] <EOL> full_torelative_paths = { } <EOL> rel_path_start = '<STR_LIT>' <EOL> def doCommand ( self ) : <EOL> self . construct_excluded_pattern ( ) <EOL> self . build_relative_paths ( ) <EOL> if len ( self . relative_paths ) == <NUM_LIT:1> : <EOL> self . selected_dir = self . relative_paths [ <NUM_LIT:0> ] <EOL> self . selected_dir = self . full_torelative_paths [ self . selected_dir ] <EOL> self . window . show_input_panel ( self . INPUT_PANEL_CAPTION , '<STR_LIT>' , self . file_name_input , None , None ) <EOL> elif len ( self . relative_paths ) > <NUM_LIT:1> : <EOL> self . move_current_directory_to_top ( ) <EOL> self . window . show_quick_panel ( self . relative_paths , self . dir_selected ) <EOL> else : <EOL> view = self . window . active_view ( ) <EOL> self . selected_dir = os . path . dirname ( view . file_name ( ) ) <EOL> self . window . show_input_panel ( self . INPUT_PANEL_CAPTION , '<STR_LIT>' , self . file_name_input , None , None ) <EOL> def construct_excluded_pattern ( self ) : <EOL> patterns = [ pat . replace ( '<STR_LIT:|>' , '<STR_LIT:\\>' ) for pat in self . get_setting ( '<STR_LIT>' ) ] <EOL> self . excluded = re . compile ( '<STR_LIT:|>' . join ( patterns ) ) <EOL> def get_setting ( self , key ) : <EOL> settings = None <EOL> view = self . window . active_view ( ) <EOL> if view : <EOL> settings = self . window . active_view ( ) . settings ( ) <EOL> if settings and settings . has ( '<STR_LIT>' ) and key in settings . get ( '<STR_LIT>' ) : <EOL> results = settings . get ( '<STR_LIT>' ) [ key ] <EOL> else : <EOL> settings = sublime . load_settings ( '<STR_LIT>' ) <EOL> results = settings . get ( key ) <EOL> return results <EOL> def build_relative_paths ( self ) : <EOL> folders = self . window . folders ( ) <EOL> self . relative_paths = [ ] <EOL> self . full_torelative_paths = { } <EOL> for path in folders : <EOL> rootfolders = os . path . split ( path ) [ - <NUM_LIT:1> ] <EOL> self . rel_path_start = os . path . split ( path ) [ <NUM_LIT:0> ] <EOL> if not self . excluded . search ( rootfolders ) : <EOL> self . full_torelative_paths [ rootfolders ] = path <EOL> self . relative_paths . append ( rootfolders ) <EOL> for base , dirs , files in os . walk ( path ) : <EOL> for dir in dirs : <EOL> relative_path = os . path . relpath ( os . path . join ( base , dir ) , self . rel_path_start ) <EOL> if not self . excluded . search ( relative_path ) : <EOL> self . full_torelative_paths [ relative_path ] = os . path . join ( base , dir ) <EOL> self . relative_paths . append ( relative_path ) <EOL> else : <EOL> dirs . remove ( dir ) <EOL> def move_current_directory_to_top ( self ) : <EOL> view = self . window . active_view ( ) <EOL> if view and view . file_name ( ) : <EOL> cur_dir = os . path . relpath ( os . path . dirname ( view . file_name ( ) ) , self . rel_path_start ) <EOL> if cur_dir in self . full_torelative_paths : <EOL> i = self . relative_paths . index ( cur_dir ) <EOL> self . relative_paths . insert ( <NUM_LIT:0> , self . relative_paths . pop ( i ) ) <EOL> else : <EOL> self . relative_paths . insert ( <NUM_LIT:0> , os . path . dirname ( view . file_name ( ) ) ) <EOL> return <EOL> def dir_selected ( self , selected_index ) : <EOL> if selected_index != - <NUM_LIT:1> : <EOL> self . selected_dir = self . relative_paths [ selected_index ] <EOL> self . selected_dir = self . full_torelative_paths [ self . selected_dir ] <EOL> self . window . show_input_panel ( self . INPUT_PANEL_CAPTION , '<STR_LIT>' , self . file_name_input , None , None ) <EOL> def file_name_input ( self , file_name ) : <EOL> full_path = os . path . join ( self . selected_dir , file_name ) <EOL> if os . path . lexists ( full_path ) : <EOL> sublime . error_message ( '<STR_LIT>' % full_path ) <EOL> return <EOL> else : <EOL> self . create_and_open_file ( full_path ) <EOL> def create ( self , filename ) : <EOL> base , filename = os . path . split ( filename ) <EOL> self . create_folder ( base ) <EOL> def create_folder ( self , base ) : <EOL> if not os . path . exists ( base ) : <EOL> parent = os . path . split ( base ) [ <NUM_LIT:0> ] <EOL> if not os . path . exists ( parent ) : <EOL> self . create_folder ( parent ) <EOL> os . mkdir ( base ) <EOL> class QuickCreateFileCommand ( QuickCreateFileCreatorBase ) : <EOL> INPUT_PANEL_CAPTION = '<STR_LIT>' <EOL> def run ( self ) : <EOL> self . doCommand ( ) <EOL> def create_and_open_file ( self , path ) : <EOL> if not os . path . exists ( path ) : <EOL> self . create ( path ) <EOL> self . window . open_file ( path ) <EOL> class QuickCreateDirectoryCommand ( QuickCreateFileCreatorBase ) : <EOL> INPUT_PANEL_CAPTION = '<STR_LIT>' <EOL> def run ( self ) : <EOL> self . doCommand ( ) <EOL> def create_and_open_file ( self , path ) : <EOL> self . create_folder ( path ) </s>
