File: Selection.py

package info (click to toggle)
pybliographer 1.2.12-4squeeze1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 3,560 kB
  • ctags: 1,413
  • sloc: python: 9,817; xml: 2,560; sh: 813; makefile: 621
file content (131 lines) | stat: -rw-r--r-- 3,607 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# This file is part of pybliographer
# 
# Copyright (C) 1998-2004 Frederic GOBRY
# Email : gobry@pybliographer.org
# 	   
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2 
# of the License, or (at your option) any later version.
#   
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details. 
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
# 
# 

from Pyblio.Iterator import Iterator

class SearchSelection (Iterator):
    ''' Iterates on a subset of another iterator. The subset is
    defined by a Search object '''
    
    def __init__ (self, search, iterator):
        self.search = search
        self.iter   = iterator
	self.base = iterator.base
        return

    def __iter__ (self):
        test = self.search.match
        for i in self.iter :
            if test(i):
                yield i
        raise StopIteration
    


    def first (self):
        entry = self.iter.first ()
        if not entry: return None
            
        while not self.search.match (entry):
            entry = self.iter.next ()
            if not entry: return None
            
        return entry


    def next (self):
        entry = self.iter.next ()
        if not entry: return None
        
        while not self.search.match (entry):
            entry = self.iter.next ()
            if not entry: return None

        return entry


    def __str__ (self):
        return '<Search Iterator on %s>' % self.search

    
class SortSelection (Iterator):
    ''' Iterates on a sorted version of another iterator. The sort
    criterion is defined by a Sort object '''

    def __init__ (self, sort, iterator):
        # compute asap
        self.data  = sort.sort (iterator)
        self.base = iterator.base 
        self.count = 0
        return

    def __iter__ (self):
        self.count = 0
        for i in self.data:
            yield i
            self.count += 1
        raise StopIteration

    def first (self):
        self.count = 0
        return self.next ()

    def next (self):
        try:
            ret = self.data [self.count]
        except IndexError:
            return None
        self.count = self.count + 1
        return ret
    
    def __str__ (self):
        return '<Sort Iterator>'

        
class Selection:
    ''' This class provides iterators that implement constraints (like
    sorting and searching) on another iterator '''
    
    def __init__ (self, search = None, sort = None):
        self.search = search
        self.sort   = sort
        return
    
    def iterator (self, iterator):
        ''' returns an iterator that will provide the entries of the
        database according to the properties of the current Selection
        '''

        # no filtering ? then pass through
        if not self.search and not self.sort:
            return iterator

        if not self.sort:
            return SearchSelection (self.search, iterator)

        if not self.search:
            return SortSelection (self.sort, iterator)

        return SortSelection (self.sort,
                              SearchSelection (self.search, iterator))