Home Download Buy Blog Forum Support

[BUG] reading plugin settings

[BUG] reading plugin settings

Postby farfromrefuge on Fri Jan 17, 2014 12:11 pm

Hi,

in my plugin i use that method to read settings
Code: Select all
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

When i run my plugin (through a key press), i first load a setting like such
Code: Select all
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
farfromrefuge
 
Posts: 38
Joined: Mon Oct 19, 2009 11:49 am

Re: [BUG] reading plugin settings

Postby facelessuser on Fri Jan 17, 2014 5:22 pm

ST3 or ST2?
facelessuser
 
Posts: 1575
Joined: Tue Apr 05, 2011 7:38 pm

Re: [BUG] reading plugin settings

Postby farfromrefuge on Mon Jan 20, 2014 9:22 am

Oups sorry i did mention that :s
ST3 wasn't happening on ST2
farfromrefuge
 
Posts: 38
Joined: Mon Oct 19, 2009 11:49 am

Re: [BUG] reading plugin settings

Postby facelessuser on Mon Jan 20, 2014 6:05 pm

farfromrefuge wrote:ST3 wasn't happening on ST2

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.

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)
facelessuser
 
Posts: 1575
Joined: Tue Apr 05, 2011 7:38 pm

[SOLVED] Re: [BUG] reading plugin settings

Postby farfromrefuge on Wed Jan 22, 2014 2:32 pm

Thank you!!!!!

i didnt know about plugin_loaded and would not have figured it out without your help!
farfromrefuge
 
Posts: 38
Joined: Mon Oct 19, 2009 11:49 am

Re: [BUG] reading plugin settings

Postby skuroda on Thu Jan 23, 2014 6:48 am

As a reference, you may want to take a look at http://www.sublimetext.com/docs/3/porting_guide.html and http://www.sublimetext.com/docs/3/api_reference.html (specifically plugin life cycle in the API docs).
skuroda
 
Posts: 685
Joined: Fri Oct 05, 2012 8:11 am


Return to Plugin Development

Who is online

Users browsing this forum: Google [Bot] and 6 guests