Skip to content

Instantly share code, notes, and snippets.

@EhsanKia
Created September 27, 2016 04:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EhsanKia/8883ee53b865e842390c4d13e51478f7 to your computer and use it in GitHub Desktop.
Save EhsanKia/8883ee53b865e842390c4d13e51478f7 to your computer and use it in GitHub Desktop.
Keypirinha Plugin to replicate Launchy's database format
#
# Launchy Package configuration file
#
[main]
# Plugin's main configuration section.
# (Nothing here for now)
[directories]
# This is where you specify the directories you want to index
# You can copy the [directories] configuration found in your launchy.ini
# right in this section and it will work as expected.
#
# You can also manually modify and add your own configuration.
# Each directory configuration has the following parameters:
# name: The path of the root directory to scan (Required)
# types: Comma separated list of file types to match (default: *.*)
# depth: How deep to scan in subfolders (default: 0)
# indexdirs: Wether we should index directories (default: false)
#
# Each entry needs to be numbered incrementally, and size needs to be provided.
# Here is an example configuration with two items:
#
# 1\name=F:\\
# 1\indexDirs=true
# 1\indexExes=false
# 1\depth=1
# 2\name=C:\\Documents\\Shortcuts
# 2\types=*.lnk, *.py
# 2\indexDirs=false
# 2\indexExes=true
# 2\depth=0
# size=2
#
# Whenever this configuration updated, you need to manually update your catalog.
# Currently, there is no support for the "indexexes" option. By default, any file
# matched by the types pattern will be indexed, executable or not.
[var]
# As in every Keypirinha's configuration file, you may optionally include a
# [var] section to declare variables that you want to reuse anywhere else in
# this file.
#
# Note that the [var] section is inherited, which means that any value defined
# in the main configuration file of the application (i.e.: "Keypirinha.ini") has
# already been made available to this file as well so you do not need to
# duplicate it here unless you want to override it.
#
# REMINDER: For convenience, Keypirinha silently populates this section with
# predefined values that may come handy. Here are some of them: APP_DIR,
# APP_EXE, PROFILE_DIR, PROFILE_DIR_INSTALLED_PACKS, PROFILE_DIR_LIVE_PACKS,
# PROFILE_DIR_USER and the KNOWNFOLDER_* and KNOWNFOLDERGUID_* values.
#
# See the "Configuration" chapter of the documentation for more information.
[env]
# For convenience, Keypirinha populates this [env] section in every loaded
# configuration file so you can easily access to environment variables like
# PATH for example from this file using syntax: ${env:PATH}
#
# If an environment variable happens to be changed while Keypirinha is running
# and this modification impacts current configuration, application and packages
# configuration will be reloaded if needed only.
#
# See the "Configuration" chapter of the documentation for more information.
# Keypirinha launcher (keypirinha.com)
import keypirinha_util as kpu
import keypirinha as kp
import os
class Launchy(kp.Plugin):
"""
Populate catalog using Launchy configuration format.
This plugin allows you to populate your catalog the same way you would
in Launchy. You can simply copy your configuration over and this plugin
will be able to parse and replicate the same list as in Launchy.
"""
def __init__(self):
super().__init__()
def _update_config(self):
self.dir_configs = []
settings = self.load_settings()
size = settings.get_int('size', 'directories')
for i in range(size):
k = str(i + 1)
self.dir_configs.append({
'name': settings.get_stripped(k + '\\name', 'directories'),
'types': settings.get_stripped(k + '\\types', 'directories', fallback='*'),
'depth': settings.get_int(k + '\\depth', 'directories', fallback=0),
'indexdirs': settings.get_bool(k + '\\indexdirs', 'directories', fallback=False),
})
self.settings = settings
def _load_dir(self, config):
if config['name'] is None:
return
root_path = os.path.expandvars(config['name'].replace('\\\\', '\\'))
if not os.path.exists(root_path):
return
paths = []
for glob in config['types'].split(','):
if glob.strip() == '@Invalid()':
continue
self.should_terminate()
files = kpu.scan_directory(root_path, name_patterns=glob.strip(),
flags=kpu.ScanFlags.FILES, max_level=config['depth'])
paths.extend(files)
if config['indexdirs']:
self.should_terminate()
dirs = kpu.scan_directory(root_path, name_patterns='*',
flags=kpu.ScanFlags.DIRS, max_level=config['depth'])
paths.extend(dirs)
self.merge_catalog([
self.create_item(
category=kp.ItemCategory.FILE,
label=os.path.basename(path),
short_desc="",
target=os.path.join(root_path, path),
args_hint=kp.ItemArgsHint.ACCEPTED,
hit_hint=kp.ItemHitHint.KEEPALL)
for path in paths])
def on_start(self):
self._update_config()
def on_catalog(self):
self.set_catalog([])
for config in self.dir_configs:
self._load_dir(config)
def on_execute(self, item, action):
kpu.execute_default_action(self, item, action)
def on_events(self, flags):
if flags & kp.Events.PACKCONFIG:
self._update_config()
if self.settings.get_bool('refresh_catalog_on_config_change', 'main'):
self.on_catalog()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment