Check default profile paths, added option for custom path (#4)

This commit is contained in:
Noam
2019-04-16 16:38:27 +03:00
parent 41fd08e14e
commit b65e0f7716
2 changed files with 33 additions and 17 deletions

43
main.py
View File

@@ -19,7 +19,7 @@ usage_cache = {}
# Usage tracking # Usage tracking
script_directory = os.path.dirname(os.path.realpath(__file__)) script_directory = os.path.dirname(os.path.realpath(__file__))
usage_db = os.path.join(script_directory, 'usage.json') usage_db = os.path.join(script_directory, "usage.json")
if os.path.exists(usage_db): if os.path.exists(usage_db):
with open(usage_db, 'r') as db: with open(usage_db, 'r') as db:
# Read JSON string # Read JSON string
@@ -28,18 +28,23 @@ if os.path.exists(usage_db):
usage_cache = json.loads(raw) usage_cache = json.loads(raw)
# Initialize items cache and Remmina profiles path # Initialize items cache and Remmina profiles path
remmina_bin = '' remmina_bin = ""
# Locate Remmina profiles and binary # Locate Remmina profiles and binary
remmina_profiles_path = "{}/.local/share/remmina".format(os.environ.get('HOME')) default_paths = ["{}/.local/share/remmina".format(os.environ.get('HOME')),
"{}/.remmina".format(os.environ.get('HOME'))]
# remmina_profiles_path = "{}/.local/share/remmina".format(os.environ.get('HOME'))
# remmina_profiles_path_alt = "{}/.remmina".format(os.environ.get('HOME'))
remmina_bin = distutils.spawn.find_executable('remmina') remmina_bin = distutils.spawn.find_executable('remmina')
# This extension is useless without remmina # This extension is useless without remmina
if remmina_bin is None or remmina_bin == '': if remmina_bin is None or remmina_bin == "":
logger.error('Remmina executable path could not be determined') logger.error("Remmina executable path could not be determined")
exit() exit()
# Check if Remmina profiles directory exists # Check if Remmina profiles directory exists
if not os.path.isdir(remmina_profiles_path): remmina_profiles_path = None
logger.error("Remmina profiles directory doesn't exist ({})".format(remmina_profiles_path)) # Check default paths first
exit() for p in default_paths:
if os.path.isdir(p):
remmina_profiles_path = p
class RemminaExtension(Extension): class RemminaExtension(Extension):
@@ -61,8 +66,7 @@ class RemminaExtension(Extension):
temp = profiles temp = profiles
profiles = sorted(temp) profiles = sorted(temp)
except Exception as e: except Exception as e:
print('Failed getting profile files') logger.error("Failed getting Remmina profile files")
for p in profiles: for p in profiles:
base = os.path.basename(p) base = os.path.basename(p)
title, desc, proto = profile_details(p) title, desc, proto = profile_details(p)
@@ -82,11 +86,17 @@ class RemminaExtension(Extension):
class KeywordQueryEventListener(EventListener): class KeywordQueryEventListener(EventListener):
def on_event(self, event, extension): def on_event(self, event, extension):
global remmina_profiles_path
if extension.preferences["profiles"] is not "" \
or not remmina_profiles_path:
# Tilde (~) won't work alone, need expanduser()
remmina_profiles_path = os.path.expanduser(extension.preferences["profiles"])
# pref_profiles_path = extension.preferences['profiles']
logger.debug("Remmina profiles path: {}".format(remmina_profiles_path))
# Get query # Get query
term = (event.get_argument() or '').lower() term = (event.get_argument() or "").lower()
# Display all items when query empty # Display all items when query empty
profiles_list = extension.list_profiles(term) profiles_list = extension.list_profiles(term)
return RenderResultListAction(profiles_list[:8]) return RenderResultListAction(profiles_list[:8])
@@ -95,7 +105,7 @@ class ItemEnterEventListener(EventListener):
global usage_cache global usage_cache
# Get query # Get query
data = event.get_data() data = event.get_data()
on_enter = data['id'] on_enter = data["id"]
# The profilefile name is the ID # The profilefile name is the ID
base = os.path.basename(on_enter) base = os.path.basename(on_enter)
b = os.path.splitext(base)[0] b = os.path.splitext(base)[0]
@@ -107,7 +117,6 @@ class ItemEnterEventListener(EventListener):
# Update usage JSON # Update usage JSON
with open(usage_db, 'w') as db: with open(usage_db, 'w') as db:
db.write(json.dumps(usage_cache, indent=2)) db.write(json.dumps(usage_cache, indent=2))
return RunScriptAction('#!/usr/bin/env bash\n{} -c {}\n'.format(remmina_bin, on_enter), None).run() return RunScriptAction('#!/usr/bin/env bash\n{} -c {}\n'.format(remmina_bin, on_enter), None).run()
@@ -115,9 +124,9 @@ def create_item(name, icon, keyword, description, on_enter):
return ExtensionResultItem( return ExtensionResultItem(
name=name, name=name,
description=description, description=description,
icon='images/{}.svg'.format(icon), icon="images/{}.svg".format(icon),
on_enter=ExtensionCustomAction( on_enter=ExtensionCustomAction(
{'id': on_enter}) {"id": on_enter})
) )
@@ -188,5 +197,5 @@ def profile_details(profile_path):
return "", "", "rdp" return "", "", "rdp"
if __name__ == '__main__': if __name__ == "__main__":
RemminaExtension().run() RemminaExtension().run()

View File

@@ -15,6 +15,13 @@
"name": "Remmina", "name": "Remmina",
"description": "Quick access to Remmina profiles", "description": "Quick access to Remmina profiles",
"default_value": "r" "default_value": "r"
},
{
"id": "profiles",
"type": "input",
"name": "Remmina Profiles Directory",
"description": "Where Remmina stores profile configuration files <br /><code>~/.local/share/remmina</code> and <code>~/.remmina</code> are checked by default",
"default_value": ""
} }
] ]
} }