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 

19 

20class Resource: 

21 

22 path = None 

23 

24 def __init__(self, parent=None, path=None): 

25 self.session = parent.session 

26 self.parent = parent 

27 self.url = self.parent.url 

28 if path is None: 

29 path = self.path 

30 if path is not None: 

31 self.url = join(self.url, str(path)) 

32 

33 def get(self, **kwargs): 

34 return self.session.get(self.url, **kwargs).json() 

35 

36 

37class Deletable: 

38 

39 def delete(self, key): 

40 self.session.delete(join(self.url, str(key))) 

41 

42 

43class Mutable: 

44 

45 def create_or_update(self, key, **kwargs): 

46 if key is None: 

47 return self.session.post(self.url, **kwargs).json() 

48 else: 

49 return self.session.put(join(self.url, str(key)), **kwargs) 

50 

51 def create(self, **kwargs): 

52 return self.create_or_update(None, **kwargs) 

53 

54 def update(self, key, **kwargs): 

55 return self.create_or_update(key, **kwargs) 

56 

57 

58class Mapping: 

59 

60 def __getitem__(self, key): 

61 return self.mapped_class(self, key)