Sublime Forum

Reading settings in AplicationCommand Constructor

#1

Is it possible to read in settings from my plugin’s constructor?

I have the following directory structure:

Packages/MyPlugin/ MyPlugin.py MyPlugin.sublime-settings

MyPlugin.sublime-settings looks like:

{"hello":"world"}
MyPlugin.py looks like:

[code]import sublime, sublime_plugin, sys

class MyPlugin(sublime_plugin.ApplicationCommand):
# On init read the settings and build the new key mappings
def init(self):
super(MyPlugin, self).init()
sys.stdout.write(‘Init MyPlugin\n’)

	s = sublime.load_settings('MyPlugin.sublime-settings')
	w = s.get('hello', 'backup')
	sys.stdout.write('type:' + type(w).__name__+'\n')[/code]

When my plugin loads I always get:

type:NoneType
0 Likes

[ST3] On start plugin load
#2

Use the event “plugin_loaded” for this.
Best way:

[code]SETTINGS_FILE = “MyPlugin.sublime-settings”
settings = None

class MyPlugin(sublime_plugin.ApplicationCommand):
# your stuff

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

0 Likes

#3

I tried the following, but the event never seems to fire. I tried changing the class so it inherits from EventListener and I still don’t get a log

[code]import sublime_plugin

class MyPlugin(sublime_plugin.ApplicationCommand):

def plugin_loaded():
    print('plugin_loaded in MyPlugin')[/code]
0 Likes

#4

If I recall correctly, plugin_loaded is not a method called from the command. Place it outside of the class.

0 Likes