File: Sort.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 (174 lines) | stat: -rw-r--r-- 4,877 bytes parent folder | download | duplicates (3)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# -*- coding: iso-8859-1 -*-
# 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 import Fields, Types

import re, string, time

class Sort:
    ''' This class defines the methods used to sort a database '''
    
    def __init__ (self, fields = None):
        ''' Create a Sort class with a given set of SortFields '''
       
        self.fields = fields or []
        return

    def sort (self, iterator):
        ''' Returns a list of keys sorted according to the current
        sort settings '''
	
        self.base = iterator.base

        S = []
        extractors = [f.get_extractor() for f in self.fields]
        for e in iterator:
            s = []
            for f in extractors:
                s.extend (f (e))
	    s.append (e)
            S.append (s)
        S.sort ()
        result = [x [-1] for x in S]
        return result

    def __repr__ (self):
        return 'Sort (%s)' % str (self.fields)

class AnySort (object):
    
    def __init__ (self, ascend = 1):
        self.ascend = ascend
        return

    def get_extractor (self, extractor=None):
        extractor = extractor or self.extractor
        if self.ascend < 0:
            return lambda e: map (
                lambda S: ''.join([unichr(~ord(c) & 65535) for c in S]),
                extractor (e))
        else :
            return extractor
    
class TypeSort (AnySort):

    def get_field (self, entry):
        return entry.type

    def __repr__ (self):
        return 'TypeSort (%d)' % self.ascend

    def __cmp__ (self, other):
        if isinstance (other, TypeSort): return 0
        return -1

    def extractor (self, entry):
        return [str(entry.type).lower()]

class KeySort (AnySort):

    def get_field (self, entry):
        return entry.key

    def __repr__ (self):
        return 'KeySort (%d)' % self.ascend

    def __cmp__ (self, other):
        if isinstance (other, KeySort): return 0
        return -1

    def extractor (self, entry):
        return [str(entry.key).lower()]

class FieldSort (AnySort):
    
    def __init__ (self, field, ascend = 1):
        self.field  = field
        AnySort.__init__ (self, ascend)

    def get_extractor (self):
        field = self.field
        if field ==  'date':
            if self.ascend < 0:
                return lambda x: [- self.date_extractor(x) [0]]
            else: return self.date_extractor
        elif field == '-author/editor-':
            return AnySort.get_extractor (
                self, self.author_editor_extractor)
        elif field in ['author', 'editor']:
            return AnySort.get_extractor (
                self,
                lambda entry: map (
                rakify, entry.get(field, [])))
        else:
            return AnySort.get_extractor(self)
        
    def get_field (self, entry):
        try:
            return entry [self.field]
        except KeyError:
            return Types.get_field (self.field).type ('')

    def __repr__ (self):
        return 'FieldSort (%s, %d)' % (`self.field`, self.ascend)

    def __cmp__ (self, other):
        if not hasattr (other, 'field'): return -1
        
        return cmp (string.lower (self.field),
                    string.lower (other.field))

    def extractor (self, entry):
	try:
	    return   [str(entry[self.field]).rstrip().lower()]
	except KeyError :
	    return []
	
    def author_editor_extractor (self, entry):
        return map (rakify, entry.get('author', entry.get('editor', [])))
 
    def date_extractor (self, entry):
        d = entry.get('date', 0)
        try:
            return [d.asInt()]
        except AttributeError:
            return [0]
        
def rakify (author):

    
    s = "%s,%s" %(author.last,author.first)
    s = s.lower()
    Z = []
    for c in s:
        if c in '':
            n = ''.index (c)
            Z.append ('%ce' % ('aouAOU' [n]))
        elif c == '':
            Z.append ('ss')
        elif c in '-./':
            Z.append (' ')
        else:
            Z.append (c)
    S = ''.join (Z)
    S = re.sub(' +', ' ', S)
    return S