1# http://pyrocko.org - GPLv3
2#
3# The Pyrocko Developers, 21st Century
4# ---|P------/S----------~Lg----------
6import time
7from collections import defaultdict
8from pyrocko.io.io_common import FileLoadError
11def provided_formats():
12 return ['virtual']
15def detect(first512):
16 return None
19class UniqueKeyRequired(Exception):
20 pass
23def get_stats(file_path):
24 try:
25 return float(data_mtimes[file_path]), 0
26 except KeyError:
27 raise FileLoadError(file_path)
30def touch(file_path):
31 try:
32 data_mtimes[file_path] = time.time()
33 except KeyError:
34 raise FileLoadError(file_path)
37data = defaultdict(list)
38data_mtimes = {}
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)
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()
56 ks.add(k)
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
66def remove(file_paths):
67 for file_path in file_paths:
68 del data[file_path]
71def iload(format, file_path, segment, content):
72 assert format == 'virtual'
74 for nut in data[file_path]:
75 if segment is None or segment == nut.file_segment:
76 yield nut