BOSWatch 3
Python Script to receive and decode German BOS Information with rtl_fm and multimon-NG
 
Loading...
Searching...
No Matches
boswatch.processManager.ProcessManager Class Reference

class to manage a extern sub process More...

Public Member Functions

def __init__ (self, process, textMode=False)
 
def addArgument (self, arg)
 add a new argument
 
def clearArguments (self)
 clear all arguments
 
def start (self)
 start the new process
 
def stop (self)
 Stop the process by sending SIGTERM and wait for ending.
 
def readline (self)
 Read one line from stdout stream.
 
def skipLines (self, lineCount=1)
 Skip given number of lines from the output.
 
def skipLinesUntil (self, matchText)
 Skip lines from the output until the given string is in it.
 
def setStdin (self, stdin)
 Set the stdin stream instance.
 
def setStdout (self, stdout)
 Set the stdout stream instance.
 
def setStderr (self, stderr)
 Set the stderr stream instance.
 
def stdout (self)
 Property to get the stdout stream.
 
def stderr (self)
 Property to get the stderr stream.
 
def isRunning (self)
 Property to get process running state.
 

Protected Attributes

 _args
 
 _stdin
 
 _stdout
 
 _stderr
 
 _processHandle
 
 _textMode
 

Detailed Description

class to manage a extern sub process

Constructor & Destructor Documentation

◆ __init__()

def boswatch.processManager.ProcessManager.__init__ (   self,
  process,
  textMode = False 
)
25 def __init__(self, process, textMode=False):
26 logging.debug("create process instance %s - textMode: %s", process, textMode)
27 self._args = []
28 self._args.append(process)
29 self._stdin = None
30 self._stdout = subprocess.PIPE
31 self._stderr = subprocess.STDOUT
32 self._processHandle = None
33 self._textMode = textMode
34

Member Function Documentation

◆ addArgument()

def boswatch.processManager.ProcessManager.addArgument (   self,
  arg 
)

add a new argument

    @param arg: argument to add as string
35 def addArgument(self, arg):
36 r"""!add a new argument
37
38 @param arg: argument to add as string"""
39 logging.debug("add argument to process: %s -> %s", self._args[0], arg)
40 for splitArg in arg.split():
41 self._args.append(splitArg)
42

◆ clearArguments()

def boswatch.processManager.ProcessManager.clearArguments (   self)

clear all arguments

43 def clearArguments(self):
44 r"""!clear all arguments"""
45 self._args = self._args[0:1] # kept first element (process name)
46

◆ start()

def boswatch.processManager.ProcessManager.start (   self)

start the new process

    @return: True or False
47 def start(self):
48 r"""!start the new process
49
50 @return: True or False"""
51 logging.debug("start new process: %s %s", self._args[0], self._args[1:])
52 try:
53 self._processHandle = subprocess.Popen(self._args,
54 stdin=self._stdin,
55 stdout=self._stdout,
56 stderr=self._stderr,
57 universal_newlines=self._textMode,
58 shell=False)
59 if not self.isRunning:
60 logging.error("cannot start process")
61 return False
62 logging.debug("process started with PID %d", self._processHandle.pid)
63 return True
64
65 except FileNotFoundError:
66 logging.error("File not found: %s", self._args[0])
67 return False
68

◆ stop()

def boswatch.processManager.ProcessManager.stop (   self)

Stop the process by sending SIGTERM and wait for ending.

69 def stop(self):
70 r"""!Stop the process by sending SIGTERM and wait for ending"""
71 logging.debug("stopping process: %s", self._args[0])
72 if self.isRunning:
73 self._processHandle.terminate()
74 while self.isRunning:
75 pass
76 logging.debug("process %s returned %d", self._args[0], self._processHandle.returncode)
77

◆ readline()

def boswatch.processManager.ProcessManager.readline (   self)

Read one line from stdout stream.

    @return singe line or None
78 def readline(self):
79 r"""!Read one line from stdout stream
80
81 @return singe line or None"""
82 if self.isRunning and self._stdout is not None:
83 try:
84 line = self._processHandle.stdout.readline().strip()
85 except UnicodeDecodeError:
86 return None
87 return line
88 return None
89

◆ skipLines()

def boswatch.processManager.ProcessManager.skipLines (   self,
  lineCount = 1 
)

Skip given number of lines from the output.

    @param lineCount: number of lines to skip
90 def skipLines(self, lineCount=1):
91 r"""!Skip given number of lines from the output
92
93 @param lineCount: number of lines to skip
94 """
95 logging.debug("skip %d lines from output", lineCount)
96 while self.isRunning and lineCount:
97 self.readline()
98 lineCount -= 1
99

◆ skipLinesUntil()

def boswatch.processManager.ProcessManager.skipLinesUntil (   self,
  matchText 
)

Skip lines from the output until the given string is in it.

    @param matchText: string to search for in output
100 def skipLinesUntil(self, matchText):
101 r"""!Skip lines from the output until the given string is in it
102
103 @param matchText: string to search for in output
104 """
105 logging.debug("skip lines till '%s' from output", matchText)
106 if not self._textMode:
107 matchText = bytes(matchText, "utf-8")
108 while self.isRunning and matchText not in self.readline():
109 pass
110

◆ setStdin()

def boswatch.processManager.ProcessManager.setStdin (   self,
  stdin 
)

Set the stdin stream instance.

111 def setStdin(self, stdin):
112 r"""!Set the stdin stream instance"""
113 self._stdin = stdin
114

◆ setStdout()

def boswatch.processManager.ProcessManager.setStdout (   self,
  stdout 
)

Set the stdout stream instance.

115 def setStdout(self, stdout):
116 r"""!Set the stdout stream instance"""
117 self._stdout = stdout
118

◆ setStderr()

def boswatch.processManager.ProcessManager.setStderr (   self,
  stderr 
)

Set the stderr stream instance.

119 def setStderr(self, stderr):
120 r"""!Set the stderr stream instance"""
121 self._stderr = stderr
122

◆ stdout()

def boswatch.processManager.ProcessManager.stdout (   self)

Property to get the stdout stream.

124 def stdout(self):
125 r"""!Property to get the stdout stream"""
126 return self._processHandle.stdout
127

◆ stderr()

def boswatch.processManager.ProcessManager.stderr (   self)

Property to get the stderr stream.

129 def stderr(self):
130 r"""!Property to get the stderr stream"""
131 return self._processHandle.stderr
132

◆ isRunning()

def boswatch.processManager.ProcessManager.isRunning (   self)

Property to get process running state.

    @return True or False
134 def isRunning(self):
135 r"""!Property to get process running state
136
137 @return True or False"""
138 if self._processHandle:
139 if self._processHandle.poll() is None:
140 return True
141 return False

Field Documentation

◆ _args

boswatch.processManager.ProcessManager._args
protected

◆ _stdin

boswatch.processManager.ProcessManager._stdin
protected

◆ _stdout

boswatch.processManager.ProcessManager._stdout
protected

◆ _stderr

boswatch.processManager.ProcessManager._stderr
protected

◆ _processHandle

boswatch.processManager.ProcessManager._processHandle
protected

◆ _textMode

boswatch.processManager.ProcessManager._textMode
protected