1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
35
36 """ This parser knows how to split ISI records in fields """
37
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
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
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
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
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