Package Pyblio :: Package Cite :: Package Style :: Module Base
[hide private]
[frames] | no frames]

Source Code for Module Pyblio.Cite.Style.Base

  1  # -*- coding: utf-8 -*- 
  2  # This file is part of pybliographer 
  3  #  
  4  # Copyright (C) 1998-2006 Frederic GOBRY 
  5  # Email : gobry@pybliographer.org 
  6  #           
  7  # This program is free software; you can redistribute it and/or 
  8  # modify it under the terms of the GNU General Public License 
  9  # as published by the Free Software Foundation; either version 2  
 10  # of the License, or (at your option) any later version. 
 11  #    
 12  # This program is distributed in the hope that it will be useful, 
 13  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 15  # GNU General Public License for more details.  
 16  #  
 17  # You should have received a copy of the GNU General Public License 
 18  # along with this program; if not, write to the Free Software 
 19  # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
 20  #  
 21   
 22  """ 
 23  Generate citation keys. 
 24  """ 
 25   
 26  from sets import Set 
 27   
28 -def _alphaloop():
29 a = ord('a') 30 while a <= ord('z'): 31 yield chr(a) 32 a += 1 33 return
34
35 -class Unambiguous(object):
36 """ Base class to handle caching of generated keys. Ensures that 37 the same uid will always get the same key.""" 38
39 - def __init__(self, db):
40 self.map = {} 41 self.db = db 42 return
43
44 - def cache_lookup(self, uid):
45 if self.map.has_key(uid): 46 return self.map[uid] 47 return None
48
49 - def cache_update(self, uid, k):
50 self.map[uid] = k 51 return k
52
53 - def make_key(self, uid):
54 k = self.cache_lookup(uid) 55 if k: 56 return k 57 58 k = self._generate(uid) 59 return self.cache_update(uid, k)
60 61
62 -class Alpha(Unambiguous):
63 """ Base class to add a disambiguation letter after generated keys 64 that clash with already generated ones.""" 65
66 - def __init__(self, db):
67 Unambiguous.__init__(self, db) 68 self.seen = Set() 69 return
70
71 - def cache_update(self, uid, k):
72 if k in self.seen: 73 extra = _alphaloop() 74 while 1: 75 full = k + ':' + extra.next() 76 if full not in self.seen: 77 k = full 78 break 79 80 self.seen.add(k) 81 return Unambiguous.cache_update(self, uid, k)
82 83
84 -class Numeric(Unambiguous):
85 """ Return a numeric key for documents, in the order in which they 86 are requested.""" 87
88 - def __init__(self, db):
89 Unambiguous.__init__(self, db) 90 self.current = 1 91 return
92
93 - def _generate(self, uid):
94 k = str(self.current) 95 self.current += 1 96 return k
97 98
99 -def document_order(uids):
100 """ Return citations in their order of appearance in the 101 document. This is the order in which they are presented in input, 102 except that we don't want duplicates. """ 103 seen = Set() 104 for uid, key, extra in uids: 105 if uid in seen: 106 continue 107 seen.add(uid) 108 yield (uid, key) 109 return
110