Begin add run app from desktop entries
This commit is contained in:
parent
74c45b61bc
commit
b8ab8d254f
5 changed files with 85 additions and 0 deletions
|
@ -6,6 +6,7 @@
|
|||
- brains/neurotransmitter.yml
|
||||
- brains/kill.yml
|
||||
- brains/search.yml
|
||||
- brains/run.yml
|
||||
|
||||
- name: "order-not-found-synapse"
|
||||
signals: []
|
||||
|
|
9
brains/run.yml
Normal file
9
brains/run.yml
Normal file
|
@ -0,0 +1,9 @@
|
|||
- name: "run-app-fr"
|
||||
signals:
|
||||
- order: "lance {{ app }}"
|
||||
- order: "ouvre {{ app }}"
|
||||
neurons:
|
||||
- run_app:
|
||||
app: "{{ app }}"
|
||||
found_message: "Lancement de l'application {{ app }}"
|
||||
not_found_message: "Impossible de trouver l'application {{ app }}"
|
1
resources/neurons/run_app/.gitignore
vendored
Normal file
1
resources/neurons/run_app/.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
__pycache__
|
1
resources/neurons/run_app/__init__.py
Normal file
1
resources/neurons/run_app/__init__.py
Normal file
|
@ -0,0 +1 @@
|
|||
from .run_app import Run_app
|
73
resources/neurons/run_app/run_app.py
Normal file
73
resources/neurons/run_app/run_app.py
Normal file
|
@ -0,0 +1,73 @@
|
|||
import subprocess
|
||||
import os
|
||||
|
||||
from kalliope.core import NeuronModule
|
||||
from kalliope.core.NeuronModule import MissingParameterException, MissingParameterException
|
||||
|
||||
home_dir = os.path.expanduser('~')
|
||||
|
||||
entries_dirs = [
|
||||
os.path.join(home_dir, ".local/share/applications"),
|
||||
"/usr/local/share/applications",
|
||||
"/usr/share/applications"
|
||||
]
|
||||
|
||||
def parse_exec(line):
|
||||
splitted_line = line.split("=")
|
||||
if len(splitted_line) < 2:
|
||||
return None
|
||||
else:
|
||||
return splitted_line[1]
|
||||
|
||||
def check_file(file_path, query):
|
||||
exec = None # Equals to Exec command of .desktop if is before search terms
|
||||
found = False
|
||||
for line in open(file_path, 'r').readlines():
|
||||
if "exec" in line.lower():
|
||||
exec = parse_exec(line)
|
||||
if query.lower() in line.lower():
|
||||
found = True
|
||||
if exec is not None and found is True:
|
||||
return exec
|
||||
return None
|
||||
|
||||
def search_app_name_in_desktop_entries(query: str):
|
||||
for d in entries_dirs:
|
||||
if os.path.exists(d):
|
||||
for f in os.listdir(d):
|
||||
if ".desktop" in f:
|
||||
result = check_file(os.path.join(d, f), query)
|
||||
if result is not None:
|
||||
return result
|
||||
return None
|
||||
|
||||
class Run_app(NeuronModule):
|
||||
def __init__(self, **kwargs):
|
||||
NeuronModule.__init__(self, **kwargs)
|
||||
|
||||
self.query = kwargs.get('app', None)
|
||||
self.found_message = kwargs.get('found_message', None)
|
||||
self.not_found_message = kwargs.get('not_found_message', None)
|
||||
|
||||
if self._is_parameters_ok():
|
||||
result = search_app_name_in_desktop_entries(self.query)
|
||||
if result is not None:
|
||||
subprocess.Popen(result, shell=True)
|
||||
self.say(self.found_message)
|
||||
else:
|
||||
self.say(self.not_found_message)
|
||||
|
||||
def _is_parameters_ok(self):
|
||||
"""
|
||||
Check if received parameters are ok to perform operations in the neuron
|
||||
:return: true if parameters are ok, raise an exception otherwise
|
||||
.. raises:: MissingParameterException
|
||||
"""
|
||||
if self.query is None:
|
||||
raise MissingParameterException("Query parameter is missing.")
|
||||
if self.found_message is None:
|
||||
raise MissingParameterException("Found message parameter is missing.")
|
||||
if self.not_found_message is None:
|
||||
raise MissingParameterException("Not found message parameter is missing.")
|
||||
|
||||
return True
|
Loading…
Add table
Reference in a new issue