Coverage for /usr/local/lib/python3.11/dist-packages/pyrocko/squirrel/io/backends/yaml.py: 83%

29 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-10-04 09:52 +0000

1# http://pyrocko.org - GPLv3 

2# 

3# The Pyrocko Developers, 21st Century 

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

5 

6''' 

7Squirrel IO adaptor to read YAML files. 

8''' 

9 

10import logging 

11 

12from pyrocko.io.io_common import get_stats, touch # noqa 

13from pyrocko import model as pmodel 

14from ... import model 

15 

16from pyrocko import guts 

17 

18logger = logging.getLogger('psq.io.yaml') 

19 

20 

21def provided_formats(): 

22 return ['yaml'] 

23 

24 

25def detect_pyrocko_yaml(first512): 

26 try: 

27 first512 = first512.decode('utf-8') 

28 except UnicodeDecodeError: 

29 return False 

30 

31 for line in first512.splitlines(): 

32 if line.startswith('--- !pf.'): 

33 return True 

34 

35 return False 

36 

37 

38def detect(first512): 

39 if detect_pyrocko_yaml(first512): 

40 return 'yaml' 

41 

42 return None 

43 

44 

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

46 for iobj, obj in enumerate(guts.iload_all(filename=file_path)): 

47 if isinstance(obj, pmodel.Event): 

48 nut = model.make_event_nut( 

49 file_segment=0, 

50 file_element=iobj, 

51 codes=model.CodesX(obj.catalog or ''), 

52 tmin=obj.time, 

53 tmax=obj.time) 

54 

55 if 'event' in content: 

56 nut.content = obj 

57 

58 yield nut