__init__.py :  » Network » Grail-Internet-Browser » grail-0.6 » bookmarks » search » 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 » Network » Grail Internet Browser 
Grail Internet Browser » grail 0.6 » bookmarks » search » __init__.py
"""Top-level interface to the search mechanisms."""

__version__ = '$Revision: 1.3 $'

import bookmarks.nodes
import copy


def get_editor(which, frame, options=None):
    klass = __get_component_class(which, "Editor")
    if klass is not None:
        if options is None:
            return klass(frame)
        else:
            return klass(frame, options)


def get_matcher(which, options):
    klass = __get_component_class(which, "Matcher")
    if klass is not None:
        return klass(options)


def find_nodes(folder, matcher, copynodes=1):
    # determine if folder matches,
    # then check to see what children match,
    # calling find_nodes() recursively on
    # nested folders
    if not hasattr(matcher, "match_Folder"):
        return None
    match, recurse = matcher.match_Folder(folder)
    children = []
    if recurse:
        for child in folder.children():
            nodetype = child.get_nodetype()
            if nodetype == "Folder":
                node = find_nodes(child, matcher)
                if node is not None:
                    children.append(node)
            elif hasattr(matcher, "match_" + nodetype):
                method = getattr(matcher, "match_" + nodetype)
                if method(child):
                    if copynodes:
                        children.append(copy.copy(child))
                    else:
                        # This approach is probably broken....
                        children.append(bookmarks.nodes.Alias(child))
    if match or children:
        folder = copy.copy(folder)
        folder.set_children(children)
        if children:
            folder.expand()
        else:
            folder.collapse()
        return folder


# Interfaces that need to be implemented in the specific search modules.
# These classes are defined primarily for documentation purposes; read
# the docstrings!

class EditorInterface:

    def __init__(self, frame, options=None):
        self.__options = options

    def get_options(self):
        """Return the current options object."""
        return self.__options


class MatcherInterface:

    def __init__(self, options):
        pass

    def match_Bookmark(self, bookmark):
        """Determine if a bookmark matches the search specification.

        Returns true iff the bookmark matches the search specification.
        """
        return 0

    def match_Folder(self, folder):
        """Determine if a folder matches the search specification.

        Returns two values: (`match', `recurse'), where `match' indicates
        whether the folder node itself matches, and `recurse' indicates
        whether a recursive search of the children should be performed.
        The default result fails to match the node but does continue to
        search the children.
        """
        return 0, 1

    def match_SomeNodeType(self, somenodetype):
        return 0


# Internal helpers:

def __get_search_module(which):
    d = {}
    s = "from bookmarks.search import %sSearch" % which
    try:
        exec s in d
    except ImportError:
        return None
    try:
        return d[which + "Search"]
    except KeyError:
        return None


def __get_component_class(which, name):
    m = __get_search_module(which)
    if m is not None:
        try:
            return getattr(m, which + name)
        except AttributeError:
            return None
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.