def getJsonOrGenerate(name, callback):
filename = getI18nCachePath() + name + '.json'
if os.path.exists(filename):
with open(filename, 'r', encoding='utf8') as f:
json = f.read(10485760)
content = sublime.decode_value(json)
else:
content = callback()
dirname = os.path.dirname(filename)
if not os.path.isdir(dirname):
os.makedirs(dirname)
with open(filename, 'w', encoding="utf8") as f:
f.write(sublime.encode_value(content))
return content
python类encode_value()的实例源码
def save_stylesheets(self, style_sheets):
"""Save the stylesheet dictionary to file"""
content = sublime.encode_value(style_sheets, True)
with open(self.theme_file_path, "w") as f:
f.write(content)
self.style_sheets = style_sheets
def tweak_theme():
view = sublime.active_window().active_view()
theme = view.settings().get('theme')
if theme is None:
print("Can't guess current theme.")
return
theme_path = os.path.join(sublime.packages_path(), 'User', theme)
if os.path.exists(theme_path):
with open(theme_path, mode='r', encoding='utf-8') as f:
theme_text = f.read()
if PYTEST_MARKERS.search(theme_text):
print("Already patched")
return
safety_path = os.path.join(
sublime.packages_path(), 'User', 'Original-' + theme)
with open(safety_path, mode='w', encoding='utf-8') as f:
f.write(theme_text)
theme = sublime.decode_value(theme_text)
else:
theme = []
theme.extend(PYTEST_RULES)
tweaked_theme = sublime.encode_value(theme, True)
with open(theme_path, mode='w', encoding='utf-8') as f:
f.write(tweaked_theme)
print('Done tweaking!')
# sublime.active_window().open_file(theme_path)
def send_command(self, callback: Callable, **data: Any) -> None:
"""Send the given command that should be handled bu the given callback
"""
data['uid'] = self.add_callback(callback)
try:
self.push(
bytes('{}\r\n'.format(sublime.encode_value(data)), 'utf8')
)
except NameError:
self.push(bytes('{}\r\n'.format(json.dumps(data)), 'utf8'))
def save_stylesheets(self, style_sheets):
"""Save the stylesheet dictionary to file"""
content = sublime.encode_value(style_sheets, True)
with open(self.theme_file_path, "w") as f:
f.write(content)
self.style_sheets = style_sheets