only use kill -9 when needed

This commit is contained in:
JuanCanham 2020-04-06 18:20:52 +01:00 committed by GitHub
parent d6a966d14c
commit 0ceb68135a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -21,7 +21,7 @@ from mycroft import MycroftSkill, intent_handler
from urllib.parse import quote from urllib.parse import quote
from gi.repository import Gio as gio from gi.repository import Gio as gio
IFL_TEMPLATE = 'http://www.google.com/search?&sourceid=navclient&btnI=I&q=%s' IFL_TEMPLATE = "http://www.google.com/search?&sourceid=navclient&btnI=I&q=%s"
class DesktopLauncherSkill(MycroftSkill): class DesktopLauncherSkill(MycroftSkill):
@ -45,55 +45,77 @@ class DesktopLauncherSkill(MycroftSkill):
else: else:
self.appmap[name] = entry self.appmap[name] = entry
self.register_vocabulary(name, 'Application') self.register_vocabulary(name, "Application")
if name != tokenized_name: if name != tokenized_name:
self.register_vocabulary(tokenized_name, 'Application') self.register_vocabulary(tokenized_name, "Application")
if tokenized_name in self.appmap: if tokenized_name in self.appmap:
self.appmap[tokenized_name] += entry self.appmap[tokenized_name] += entry
else: else:
self.appmap[tokenized_name] = entry self.appmap[tokenized_name] = entry
@intent_handler(IntentBuilder('LaunchDesktopApplicationIntent') @intent_handler(
.require('LaunchKeyword') IntentBuilder("LaunchDesktopApplicationIntent")
.require('Application')) .require("LaunchKeyword")
.require("Application")
)
def handle_launch_desktop_app(self, message): def handle_launch_desktop_app(self, message):
"""Launch a dektop application using Desktop file.""" """Launch a dektop application using Desktop file."""
app_name = message.data.get('Application') app_name = message.data.get("Application")
apps = self.appmap.get(app_name) apps = self.appmap.get(app_name)
if apps and len(apps) > 0: if apps and len(apps) > 0:
self.log.info('Launching {}'.format(app_name)) self.log.info("Launching {}".format(app_name))
apps[0].launch() apps[0].launch()
@intent_handler(IntentBuilder('CloseDesktopApplicationIntent') @intent_handler(
.require('CloseKeyword').require('Application')) IntentBuilder("CloseDesktopApplicationIntent")
.require("CloseKeyword")
.require("Application")
)
def handle_close_desktop_app(self, message): def handle_close_desktop_app(self, message):
"""Close application using killall -9.""" """ Close application using killall """
app_name = message.data.get('Application') app_name = message.data.get("Application")
self.log.info('Killing {}'.format(app_name)) if not self.kill_process_by_name(app_name):
if subprocess.call(['killall', '-9', app_name]): self.log.info(
# Couldn't be killed try to get executable from desktop file "Couldn't kill {} try to get executable from desktop file".format(
app_name
)
)
apps = self.appmap.get(app_name) apps = self.appmap.get(app_name)
# Try to find executable name
if apps: if apps:
app_name = apps[0].get_string('Exec') app_name = apps[0].get_string("Exec")
subprocess.call(['killall', '-9', app_name]) if not self.kill_process_by_name(app_name):
self.speak("failed to close {}".format(app_name))
@intent_handler(IntentBuilder('LaunchWebsiteIntent') def kill_process_by_name(self, process_name):
.require('LaunchKeyword').require('Website')) """ Kill a process using the least powerful signal that works """
self.log.info("Killing {}".format(app_name))
for signal in ["SIGINT", "SIGQUIT", "SIGTERM", "SIGKILL"]:
if subprocess.call(["killall", "-s", signal, app_name]) == 0:
self.log.info("Killed {} using {}".format(app_name, signal))
return True
return False
@intent_handler(
IntentBuilder("LaunchWebsiteIntent").require("LaunchKeyword").require("Website")
)
def handle_launch_website(self, message): def handle_launch_website(self, message):
"""Open a website in the selected webbrowser.""" """Open a website in the selected webbrowser."""
site = message.data.get("Website") site = message.data.get("Website")
webbrowser.open(IFL_TEMPLATE % (quote(site))) webbrowser.open(IFL_TEMPLATE % (quote(site)))
@intent_handler(IntentBuilder('SearchWebsiteIntent') @intent_handler(
.require('SearchKeyword').require('Website') IntentBuilder("SearchWebsiteIntent")
.require('SearchTerms')) .require("SearchKeyword")
.require("Website")
.require("SearchTerms")
)
def handle_search_website(self, message): def handle_search_website(self, message):
"""Open a webbrowser searching for query.""" """Open a webbrowser searching for query."""
site = message.data.get('Website') site = message.data.get("Website")
search_terms = message.data.get('SearchTerms') search_terms = message.data.get("SearchTerms")
search_str = '{} {}'.format(site, search_terms) search_str = "{} {}".format(site, search_terms)
webbrowser.open(IFL_TEMPLATE % (quote(search_str))) webbrowser.open(IFL_TEMPLATE % (quote(search_str)))