--- projects/pystatgrab/_statgrab.pyx 2005/07/30 18:16:14 1.17 +++ projects/pystatgrab/_statgrab.pyx 2008/06/16 21:06:49 1.21 @@ -17,7 +17,7 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # -# $Id: _statgrab.pyx,v 1.17 2005/07/30 18:16:14 tdb Exp $ +# $Id: _statgrab.pyx,v 1.21 2008/06/16 21:06:49 tdb Exp $ # ctypedef long time_t @@ -27,6 +27,8 @@ ctypedef int gid_t cdef extern from "statgrab.h": cdef extern int sg_init() + cdef extern int sg_shutdown() + cdef extern int sg_snapshot() cdef extern int sg_drop_privileges() ctypedef enum sg_error: @@ -272,16 +274,17 @@ py_SG_PROCESS_STATE_ZOMBIE = SG_PROCESS_STATE_ZOMBIE py_SG_PROCESS_STATE_UNKNOWN = SG_PROCESS_STATE_UNKNOWN -class Result: +class Result(dict): def __init__(self, attrs): - self.attrs = attrs - for attr in attrs: - setattr(self, attr, attrs[attr]) - def __getitem__(self, item): - return getattr(self, item) - def __repr__(self): - return str(self.attrs) + self.attrs = attrs # to maintain compatibility + super(Result, self).__init__(attrs) + def __getattr__(self, item): + try: + return self.__getitem__(item) + except KeyError: + raise AttributeError(item) + class StatgrabException(Exception): def __init__(self, value): self.value = value @@ -295,6 +298,18 @@ def py_sg_init(): else: return False +def py_sg_shutdown(): + if sg_shutdown() == 0: + return True + else: + return False + +def py_sg_snapshot(): + if sg_snapshot() == 0: + return True + else: + return False + def py_sg_drop_privileges(): if sg_drop_privileges() == 0: return True @@ -597,13 +612,17 @@ def py_sg_get_process_stats(): raise StatgrabException, 'sg_get_process_stats() returned NULL' list = [] for i from 0 <= i < entries: - if s.process_name == NULL: - s.process_name = '' - if s.proctitle == NULL: - s.proctitle = '' + if s.process_name is NULL: + process_name = '' + else: + process_name = s.process_name + if s.proctitle is NULL: + proctitle = '' + else: + proctitle = s.proctitle list.append(Result( - {'process_name': s.process_name, - 'proctitle' : s.proctitle, + {'process_name': process_name, + 'proctitle' : proctitle, 'pid' : s.pid, 'parent' : s.parent, 'pgid' : s.pgid,