Package Pyblio :: Package Format :: Module S2
[hide private]
[frames] | no frames]

Source Code for Module Pyblio.Format.S2

  1  # This file is part of pybliographer 
  2  #  
  3  # Copyright (C) 1998-2006 Frederic GOBRY 
  4  # Email : gobry@pybliographer.org 
  5  #           
  6  # This program is free software; you can redistribute it and/or 
  7  # modify it under the terms of the GNU General Public License 
  8  # as published by the Free Software Foundation; either version 2  
  9  # of the License, or (at your option) any later version. 
 10  #    
 11  # This program is distributed in the hope that it will be useful, 
 12  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 14  # GNU General Public License for more details.  
 15  #  
 16  # You should have received a copy of the GNU General Public License 
 17  # along with this program; if not, write to the Free Software 
 18  # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
 19  # 
 20   
 21  """ 
 22  (Data structures for stage 2 of the formatter) 
 23  """ 
 24   
 25   
 26  from Pyblio.Format.S3 import Tag 
 27  from Pyblio.Format.Base import Missing 
 28   
 29   
30 -def T(*args):
31 return Tag('t', args, {})
32 33
34 -class Text(object):
35
36 - def __init__(self, t):
37 self.t = t 38 return
39
40 - def __call__(self, record):
41 return unicode(self.t)
42
43 - def __repr__(self):
44 return "S2.Text(%r)" % self.t
45
46 -class Sum(object):
47
48 - def __init__(self, a, b):
49 self.a = a 50 self.b = b 51 return
52
53 - def __call__(self, record):
54 # The sum fails if one of the two members fails 55 return T(self.a(record), self.b(record))
56
57 - def __repr__(self):
58 return "S2.Sum(%r, %r)" % (self.a, self.b)
59 60 61
62 -class Or(object):
63
64 - def __init__(self, a, b):
65 self.a = a 66 self.b = b 67 return
68
69 - def __call__(self, record):
70 # Return b except if a is defined 71 try: 72 return self.a(record) 73 except Missing: 74 return self.b(record)
75
76 - def __repr__(self):
77 return "S2.Or(%r, %r)" % (self.a, self.b)
78 79
80 -class Join(object):
81
82 - def __init__(self, middle, last, children):
83 self.middle = middle 84 self.last = last 85 self.children = children 86 return
87
88 - def __call__(self, record):
89 90 middle = self.middle(record) 91 last = self.last(record) 92 93 ls = [] 94 95 for arg in self.children: 96 if isinstance (arg, (str, unicode)): 97 ls.append (arg) 98 continue 99 100 try: v = arg(record) 101 except Missing: continue 102 103 if isinstance (v, (list, tuple)): 104 ls.extend(v) 105 else: 106 ls.append (v) 107 108 if len(ls) == 0: 109 raise Missing('empty join') 110 111 f = [ls.pop (0)] 112 113 while ls: 114 l = ls.pop (0) 115 if ls: f.append(middle) 116 else: f.append(last) 117 118 f.append(l) 119 120 return T(*f)
121
122 - def __repr__(self):
123 return "S2.Join(%r, %r, %r)" % ( 124 self.middle, self.last, self.children)
125 126
127 -class Switch(object):
128
129 - def __init__(self, fetch, switch, default):
130 self.fetch = fetch 131 self.switch = switch 132 self.default = default 133 return
134
135 - def __call__(self, record):
136 try: 137 txo = self.fetch(record) 138 except (KeyError, IndexError), msg: 139 if self.default: 140 c = self.default 141 else: 142 raise Missing('no such attribute in record: %s' % str(msg)) 143 else: 144 c = self.switch.get(txo, self.default) 145 146 if not c: 147 raise Missing('unsupported switch case') 148 149 return c(record)
150