Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# (c) Stefan Countryman, 2019 

2 

3""" 

4Mixins for ``FileHandler`` classes that add extra functionality. 

5""" 

6 

7import re 

8import os 

9from abc import ABC 

10from llama.flags import FlagDict 

11from llama.vetoes import ( 

12 eventdir_path_contains_string_manual, 

13 eventdir_path_contains_string_scratch, 

14 eventdir_path_contains_string_test, 

15 eventdir_path_contains_string_injection, 

16) 

17 

18# define extra ``FileHandler`` vetoes going beyond the defaults 

19def role_flag_not_observation(eventdir): 

20 """Return whether a trigger directory has it's "ROLE" flag set to 

21 something other than "observation".""" 

22 return FlagDict(eventdir)['ROLE'] != 'observation' 

23 

24 

25def eventid_is_not_valid_graceid(eventdir): 

26 """Check whether the eventid BREAKS from the pattern used for the GraceIDs 

27 of ANY (test OR observation) LVC events. Returns True if this eventid looks 

28 like a manually-generated non-GraceDB ID.""" 

29 eventid = os.path.split(os.path.abspath(eventdir))[1] 

30 regex = re.compile(r'^(MS|S|M|G)\d+[^\W\d_]*$', flags=re.MULTILINE) 

31 return regex.match(eventid) is None 

32 

33 

34def eventid_is_not_real_event_graceid(eventdir): 

35 """Check whether the eventid BREAKS from the pattern used for the GraceIDs 

36 of real LVC events. Returns True if this eventid looks like a fake or 

37 injected GraceID (or just a manually-generated non-GraceDB ID).""" 

38 eventid = os.path.split(os.path.abspath(eventdir))[1] 

39 return len(re.findall(r'^[SG]\d+[^\W\d_]*$', eventid, 

40 flags=re.MULTILINE)) == 0 

41 

42 

43def online_flag_false(eventdir): 

44 """Return whether a trigger directory has its "ONLINE" flag set to 

45 "false".""" 

46 return FlagDict(eventdir)['ONLINE'] == 'false' 

47 

48 

49class ObservingVetoMixin(ABC): 

50 """ 

51 These files will not be generated for mock data, test events, nor 

52 anything that doesn't look like an online LVC trigger. Veto these files if 

53 'test' or 'injection' appear anywhere in the event directory path, or if 

54 the eventid does not follow the real GraceID regex. 

55 """ 

56 

57 class_vetoes = ( 

58 (role_flag_not_observation, None), 

59 # (eventid_is_not_real_event_graceid, None), 

60 (eventdir_path_contains_string_scratch, None), 

61 (eventdir_path_contains_string_test, None), 

62 (eventdir_path_contains_string_injection, None), 

63 ) 

64 

65 

66class OnlineVetoMixin(ABC): 

67 """ 

68 Mixin for ``FileHandler`` classes. 

69 

70 Automatically veto on anything that doesn't look like a GraceDB online 

71 event. Use this to prevent accidentally querying GraceDB or other services 

72 for events that are meant to run on a local computer. 

73 """ 

74 

75 class_vetoes = ( 

76 (online_flag_false, None), 

77 (eventdir_path_contains_string_manual, None), 

78 # (eventid_is_not_valid_graceid, None), 

79 ) 

80 

81 

82class FileExtensionMixin(ABC): 

83 """ 

84 For ``FileHandler`` abstract subclasses that have multiple possible output 

85 formats (and programmatically-generated filenames calculated therefrom in 

86 their implementation subclasses). 

87 

88 Required Class Attributes 

89 ------------------------- 

90 The following class attributes must be defined in subclasses, either 

91 manually or programmatically (see: ``FileHandler.set_class_attributes`` and 

92 its implementations in subclasses). 

93 

94 ``FILEEXT`` 

95 ~~~~~~~~~~~ 

96 The file extension for this file as a string. 

97 """ 

98 

99 FILEEXT = None 

100 

101 _REQUIRED = ("FILEEXT",)