Sublime Forum

[BUG] reading plugin settings

#1

Hi,

in my plugin i use that method to read settings

[code]SETTINGS_FILE = PLUGIN_NAME + “.sublime-settings”
SETTINGS_PREFIX = PLUGIN_NAME.lower() + ‘_’

settings = sublime.load_settings(SETTINGS_FILE)

def get_setting(key, default=None, view=None):
try:
if view == None:
view = sublime.active_window().active_view()
s = view.settings()
if s.has(SETTINGS_PREFIX + key):
return s.get(SETTINGS_PREFIX + key)
except:
pass
if settings.has(key):
print('get_setting: ’ + key + ‘,’ + settings.get(key, default))
return settings.get(key, default)
else:
return default[/code]
When i run my plugin (through a key press), i first load a setting like such

self.defaultKeychain       = str(get_setting("iosKeychain", "unknown"))

My problem is that if close/start ST, then on a command run those settings wont be read :s
It will only work if i open the actual plugin py file and just save the file (which i think means a reload of the plugin).

DOes anyone have any clue on why this is happening?

Thanks

0 Likes

#2

ST3 or ST2?

0 Likes

#3

Oups sorry i did mention that :s
ST3 wasn’t happening on ST2

0 Likes

#4

ST3 would make sense. You can’t read the settings file until the plugin is loaded. So you can setup settings in the “plugin_loaded” function which gets run as soon as your plugin is loaded.

[pre=#232628]import sublime
import sublime_plugin

SETTINGS_FILE = “MyPlugin.sublime-settings”
settings = None

class MyCommand(sublime_plugin.ApplicationCommand):
def run(self):
print(SETTINGS_FILE.get(“this_should_work_now”))

def plugin_loaded():
global settings
settings = sublime.load_settings(SETTINGS_FILE)[/pre]

0 Likes

#5

Thank you!!!

i didnt know about plugin_loaded and would not have figured it out without your help!

0 Likes

#6

As a reference, you may want to take a look at sublimetext.com/docs/3/api_reference.html (specifically plugin life cycle in the API docs).

0 Likes