1# http://pyrocko.org - GPLv3 

2# 

3# The Pyrocko Developers, 21st Century 

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

5 

6from __future__ import absolute_import, print_function 

7 

8import time 

9from collections import defaultdict 

10from pyrocko.io.io_common import FileLoadError 

11 

12 

13def provided_formats(): 

14 return ['virtual'] 

15 

16 

17def detect(first512): 

18 return None 

19 

20 

21class UniqueKeyRequired(Exception): 

22 pass 

23 

24 

25def get_stats(file_path): 

26 try: 

27 return float(data_mtimes[file_path]), 0 

28 except KeyError: 

29 raise FileLoadError(file_path) 

30 

31 

32def touch(file_path): 

33 try: 

34 data_mtimes[file_path] = time.time() 

35 except KeyError: 

36 raise FileLoadError(file_path) 

37 

38 

39data = defaultdict(list) 

40data_mtimes = {} 

41 

42 

43def add_nuts(nuts): 

44 file_paths = set() 

45 for nut in nuts: 

46 file_paths.add(nut.file_path) 

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

48 

49 for file_path in file_paths: 

50 data[file_path].sort( 

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

52 ks = set() 

53 for nut in data[file_path]: 

54 k = nut.file_segment, nut.file_element 

55 if k in ks: 

56 raise UniqueKeyRequired() 

57 

58 ks.add(k) 

59 

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

61 old_mtime = data_mtimes.get(file_path, None) 

62 if old_mtime is None: 

63 data_mtimes[file_path] = mtime 

64 else: 

65 data_mtimes[file_path] = old_mtime + 1 

66 

67 

68def remove(file_paths): 

69 for file_path in file_paths: 

70 del data[file_path] 

71 

72 

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

74 assert format == 'virtual' 

75 

76 for nut in data[file_path]: 

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

78 yield nut