filters.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 » filters.py
# -*- coding: utf-8 -*- 
"""
    filesearch.filters
    ~~~~~~~~~~~~~~~~~~

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

import re
import gtk
import sre_constants

from glob import fnmatch
from os.path import basename

from pida.core.locale import Locale
locale = Locale('filesearch')
_ = locale.gettext

BINARY_RE = re.compile(r'[\000-\010\013\014\016-\037\200-\377]|\\x00')


class ValidationError(Exception):
    """
    An exception that is raised if the user entered invalid data into a
    filter's field.
    The search catches it and informs the user.
    """


class Filter(object):
    """
    A search filter that lowers the search result.
    """

    #: The description of the filter
    filter_desc = ''

    @staticmethod
    def get_entries():
        """
        This method should return a dictionary containing all input elements
        the filter needs.
        Example::

            return {
                'entry': gtk.Entry()
            }
        """

    def __init__(self):
        """
        The init function is called if the user added a new filter.
        It get's all input elements defined in ``get_entries`` as keyword
        arguments.
        """

    def validate(self):
        """
        This function is called before the search process starts.
        It should validate all input elements and raise a ``ValidationError``
        on error.
        """

    def check(self, path):
        """
        This function should return ``True`` if ``path`` matches the filter,
        else ``False``.
        """


class FileNameMatchesFilter(Filter):
    """
    Checks whether the file name matches a given regular expression.
    """
    description = _('Name matches')

    def __init__(self, entry):
        self.entry = entry

    def validate(self):
        pattern = self.entry.get_text()
        pattern = fnmatch.translate(pattern).rstrip('$')
        try:
            self.regex = re.compile(pattern, re.IGNORECASE)
        except sre_constants.error, e:
            raise ValidationError(_('Invalid Regex'))

    @staticmethod
    def get_entries():
        return {
            'entry': gtk.Entry()
        }

    def check(self, path):
        return bool(self.regex.search(basename(path)))


class ContentMatchesFilter(Filter):
    """
    Checks whether the file content matches a given regular expression.
    """
    description = _('Content matches')

    def __init__(self, entry):
        self.entry = entry

    def validate(self):
        pattern = self.entry.get_text()
        pattern = fnmatch.translate(pattern).rstrip('$')
        try:
            self.regex = re.compile(pattern, re.IGNORECASE)
        except sre_constants.error, e:
            raise ValidationError(_('Invalid Regex'))

    @staticmethod
    def get_entries():
        return {
            'entry': gtk.Entry()
        }

    def check(self, path):
        f = file(path)
        found = False

        for line in f.readlines():
            if BINARY_RE.search(line):
                # binary file, abort
                break

            if self.regex.search(line):
                found = True
                break

        f.close()
        return found


filter_list = [FileNameMatchesFilter, ContentMatchesFilter]
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.