| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -129,6 +129,7 @@ Creates an instance of `PythonShell` and starts the Python process | |||
| 129 | 129 | * `text`: each line of data (ending with "\n") is emitted as a message (default) | |
| 130 | 130 | * `json`: each line of data (ending with "\n") is parsed as JSON and emitted as a message | |
| 131 | 131 | * `binary`: data is streamed as-is through `stdout` and `stdin` | |
| 132 | + * `parser`: each line of data (ending with "\n") is parsed with this customer parser (overrides `mode`) | ||
| 132 | 133 | * `pythonPath`: The path where to locate the "python" executable. Default: "python" | |
| 133 | 134 | * `pythonOptions`: Array of option switches to pass to "python" | |
| 134 | 135 | * `scriptPath`: The default path where to look for scripts. Default: "./python" | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -42,6 +42,7 @@ var PythonShell = function (script, options) { | |||
| 42 | 42 | this.script = path.join(options.scriptPath || './python', script); | |
| 43 | 43 | this.command = pythonOptions.concat(this.script, scriptArgs); | |
| 44 | 44 | this.mode = options.mode || 'text'; | |
| 45 | + this.parser = options.parser; | ||
| 45 | 46 | this.terminated = false; | |
| 46 | 47 | this.childProcess = spawn(pythonPath, this.command, options); | |
| 47 | 48 | ||
@@ -185,21 +186,21 @@ PythonShell.prototype.receive = function (data) { | |||
| 185 | 186 | this._remaining = lastLine; | |
| 186 | 187 | ||
| 187 | 188 | lines.forEach(function (line) { | |
| 188 | - if (self.mode === 'json') { | ||
| 189 | + if (self.parser) { | ||
| 189 | 190 | try { | |
| 190 | - self.emit('message', JSON.parse(line)); | ||
| 191 | - } catch (err) { | ||
| 191 | + self.emit('message', self.parser(line)); | ||
| 192 | + } catch(err) { | ||
| 192 | 193 | self.emit('error', extend( | |
| 193 | - new Error('invalid JSON message: ' + data + ' >> ' + err), | ||
| 194 | + new Error('invalid message: ' + data + ' >> ' + err), | ||
| 194 | 195 | { inner: err, data: line} | |
| 195 | 196 | )); | |
| 196 | 197 | } | |
| 197 | - } else if (typeof self.mode === 'function') { | ||
| 198 | + } else if (self.mode === 'json') { | ||
| 198 | 199 | try { | |
| 199 | - self.emit('message', self.mode(line)); | ||
| 200 | - } catch(err) { | ||
| 200 | + self.emit('message', JSON.parse(line)); | ||
| 201 | + } catch (err) { | ||
| 201 | 202 | self.emit('error', extend( | |
| 202 | - new Error('invalid message: ' + data + ' >> ' + err), | ||
| 203 | + new Error('invalid JSON message: ' + data + ' >> ' + err), | ||
| 203 | 204 | { inner: err, data: line} | |
| 204 | 205 | )); | |
| 205 | 206 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments