Coverage for /usr/local/lib/python3.11/dist-packages/grond/run_info.py: 41%

32 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-10-26 16:25 +0000

1import logging 

2from pyrocko import guts 

3from pyrocko.guts import List, Unicode, Object 

4 

5from grond.version import __version__ 

6from grond.meta import GrondError 

7 

8guts_prefix = 'grond' 

9logger = logging.getLogger('grond.report') 

10 

11 

12class RunInfo(Object): 

13 tags = List.T( 

14 Unicode.T(), 

15 help='List of user defined labels') 

16 

17 def add_tag(self, tag): 

18 if tag not in self.tags: 

19 self.tags.append(tag) 

20 self.tags.sort() 

21 else: 

22 logger.warning('While adding tag: tag already set: %s' % tag) 

23 

24 def remove_tag(self, tag): 

25 try: 

26 self.tags.remove(tag) 

27 except ValueError: 

28 logger.warning('While removing tag: tag not set: %s' % tag) 

29 

30 

31def read_info(path): 

32 try: 

33 info = guts.load(filename=path) 

34 except OSError: 

35 raise GrondError( 

36 'Cannot read Grond run info file: %s' % path) 

37 

38 if not isinstance(info, RunInfo): 

39 raise GrondError( 

40 'Invalid Grond run info in file "%s".' % path) 

41 

42 return info 

43 

44 

45def write_info(info, path): 

46 try: 

47 guts.dump( 

48 info, 

49 filename=path, 

50 header='Grond run info file, version %s' % __version__) 

51 

52 except OSError: 

53 raise GrondError( 

54 'Cannot write Grond run info file: %s' % path)