从位于非标准目录中的文件中获取设置值
Get a setting value, from file located in non-standard dir
我有这样的 Sublime 目录结构:
Packages
|-- Foo
| |-- Markdown.sublime-settings
|
|-- Bar
| |-- plugin.py
|
|-- User
|-- Markdown.sublime-settings
然后,我尝试获取 wrap_width
值,存储在 Foo/Markdown.sublime-setting
中。出于某种原因,load_setting
方法似乎不起作用,尽管 save_settings
工作正常。
import sublime
import sublime_plugin
class MarkdownSettings(sublime_plugin.EventListener):
def on_activated(self, view):
path = view.file_name()
if path:
e = view.file_name().split('.')[1]
if e == ("md" or "mmd"):
# Simple test. It works
x = sublime.load_settings("Markdown.sublime-settings")
wrap_width = x.get("wrap_width")
print(wrap_width) # Prints 50
# If I change directory to "../Foo", `load_setting` method would not work
x = sublime.load_settings("../Foo/Markdown.sublime-settings")
wrap_width = x.get("wrap_width")
print(wrap_width) # Prints None
# The code below is added just for demonstration purposes,
# to show that `save_setting` method works fine.
x = sublime.load_settings("../Foo/Markdown.sublime-settings")
x.set("wrap_width", 20)
sublime.save_settings("../Foo/Markdown.sublime-settings") # File updated
如何获取存储在 Foo/Markdown.sublime-settings
中的 wrap_width
值?
不支持使用带有 load_settings
的路径。
来自http://www.sublimetext.com/docs/3/api_reference.html#sublime:
Loads the named settings. The name should include a file name and extension, but not a path. The packages will be searched for files matching the base_name
, and the results will be collated into the settings object. Subsequent calls to load_settings()
with the base_name
will return the same object, and not load the settings from disk again.
如果你真的需要这样做,你应该使用sublime.decode_value(sublime.load_resource('Packages/Foo/Markdown.sublime-settings'))
。
我有这样的 Sublime 目录结构:
Packages
|-- Foo
| |-- Markdown.sublime-settings
|
|-- Bar
| |-- plugin.py
|
|-- User
|-- Markdown.sublime-settings
然后,我尝试获取 wrap_width
值,存储在 Foo/Markdown.sublime-setting
中。出于某种原因,load_setting
方法似乎不起作用,尽管 save_settings
工作正常。
import sublime
import sublime_plugin
class MarkdownSettings(sublime_plugin.EventListener):
def on_activated(self, view):
path = view.file_name()
if path:
e = view.file_name().split('.')[1]
if e == ("md" or "mmd"):
# Simple test. It works
x = sublime.load_settings("Markdown.sublime-settings")
wrap_width = x.get("wrap_width")
print(wrap_width) # Prints 50
# If I change directory to "../Foo", `load_setting` method would not work
x = sublime.load_settings("../Foo/Markdown.sublime-settings")
wrap_width = x.get("wrap_width")
print(wrap_width) # Prints None
# The code below is added just for demonstration purposes,
# to show that `save_setting` method works fine.
x = sublime.load_settings("../Foo/Markdown.sublime-settings")
x.set("wrap_width", 20)
sublime.save_settings("../Foo/Markdown.sublime-settings") # File updated
如何获取存储在 Foo/Markdown.sublime-settings
中的 wrap_width
值?
不支持使用带有 load_settings
的路径。
来自http://www.sublimetext.com/docs/3/api_reference.html#sublime:
Loads the named settings. The name should include a file name and extension, but not a path. The packages will be searched for files matching the
base_name
, and the results will be collated into the settings object. Subsequent calls toload_settings()
with thebase_name
will return the same object, and not load the settings from disk again.
如果你真的需要这样做,你应该使用sublime.decode_value(sublime.load_resource('Packages/Foo/Markdown.sublime-settings'))
。