1
2
3
4
5
6
7
8 """
9 This module provides basic functionality for handling shoebox format files.
10 These feed into the more sophisticated Shoebox tools available in the
11 modules I{lexicon}, I{text}, and I{metadata}.
12 """
13
14 import re
15 from UserDict import UserDict
16
17
19 """
20 This function returns the field marker and field value of a Shoebox field.
21
22 @return: parses field as string and returns tuple with field marker and field value
23 @rtype: tuple
24 """
25 mo = re.match(r"\\(.*?) (.*)", line)
26 if mo:
27 fm = mo.group(1)
28 fv = mo.group(2)
29 return (fm, fv)
30 else:
31 return None
32
33
35 """
36 Class used to represent a standard fromat field. A field
37 consists of a field marker and its value, stored as a tuple.
38 """
39
40 - def __init__(self, fieldMarker, fieldValue):
41 """
42 This method constructs a Field object as a tuple of a field
43 marker and a field value.
44 @param fieldMarker: a field's marker
45 @type fieldMarker: string
46 @param fieldValue : a field's value (the actual data)
47 @type fieldValue : string
48 """
49 self._field = (fieldMarker, fieldValue)
50
52 """
53 This method returns the string representation of a Field object.
54
55 @return: a Field object formatted as a string
56 @rtype: string
57 """
58 return "\\%s %s" % (self.getMarker(), self.getValue())
59
61 """
62 This method returns the marker for a field.
63
64 @return: a field's marker
65 @rtype: string
66 """
67 return self._field[0]
68
70 """
71 This method checks whether a field has a single value, in
72 which case it returns true, or multiple values, in which
73 case it returns false.
74
75 @return: whether the value for a given field is unique
76 @rtype: boolean
77 """
78 if not self.get_values() or len(self.get_values()) > 1:
79 return True
80 else:
81 return False
82
84 """
85 This method checks whether a field has a value or not.
86
87 @return: whether a given field has a value
88 @rtype: boolean
89 """
90 if self.get_values():
91 return True
92 else:
93 return False
94
96 """
97 This method returns the values for a field, either as a raw list of
98 values or, if a separator string is provided, as a formatted string.
99
100 @return: the values for a field; if sep provided, formatted as string
101 @rtype: a list of values or a string of these values joined by I{sep}
102 """
103 values = self._field[1]
104 if sep == None:
105 return values
106 else:
107 return sep.join(values)
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
146 """
147 Dictionary that retains the order in which keys were added to it.
148 """
152
156
161
163 UserDict.clear(self)
164 self._keys = []
165
170
172 return zip(self._keys, self.values())
173
176
178 try:
179 key = self._keys[-1]
180 except IndexError:
181 raise KeyError('dictionary is empty')
182 val = self[key]
183 del self[key]
184
185 return (key, val)
186
191
197
199 return map(self.get, self._keys)
200