Changeset 465

Show
Ignore:
Timestamp:
11/26/2007 07:40:47 AM (8 months ago)
Author:
athomas
Message:

pyndexter: More work on the simplification refactoring branch.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • pyndexter/branches/simplification/pyndexter/indexers/_hyperestraier.py

    r458 r465  
    4848import hyperestraier 
    4949from pyndexter import * 
     50from pyndexter import errors 
    5051from pyndexter.util import URI 
    5152 
     
    5960    uri = URI(uri) 
    6061    uri.scheme = 'http' 
     62    uri.path = 'node/' + uri.path 
    6163    return HyperestraierIndexer(uri) 
    6264 
     
    6870 
    6971        uri = URI(uri, port=1978) 
     72        scrubbed = URI(uri) 
     73        scrubbed.username = None 
     74        scrubbed.password = None 
    7075        self.db = hyperestraier.Node() 
    71         self.db.set_url(str(uri)) 
     76        self.db.set_url(str(scrubbed)) 
     77        self.db.set_auth(uri.username, uri.password) 
    7278 
    7379    def index(self, document): 
     80        uri = unicode(document.uri) 
    7481        hdoc = hyperestraier.Document() 
    75         for k, v in document.attributes.iteritems(): 
     82        for k, v in document.iteritems(): 
    7683            hdoc.add_attr(u'@' + k, v) 
    77         for line in document.content.splitlines(): 
     84        hdoc.add_attr(u'@uri', uri) 
     85        for line in document.texts: 
    7886            hdoc.add_text(line) 
    79         self.db.put_doc(hdoc, 1) 
     87        if not self.db.put_doc(hdoc): 
     88            raise errors.IndexerError('Failed to index %s' % document.uri) 
    8089 
    8190    def discard(self, uri): 
    8291        uri = unicode(uri) 
    8392        if not self.db.out_doc_by_uri(uri): 
    84             raise DocumentNotFound(uri) 
     93            raise errors.DocumentNotFound(uri) 
    8594 
    8695    def fetch(self, uri): 
    8796        uri = unicode(uri) 
    8897        doc = self.db.get_doc_by_uri(uri) 
     98        if not doc: 
     99            raise errors.DocumentNotFound(uri) 
    89100        attributes = self._translate_attributes(doc) 
    90101        return Document(uri, texts=doc.dtexts, quality=0.99, 
     
    98109        self.db.optimize() 
    99110 
    100     def flush(self): 
    101         self.db.sync() 
    102  
    103111    def close(self): 
    104         self.db.close() 
     112        self.flush() 
    105113        self.db = None 
    106114 
  • pyndexter/branches/simplification/pyndexter/__init__.py

    r459 r465  
    1818from StringIO import StringIO 
    1919from UserDict import DictMixin 
    20 from pyndexter.errors import * 
    2120from pyndexter.util import URI 
    2221from pyndexter.query import Query 
     
    3433 
    3534__all__ = ['Document', 'Query', 'Hit', 'Indexer', 'ResultSet', 'Stemmer', 
    36            'Error', 'DocumentNotFound', 'InvalidURI', 'InvalidIndexer', 
    37            'connect'] 
     35           'connect', 'URI'] 
    3836 
    3937 
     
    4644 
    4745 
    48 class Error(Exception): 
    49     """Base of all pyndexter exceptions.""" 
    50  
    51 class DocumentNotFound(Error): 
    52     """Raised when a document could not be found, usually by the fetch() 
    53     methods.""" 
    54  
    55 class PluginError(Error): 
    56     """An error occurred in the plugin system.""" 
    57  
    58  
    5946class Stemmer(object): 
    6047    """Abstraction for a stemming algorithm.""" 
     
    8168    def __init__(self, uri, quality=1.0, attributes=None, text=None, 
    8269                 texts=None): 
    83         self.uri = uri 
     70        self.uri = URI(uri) 
    8471        self.quality = quality 
    8572        self._attributes = {} 
     
    206193    def __len__(self): 
    207194        """ Return the length of the result set. """ 
    208         raise NotImplementedError 
     195        count = 0 
     196        for i in self: 
     197            count += 1 
     198        return count 
    209199 
    210200    def __getitem__(self, index): 
    211201        """Return a Hit object for a specific index in the search result. 
    212202        Not necessarily implemented by all Indexers.""" 
    213         raise NotImplementedError 
     203        for i, value in enumerate(self): 
     204            if i == index: 
     205                return value 
     206        raise IndexError(index) 
    214207 
    215208    def __getslice__(self, i, j): 
     
    279272 
    280273class IndexerWrapper(Indexer): 
    281     """An Indexer wrapper that does type translation for end users.""" 
     274    """An Indexer wrapper that does some convenient type conversion for end 
     275    users.""" 
    282276    def __init__(self, indexer): 
    283277        Indexer.__init__(self) 
     
    296290 
    297291    def discard(self, uri): 
    298         if isinstance(uri, document): 
    299             uri = document.uri 
    300         return self.indexer.discard(URI(uri)
     292        if isinstance(uri, Document): 
     293            uri = uri.uri 
     294        return self.indexer.discard(uri
    301295 
    302296    def search(self, query): 
     
    309303 
    310304 
    311 def load_plugin(name, entry_point, plugin_paths=None): 
     305def iter_plugins(entry_point, plugin_paths=None): 
    312306    import pkg_resources 
    313307 
     
    340334 
    341335    for entry in pkg_resources.working_set.iter_entry_points(entry_point): 
    342         if entry.name == name: 
    343             try: 
    344                 _debug('Loading %s' % entry) 
    345                 factory = entry.load(require=True) 
    346                 return factory 
    347             except (ImportError, pkg_resources.DistributionNotFound, 
    348                     pkg_resources.VersionConflict, pkg_resources.UnknownExtra), e: 
    349                 _debug('Failed to load %s: %s' % (name, _format_error(entry, e))) 
    350                 raise PluginError('%s (%s)' % (name, _format_error(entry, e))) 
    351     raise PluginError('No suitable plugins found for %s' % name) 
     336        try: 
     337            _debug('Loading %s' % entry) 
     338            plugin = entry.load(require=True) 
     339            yield entry.name, plugin 
     340        except (ImportError, pkg_resources.DistributionNotFound, 
     341                pkg_resources.VersionConflict, pkg_resources.UnknownExtra), e: 
     342            _debug('Failed to load %s: %s' % (entry.name, _format_error(entry, e))) 
     343            yield None, PluginError('%s (%s)' % (entry.name, _format_error(entry, e))) 
     344 
     345 
     346def load_plugin(plugin_name, entry_point, plugin_paths=None): 
     347    for name, plugin in iter_plugins(entry_point, plugin_paths): 
     348        if name is None: 
     349            raise plugin 
     350        if name == plugin_name: 
     351            return plugin 
     352    raise PluginError('No suitable plugins found named "%s"' % name) 
    352353 
    353354 
  • pyndexter/branches/simplification/pyndexter/query.py

    r458 r465  
    99 
    1010import re 
    11 from pyndexter import Error 
     11from pyndexter.errors import InvalidQuery 
    1212 
    1313 
    1414__all__ = ['Query'] 
    15  
    16  
    17 class InvalidQuery(Error): 
    18     """Invalid query string.""" 
    1915 
    2016 
  • pyndexter/branches/simplification/pyndexter/util.py

    r457 r465  
    2525""".split() 
    2626 
     27 
    2728class URI(object): 
    2829    """Parse a URI into its component parts. The `query` component is passed 
     
    4041    The URI constructor can be passed a string: 
    4142 
    42     >>> u = URI('http://user:password@www.example.com/some/path?parm=1&parm=2&other=3#fragment') 
     43    >>> u = URI('http://user:password@www.example.com:12345/some/path?parm=1&parm=2&other=3#fragment') 
    4344    >>> u 
    44     URI(u'http://user:password@www.example.com/some/path?other=3&parm=1&parm=2#fragment') 
     45    URI(u'http://user:password@www.example.com:12345/some/path?other=3&parm=1&parm=2#fragment') 
    4546    >>> u.scheme 
    4647    'http' 
     
    6061    ...or the individual URI components as keyword arguments: 
    6162 
    62     >>> URI(scheme='http', username='user', password='password', host='www.example.com', path='/some/path', query={'parm': [1, 2], 'other': [3]}, fragment='fragment') 
    63     URI(u'http://user:password@www.example.com/some/path?other=3&parm=1&parm=2#fragment') 
     63    >>> URI(scheme='http', username='user', password='password', host='www.example.com', port=12345, path='/some/path', query={'parm': [1, 2], 'other': [3]}, fragment='fragment') 
     64    URI(u'http://user:password@www.example.com:12345/some/path?other=3&parm=1&parm=2#fragment') 
    6465 
    6566    ...or finally, another URI object: 
     
    7172    False 
    7273    >>> v 
    73     URI(u'http://user:password@www.example.com/some/path?other=3&parm=1&parm=2#fragment') 
     74    URI(u'http://user:password@www.example.com:12345/some/path?other=3&parm=1&parm=2#fragment') 
    7475 
    7576    URI also normalises the path component: 
     
    7980    """ 
    8081 
    81     _pattern = re.compile(r'(?:(?P<scheme>[^:]+)://)?(?:(?P<username>[^:@]*)(?::(?P<password>[^@]*))?@)?(?P<host>[^?/#:]*)(?::(P<port>[\d+]+))?(?P<path>/[^#?]*)?(?:\?(?P<query>[^#]*))?(?:#(?P<fragment>.*))?') 
    82  
    83     __slots__ = ('scheme', 'username', 'password', 'host', 'port', '_path', 
    84                  'query', 'fragment') 
     82    _pattern = re.compile(r""" 
     83        (?:(?P<scheme>[^:]+)://)? 
     84        (?:(?P<username>[^:@]*) 
     85            (?::(?P<password>[^@]*))?@)? 
     86        (?P<host>[^?/#:]*) 
     87        (?::(?P<port>[^/]+))? 
     88        (?P<path>/[^#?]*)? 
     89        (?:\?(?P<query>[^#]*))? 
     90        (?:\#(?P<fragment>.*))? 
     91        """, re.VERBOSE) 
     92 
     93    __slots__ = ['scheme', 'username', 'password', 'host', 'port', '_path', 
     94                 'query', 'fragment'] 
    8595 
    8696    def __init__(self, uri=None, scheme=None, username=None, password=None, 
     
    155165 
    156166    def __str__(self): 
     167        return unicode(self).encode('utf-8') 
     168 
     169    def __unicode__(self): 
    157170        uri = unicode(self.scheme and (quote(self.scheme) + u'://') or u'') 
    158171        if self.username or self.password: 
  • pyndexter/branches/simplification/setup.py

    r459 r465  
    44    name='pyndexter', 
    55    description="An abstraction layer for full-text indexing engines.", 
    6     long_description="""Pyndexter (pronounced 'poindexter') is an abstraction 
    7         layer for full-text indexing and search engines. It presents a uniform 
    8         query syntax to the user, includes a basic but functional pure-Python 
    9         indexer, and has adapters for Hype, Hyperestraier, Lucene, Lupy, 
    10         Pyndex, Swish-e and Xapian.""", 
     6    long_description="""Pyndexter is an abstraction layer for full-text 
     7        indexing and search engines. It presents a uniform query syntax to the 
     8        user, includes a basic but functional pure-Python indexer, and has 
     9        adapters for Hype, Hyperestraier, Lucene, Lupy, Pyndex, Swish-e and 
     10        Xapian.""", 
    1111    url='http://swapoff.org/pyndexter', 
    1212    download_url='http://swapoff.org/pyndexter', 
     
    1616    author_email='alec@swapoff.org', 
    1717    version='0.4', 
    18     #test_suite='pyndexter.test.suite', 
     18    test_suite='pyndexter.tests.suite', 
    1919    classifiers=['Development Status :: 3 - Alpha', 
    2020                 'Environment :: Plugins',