Re: [Deskbar] how to return multiple matches from query





2007/4/18, John Russell <jjrussell gmail com>:
Sure.  Attached. Please don't make fun of my Python.  I'm still having
trouble with mixed tabs and spaces.

Hi,

CCing deskbar-applet list (I hope you intended too :-D)

You should use " self.name" instead of "text" in your match objects get_hash() and action() methods. The "text" argument is the search string as typed by the user. self.name is the actual prog you found by globbing. It looks like it works here now.

Modified version attached.

Cheers, and good luck!
Mikkel
 

On 4/18/07, Mikkel Kamstrup Erlandsen <mikkel kamstrup gmail com> wrote:
> 2007/4/18, John Russell <jjrussell gmail com >:
>
> > I'm writing a handler that searches the path for the query string.
> > Its based on the existing program one except I want it to show partial
> > matches.  I've got the logic working except deskbar only shows the
> > first match in my list of match objects returned from the query method
> > in the Handler.
> >
> > Is there some setting that needs setting for it to display multiple
> matches?
>
> Can you post the source? It should work without any effort if you just
> return a list of Match objects.
>
> Cheers,
> Mikkel
>
>
>


#!/usr/bin/env python

#
#  path-glob.py : A partial match handler for programs in the path
#
#  Copyright (C) 2007 by John Russell
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
# 
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
# 
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# 
#  Authors: John Russell <jjrussell gmail com>
#
# 0.1 : Initial release.


# Enable this to see what goes wrong when evaluating the string.
# Since half-written strings get evaluated, this produces a lot
# of crap as well as the answer you want.

import os
from os.path import join, isfile, abspath, splitext, expanduser, exists, isdir, basename

from gettext import gettext as _
import gnomevfs
from deskbar.defs import VERSION
import deskbar, deskbar.Indexer, deskbar.Utils
import deskbar.gnomedesktop
from deskbar.Handler import Handler
from deskbar.Match import Match
import gtk, gtk.gdk
from glob import glob
from deskbar.Utils import spawn_async

_debug = True

if _debug:
    import traceback
HANDLERS = {
    "PathGlobHandler" : {
    "name" : _("PathGlob"), 
    "description" : _("Provide partial completions for items in the path"), 
    "version" : "0.1", 
    }
}

EXACT_MATCH_PRIO = 100
EXACT_WORD_PRIO = 50

class PathGlobMatch(deskbar.Match.Match):
    def __init__(self, backend, name=None, use_terminal=False, priority=0, **args):
        deskbar.Match.Match.__init__(self, backend, name=name, **args)
        self.use_terminal = use_terminal
        self._priority = EXACT_MATCH_PRIO
        
    def set_with_terminal(self, terminal):
        self.use_terminal = terminal
        
    def get_hash(self, text=None):
        print "getting hash, text is " + text
        if not self.use_terminal:
            return self.name
        else:
            return (self.name, True)
        
    def action(self, text=None):
        if self.use_terminal:
            try:
                prog = subprocess.Popen(
                    self.name.split(" "),
                    stdout=subprocess.PIPE,
                    stderr=subprocess.STDOUT)
                
                zenity = subprocess.Popen(
                    ["zenity", "--title="+self.name,
                        "--window-icon="+join(deskbar.ART_DATA_DIR, "generic.png"),
                        "--width=700",
                        "--height=500",
                        "--text-info"],
                    stdin=prog.stdout)
    
                # Reap the processes when they have done
                gobject.child_watch_add(zenity.pid, lambda pid, code: None)
                gobject.child_watch_add(prog.pid, lambda pid, code: None)
                return
            except:
                #No zenity, get out of the if, and launch without GUI
                pass
        
        spawn_async(text.split(" "))            
    
    def get_category(self):
        return "actions"
    
    def get_verb(self):
        #return _("Execute %s") % "<b>%(name)s</b>"
        return _("Execute <b>%(name)s</b>")

class PathGlobHandler(Handler):
    def __init__(self):
        Handler.__init__(self, gtk.STOCK_EXECUTE)
    
    def initialize(self):
        print "we are initialized"
    
    def query(self, query, num_of_results=100):
        print "querying for " + query
        results = []
        matches = is_program_glob_in_path(query)
        print "==========++> we found some matches: " + str(len(matches))
        print matches
        for match in matches:
             results.append(PathGlobMatch(self, match))
        return results

PATH = [path for path in os.getenv("PATH").split(os.path.pathsep) if path.strip() != "" and exists(path) and isdir(path)]
def is_program_glob_in_path(program):
    results = []
    if len(program) > 1:
        print 1
        for path in PATH:
            prog_path = join(path, program)
            print "globbing for " + prog_path + "*"
            possible_matches = glob(prog_path + "*")
            for possible in possible_matches:
                if exists(possible) and isfile(possible) and os.access(possible, os.F_OK | os.R_OK | os.X_OK):
                    print "==========>found a patch: " + possible
                    results.append(basename(possible))
    
    return results


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]