Skip to content

Commit

Permalink
v0.0.2 - Added sorting by usage
Browse files Browse the repository at this point in the history
  • Loading branch information
noam09 committed Sep 5, 2018
1 parent b54b422 commit b0027ed
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*tmp*
*.gvd*
*.gvd*
usage.json
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# ulauncher-remmina

🖥 Ulauncher extension for quick access to [Remmina](https://remmina.org) profiles.
🖥 [Ulauncher](https://ulauncher.io) extension for quick access to [Remmina](https://remmina.org) profiles.

You can enter several queries to match strings in profiles' descriptions (e.g. `r ssh stan`)

Expand Down
58 changes: 54 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
import os
import json
import logging
import distutils.spawn
from ulauncher.api.client.Extension import Extension
from ulauncher.api.client.EventListener import EventListener
from ulauncher.api.shared.event import KeywordQueryEvent
from ulauncher.api.shared.event import KeywordQueryEvent, ItemEnterEvent
from ulauncher.api.shared.item.ExtensionResultItem import ExtensionResultItem
from ulauncher.api.shared.item.SmallResultItem import SmallResultItem
from ulauncher.api.shared.action.RenderResultListAction import RenderResultListAction
from ulauncher.api.shared.action.RunScriptAction import RunScriptAction
from ulauncher.api.shared.action.ExtensionCustomAction import ExtensionCustomAction

logging.basicConfig()
logger = logging.getLogger(__name__)

global usage_cache
usage_cache = {}

# Usage tracking
script_directory = os.path.dirname(os.path.realpath(__file__))
usage_db = os.path.join(script_directory, 'usage.json')
if os.path.exists(usage_db):
with open(usage_db, 'r') as db:
# Read JSON string
raw = db.read()
# JSON to dict
usage_cache = json.loads(raw)

# Initialize items cache and Remmina profiles path
remmina_bin = ''
# Locate Remmina profiles and binary
Expand All @@ -32,6 +47,7 @@ def __init__(self):

super(RemminaExtension, self).__init__()
self.subscribe(KeywordQueryEvent, KeywordQueryEventListener())
self.subscribe(ItemEnterEvent, ItemEnterEventListener())

def list_profiles(self, query):
profiles = []
Expand Down Expand Up @@ -59,6 +75,7 @@ def list_profiles(self, query):
if (query in base.lower()) or all(x in desc for x in keywords):
items_cache.append(create_item(title, proto, p, desc, p))

items_cache = sorted(items_cache, key=sort_by_usage, reverse=True)
return items_cache


Expand All @@ -69,16 +86,49 @@ def on_event(self, event, extension):
# Display all items when query empty
profiles_list = extension.list_profiles(term)

return RenderResultListAction(profiles_list)
return RenderResultListAction(profiles_list[:8])


class ItemEnterEventListener(EventListener):
def on_event(self, event, extension):
global usage_cache
# Get query
data = event.get_data()
on_enter = data['id']
# The profilefile name is the ID
base = os.path.basename(on_enter)
b = os.path.splitext(base)[0]
# Check usage and increment
if b in usage_cache:
usage_cache[b] = usage_cache[b]+1
else:
usage_cache[b] = 1
# Update usage JSON
with open(usage_db, 'w') as db:
db.write(json.dumps(usage_cache, indent=2))

return RunScriptAction('#!/usr/bin/env bash\n{} -c {}\n'.format(remmina_bin, on_enter), None).run()


def create_item(name, icon, keyword, description, on_enter):
return ExtensionResultItem(
name=name,
description=description,
icon='images/{}.svg'.format(icon),
on_enter=RunScriptAction('#!/usr/bin/env bash\n{} -c {}\n'.format(remmina_bin, on_enter), None)
)
on_enter=ExtensionCustomAction(
{'id': on_enter})
)


def sort_by_usage(i):
global usage_cache
# Convert item name to ID format
j = i._name.lower()
# Return score according to usage
if j in usage_cache:
return usage_cache[j]
# Default is 0 (no usage rank / unused)
return 0


def profile_details(profile_path):
Expand Down

0 comments on commit b0027ed

Please sign in to comment.