<s> """<STR_LIT>""" <EOL> import sublime <EOL> import os <EOL> import zipfile <EOL> import tempfile <EOL> import re <EOL> import codecs <EOL> __all__ = [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> ] <EOL> VERSION = int ( sublime . version ( ) ) <EOL> def get_resource ( package_name , resource , encoding = "<STR_LIT:utf-8>" ) : <EOL> return _get_resource ( package_name , resource , encoding = encoding ) <EOL> def get_binary_resource ( package_name , resource ) : <EOL> return _get_resource ( package_name , resource , return_binary = True ) <EOL> def _get_resource ( package_name , resource , return_binary = False , encoding = "<STR_LIT:utf-8>" ) : <EOL> packages_path = sublime . packages_path ( ) <EOL> content = None <EOL> if VERSION > <NUM_LIT> : <EOL> try : <EOL> if return_binary : <EOL> content = sublime . load_binary_resource ( "<STR_LIT>" + package_name + "<STR_LIT:/>" + resource ) <EOL> else : <EOL> content = sublime . load_resource ( "<STR_LIT>" + package_name + "<STR_LIT:/>" + resource ) <EOL> except IOError : <EOL> pass <EOL> else : <EOL> path = None <EOL> if os . path . exists ( os . path . join ( packages_path , package_name , resource ) ) : <EOL> path = os . path . join ( packages_path , package_name , resource ) <EOL> content = _get_directory_item_content ( path , return_binary , encoding ) <EOL> if VERSION >= <NUM_LIT> : <EOL> sublime_package = package_name + "<STR_LIT>" <EOL> packages_path = sublime . installed_packages_path ( ) <EOL> if content is None : <EOL> if os . path . exists ( os . path . join ( packages_path , sublime_package ) ) : <EOL> content = _get_zip_item_content ( os . path . join ( packages_path , sublime_package ) , resource , return_binary , encoding ) <EOL> packages_path = os . path . dirname ( sublime . executable_path ( ) ) + os . sep + "<STR_LIT>" <EOL> if content is None : <EOL> if os . path . exists ( os . path . join ( packages_path , sublime_package ) ) : <EOL> content = _get_zip_item_content ( os . path . join ( packages_path , sublime_package ) , resource , return_binary , encoding ) <EOL> return content . replace ( "<STR_LIT:\r\n>" , "<STR_LIT:\n>" ) . replace ( "<STR_LIT:\r>" , "<STR_LIT:\n>" ) <EOL> def find_resource ( resource_pattern , package = None ) : <EOL> file_set = set ( ) <EOL> if package == None : <EOL> for package in get_packages_list ( ) : <EOL> file_set . update ( find_resource ( resource_pattern , package ) ) <EOL> ret_list = list ( file_set ) <EOL> else : <EOL> file_set . update ( _find_directory_resource ( os . path . join ( sublime . packages_path ( ) , package ) , resource_pattern ) ) <EOL> if VERSION >= <NUM_LIT> : <EOL> zip_location = os . path . join ( sublime . installed_packages_path ( ) , package + "<STR_LIT>" ) <EOL> file_set . update ( _find_zip_resource ( zip_location , resource_pattern ) ) <EOL> zip_location = os . path . join ( os . path . dirname ( sublime . executable_path ( ) ) , "<STR_LIT>" , package + "<STR_LIT>" ) <EOL> file_set . update ( _find_zip_resource ( zip_location , resource_pattern ) ) <EOL> ret_list = map ( lambda e : package + "<STR_LIT:/>" + e , file_set ) <EOL> return sorted ( ret_list ) <EOL> def list_package_files ( package , ignore_patterns = [ ] ) : <EOL> """<STR_LIT>""" <EOL> package_path = os . path . join ( sublime . packages_path ( ) , package , "<STR_LIT>" ) <EOL> path = None <EOL> file_set = set ( ) <EOL> file_list = [ ] <EOL> if os . path . exists ( package_path ) : <EOL> for root , directories , filenames in os . walk ( package_path ) : <EOL> temp = root . replace ( package_path , "<STR_LIT>" ) <EOL> for filename in filenames : <EOL> file_list . append ( os . path . join ( temp , filename ) ) <EOL> file_set . update ( file_list ) <EOL> if VERSION >= <NUM_LIT> : <EOL> sublime_package = package + "<STR_LIT>" <EOL> packages_path = sublime . installed_packages_path ( ) <EOL> if os . path . exists ( os . path . join ( packages_path , sublime_package ) ) : <EOL> file_set . update ( _list_files_in_zip ( packages_path , sublime_package ) ) <EOL> packages_path = os . path . dirname ( sublime . executable_path ( ) ) + os . sep + "<STR_LIT>" <EOL> if os . path . exists ( os . path . join ( packages_path , sublime_package ) ) : <EOL> file_set . update ( _list_files_in_zip ( packages_path , sublime_package ) ) <EOL> file_list = [ ] <EOL> for filename in file_set : <EOL> if not _ignore_file ( filename , ignore_patterns ) : <EOL> file_list . append ( _normalize_to_sublime_path ( filename ) ) <EOL> return sorted ( file_list ) <EOL> def _ignore_file ( filename , ignore_patterns = [ ] ) : <EOL> ignore = False <EOL> directory , base = os . path . split ( filename ) <EOL> for pattern in ignore_patterns : <EOL> if re . match ( pattern , base ) : <EOL> return True <EOL> if len ( directory ) > <NUM_LIT:0> : <EOL> ignore = _ignore_file ( directory , ignore_patterns ) <EOL> return ignore <EOL> def _normalize_to_sublime_path ( path ) : <EOL> path = os . path . normpath ( path ) <EOL> path = re . sub ( r"<STR_LIT>" , "<STR_LIT>" , path ) <EOL> path = re . sub ( r"<STR_LIT:\\>" , "<STR_LIT:/>" , path ) <EOL> return path <EOL> def get_package_and_resource_name ( path ) : <EOL> """<STR_LIT>""" <EOL> package = None <EOL> resource = None <EOL> path = _normalize_to_sublime_path ( path ) <EOL> if os . path . isabs ( path ) : <EOL> packages_path = _normalize_to_sublime_path ( sublime . packages_path ( ) ) <EOL> if path . startswith ( packages_path ) : <EOL> package , resource = _search_for_package_and_resource ( path , packages_path ) <EOL> if int ( sublime . version ( ) ) >= <NUM_LIT> : <EOL> packages_path = _normalize_to_sublime_path ( sublime . installed_packages_path ( ) ) <EOL> if path . startswith ( packages_path ) : <EOL> package , resource = _search_for_package_and_resource ( path , packages_path ) <EOL> packages_path = _normalize_to_sublime_path ( os . path . dirname ( sublime . executable_path ( ) ) + os . sep + "<STR_LIT>" ) <EOL> if path . startswith ( packages_path ) : <EOL> package , resource = _search_for_package_and_resource ( path , packages_path ) <EOL> else : <EOL> path = re . sub ( r"<STR_LIT>" , "<STR_LIT>" , path ) <EOL> split = re . split ( r"<STR_LIT:/>" , path , <NUM_LIT:1> ) <EOL> package = split [ <NUM_LIT:0> ] <EOL> package = package . replace ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> resource = split [ <NUM_LIT:1> ] <EOL> return ( package , resource ) <EOL> def get_packages_list ( ignore_packages = True , ignore_patterns = [ ] ) : <EOL> """<STR_LIT>""" <EOL> package_set = set ( ) <EOL> package_set . update ( _get_packages_from_directory ( sublime . packages_path ( ) ) ) <EOL> if int ( sublime . version ( ) ) >= <NUM_LIT> : <EOL> package_set . update ( _get_packages_from_directory ( sublime . installed_packages_path ( ) , "<STR_LIT>" ) ) <EOL> executable_package_path = os . path . dirname ( sublime . executable_path ( ) ) + os . sep + "<STR_LIT>" <EOL> package_set . update ( _get_packages_from_directory ( executable_package_path , "<STR_LIT>" ) ) <EOL> if ignore_packages : <EOL> ignored_list = sublime . load_settings ( <EOL> "<STR_LIT>" ) . get ( "<STR_LIT>" , [ ] ) <EOL> else : <EOL> ignored_list = [ ] <EOL> for package in package_set : <EOL> for pattern in ignore_patterns : <EOL> if re . match ( pattern , package ) : <EOL> ignored_list . append ( package ) <EOL> break <EOL> for ignored in ignored_list : <EOL> package_set . discard ( ignored ) <EOL> return sorted ( list ( package_set ) ) <EOL> def get_sublime_packages ( ignore_packages = True , ignore_patterns = [ ] ) : <EOL> package_list = get_packages_list ( ignore_packages , ignore_patterns ) <EOL> extracted_list = _get_packages_from_directory ( sublime . packages_path ( ) ) <EOL> return [ x for x in package_list if x not in extracted_list ] <EOL> def _get_packages_from_directory ( directory , file_ext = "<STR_LIT>" ) : <EOL> package_list = [ ] <EOL> for package in os . listdir ( directory ) : <EOL> if not package . endswith ( file_ext ) : <EOL> continue <EOL> else : <EOL> package = package . replace ( file_ext , "<STR_LIT>" ) <EOL> package_list . append ( package ) <EOL> return package_list <EOL> def _search_for_package_and_resource ( path , packages_path ) : <EOL> """<STR_LIT>""" <EOL> relative_package_path = path . replace ( packages_path + "<STR_LIT:/>" , "<STR_LIT>" ) <EOL> package , resource = re . split ( r"<STR_LIT:/>" , relative_package_path , <NUM_LIT:1> ) <EOL> package = package . replace ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> return ( package , resource ) <EOL> def _list_files_in_zip ( package_path , package ) : <EOL> if not os . path . exists ( os . path . join ( package_path , package ) ) : <EOL> return [ ] <EOL> ret_value = [ ] <EOL> with zipfile . ZipFile ( os . path . join ( package_path , package ) ) as zip_file : <EOL> ret_value = zip_file . namelist ( ) <EOL> return ret_value <EOL> def _get_zip_item_content ( path_to_zip , resource , return_binary , encoding ) : <EOL> if not os . path . exists ( path_to_zip ) : <EOL> return None <EOL> ret_value = None <EOL> with zipfile . ZipFile ( path_to_zip ) as zip_file : <EOL> namelist = zip_file . namelist ( ) <EOL> if resource in namelist : <EOL> ret_value = zip_file . read ( resource ) <EOL> if not return_binary : <EOL> ret_value = ret_value . decode ( encoding ) <EOL> return ret_value <EOL> def _get_directory_item_content ( filename , return_binary , encoding ) : <EOL> content = None <EOL> if os . path . exists ( filename ) : <EOL> if return_binary : <EOL> mode = "<STR_LIT:rb>" <EOL> encoding = None <EOL> else : <EOL> mode = "<STR_LIT:r>" <EOL> with codecs . open ( filename , mode , encoding = encoding ) as file_obj : <EOL> content = file_obj . read ( ) <EOL> return content <EOL> def _find_zip_resource ( path_to_zip , pattern ) : <EOL> ret_list = [ ] <EOL> if os . path . exists ( path_to_zip ) : <EOL> with zipfile . ZipFile ( path_to_zip ) as zip_file : <EOL> namelist = zip_file . namelist ( ) <EOL> for name in namelist : <EOL> if re . search ( pattern , name ) : <EOL> ret_list . append ( name ) <EOL> return ret_list <EOL> def _find_directory_resource ( path , pattern ) : <EOL> ret_list = [ ] <EOL> if os . path . exists ( path ) : <EOL> path = os . path . join ( path , "<STR_LIT>" ) <EOL> for root , directories , filenames in os . walk ( path ) : <EOL> temp = root . replace ( path , "<STR_LIT>" ) <EOL> for filename in filenames : <EOL> if re . search ( pattern , os . path . join ( temp , filename ) ) : <EOL> ret_list . append ( os . path . join ( temp , filename ) ) <EOL> return ret_list <EOL> def extract_zip_resource ( path_to_zip , resource , extract_dir = None ) : <EOL> if extract_dir is None : <EOL> extract_dir = tempfile . mkdtemp ( ) <EOL> file_location = None <EOL> if os . path . exists ( path_to_zip ) : <EOL> with zipfile . ZipFile ( path_to_zip ) as zip_file : <EOL> file_location = zip_file . extract ( resource , extract_dir ) <EOL> return file_location <EOL> def extract_package ( package ) : <EOL> if VERSION >= <NUM_LIT> : <EOL> package_location = os . path . join ( sublime . installed_packages_path ( ) , package + "<STR_LIT>" ) <EOL> if not os . path . exists ( package_location ) : <EOL> package_location = os . path . join ( os . path . dirname ( sublime . executable_path ( ) ) , "<STR_LIT>" , package + "<STR_LIT>" ) <EOL> if not os . path . exists ( package_location ) : <EOL> package_location = None <EOL> if package_location : <EOL> with zipfile . ZipFile ( package_location ) as zip_file : <EOL> extract_location = os . path . join ( sublime . packages_path ( ) , package ) <EOL> zip_file . extractall ( extract_location ) <EOL> import sys <EOL> if VERSION > <NUM_LIT> : <EOL> from imp import reload <EOL> if "<STR_LIT>" in sys . modules : <EOL> reload ( sys . modules [ "<STR_LIT>" ] ) <EOL> else : <EOL> if "<STR_LIT>" in sys . modules : <EOL> reload ( sys . modules [ "<STR_LIT>" ] ) </s>
