44
55from .cmd import launchctl
66from .plist import discover_filename
7+ from .util import convert_NSDictionary_to_dict
78
89
910class LaunchdJob (object ):
@@ -54,40 +55,37 @@ def properties(self):
5455 '''
5556 This is a lazily loaded dictionary containing the launchd runtime
5657 information of the job in question. Internally, this is retrieved
57- using `launchctl -x LABEL`. Keep in mind that some dictionary keys
58- are not always present (for example 'PID').
58+ using ServiceManagement.SMJobCopyDictionary(). Keep in mind that
59+ some dictionary keys are not always present (for example 'PID').
5960 If the job specified by the label cannot be found in launchd, then
6061 this method raises a ValueError exception.
6162 '''
63+ if hasattr (self , '_nsproperties' ):
64+ self ._properties = convert_NSDictionary_to_dict (self ._nsproperties )
65+ del self ._nsproperties
66+ #self._nsproperties = None
6267 if self ._properties is None :
6368 self .refresh ()
6469 return self ._properties
6570
6671 def exists (self ):
67- from subprocess import CalledProcessError
68- try :
69- _ = job_properties (self .label )
70- except (CalledProcessError , ValueError ):
71- return False
72- else :
73- return True
72+ return ServiceManagement .SMJobCopyDictionary (None , self .label ) != None
7473
7574 def refresh (self ):
76- from subprocess import CalledProcessError
77- try :
78- self ._properties = job_properties (self .label )
79- except (CalledProcessError , ValueError ):
75+ val = ServiceManagement .SMJobCopyDictionary (None , self .label )
76+ if val is None :
8077 self ._reset ()
81- raise ValueError ("The job ( '%s') does not exist! " % self .label )
78+ raise ValueError ("job '%s' does not exist" % self .label )
8279 else :
80+ self ._properties = convert_NSDictionary_to_dict (val )
8381 # update pid and laststatus attributes
84- if 'PID' in self . _properties :
82+ try :
8583 self ._pid = self ._properties ['PID' ]
86- else :
84+ except KeyError :
8785 self ._pid = None
88- if 'LastExitStatus' in self . _properties :
86+ try :
8987 self ._laststatus = self ._properties ['LastExitStatus' ]
90- else :
88+ except KeyError :
9189 self ._laststatus = None
9290
9391 @property
@@ -101,28 +99,22 @@ def plistfilename(self):
10199 return self ._plist_fname
102100
103101
104- def job_properties (joblabel ):
105- val = ServiceManagement .SMJobCopyDictionary (None , joblabel )
106- if val is None :
107- raise ValueError ("job %s does not exist" % joblabel )
108- return dict (val )
109-
110-
111102def jobs ():
112103 for entry in ServiceManagement .SMCopyAllJobDictionaries (None ):
113- if entry ['Label' ].startswith ("0x" ):
114- continue
115104 label = entry ['Label' ]
116- if 'PID' in entry :
105+ if label .startswith ("0x" ):
106+ continue
107+ try :
117108 pid = int (entry ['PID' ])
118- else :
109+ except KeyError :
119110 pid = None
120- if 'LastExitStatus' in entry :
111+ try :
121112 status = int (entry ['LastExitStatus' ])
122- else :
113+ except KeyError :
123114 status = None
124- yield LaunchdJob (label , pid , status )
125-
115+ job = LaunchdJob (label , pid , status )
116+ job ._nsproperties = entry
117+ yield job
126118
127119
128120def start ():
0 commit comments