Skip to content

Instantly share code, notes, and snippets.

@OdatNurd
Last active March 15, 2021 12:54
Show Gist options
  • Save OdatNurd/4be96e76bfa52c6d0669b63e83aceb10 to your computer and use it in GitHub Desktop.
Save OdatNurd/4be96e76bfa52c6d0669b63e83aceb10 to your computer and use it in GitHub Desktop.
Triggering report generation after package update
import sublime
watcher = None
class SettingsWatcher():
def __init__(self):
self.removed = set()
self.settings = sublime.load_settings("Preferences.sublime-settings")
self.cached_ignored = set(self.settings.get("ignored_packages", []))
self.settings.add_on_change("_oa_sw", lambda: self.settings_changed())
def unregister(self):
self.settings.clear_on_change("_oa_sw")
def run_report(self, removed_set):
# If the set we got is not the same as the one we currently have, the
# list changed since the job was scheduled, so do nothing here. If the
# new list still shows that something was removed from the list, a new
# job was scheduled. Otherwise we assume that the ignored package was
# added back.
if removed_set != self.removed:
return
# Empty the list of removed packages that triggered us.
self.removed = set()
print("Generating expired override report")
def settings_changed(self):
# Get the new ignored list; leave if it's the same
new_list = set(self.settings.get("ignored_packages", []))
if new_list == self.cached_ignored:
return
# Determine the packages added to and removed from the ignored packages
# setting since the last time we were loaded, then update our cache.
removed = self.cached_ignored - new_list
added = new_list - self.cached_ignored
self.cached_ignored = new_list
# Add the newly removed packages to our list of removed packages.
self.removed |= removed
# Remove from the list of removed packages any that might have been
# added back in.
self.removed -= added
# If there are any packages listed as removed, trigger a report. We pad
# the time a bit so that we can ignore this report if the list is still
# in flux.
if len(self.removed) != 0:
# Command takes a copy of the set of removed packages as we know it
# right now; this lets the command know if it should do nothing if
# the list changed before the timeout expires.
current = set(self.removed)
sublime.set_timeout(lambda: self.run_report(current), 60 * 1000)
def plugin_loaded():
global watcher
watcher = SettingsWatcher()
def plugin_unloaded():
global watcher
watcher.unregister()
@OdatNurd
Copy link
Author

As a note, On Windows/OSX Package names are not case sensitive because the file system isn't, so adding "VINTAGE" to the list of ignored packages is the same as adding "Vintage". For this to be more bullet proof, the package sets used here need to be case insensitive, such as https://github.com/OdatNurd/OverrideAudit/blob/master/lib/packages.py#L61.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment