Package Pyblio :: Package Parsers :: Package Semantic :: Module BibTeX
[hide private]
[frames] | no frames]

Source Code for Module Pyblio.Parsers.Semantic.BibTeX

 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  from Pyblio.Parsers.Syntax import BibTeX 
21  from Pyblio import Attribute 
22   
23  _monthmap={ 
24      'january': 1,   'jan': 1, 
25      'february': 2,  'feb': 2, 
26      'march': 3,     'mar': 3, 
27      'april': 4,     'apr': 4, 
28      'may': 5, 
29      'june': 6,      'jun': 6, 
30      'july': 7,      'jul': 7, 
31      'august': 8,    'aug': 8, 
32      'september': 9, 'sep': 9, 
33      'october': 10,  'oct': 10, 
34      'november': 11, 'nov': 11, 
35      'december': 12, 'dec': 12, 
36      } 
37   
38 -class Reader(BibTeX.Reader):
39 """ Default BibTeX parser. 40 """ 41
42 - def string_add(self, data):
43 # Simply fill in the provided strings 44 for key, value in data.fields: 45 self.env.strings[key] = value 46 return
47
48 - def type_add(self, name):
49 txo = self.db.schema.txo['doctype'].byname(name.lower()) 50 self.record.add('doctype', txo, Attribute.Txo) 51 return
52
53 - def record_begin(self):
54 self.date = Attribute.Date() 55 return
56
57 - def record_end(self):
58 if self.key is not None: 59 self.id_add('id', self.key) 60 61 if self.date != Attribute.Date(): 62 self.record['date'] = [self.date] 63 return
64
65 - def do_year(self, value):
66 year = self.to_text(value).strip() 67 if not year: return 68 69 try: 70 self.date.year = int(year) 71 except ValueError, msg: 72 raise ValueError('in %s: %s' % (self.key, msg)) 73 return
74
75 - def do_month(self, value):
76 month = self.to_text(value).lower().strip() 77 if not month: return 78 79 try: 80 self.date.month =_monthmap[month] 81 except KeyError, msg: 82 raise KeyError('in %s: %s' % (self.key, msg)) 83 return
84 85 86
87 -class Writer(BibTeX.Writer):
88 pass
89