Changeset 387

Show
Ignore:
Timestamp:
02/12/07 02:48:24 (2 years ago)
Author:
athomas
Message:

pyndexter: URI objects are now passed around exclusively (hopefully) internally. I expect I have missed some corners though.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • pyndexter/trunk/pyndexter/indexers/builtin.py

    r386 r387  
    158158    def index(self, document): 
    159159 
    160         uri = self._wid(document.uri
     160        uri = unicode(self._wid(document.uri)
    161161        words = self._wids(self.framework.reduce(document.content)) 
    162162        doc_set = set([uri]) 
     
    189189        self.uris.replace(uri, words) 
    190190 
     191    replace = index 
     192 
    191193    def discard(self, uri): 
     194        uri = unicode(uri) 
    192195        try: 
    193196            del self.attributes[uri] 
     
    200203        self.uris.remove(uri) 
    201204 
    202     replace = index 
    203  
    204205    def __iter__(self): 
    205206        for uri in self.uris.keys(): 
    206             yield self._word(uri
     207            yield URI(self._word(uri)
    207208 
    208209    def fetch(self, uri): 
     210        uri = unicode(uri) 
    209211        uriid = self._wid(uri) 
    210212        attributes = self.attributes.get(uriid, {}) 
     
    292294        framework = indexer.framework 
    293295        attributes = indexer.attributes.get(uri, {}) 
    294         attributes['uri'] = uri 
     296        attributes['uri'] = URI(uri) 
    295297        attributes = dict([(k.encode('utf-8'), v) for k, v in attributes.iteritems()]) 
    296298        return Hit(current=framework.fetch, indexed=indexer.fetch, **attributes) 
  • pyndexter/trunk/pyndexter/indexers/hype.py

    r381 r387  
    3939 
    4040    def index(self, document): 
    41         hdoc = hype.Document(document.uri
     41        hdoc = hype.Document(unicode(document.uri)
    4242        for k, v in document.attributes.iteritems(): 
    4343            if k != 'uri': 
     
    4848 
    4949    def discard(self, uri): 
    50         doc = self.db.get_doc_by_uri(uri
     50        doc = self.db.get_doc_by_uri(unicode(uri)
    5151        if not doc: 
    5252            raise DocumentNotFound(uri) 
     
    6262 
    6363    def fetch(self, uri): 
    64         doc = self.db.get_doc_by_uri(uri
     64        doc = self.db.get_doc_by_uri(unicode(uri)
    6565        if not doc: 
    6666            raise DocumentNotFound(uri) 
     
    8383            else: 
    8484                attributes[k] = hdoc.get(k) 
     85        attributes['uri'] = URI(attributes['uri']) 
    8586        return attributes 
    8687 
  • pyndexter/trunk/pyndexter/indexers/hyperestraier.py

    r381 r387  
    5353 
    5454    def discard(self, uri): 
    55         uri = uri.encode('utf-8') 
    56         id = self.db.uri_to_id(uri) 
     55        uuri = unicode(uri).encode('utf-8') 
     56        id = self.db.uri_to_id(uuri) 
    5757        if id == -1: 
    5858            raise DocumentNotFound(uri) 
     
    6060 
    6161    def fetch(self, uri): 
    62         uri = uri.encode('utf-8') 
    63         id = self.db.uri_to_id(uri) 
     62        uuri = unicode(uri).encode('utf-8') 
     63        id = self.db.uri_to_id(uuri) 
    6464        if id == -1: 
    6565            raise DocumentNotFound(uri) 
     
    101101            else: 
    102102                attributes[k] = hdoc.attr(k).decode('utf-8') 
     103        attributes['uri'] = URI(attributes['uri']) 
    103104        return attributes 
    104105 
  • pyndexter/trunk/pyndexter/indexers/lucene.py

    r381 r387  
    3131        doc = PyLucene.Document() 
    3232        for k, v in document.attributes.iteritems(): 
    33             doc.add(PyLucene.Field(str(k), str(v), PyLucene.Field.Store.YES, 
     33            doc.add(PyLucene.Field(unicode(k), unicode(v), 
     34                                   PyLucene.Field.Store.YES, 
    3435                                   PyLucene.Field.Index.TOKENIZED)) 
    3536        reader = PyLucene.StringReader(document.content) 
     
    3940    def discard(self, uri): 
    4041        reader = PyLucene.IndexReader.open(self.db_path) 
    41         reader.deleteDocuments(PyLucene.Term('uri', uri)) 
     42        reader.deleteDocuments(PyLucene.Term('uri', unicode(uri))) 
    4243        reader.close() 
    4344 
     
    8586        for field in hit.fields(): 
    8687            attributes[field.name().encode('utf-8')] = field.stringValue() 
     88        attributes['uri'] = URI(attributes['uri']) 
    8789        return Hit(current=self.indexer.framework.fetch, 
    8890                   indexed=self.indexer.fetch, **attributes) 
  • pyndexter/trunk/pyndexter/indexers/lupy.py

    r380 r387  
    3333 
    3434    def index(self, document): 
    35         attributes = dict([('_' + k.encode('utf-8'), str(v)) 
     35        attributes = dict([('_' + k.encode('utf-8'), unicode(v)) 
    3636                           for k, v in document.attributes.iteritems() 
    3737                           if v is not None]) 
     
    4040 
    4141    def discard(self, uri): 
    42         self.db.delete(uri=uri
     42        self.db.delete(uri=unicode(uri)
    4343 
    4444    def search(self, query): 
     
    8787        fields = dict([(str(k), doc.get(k)) for k in doc.fieldNames]) 
    8888        fields['score'] = self.context.score(index) 
     89        fields['uri'] = URI(fields['uri']) 
    8990        return Hit(current=self.indexer.framework.fetch, 
    9091                   indexed=self.indexer.fetch, **fields) 
  • pyndexter/trunk/pyndexter/indexers/pyndex.py

    r381 r387  
    2929 
    3030    def index(self, document): 
    31         self.db.index(document.uri.encode('utf-8'), document.content.encode('utf-8')) 
     31        uri = unicode(document.uri).encode('utf-8') 
     32        self.db.index(uri, document.content.encode('utf-8')) 
    3233 
    3334    def discard(self, uri): 
    3435        # FIXME Is there a supported way of deleting documents? This is hackish. 
    3536        # FIXME Is there a way of storing attributes? 
    36         self.db.index(uri.encode('utf-8'), '') 
     37        self.db.index(unicode(uri).encode('utf-8'), '') 
    3738 
    3839    def search(self, query): 
    3940        # FIXME Should probably do a search on each term, and perform set 
    4041        # operations. 
    41         qs = query.as_string(and_=' ', or_=' ', not_=''
     42        qs = ' '.join(query.terms()
    4243        return PyndexResult(self, query, self.db.find(qs)) 
    4344 
     
    5657class PyndexResult(Result): 
    5758    def __iter__(self): 
    58         for uri in self.context: 
    59             yield Hit(uri=uri.doc.docname, score=uri.score, 
    60                       current=self.indexer.framework.fetch, 
    61                       indexed=self.indexer.fetch) 
     59        for hit in self.context: 
     60            yield self._translate(hit) 
     61 
     62    def __getitem__(self, index): 
     63        return self._translate(self.context[index]) 
     64 
     65    def _translate(self, hit): 
     66        return Hit(uri=URI(hit.doc.docname), score=hit.score, 
     67                   current=self.indexer.framework.fetch, 
     68                   indexed=self.indexer.fetch) 
  • pyndexter/trunk/pyndexter/indexers/xapian.py

    r383 r387  
    4444        # FIXME Xapian doesn't support UTF-8 yet. "Coming soon." 
    4545        content = document.content.encode('utf-8') 
    46         uri = document.uri.encode('utf-8') 
     46        uri = unicode(document.uri).encode('utf-8') 
    4747 
    4848        doc.set_data(content) 
     
    5959 
    6060    def discard(self, uri): 
    61         self.db.delete_document('Q' + uri.encode('utf-8')) 
     61        self.db.delete_document('Q' + unicode(uri).encode('utf-8')) 
    6262 
    6363    def fetch(self, uri): 
    64         term = 'Q' + uri.encode('utf-8') 
     64        term = 'Q' + unicode(uri).encode('utf-8') 
    6565        for docid in self.db.postlist(term): 
    6666            doc = self.db.get_document(docid[0]) 
     
    6868            return Document(uri=uri, content=doc.get_data().decode('utf-8'), 
    6969                            quality=0.95) 
     70        raise DocumentNotFound(uri) 
    7071 
    7172    def __iter__(self): 
     
    126127        uri = terms.next()[0][1:] 
    127128        assert uri, 'uniQue term (URI) not found in document term list' 
    128         return Hit(uri
     129        return Hit(URI(uri)
    129130                   current=self.indexer.framework.fetch, 
    130131                   indexed=self.indexer.fetch, 
  • pyndexter/trunk/pyndexter/__init__.py

    r386 r387  
    238238        the current state of the `Source` and that in the provided state. Each 
    239239        tuple is in the form `(<transition>, uri)`, where <transition> is one 
    240         of `ADDED`, `REMOVED` or `MODIFIED`. """ 
     240        of `ADDED`, `REMOVED` or `MODIFIED` and uri is a URI object.""" 
    241241        current = set() 
    242242        try: 
     
    247247                               'Exception was %s: %s' % (e.__class__.__name__, e)) 
    248248        for uri in self: 
    249             current.add(uri) 
    250             if uri not in state: 
     249            uuri = unicode(uri) 
     250            current.add(uuri) 
     251            if uuri not in state: 
    251252                yield (ADDED, uri) 
    252             elif self.fetch(uri).changed != state[uri]: 
     253            elif self.fetch(uri).changed != state[uuri]: 
    253254                yield (MODIFIED, uri) 
    254255        for removed in set(state.keys()).difference(current): 
    255             yield (REMOVED, removed
     256            yield (REMOVED, URI(removed)
    256257 
    257258    # Useful helper methods 
     
    261262        from fnmatch import fnmatch 
    262263        for pattern in self.exclude: 
    263             if fnmatch(uri, pattern): 
     264            if fnmatch(unicode(uri), pattern): 
    264265                return False 
    265266        for pattern in self.include: 
    266             if fnmatch(uri, pattern): 
     267            if fnmatch(unicode(uri), pattern): 
    267268                return True 
    268269        return False 
     
    779780            if stemmer is None: 
    780781                stemmer = lambda word: word 
    781             elif isinstance(stemmer, basestring): 
     782            elif isinstance(stemmer, (basestring, URI)): 
    782783                Stemmer = self._load_plugin('stemmer', stemmer) 
    783784                stemmer = Stemmer(uri=stemmer) 
     
    786787            self.reduce = reduce 
    787788 
    788         if isinstance(indexer, basestring): 
    789             self.indexer = self._load_plugin('indexer', indexer) 
    790             self.indexer = self.indexer(framework=self, uri=indexer) 
    791         else: 
    792             self.indexer = indexer 
    793  
    794         if state_store is None and indexer: 
    795             self.state_store = self.indexer.state_store() 
    796         else: 
    797             self.state_store = state_store 
     789        self.state_store = state_store 
     790        self.indexer = indexer 
    798791 
    799792        from pyndexter.sources.metasource import MetaSource 
     
    801794 
    802795    def set_indexer(self, indexer): 
    803         """Set the `Framework` `Indexer`.""" 
    804         self.indexer = indexer 
     796        """Set the `Framework` indexer. Can either be a URI or an `Indexer` 
     797        object.""" 
     798        if isinstance(indexer, (basestring, URI)): 
     799            Indexer = self._load_plugin('indexer', indexer) 
     800            self._indexer = Indexer(framework=self, uri=indexer) 
     801        else: 
     802            self._indexer = indexer 
     803 
    805804        if self.state_store is None: 
    806             self.state_store = indexer.state_store() 
     805            self.state_store = self.indexer.state_store() 
     806 
     807    def get_indexer(self): 
     808        return self._indexer 
     809 
     810    indexer = property(get_indexer, set_indexer) 
    807811 
    808812    def add_source(self, source): 
    809813        """ Add a source to be indexed to the framework. Can either be a 
    810814        `Source` instance or a URI.""" 
    811         if isinstance(source, basestring): 
     815        if isinstance(source, (basestring, URI)): 
    812816            Source = self._load_plugin('source', source) 
    813817            source = Source(framework=self, uri=source) 
     
    816820    def fetch(self, uri): 
    817821        """ Fetch a document. """ 
     822        uri = URI(uri) 
    818823        return self.source.fetch(uri) 
    819824 
     
    861866        URI.""" 
    862867        self._assert_rw() 
    863         if isinstance(document, basestring): 
     868        if isinstance(document, (URI, basestring)): 
    864869            document = self.fetch(document) 
    865870        return self.indexer.index(document) 
  • pyndexter/trunk/pyndexter/sources/file.py

    r384 r387  
    4646                    continue 
    4747                if S_ISDIR(stat.st_mode): 
    48                     for file in walk_path(os.path.join(path, file)): 
    49                         yield file 
    50                 elif self.predicate(full_path) and os.access(full_path, os.R_OK) \ 
     48                    for uri in walk_path(os.path.join(path, file)): 
     49                        yield uri 
     50                elif self.predicate(URI(scheme='file', path=full_path)) \ 
     51                        and os.access(full_path, os.R_OK) \ 
    5152                        and S_ISREG(stat.st_mode): 
    52                     yield (self._file2uri(full_path).decode(self.encoding), stat) 
     53                    yield (URI(scheme='file', path=full_path.decode(self.encoding)), stat) 
    5354 
    54         for file, stat in walk_path('/'): 
    55             self._state[file] = stat.st_mtime 
    56             yield file 
     55        for uri, stat in walk_path('/'): 
     56            self._state[unicode(uri)] = stat.st_mtime 
     57            yield uri 
    5758 
    5859    def matches(self, uri): 
    59         scheme, netloc, path, query, fragment = urlsplit(uri, 'file') 
    60         path = os.path.normpath(unquote(path)) 
    61         return scheme == 'file' and \ 
    62                path.startswith(self.path) and \ 
    63                self.predicate(path) 
     60        return uri.scheme == 'file' and \ 
     61               uri.path.startswith(self.path) and \ 
     62               self.predicate(uri) 
    6463 
    6564 
    6665    def fetch(self, uri): 
    67         path = self._uri2file(uri) 
    6866        try: 
    69             stat = os.stat(path) 
     67            stat = os.stat(uri.path) 
    7068        except Exception, e: 
    7169            raise DocumentNotFound(uri, e) 
     
    7573 
    7674    def exists(self, uri): 
    77         return os.path.exists(self._uri2file(uri)
     75        return os.path.exists(uri.path
    7876 
    7977    def __hash__(self): 
    80         return hash(self._file2uri(self.path) + '-'.join(self.exclude) + \ 
     78        return hash('file://' + self.path + '-'.join(self.exclude) + \ 
    8179                    '+'.join(self.include)) 
    8280 
    8381    # Internal methods 
    8482    def _fetch_content(self, uri): 
    85         path = self._uri2file(uri) 
    86         return codecs.open(path, encoding='utf-8', errors='replace').read() 
    87  
    88     def _file2uri(self, file): 
    89         return urlunsplit(('file', '', quote(file), '', '')) 
    90  
    91     def _uri2file(self, uri): 
    92         scheme, location, path, query, fragment = urlsplit(uri, 'file') 
    93         if scheme != 'file': 
    94             raise InvalidURI("URI scheme in '%s' not supported by FileSource" 
    95                              % scheme) 
    96         path = os.path.normpath(unquote(path)) 
    97         if not path.startswith(self.path): 
    98             raise InvalidURI("Requested URI '%s' is not from this FileSource" 
    99                              % uri) 
    100         return path.decode(self.encoding) 
     83        return codecs.open(uri.path, encoding='utf-8', errors='replace').read() 
    10184 
    10285 
  • pyndexter/trunk/pyndexter/util.py

    r385 r387  
    140140 
    141141    def __repr__(self): 
    142         uri = self.scheme and (quote(self.scheme) + '://') or '' 
     142        return "<URI u'%s'>" % unicode(self) 
     143 
     144    def __str__(self): 
     145        uri = unicode(self.scheme and (quote(self.scheme) + u'://') or u'') 
    143146        if self.username or self.password: 
    144147            if self.username: 
    145148                uri += quote(self.username) 
    146149            if self.password: 
    147                 uri += ':' + quote(self.password) 
    148             uri += '@' 
     150                uri += u':' + quote(self.password) 
     151            uri += u'@' 
    149152        uri += quote(self.host) 
    150153        if self.port: 
    151             uri += ':%s' % port 
     154            uri += u':%s' % port 
    152155        uri += quote(self.path) 
    153156        if self.query: 
    154             uri += '?' + '&'.join(['&'.join(['%s=%s' % (k, quote(str(v))) for v in l]) 
    155                                    for k, l in sorted(self.query.items())]) 
     157            uri += u'?' + u'&'.join([u'&'.join([u'%s=%s' % (k, quote(str(v))) 
     158                                                for v in l]) 
     159                                     for k, l in sorted(self.query.items())]) 
    156160        if self.fragment: 
    157             uri += '#' + quote(self.fragment) 
     161            uri += u'#' + quote(self.fragment) 
    158162        return uri 
    159163 
  • pyndexter/trunk/.todo

    r384 r387  
    125125    <note priority="medium" time="1171055477"> 
    126126        Write a decent test suite. 
     127        <note priority="medium" time="1171271157"> 
     128            Test that searches return the right hits. Don't care about order. 
     129        </note> 
     130        <note priority="medium" time="1171271356"> 
     131            Test that all interfaces pass and receive unicode correctly. 
     132        </note> 
     133        <note priority="medium" time="1171271371"> 
     134            Test that all indexers and sources pass URI objects correctly. 
     135        </note> 
    127136    </note> 
    128137</todo>