Package chianti :: Package fortranformat :: Module _edit_descriptors
[hide private]
[frames] | no frames]

Source Code for Module chianti.fortranformat._edit_descriptors

  1  import sys 
  2  IS_PYTHON3 = sys.version_info[0] >= 3 
  3  
 
  4  if IS_PYTHON3: 
  5      exec('from ._exceptions import *') 
  6  else: 
  7      exec('from _exceptions import *') 
  8  
 
  9  
 
10 -def get_edit_descriptor_obj(name):
11 '''Returns a new object instance from a string''' 12 name = name.upper() 13 if name == 'A': 14 return A() 15 elif name == 'B': 16 return B() 17 elif name == 'BN': 18 return BN() 19 elif name == 'BZ': 20 return BZ() 21 elif name == ':': 22 return Colon() 23 elif name == 'D': 24 return D() 25 elif name == 'E': 26 return E() 27 elif name == 'EN': 28 return EN() 29 elif name == 'ES': 30 return ES() 31 elif name == 'F': 32 return F() 33 elif name == 'G': 34 return G() 35 elif name == 'H': 36 return H() 37 elif name == 'I': 38 return I() 39 elif name == 'L': 40 return L() 41 elif name == 'O': 42 return O() 43 elif name == 'P': 44 return P() 45 elif name =='S': 46 return S() 47 elif name == '/': 48 return Slash() 49 elif name == 'SP': 50 return SP() 51 elif name == 'SS': 52 return SS() 53 elif name == 'T': 54 return T() 55 elif name == 'TL': 56 return TL() 57 elif name == 'TR': 58 return TR() 59 elif name == 'X': 60 return X() 61 elif name == 'Z': 62 return Z() 63 else: 64 raise InvalidFormat('Expected an edit descriptor, got %s' % name)
65 66 # All the tokens defined in the F77 specification unless specified 67
68 -class A(object):
69 - def __init__(self):
70 self.repeat = None 71 self.width = None
72 - def __repr__(self):
73 return '<A repeat=' + str(self.repeat) + \ 74 ' width=' + str(self.width) + '>'
75
76 -class QuotedString(object):
77 - def __init__(self, char_string=None):
78 self.char_string = char_string
79 - def get_width(self):
80 return len(self.char_string)
81 width = property(get_width)
82 - def __repr__(self):
83 return '<QuotedString char_string=' + str(self.char_string) + '>'
84 85 # Only in F95
86 -class B(object):
87 - def __init__(self):
88 self.repeat = None 89 self.width = None 90 self.min_digits = None
91 - def __repr__(self):
92 return '<B repeat=' + str(self.repeat) + \ 93 ' width=' + str(self.width) + \ 94 ' min_digits=' + str(self.min_digits) + '>'
95
96 -class BN(object):
97 - def __init__(self):
98 pass
99 - def __repr__(self):
100 return '<BN>'
101
102 -class BZ(object):
103 - def __init__(self):
104 pass
105 - def __repr__(self):
106 return '<BZ>'
107
108 -class Colon(object):
109 - def __init__(self):
110 pass
111 - def __repr__(self):
112 return '<Colon>'
113
114 -class D(object):
115 - def __init__(self):
116 self.repeat = None 117 self.width = None 118 self.decimal_places = None
119 - def __repr__(self):
120 return '<D repeat=' + str(self.repeat) + \ 121 ' width=' + str(self.width) + \ 122 ' decimal_places=' + str(self.decimal_places) + '>'
123
124 -class E(object):
125 - def __init__(self):
126 self.repeat = None 127 self.width = None 128 self.decimal_places = None 129 self.exponent = None
130 - def __repr__(self):
131 return '<E repeat=' + str(self.repeat) + \ 132 ' width=' + str(self.width) + \ 133 ' decimal_places=' + str(self.decimal_places) + \ 134 ' exponent=' + str(self.exponent) + '>'
135 136 # Only in F95
137 -class EN(object):
138 - def __init__(self):
139 self.repeat = None 140 self.width = None 141 self.decimal_places = None 142 self.exponent = None
143 - def __repr__(self):
144 return '<EN repeat=' + str(self.repeat) + \ 145 ' width=' + str(self.width) + \ 146 ' decimal_places=' + str(self.decimal_places) + \ 147 ' exponent=' + str(self.exponent) + '>'
148 149 # Only in F95
150 -class ES(object):
151 - def __init__(self):
152 self.repeat = None 153 self.width = None 154 self.decimal_places = None 155 self.exponent = None
156 - def __repr__(self):
157 return '<ES repeat=' + str(self.repeat) + \ 158 ' width=' + str(self.width) + \ 159 ' decimal_places=' + str(self.decimal_places) + \ 160 ' exponent=' + str(self.exponent) + '>'
161
162 -class F(object):
163 - def __init__(self):
164 self.repeat = None 165 self.width = None 166 self.decimal_places = None
167 - def __repr__(self):
168 return '<F repeat=' + str(self.repeat) + \ 169 ' width=' + str(self.width) + \ 170 ' decimal_places=' + str(self.decimal_places) + '>'
171
172 -class FormatGroup(object):
173 pass
174
175 -class G(object):
176 - def __init__(self):
177 self.repeat = None 178 self.width = None 179 self.decimal_places = None 180 self.exponent = None
181 - def __repr__(self):
182 return '<G repeat=' + str(self.repeat) + \ 183 ' width=' + str(self.width) + \ 184 ' decimal_places=' + str(self.decimal_places) + \ 185 ' exponent=' + str(self.exponent) + '>'
186 187 # Only in F77
188 -class H(object):
189 - def __init__(self):
190 self.num_chars = None 191 self.char_string = None
192 - def __repr__(self):
193 return '<H num_chars=' + str(self.num_chars) + \ 194 ' char_string=' + str(self.char_string) + '>'
195
196 -class I(object):
197 - def __init__(self):
198 self.repeat = None 199 self.width = None 200 self.min_digits = None
201 - def __repr__(self):
202 return '<I repeat=' + str(self.repeat) + \ 203 ' width=' + str(self.width) + \ 204 ' min_digits=' + str(self.min_digits) + '>'
205
206 -class L(object):
207 - def __init__(self):
208 self.repeat = None 209 self.width = None
210 - def __repr__(self):
211 return '<L repeat=' + str(self.repeat) + \ 212 ' width=' + str(self.width) + '>'
213 214 # Only in F95
215 -class O(object):
216 - def __init__(self):
217 self.repeat = None 218 self.width = None 219 self.min_digits = None
220 - def __repr__(self):
221 return '<O repeat=' + str(self.repeat) + \ 222 ' width=' + str(self.width) + \ 223 ' min_digits=' + str(self.min_digits) + '>'
224
225 -class P(object):
226 - def __init__(self):
227 self.scale = None
228 - def __repr__(self):
229 return '<P scale=' + str(self.scale) + '>'
230
231 -class S(object):
232 - def __init__(self):
233 pass
234 - def __repr__(self):
235 return '<S>'
236
237 -class Slash(object):
238 - def __init__(self):
239 self.repeat = None 240 pass
241 - def __repr__(self):
242 return '<Slash repeat=' + str(self.repeat) + '>'
243
244 -class SP(object):
245 - def __init__(self):
246 pass
247 - def __repr__(self):
248 return '<SP>'
249
250 -class SS(object):
251 - def __init__(self):
252 pass
253 - def __repr__(self):
254 return '<SS>'
255
256 -class T(object):
257 - def __init__(self):
258 self.num_chars = None
259 - def __repr__(self):
260 return '<T num_chars=' + str(self.num_chars) + '>'
261
262 -class TL(object):
263 - def __init__(self):
264 self.num_chars = None
265 - def __repr__(self):
266 return '<TL num_chars=' + str(self.num_chars) + '>'
267
268 -class TR(object):
269 - def __init__(self):
270 self.num_chars = None
271 - def __repr__(self):
272 return '<TR num_chars=' + str(self.num_chars) + '>'
273
274 -class X(object):
275 - def __init__(self):
276 self.num_chars = None
277 - def __repr__(self):
278 return '<X num_chars=' + str(self.num_chars) + '>'
279 280 # Only in F95
281 -class Z(object):
282 - def __init__(self):
283 self.repeat = None 284 self.width = None 285 self.min_digits = None
286 - def __repr__(self):
287 return '<Z repeat=' + str(self.repeat) + \ 288 ' width=' + str(self.width) + \ 289 ' min_digits=' + str(self.min_digits) + '>'
290 291 # Categorise the edit descriptors depnding on how they should be parsed 292 293 ED1 = ['BN', 'BZ', 'SP', 'SS', 'S'] # Of form X only 294 ED2 = ['X'] # Of form nX only 295 ED3 = ['T', 'TR', 'TL', 'L'] # Of form Xn only 296 ED4 = ['A'] # Of form X or Xn 297 ED5 = ['D', 'F'] # Of form Xn.m only 298 ED6 = ['B', 'I', 'O', 'Z'] # Of form Xn or Xn.m 299 ED7 = ['E', 'EN', 'ES', 'G'] # Of form Xn.m or Xn.mEe 300 ED8 = ['P'] # Of form kX only, where k is a signed integer, may omit comma if followed by Type 5 or 7 edit descriptor 301 ED9 = [':'] # Of form X only, may omit comma either side 302 ED10 = ['/'] # Of form X only, may omit following comma and leading comma if no repeat 303 REPEATABLE_EDS = ['L', 'A', 'D', 'F', 'B', 'I', 'O', 'Z', 'E', 'EN', 'ES', 'G', '/'] 304 OUTPUT_EDS = (L, A, D, F, B, I, O, Z, E, EN, ES, G) 305 CONTROL_EDS = (BN, BZ, P, SP, SS, S, X, T, TR, TL, Colon, Slash) 306 NON_REVERSION_EDS = (P, S, SP, SS, BN, BZ) 307 ALL_ED = ED1 + ED2 + ED3 + ED4 + ED5 + ED6 + ED7 + ED8 + ED9 + ED10 308