I need to make a sublime plugin that requires per project settings (API keys, passwords, etc.) which sublime doesn’t implement.
The latest update added a method to list all folders (active_window().folders()) open in a project, which means I can build a Settings class that searches all files for a specific settings file.
from ConfigParser import RawConfigParser
class ProjectSettingsMixin(object):
"""
Create project specific settings. As of Jun 15 2011 - not supported by Sublime.
Usage: mix this class into any sublime text base plugin class
ex: class MyCommand(sublime_plugin.WindowCommand, ProjectSettingsMixin)
Uses the python ConfigParser library.
Access settings via self.settings.get('header', 'key')
"""
SETTINGS_FILE = 'sublime.config'
def get_project_settings_file(self):
"""
Find project specific settings in a folder called "sublime_settings"
- there is no support for project specific settings as of June 15, 2011.
"""
for folder in self.window.folders():
johnnie_walker = os.walk(os.path.abspath(folder))
for directory, _, files in johnnie_walker:
for file in files:
if file == self.SETTINGS_FILE:
return os.path.join(directory, file)
raise Exception("Could not find settings file {0} in folders {1}".format(
self.SETTINGS_FILE, self.window.folders()))
@property
def settings(self):
if not hasattr(self, '_settings'):
config_parser = RawConfigParser()
config_parser.read(self.get_project_settings_file())
self._settings = config_parser
return self._settings
This method ensures the expensive os.walk() is only done once.