#! /usr/bin/env python # -*- coding: utf-8 -*- __author__="Thibauld Nion" __license__ = """BSD Copyright (c) 2009, Thibauld Nion All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ __version__ = "0.2" from gettext import gettext as _ import os HANDLERS = ["OpenLocationModule"] import deskbar.interfaces.Action class OpenLocationAction(deskbar.interfaces.Action): """ Open the location described by a path. """ def __init__(self, path): deskbar.interfaces.Action.__init__(self, path) self._path = path def activate(self, text=None): deskbar.core.Utils.spawn_async( ["/usr/bin/xdg-open", self._path] ) def get_verb(self): return _("Open") + " %(name)s" def get_icon(self): return "gtk-open" def is_valid(self, text=None): return (os.path.isdir(self._path) and os.access(self._path, os.F_OK | os.R_OK | os.X_OK)) import deskbar.interfaces.Match class OpenLocationMatch (deskbar.interfaces.Match): """ Description of a match corresponding to the OpenLocationAction """ def __init__(self, path, **kwargs): deskbar.interfaces.Match.__init__(self, name=path, icon="gtk-open", category="places", **kwargs) self._path = path self.add_action( OpenLocationAction(self._path) ) def get_hash(self): return self._path import deskbar.core.Utils import deskbar.interfaces.Module class OpenLocationModule (deskbar.interfaces.Module): """ Process queries to try to find matching directories """ INFOS = {'icon': deskbar.core.Utils.load_icon("gtk-open"), 'name': _('Open location'), 'description': _('Open a directory corresponding to the given path, trying to also to find matches for incomplete paths.'), 'version': __version__, } def __init__(self): deskbar.interfaces.Module.__init__(self) def query(self, text): """ When the query looks like a directory path offer to open it. If the query does not correspond to a directory, tries to see if the path matches the beingin of existing directories' paths and offer to open those "completed" directories. """ match_list = [] # make sure we correctly expand '~' if present dir_path = os.path.expanduser(text) if os.path.isdir(dir_path) and os.access(dir_path, os.F_OK | os.R_OK | os.X_OK): match_list.append(OpenLocationMatch(dir_path)) else: # if path does no exists, try to find matching directories parent_dir_path = os.path.dirname(dir_path) base = os.path.basename(dir_path) if os.path.isdir(parent_dir_path) and os.access(parent_dir_path, os.F_OK | os.R_OK | os.X_OK): for f in os.listdir(parent_dir_path): if f.startswith(base): candidate_dir_path=os.path.join(parent_dir_path, f) if os.path.isdir(candidate_dir_path) and os.access(candidate_dir_path, os.F_OK | os.R_OK | os.X_OK): match_list.append(OpenLocationMatch(candidate_dir_path)) if len(match_list)>0: self._emit_query_ready( text, match_list ) # for test purpose if __name__ == "__main__": import sys cm = OpenLocationModule() result = cm.query("~/.config") if result == None: sys.exit(1)