Merge branch 'master' of ssh://lausser,shinken@shinken.git.sourceforge.net/gitroot...
[shinken.git] / shinken / comment.py
blob280f6df01d19c38f65895df200d57bea33ca3e79
1 #!/usr/bin/env python
2 #Copyright (C) 2010 Gabes Jean, naparuba@gmail.com
4 #This file is part of Shinken.
6 #Shinken is free software: you can redistribute it and/or modify
7 #it under the terms of the GNU Affero General Public License as published by
8 #the Free Software Foundation, either version 3 of the License, or
9 #(at your option) any later version.
11 #Shinken is distributed in the hope that it will be useful,
12 #but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 #GNU Affero General Public License for more details.
16 #You should have received a copy of the GNU Affero General Public License
17 #along with Shinken. If not, see <http://www.gnu.org/licenses/>.
20 import time
22 class Comment:
23 id = 1
25 properties = {
26 'entry_time': None,
27 'persistent': None,
28 'author': None,
29 'comment': None,
30 'comment_type': None,
31 'entry_type': None,
32 'source': None,
33 'expires': None,
34 'expire_time': None,
35 'can_be_deleted': None,
36 # TODO: find a very good way to handle the downtime "ref"
37 # ref must effectively not be in properties because it points onto a real object.
38 # 'ref': None
44 #Adds a comment to a particular service. If the "persistent" field
45 #is set to zero (0), the comment will be deleted the next time
46 #Nagios is restarted. Otherwise, the comment will persist
47 #across program restarts until it is deleted manually.
48 def __init__(self, ref, persistent, author, comment, comment_type, entry_type, source, expires, expire_time):
49 self.id = self.__class__.id
50 self.__class__.id += 1
51 self.ref = ref #pointer to srv or host we are apply
52 self.entry_time = int(time.time())
53 self.persistent = persistent
54 self.author = author
55 self.comment = comment
56 #Now the hidden attributes
57 #HOST_COMMENT=1,SERVICE_COMMENT=2
58 self.comment_type = comment_type
59 #USER_COMMENT=1,DOWNTIME_COMMENT=2,FLAPPING_COMMENT=3,ACKNOWLEDGEMENT_COMMENT=4
60 self.entry_type = entry_type
61 #COMMENTSOURCE_INTERNAL=0,COMMENTSOURCE_EXTERNAL=1
62 self.source = source
63 self.expires = expires
64 self.expire_time = expire_time
65 self.can_be_deleted = False
68 def __str__(self):
69 return "Comment id=%d %s" % (self.id, self.comment)
72 #Call by picle for dataify the ackn
73 #because we DO NOT WANT REF in this pickleisation!
74 def __getstate__(self):
75 cls = self.__class__
76 # id is not in *_properties
77 res = { 'id' : self.id }
78 for prop in cls.properties:
79 if hasattr(self, prop):
80 res[prop] = getattr(self, prop)
81 return res
84 #Inversed funtion of getstate
85 def __setstate__(self, state):
86 cls = self.__class__
88 # Maybe it's not a dict but a list like in the old 0.4 format
89 # so we should call the 0.4 function for it
90 if isinstance(state, list):
91 self.__setstate_deprecated__(state)
92 return
94 self.id = state['id']
95 for prop in cls.properties:
96 if prop in state:
97 setattr(self, prop, state[prop])
99 # to prevent duplicate id in comments:
100 if self.id >= cls.id:
101 cls.id = self.id + 1
103 # Theses 2 functions are DEPRECATED and will be removed in a future version of
104 # Shinken. They should not be useful any more after a first load/save pass.
106 #Inversed funtion of getstate
107 def __setstate_deprecated__(self, state):
108 cls = self.__class__
109 #Check if the len of this state is like the previous,
110 # if not, we will do errors!
111 # -1 because of the 'id' prop
112 if len(cls.properties) != (len(state) - 1):
113 print "Passing comment"
114 return
116 self.id = state.pop()
117 for prop in cls.properties:
118 val = state.pop()
119 setattr(self, prop, val)
120 if self.id >= cls.id:
121 cls.id = self.id + 1