def __init__(self, scheme_file, color_filter=None):
"""Initialize."""
if color_filter is None:
color_filter = self.filter
self.color_scheme = path.normpath(scheme_file)
self.scheme_file = path.basename(self.color_scheme)
self.plist_file = color_filter(
readPlistFromBytes(
re.sub(
br"^[\r\n\s]*<!--[\s\S]*?-->[\s\r\n]*|<!--[\s\S]*?-->", b'',
sublime.load_binary_resource(sublime_format_path(self.color_scheme))
)
)
)
self.scheme_file = scheme_file
self.matched = {}
self.parse_scheme()
python类load_binary_resource()的实例源码
st_color_scheme_matcher.py 文件源码
项目:sublime-text-3-packages
作者: nickjj
项目源码
文件源码
阅读 30
收藏 0
点赞 0
评论 0
def __init__(self, scheme_file, color_filter=None):
"""Initialize."""
if color_filter is None:
color_filter = self.filter
self.color_scheme = path.normpath(scheme_file)
self.scheme_file = path.basename(self.color_scheme)
self.plist_file = color_filter(
readPlistFromBytes(
re.sub(
br"^[\r\n\s]*<!--[\s\S]*?-->[\s\r\n]*|<!--[\s\S]*?-->", b'',
sublime.load_binary_resource(sublime_format_path(self.color_scheme))
)
)
)
self.scheme_file = scheme_file
self.matched = {}
self.parse_scheme()
def __init__(self, scheme_file, color_filter=None):
"""Initialize."""
if color_filter is None:
color_filter = self.filter
self.color_scheme = path.normpath(scheme_file)
self.scheme_file = path.basename(self.color_scheme)
self.plist_file = color_filter(
readPlistFromBytes(
re.sub(
br"^[\r\n\s]*<!--[\s\S]*?-->[\s\r\n]*|<!--[\s\S]*?-->", b'',
sublime.load_binary_resource(sublime_format_path(self.color_scheme))
)
)
)
self.scheme_file = scheme_file
self.matched = {}
self.parse_scheme()
def run(self, *args, **kwargs):
view = sublime.active_window().active_view()
#settings = sublime.load_settings('KSP.sublime-settings')
#scheme_file = settings.get('color_scheme', 'Packages/SublimeKSP/KScript Light.tmTheme')
scheme_file = 'Packages/SublimeKSP/KScript Light.tmTheme'
plist = readPlistFromBytes(sublime.load_binary_resource(scheme_file))
result = ['[pre]']
start, end = view.sel()[0].a, view.sel()[0].b
if start == end:
start, end = 0, view.size()
for a, b, scopes in get_ranges(view.scope_name(i) for i in range(start, end)):
result.append(self.apply_style(scopes, plist, view.substr(sublime.Region(start+a, start+b))))
result.append('[/pre]')
sublime.set_clipboard(''.join(result))
def tint(img, color, opacity=255, height=None, width=None):
"""Tint the image."""
if isinstance(img, str):
try:
img = sublime.load_binary_resource(img)
except Exception:
_log('Could not open binary file!')
_debug(traceback.format_exc(), ERROR)
return ''
return imagetint.tint(img, color, opacity, height, width)
def tint_raw(img, color, opacity=255):
"""Tint the image."""
if isinstance(img, str):
try:
img = sublime.load_binary_resource(img)
except Exception:
_log('Could not open binary file!')
_debug(traceback.format_exc(), ERROR)
return ''
return imagetint.tint_raw(img, color, opacity)
def tint(img, color, opacity=255, height=None, width=None):
"""Tint the image."""
if isinstance(img, str):
try:
img = sublime.load_binary_resource(img)
except Exception:
_log('Could not open binary file!')
_debug(traceback.format_exc(), ERROR)
return ''
return imagetint.tint(img, color, opacity, height, width)
def tint_raw(img, color, opacity=255):
"""Tint the image."""
if isinstance(img, str):
try:
img = sublime.load_binary_resource(img)
except Exception:
_log('Could not open binary file!')
_debug(traceback.format_exc(), ERROR)
return ''
return imagetint.tint_raw(img, color, opacity)
def get_file_hash(self, color_scheme):
"""Generate an MD5 hash of the color scheme file to be compared for changes."""
content = sublime.load_binary_resource(color_scheme)
file_hash = hashlib.md5(content).hexdigest()
return file_hash
def load_color_scheme(self, color_scheme):
"""Read the color_scheme user settings and load the file contents."""
content = sublime.load_binary_resource(color_scheme)
return content
def tint(img, color, opacity=255, height=None, width=None):
"""Tint the image."""
if isinstance(img, str):
try:
img = sublime.load_binary_resource(img)
except Exception:
_log('Could not open binary file!')
_debug(traceback.format_exc(), ERROR)
return ''
return imagetint.tint(img, color, opacity, height, width)
def tint_raw(img, color, opacity=255):
"""Tint the image."""
if isinstance(img, str):
try:
img = sublime.load_binary_resource(img)
except Exception:
_log('Could not open binary file!')
_debug(traceback.format_exc(), ERROR)
return ''
return imagetint.tint_raw(img, color, opacity)
def _get_resource(package_name, resource, return_binary=False, encoding="utf-8"):
packages_path = sublime.packages_path()
content = None
if VERSION > 3013:
try:
if return_binary:
content = sublime.load_binary_resource("Packages/" + package_name + "/" + resource)
else:
content = sublime.load_resource("Packages/" + package_name + "/" + resource)
except IOError:
pass
else:
path = None
if os.path.exists(os.path.join(packages_path, package_name, resource)):
path = os.path.join(packages_path, package_name, resource)
content = _get_directory_item_content(path, return_binary, encoding)
if VERSION >= 3006:
sublime_package = package_name + ".sublime-package"
packages_path = sublime.installed_packages_path()
if content is None:
if os.path.exists(os.path.join(packages_path, sublime_package)):
content = _get_zip_item_content(os.path.join(packages_path, sublime_package), resource, return_binary, encoding)
packages_path = os.path.dirname(sublime.executable_path()) + os.sep + "Packages"
if content is None:
if os.path.exists(os.path.join(packages_path, sublime_package)):
content = _get_zip_item_content(os.path.join(packages_path, sublime_package), resource, return_binary, encoding)
return content
def get_file_hash(self, color_scheme):
"""Generate an MD5 hash of the color scheme file to be compared for changes."""
content = sublime.load_binary_resource(color_scheme)
file_hash = hashlib.md5(content).hexdigest()
return file_hash
def load_color_scheme(self, color_scheme):
"""Read the color_scheme user settings and load the file contents."""
content = sublime.load_binary_resource(color_scheme)
return content
def set_view(self, src, lang):
"""Setup view for conversion."""
# Get the output panel
self.view = sublime.active_window().get_output_panel('mdpopups')
# Let all plugins no to leave this view alone
self.view.settings().set('is_widget', True)
# Don't translate anything.
self.view.settings().set("translate_tabs_to_spaces", False)
# Don't mess with my indenting Sublime!
self.view.settings().set("auto_indent", False)
# Insert into the view
self.view.run_command('insert', {'characters': src})
# Setup the proper syntax
lang = lang.lower()
user_map = sublime.load_settings('Preferences.sublime-settings').get('mdpopups.sublime_user_lang_map', {})
keys = set(list(user_map.keys()) + list(lang_map.keys()))
loaded = False
for key in keys:
v = lang_map.get(key, (tuple(), tuple()))
user_v = user_map.get(key, (tuple(), tuple()))
if lang in (tuple(user_v[0]) + v[0]):
for l in (tuple(user_v[1]) + v[1]):
for ext in ST_LANGUAGES:
sytnax_file = 'Packages/%s%s' % (l, ext)
try:
sublime.load_binary_resource(sytnax_file)
except Exception:
continue
self.view.set_syntax_file(sytnax_file)
loaded = True
break
if loaded:
break
if loaded:
break
if not loaded:
# Default to plain text
for ext in ST_LANGUAGES:
# Just in case text one day switches to 'sublime-syntax'
sytnax_file = 'Packages/Plain text%s' % ext
try:
sublime.load_binary_resource(sytnax_file)
except Exception:
continue
self.view.set_syntax_file(sytnax_file)
def set_view(self, src, lang):
"""Setup view for conversion."""
# Get the output panel
self.view = sublime.active_window().get_output_panel('mdpopups')
# Let all plugins no to leave this view alone
self.view.settings().set('is_widget', True)
# Don't translate anything.
self.view.settings().set("translate_tabs_to_spaces", False)
# Don't mess with my indenting Sublime!
self.view.settings().set("auto_indent", False)
# Insert into the view
self.view.run_command('insert', {'characters': src})
# Setup the proper syntax
lang = lang.lower()
user_map = sublime.load_settings('Preferences.sublime-settings').get('mdpopups.sublime_user_lang_map', {})
keys = set(list(user_map.keys()) + list(lang_map.keys()))
loaded = False
for key in keys:
v = lang_map.get(key, (tuple(), tuple()))
user_v = user_map.get(key, (tuple(), tuple()))
if lang in (tuple(user_v[0]) + v[0]):
for l in (tuple(user_v[1]) + v[1]):
for ext in ST_LANGUAGES:
sytnax_file = 'Packages/%s%s' % (l, ext)
try:
sublime.load_binary_resource(sytnax_file)
except Exception:
continue
self.view.set_syntax_file(sytnax_file)
loaded = True
break
if loaded:
break
if loaded:
break
if not loaded:
# Default to plain text
for ext in ST_LANGUAGES:
# Just in case text one day switches to 'sublime-syntax'
sytnax_file = 'Packages/Plain text%s' % ext
try:
sublime.load_binary_resource(sytnax_file)
except Exception:
continue
self.view.set_syntax_file(sytnax_file)
def set_view(self, src, lang):
"""Setup view for conversion."""
# Get the output panel
self.view = sublime.active_window().get_output_panel('mdpopups')
# Let all plugins no to leave this view alone
self.view.settings().set('is_widget', True)
# Don't translate anything.
self.view.settings().set("translate_tabs_to_spaces", False)
# Don't mess with my indenting Sublime!
self.view.settings().set("auto_indent", False)
# Insert into the view
self.view.run_command('insert', {'characters': src})
# Setup the proper syntax
lang = lang.lower()
user_map = sublime.load_settings('Preferences.sublime-settings').get('mdpopups.sublime_user_lang_map', {})
keys = set(list(user_map.keys()) + list(lang_map.keys()))
loaded = False
for key in keys:
v = lang_map.get(key, (tuple(), tuple()))
user_v = user_map.get(key, (tuple(), tuple()))
if lang in (tuple(user_v[0]) + v[0]):
for l in (tuple(user_v[1]) + v[1]):
for ext in ST_LANGUAGES:
sytnax_file = 'Packages/%s%s' % (l, ext)
try:
sublime.load_binary_resource(sytnax_file)
except Exception:
continue
self.view.set_syntax_file(sytnax_file)
loaded = True
break
if loaded:
break
if loaded:
break
if not loaded:
# Default to plain text
for ext in ST_LANGUAGES:
# Just in case text one day switches to 'sublime-syntax'
sytnax_file = 'Packages/Plain text%s' % ext
try:
sublime.load_binary_resource(sytnax_file)
except Exception:
continue
self.view.set_syntax_file(sytnax_file)
def init_css():
global tooltip_css
color_plist = readPlistFromBytes(sublime.load_binary_resource(pref_settings.get('color_scheme')))
color_dict = {}
for x in color_plist['settings'] :
if 'scope' in x:
for s in x['scope'].split(','):
color_dict[s.strip()] = x['settings']
color_dict['__GLOBAL__'] = color_plist['settings'][0]['settings'] # first settings contains global settings, without scope(hopefully)
bg = int(color_dict['__GLOBAL__']['background'][1:],16)
fg = int(color_dict['__GLOBAL__']['foreground'][1:],16)
# Get color for keyword, support, storage, default to foreground
kw = fg if 'keyword' not in color_dict else int(color_dict['keyword']['foreground'][1:],16)
sup = fg if 'support' not in color_dict else int(color_dict['support']['foreground'][1:],16)
sto = fg if 'storage' not in color_dict else int(color_dict['storage']['foreground'][1:],16)
ent = fg if 'entity' not in color_dict else int(color_dict['entity']['foreground'][1:],16)
fct = fg if 'support.function' not in color_dict else int(color_dict['support.function']['foreground'][1:],16)
op = fg if 'keyword.operator' not in color_dict else int(color_dict['keyword.operator']['foreground'][1:],16)
num = fg if 'constant.numeric' not in color_dict else int(color_dict['constant.numeric']['foreground'][1:],16)
st = fg if 'string' not in color_dict else int(color_dict['string']['foreground'][1:],16)
# Create background and border color based on the background color
b = bg & 255
g = (bg>>8) & 255
r = (bg>>16) & 255
if b > 128:
bgHtml = b - 0x33
bgBody = b - 0x20
else:
bgHtml = b + 0x33
bgBody = b + 0x20
if g > 128:
bgHtml += (g - 0x33)<<8
bgBody += (g - 0x20)<<8
else:
bgHtml += (g + 0x33)<<8
bgBody += (g + 0x20)<<8
if r > 128:
bgHtml += (r - 0x33)<<16
bgBody += (r - 0x20)<<16
else:
bgHtml += (r + 0x33)<<16
bgBody += (r + 0x20)<<16
tooltip_css = 'html {{ background-color: #{bg:06x}; color: #{fg:06x}; }}\n'.format(bg=bgHtml, fg=fg)
tooltip_css+= 'body {{ background-color: #{bg:06x}; margin: 1px; font-size: 1em; }}\n'.format(bg=bgBody)
tooltip_css+= 'p {padding-left: 0.6em;}\n'
tooltip_css+= '.content {margin: 0.8em;}\n'
tooltip_css+= '.keyword {{color: #{c:06x};}}\n'.format(c=kw)
tooltip_css+= '.support {{color: #{c:06x};}}\n'.format(c=sup)
tooltip_css+= '.storage {{color: #{c:06x};}}\n'.format(c=sto)
tooltip_css+= '.function {{color: #{c:06x};}}\n'.format(c=fct)
tooltip_css+= '.entity {{color: #{c:06x};}}\n'.format(c=ent)
tooltip_css+= '.operator {{color: #{c:06x};}}\n'.format(c=op)
tooltip_css+= '.numeric {{color: #{c:06x};}}\n'.format(c=num)
tooltip_css+= '.string {{color: #{c:06x};}}\n'.format(c=st)
tooltip_css+= '.extra-info {font-size: 0.9em; }\n'
############################################################################