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# 

2# Copyright (C) 2019-2020 Leo P. Singer <leo.singer@ligo.org> 

3# 

4# This program is free software: you can redistribute it and/or modify 

5# it under the terms of the GNU General Public License as published by 

6# the Free Software Foundation, either version 3 of the License, or 

7# (at your option) any later version. 

8# 

9# This program is distributed in the hope that it will be useful, 

10# but WITHOUT ANY WARRANTY; without even the implied warranty of 

11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

12# GNU General Public License for more details. 

13# 

14# You should have received a copy of the GNU General Public License 

15# along with this program. If not, see <https://www.gnu.org/licenses/>. 

16# 

17from os.path import join 

18 

19from .base import Deletable, Mapping, Mutable, Resource 

20from .event import Event, Superevent 

21from .util import field_collection, str_or_collection 

22 

23 

24class BaseEvents(Deletable, Mapping, Mutable, Resource): 

25 

26 def search(self, **kwargs): 

27 url = self.url 

28 while url: 

29 response = self.session.get(url, params=kwargs).json() 

30 url = response.get('links', {}).get('next') 

31 kwargs = None 

32 yield from response.get(self.path.strip('/'), []) 

33 

34 

35class Events(BaseEvents): 

36 

37 path = 'events/' 

38 mapped_class = Event 

39 

40 def __getitem__(self, key): 

41 """Make the API forgiving of mixing up events and superevents.""" 

42 if 'S' in key: 

43 return self.parent.superevents[key] 

44 else: 

45 return super().__getitem__(key) 

46 

47 def create_or_update(self, event_id, *, 

48 filename='initial.data', 

49 filecontents=None, labels=None, **kwargs): 

50 data = (*field_collection('labels', labels), *kwargs.items()) 

51 files = {'eventFile': (filename, filecontents)} 

52 return super().create_or_update(event_id, data=data, files=files) 

53 

54 

55SUPEREVENT_CATEGORIES = {'M': 'M', 'T': 'T', 'G': 'P'} 

56 

57 

58class Superevents(BaseEvents): 

59 

60 path = 'superevents/' 

61 mapped_class = Superevent 

62 

63 def __getitem__(self, key): 

64 """Make the API forgiving of mixing up events and superevents.""" 

65 if 'S' not in key: 

66 return self.parent.events[key] 

67 else: 

68 return super().__getitem__(key) 

69 

70 def create_or_update(self, superevent_id, *, 

71 events=None, labels=None, **kwargs): 

72 data = {key: value for key, value in kwargs.items() 

73 if value is not None} 

74 if events: 

75 data['events'] = str_or_collection(events) 

76 if labels: 

77 data['labels'] = str_or_collection(labels) 

78 

79 # Automatically guess category based on prefix of preferred event 

80 preferred_event = kwargs.get('preferred_event') 

81 if preferred_event is not None: 

82 category = SUPEREVENT_CATEGORIES[preferred_event[0]] 

83 data['category'] = category 

84 

85 # FIXME: superevent creation requests must be JSON-encoded rather than 

86 # form-encoded due to https://git.ligo.org/lscsoft/gracedb/issues/195 

87 if superevent_id is None: 

88 return super().create_or_update(superevent_id, json=data) 

89 else: 

90 # FIXME: GraceDB does not support 'put' here, only 'patch'! 

91 # This is inconsistent between events and superevents. 

92 url = join(self.url, superevent_id) + '/' 

93 return self.session.patch(url, json=data)