search.py :  » IDE » PIDA » pida-0.6beta3 » pida-plugins » filesearch » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » IDE » PIDA 
PIDA » pida 0.6beta3 » pida plugins » filesearch » search.py
# -*- coding: utf-8 -*- 
"""
    filesearch.search
    ~~~~~~~~~~~~~~~~~

    :copyright: 2007 by Benjamin Wiegand.
    :license: GNU GPL, see LICENSE for more details.
"""

import cgi

from os import walk,path

from pida.services.filemanager.filemanager import state_style,state_text

from filters import filter_list


class SearchMatch(object):
    """
    Symbolizes a file that matched all search filters.
    You can add it directly to a ``kiwi.ObjectList`` object.
    """
    def __init__(self, dirpath, name):
        self.state = 'normal'
        self.name = name
        self.path = dirpath
        self.visible = False
        self.extension = path.splitext(self.name)[-1]
        self.icon_stock_id = self.get_icon_stock_id()

    def __repr__(self):
        return '<SearchMatch "%s">' % path.join(self.path, self.name)

    @property
    def markup(self):
        return self.format(cgi.escape(self.name))

    def get_icon_stock_id(self):
        #TODO: get a real mimetype icon
        return 'text-x-generic'

    @property
    def state_markup(self):
        text = state_text.get(self.state, ' ')
        wrap = '<span weight="ultrabold"><tt>%s</tt></span>'
        return wrap % self.format(text)

    def format(self, text):
        color, b, i = state_style.get(self.state, ('black', False, False))
        if b:
            text = '<b>%s</b>' % text
        if i:
            text = '<i>%s</i>' % text
        return '<span color="%s">%s</span>' % (color, text)


def get_filters():
    """
    Returns the a tuple of the filter's description and the filters itself.
    """
    return [(f.description, f) for f in filter_list]

def do_search(folder, filters, exclude_hidden=True, exclude_vcs=True):
    """
    Test all ``filters`` on ``folder``'s content recursively.
    If a file matches all filters a ``SearchMatch`` object is yielded.
    """
    for dirpath, dirnames, filenames in walk(folder):
        def _get_hidden(dirnames):
            """
            Return the directories that shouldn't be shown.
            """
            hidden = []
            for dirname in dirnames:
                if dirname.startswith('.'):
                    hidden.append(dirname)
            return hidden

        if exclude_hidden:
            # remove the hidden folders of ``dirnames`` that they don't get
            # crawled
            for dirname in _get_hidden(dirnames):
                # XXX: Check whether removing using .pop with index is faster
                dirnames.remove(dirname)

        for file_name in filenames:
            fpath = path.join(dirpath, file_name)
            errors = False

            if not path.exists(fpath):
                # this may happen if there's a invalid symlink
                continue

            for f in filters:
                if not f.check(fpath):
                    # file doesn't matches filter
                    errors = True
                    break

            if not errors:
                # file did match all filters
                yield dirpath, file_name
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.