Skip to content

Commit

Permalink
Added auto db discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
bantya committed Jan 17, 2019
1 parent 3886017 commit 46aeb49
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 25 deletions.
33 changes: 33 additions & 0 deletions src/db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import sqlite3
from sqlite3 import Error

def create_connection(db_file):
""" create a database connection to the SQLite database
specified by the db_file
:param db_file: database file
:return: Connection object or None
"""
try:
conn = sqlite3.connect(db_file)
return conn
except Error as e:
print(e)

return None

def get_names(db_file, term):
"""
Query all rows in the tasks table
:param conn: the Connection object
:return:
"""
conn = create_connection(db_file)
cur = conn.cursor()
# cur.execute('SELECT name, type FROM searchIndex LIMIT 50')
cur.execute('SELECT name FROM searchIndex WHERE name LIKE ? LIMIT 50', (term + '%',))
# query = 'SELECT name FROM searchIndex WHERE name LIKE ' + term + '%'
# cur.execute(query)
# print(query)

# return cur.fetchone()
return cur.fetchall()
8 changes: 7 additions & 1 deletion src/zealous.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Zoaler Package configuration file
# Zealous Package configuration file
# More info at http://keypirinha.com
#

Expand All @@ -13,9 +13,15 @@

[main]
# Plugin's main configuration section
# path = "C:\Program Files\Zeal"
# docset_path = "C:\Users\thAKare\AppData\Local\Zeal\Zeal\docsets"

[docs]
# The zeal docs list
# e.g.
# keyword = zeal doc name
# ap = apache
# ng = nginx

[var]
# As in every Keypirinha's configuration file, you may optionally include a
Expand Down
123 changes: 99 additions & 24 deletions src/zealous.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
# Keypirinha: a fast launcher for Windows (keypirinha.com)

import re
import os
import json
import plistlib
import subprocess
import keypirinha as kp
import keypirinha_util as kpu
from .db import get_names

class Zealous(kp.Plugin):
SECTION_DOCS = 'docs'

SECTION_MAIN = 'main'

PLIST_FILE = 'Contents\\Info.plist'

DB_FILE = 'Contents\\Resources\\docSet.dsidx'

REGEX_INPUT = r'(\S+)\s(.+)'

ITEM_CAT = kp.ItemCategory.USER_BASE + 1
Expand All @@ -20,13 +25,39 @@ def __init__(self):
self._debug = True

def on_start(self):
self._load_settings()
self._gather_docs()

package_path = self.get_package_cache_path(True)
self.docsets_json = os.path.join(package_path, 'docsets.json')

default_docset_path = kp.package_cache_dir().split('Keypirinha\\Packages')[0] + 'Zeal\\Zeal\\docsets'
self.docsets_path = self.settings.get_stripped('docset_path', self.SECTION_MAIN, default_docset_path)

json_data = self._read_json()

for key in self.docs:
docset = self.settings.get_stripped(key, self.SECTION_DOCS)
if docset not in json_data.keys():
for folder_name in os.listdir(self.docsets_path):
plist_path = os.path.join(self.docsets_path, folder_name, self.PLIST_FILE)
if os.path.isfile(plist_path):
with open(plist_path, 'rb') as fp:
pl = plistlib.load(fp)
if docset == pl["DocSetPlatformFamily"]:
self._save_docsets(docset, folder_name.split('.docset')[0])

def on_catalog(self):
self.on_start()

def on_suggest(self, user_input, items_chain):
def on_activated(self):
print('----------')

def on_deactivated(self):
print('----------')

def on_suggest(self, user_input, items_chain):
suggestions = []
input = re.search(self.REGEX_INPUT, user_input)

if input is None:
Expand All @@ -35,22 +66,69 @@ def on_suggest(self, user_input, items_chain):
for docid in self.docs:
idx, term = input.groups()
if docid == idx:
doc = self._get_setting(docid, self.SECTION_DOCS)
suggestion = self._set_suggestion(docid, doc, term)
docset = self.settings.get_stripped(docid, self.SECTION_DOCS)
docset_folder = self._get_docset_folder(docset)
db_path = self._get_docset_path(docset_folder, self.DB_FILE)

suggestions.append(self._set_suggestion(docid, docset, term))

dicta = self._get_entity_names(db_path, term)

for name in dicta:
name = ''.join(name)
suggestions.append(self._set_suggestion(docid, docset, name))

self.set_suggestions(suggestions)

def _get_docset_folder(self, key):
data = self._read_json()

if key in data.keys():
return data[key]

self.set_suggestions(suggestion)
def _save_docsets(self, docset, docset_path):
data = self._read_json()

data.update({docset: docset_path})

self._save_json(data)

def _save_json(self, data):
with open(self.docsets_json, 'w') as outfile:
json.dump(data, outfile)

def _read_json(self):
if not os.path.isfile(self.docsets_json):
self._save_json({})

return {}

try:
with open(self.docsets_json) as json_file:
return json.load(json_file)
except json.decoder.JSONDecodeError:
self._save_json({})

return {}

def _get_docset_path(self, docset, docfile):
if not docset.endswith(".docset"):
docset += '.docset'

return os.path.join(self.docsets_path, docset, docfile)

def _get_entity_names(self, doc, term):
return get_names(doc, term)

def _set_suggestion(self, docid, doc, term):
return [
self.create_item(
category = self.ITEM_CAT,
label = docid + ' : Search ' + doc + ' for ' + term,
short_desc = doc + ':' + term,
target = doc + ':' + term,
args_hint = kp.ItemArgsHint.FORBIDDEN,
hit_hint = kp.ItemHitHint.IGNORE
)
]
return self.create_item(
category = self.ITEM_CAT,
label = docid + ' : ' + term,
short_desc = doc + ':' + term,
target = doc + ':' + term,
args_hint = kp.ItemArgsHint.FORBIDDEN,
hit_hint = kp.ItemHitHint.IGNORE
)

def _set_error(self, msg):
return [
Expand All @@ -65,7 +143,8 @@ def on_execute(self, item, action):
if item.category() != self.ITEM_CAT:
return

zeal_exe = self._get_setting('path', self.SECTION_MAIN)
zeal_exe = self.settings.get_stripped('path', self.SECTION_MAIN, 'C:\\Program Files\\Zeal')

if not 'zeal.exe' in zeal_exe:
zeal_exe = os.path.join(zeal_exe, 'zeal.exe')

Expand All @@ -77,17 +156,13 @@ def on_execute(self, item, action):
except Exception as e:
self.dbg("Zeal - (%s)" % (e))
else:
self.err('Could not find your %s executable.\n\nPlease edit Zeal.sublime-settings' % (zeal_exe))
self.err('Could not find your %s executable.\n\nPlease edit path' % (zeal_exe))

def _gather_docs(self):
self.docs = self.load_settings().keys(self.SECTION_DOCS)

def _get_setting(self, setting, section):
return self.load_settings().get_stripped(
setting,
section=section,
fallback=section
)
def _load_settings(self):
self.settings = self.load_settings()

def on_events(self, flags):
if flags & kp.Events.PACKCONFIG:
Expand Down

0 comments on commit 46aeb49

Please sign in to comment.