Package Pyblio :: Package Parsers :: Package Syntax :: Module ISI
[hide private]
[frames] | no frames]

Source Code for Module Pyblio.Parsers.Syntax.ISI

  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  Parser for the ISI file format. 
 21  """ 
 22   
 23  from Pyblio.Parsers.Syntax import Tagged 
 24  from Pyblio import Attribute 
 25  from Pyblio.Exceptions import ParserError 
 26   
 27  from gettext import gettext as _ 
 28   
 29  import re, string 
 30   
 31  start_re = re.compile (r'^(\w\w)\s(.*?)\r?$') 
 32  contd_re = re.compile (r'^\s{3,3}(.*?)\r?$') 
 33   
34 -class ISIParser (Tagged.Parser):
35 36 """ This parser knows how to split ISI records in fields """ 37
38 - def line_handler (self, line, count):
39 40 if line.strip () == '': return 41 42 m = start_re.match (line) 43 44 if m: 45 tag, data = m.groups ((1, 2)) 46 47 if tag in ('FN', 'VR', 'EF'): 48 return 49 50 if tag == 'PT': 51 self.record_start () 52 53 elif self.state == self.ST_IN_FIELD: 54 self.field_end () 55 56 if tag == 'ER': 57 self.record_end () 58 return 59 60 self.field_start (tag, count) 61 self.field_data (data) 62 return 63 64 m = contd_re.match (line) 65 if m: 66 self.field_data ('\n' + m.group (1)) 67 return 68 69 if line == 'EF': return 70 71 raise ParserError(_('line %d: unexpected data: %s') % (count, repr (line)))
72 73
74 -def _mkperson (txt):
75 76 res = map (string.strip, txt.split (',')) 77 if len(res) == 1: 78 last = res[0] 79 first = None 80 else: 81 if len(res) == 2: 82 last, first = res 83 else: 84 last = txt 85 first = None 86 87 return Attribute.Person (last = last, first = first)
88 89
90 -class Reader(Tagged.Reader):
91 """This reader has no knowledge of an actual scheme to map the 92 fields to. Check Pyblio.Parsers.Semantic.ISI for a parser that 93 knows the actual ISI fields. 94 """ 95 96 Parser = ISIParser 97 98 mapping = {} 99
100 - def person_add(self, field, value):
101 ''' Parse a person name in ISI format ''' 102 103 self.record [field] = [ _mkperson (txt) for txt in value.split ('\n') ] 104 return
105 106
107 - def do_default(self, line, tag, data):
108 try: 109 meth, field = self.mapping[tag] 110 except KeyError: 111 raise ParserError(_("line %s: unknown tag '%s'") % (line, tag)) 112 113 except ValueError: 114 self.emit ('warning', 115 (_("line %s: unsupported tag '%s'") % (line, tag))) 116 return 117 118 meth(self, field, data) 119 return
120