1# http://pyrocko.org - GPLv3 

2# 

3# The Pyrocko Developers, 21st Century 

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

5 

6import time 

7from collections import defaultdict 

8from pyrocko.io.io_common import FileLoadError 

9 

10 

11def provided_formats(): 

12 return ['virtual'] 

13 

14 

15def detect(first512): 

16 return None 

17 

18 

19class UniqueKeyRequired(Exception): 

20 pass 

21 

22 

23def get_stats(file_path): 

24 try: 

25 return float(data_mtimes[file_path]), 0 

26 except KeyError: 

27 raise FileLoadError(file_path) 

28 

29 

30def touch(file_path): 

31 try: 

32 data_mtimes[file_path] = time.time() 

33 except KeyError: 

34 raise FileLoadError(file_path) 

35 

36 

37data = defaultdict(list) 

38data_mtimes = {} 

39 

40 

41def add_nuts(nuts): 

42 file_paths = set() 

43 for nut in nuts: 

44 file_paths.add(nut.file_path) 

45 data[nut.file_path].append(nut) 

46 

47 for file_path in file_paths: 

48 data[file_path].sort( 

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

50 ks = set() 

51 for nut in data[file_path]: 

52 k = nut.file_segment, nut.file_element 

53 if k in ks: 

54 raise UniqueKeyRequired() 

55 

56 ks.add(k) 

57 

58 mtime = max(nut.file_mtime or 0 for nut in data[file_path]) 

59 old_mtime = data_mtimes.get(file_path, None) 

60 if old_mtime is None: 

61 data_mtimes[file_path] = mtime 

62 else: 

63 data_mtimes[file_path] = old_mtime + 1 

64 

65 

66def remove(file_paths): 

67 for file_path in file_paths: 

68 del data[file_path] 

69 

70 

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

72 assert format == 'virtual' 

73 

74 for nut in data[file_path]: 

75 if segment is None or segment == nut.file_segment: 

76 yield nut