1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

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

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

# http://pyrocko.org - GPLv3 

# 

# The Pyrocko Developers, 21st Century 

# ---|P------/S----------~Lg---------- 

 

# The container format: 

# * A file consists of records. 

# * A record consists of a record header a record payload, and possibly 

# padding. 

# * A record header consists of a label, a version, the record size, the 

# payload size, a hash, and a record type. 

# * A record payload consists of a sequence of record entries. 

# * A record entry consists of a key, a type, and a value. 

from __future__ import absolute_import, division 

 

from struct import unpack, pack 

from io import BytesIO 

import numpy as num 

try: 

from hashlib import sha1 

except ImportError: 

from sha import new as sha1 

 

try: 

from os import SEEK_CUR 

except ImportError: 

SEEK_CUR = 1 

 

from . import util 

 

try: 

range = xrange 

except NameError: 

pass 

 

 

size_record_header = 64 

no_hash = '\0' * 20 

 

numtypes = { 

'@i2': (num.int16, '>i2'), 

'@i4': (num.int32, '>i4'), 

'@i8': (num.int64, '>i8'), 

'@u2': (num.uint16, '>u2'), 

'@u4': (num.uint32, '>u4'), 

'@u8': (num.uint64, '>u8'), 

'@f4': (num.float32, '>f4'), 

'@f8': (num.float64, '>f8'), 

} 

 

numtype2type = dict([(v[0], k) for (k, v) in numtypes.items()]) 

 

 

def packer(fmt): 

return ((lambda x: pack('>'+fmt, x)), (lambda x: unpack('>'+fmt, x)[0])) 

 

 

def unpack_array(fmt, data): 

return num.fromstring( 

data, dtype=numtypes[fmt][1]).astype(numtypes[fmt][0]) 

 

 

def pack_array(fmt, data): 

return data.astype(numtypes[fmt][1]).tostring() 

 

 

def array_packer(fmt): 

return ((lambda x: pack_array(fmt, x)), (lambda x: unpack_array(fmt, x))) 

 

 

def encoding_packer(enc): 

return ((lambda x: x.encode(enc)), (lambda x: str(x.decode(enc)))) 

 

 

def noop(x): 

return x 

 

 

def time_to_str_ns(x): 

return util.time_to_str(x, format=9).encode('utf-8') 

 

 

def str_to_time(x): 

return util.str_to_time(str(x.decode('utf8'))) 

 

 

castings = { 

'i2': packer('h'), 

'i4': packer('i'), 

'i8': packer('q'), 

'u2': packer('H'), 

'u4': packer('I'), 

'u8': packer('Q'), 

'f4': packer('f'), 

'f8': packer('d'), 

'string': encoding_packer('utf-8'), 

'time_string': (time_to_str_ns, str_to_time), 

'@i2': array_packer('@i2'), 

'@i4': array_packer('@i4'), 

'@i8': array_packer('@i8'), 

'@u2': array_packer('@u2'), 

'@u4': array_packer('@u4'), 

'@u8': array_packer('@u8'), 

'@f4': array_packer('@f4'), 

'@f8': array_packer('@f8'), 

} 

 

 

def pack_value(type, value): 

try: 

return castings[type][0](value) 

except Exception as e: 

raise FileError( 

'Packing value failed (type=%s, value=%s, error=%s).' % 

(type, str(value)[:500], e)) 

 

 

def unpack_value(type, value): 

try: 

return castings[type][1](value) 

except Exception as e: 

raise FileError( 

'Unpacking value failed (type=%s, error=%s).' % (type, e)) 

 

 

class FileError(Exception): 

pass 

 

 

class NoDataAvailable(Exception): 

pass 

 

 

class WrongRecordType(Exception): 

pass 

 

 

class MissingRecordValue(Exception): 

pass 

 

 

class Record(object): 

def __init__( 

self, parent, mode, size_record, size_payload, hash, type, format, 

do_hash): 

 

self.mode = mode 

self.size_record = size_record 

self.size_payload = size_payload 

self.hash = hash 

self.type = type 

if mode == 'w': 

self.size_payload = 0 

self.hash = None 

self._out = BytesIO() 

else: 

self.size_remaining = self.size_record - size_record_header 

self.size_padding = self.size_record - size_record_header - \ 

self.size_payload 

 

self._f = parent._f 

self._parent = parent 

self._hasher = None 

self.format = format 

if do_hash and (self.mode == 'w' or self.hash): 

self._hasher = sha1() 

self._closed = False 

 

def read(self, n=None): 

 

assert not self._closed 

assert self.mode == 'r' 

 

if n is None: 

n = self.size_payload 

 

n = min(n, self.size_remaining - self.size_padding) 

data = self._f.read(n) 

self.size_remaining -= len(data) 

 

if len(data) != n: 

raise FileError('Read returned less data than expected.') 

 

if self._hasher: 

self._hasher.update(data) 

 

return data 

 

def write(self, data): 

assert not self._closed 

assert self.mode == 'w' 

self._out.write(data) 

if self._hasher: 

self._hasher.update(data) 

 

self.size_payload += len(data) 

 

def seek(self, n, whence): 

assert not self._closed 

assert self.mode == 'r' 

assert whence == SEEK_CUR 

assert n >= 0 

 

n = min(n, self.size_remaining - self.size_padding) 

self._f.seek(n, whence) 

self._hasher = None 

self.size_remaining -= n 

 

def skip(self, n): 

self.seek(n, SEEK_CUR) 

 

def close(self): 

if self._closed: 

return 

 

if self.mode == 'r': 

if self._hasher and self._hasher.digest() != self.hash: 

self.read(self.size_remaining) 

raise FileError( 

'Hash computed from record data does not match value ' 

'given in header.') 

else: 

self.seek(self.size_remaining, SEEK_CUR) 

 

if self.size_padding: 

self._f.seek(self.size_padding, SEEK_CUR) 

else: 

if self.size_record is not None and \ 

self.size_payload > self.size_record - size_record_header: 

 

raise FileError( 

'Too much data to fit into size-limited record.') 

 

if self.size_record is None: 

self.size_record = self.size_payload + size_record_header 

 

self.size_padding = self.size_record - self.size_payload - \ 

size_record_header 

 

if self._hasher is not None: 

self.hash = self._hasher.digest() 

 

self._parent.write_record_header( 

self.size_record, self.size_payload, self.hash, self.type) 

 

self._f.write(self._out.getvalue()) 

self._out.close() 

self._f.write(b'\0' * self.size_padding) 

 

self._closed = True 

self._parent = None 

self._f = None 

 

def entries(self): 

 

sizes = [] 

sum = 0 

while sum < self.size_payload: 

size = unpack('>Q', self.read(8))[0] 

sum += size + 8 

sizes.append(size) 

 

n = len(sizes) // 3 

keys = [] 

keys = [str(self.read(sizes[j]).decode('ascii')) 

for j in range(n)] 

types = [str(self.read(sizes[j]).decode('ascii')) 

for j in range(n, 2*n)] 

for key, type, j in zip(keys, types, range(2*n, 3*n)): 

yield key, type, sizes[j] 

 

def unpack(self, exclude=None): 

 

d = {} 

for key, type, size in self.entries(): 

if self.format[key] != type: 

FileError('Record value in unexpected format.') 

 

if not exclude or key not in exclude: 

d[key] = unpack_value(type, self.read(size)) 

else: 

self.skip(size) 

d[key] = None 

 

for key in self.format: 

if key not in d: 

raise FileError('Missing record entry: %s.' % key) 

 

return d 

 

def pack(self, d): 

for key in self.format: 

if key not in d: 

raise MissingRecordValue() 

 

keys = [] 

types = [] 

values = [] 

for key in d.keys(): 

if key in self.format: 

type = self.format[key] 

if isinstance(type, tuple): 

type = self._parent.get_type(key, d[key]) 

 

keys.append(key.encode('ascii')) 

types.append(type.encode('ascii')) 

values.append(pack_value(type, d[key])) 

 

sizes = [len(x) for x in keys+types+values] 

 

self.write(pack('>%iQ' % len(sizes), *sizes)) 

for x in keys+types+values: 

self.write(x) 

 

 

class File(object): 

 

def __init__( 

self, f, 

type_label='TEST', 

version='0000', 

record_formats={}): 

 

assert len(type_label) == 4 

assert len(version) == 4 

 

self._file_type_label = type_label 

self._file_version = version 

self._record_formats = record_formats 

self._current_record = None 

self._f = f 

 

def read_record_header(self): 

data = self._f.read(size_record_header) 

 

if len(data) == 0: 

raise NoDataAvailable() 

 

if len(data) != size_record_header: 

raise FileError('Read returned less data than expected.') 

 

label, version, size_record, size_payload, hash, type = unpack( 

'>4s4sQQ20s20s', data) 

 

label = str(label.decode('ascii')) 

version = str(version.decode('ascii')) 

type = str(type.rstrip().decode('ascii')) 

 

if label != self._file_type_label: 

raise FileError('record file type label missing.') 

 

if version != self._file_version: 

raise FileError('file version %s not supported.' % version) 

 

type = type.rstrip() 

 

if hash == no_hash: 

hash = None 

 

return size_record, size_payload, hash, type 

 

def write_record_header(self, size_record, size_payload, hash, type): 

if hash is None: 

hash = no_hash 

data = pack( 

'>4s4sQQ20s20s', 

self._file_type_label.encode('ascii'), 

self._file_version.encode('ascii'), 

size_record, 

size_payload, 

hash, 

type.encode('ascii').ljust(20)[:20]) 

 

self._f.write(data) 

 

def next_record(self, check_hash=False): 

if self._current_record: 

self._current_record.close() 

 

size_record, size_payload, hash, type = self.read_record_header() 

format = self._record_formats[type] 

self._current_record = Record( 

self, 'r', size_record, size_payload, hash, type, format, 

check_hash) 

 

return self._current_record 

 

def add_record(self, type, size_record=None, make_hash=False): 

if self._current_record: 

self._current_record.close() 

 

format = self._record_formats[type] 

self._current_record = Record( 

self, 'w', size_record, 0, None, type, format, make_hash) 

return self._current_record 

 

def close(self): 

if self._current_record: 

self._current_record.close()