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

# http://pyrocko.org - GPLv3 

# 

# The Pyrocko Developers, 21st Century 

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

 

from __future__ import absolute_import, print_function 

 

import time 

from collections import defaultdict 

from pyrocko.io.io_common import FileLoadError 

 

 

def provided_formats(): 

return ['virtual'] 

 

 

def detect(first512): 

return None 

 

 

class UniqueKeyRequired(Exception): 

pass 

 

 

def get_stats(file_path): 

try: 

return float(data_mtimes[file_path]), 0 

except KeyError: 

raise FileLoadError(file_path) 

 

 

def touch(file_path): 

try: 

data_mtimes[file_path] = time.time() 

except KeyError: 

raise FileLoadError(file_path) 

 

 

data = defaultdict(list) 

data_mtimes = {} 

 

 

def add_nuts(nuts): 

file_paths = set() 

for nut in nuts: 

file_paths.add(nut.file_path) 

data[nut.file_path].append(nut) 

 

for file_path in file_paths: 

data[file_path].sort( 

key=lambda nut: (nut.file_segment, nut.file_element)) 

ks = set() 

for nut in data[file_path]: 

k = nut.file_segment, nut.file_element 

if k in ks: 

raise UniqueKeyRequired() 

 

ks.add(k) 

 

old_mtime = data_mtimes.get(file_path, 0) 

data_mtimes[file_path] = old_mtime + 1 

 

 

def remove(file_paths): 

for file_path in file_paths: 

del data[file_path] 

 

 

def iload(format, file_path, segment, content): 

assert format == 'virtual' 

 

for nut in data[file_path]: 

yield nut