8000 adding parsing of pipe for sink to close #3 · vsoch/nushell-plugin-python@acc969b · GitHub
[go: up one dir, main page]

Skip to content

Commit acc969b

Browse files
committed
adding parsing of pipe for sink to close #3
Signed-off-by: Vanessa Sochat <vsochat@stanford.edu>
1 parent c5e5b5c commit acc969b

File tree

5 files changed

+77
-12
lines changed

5 files changed

+77
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Critical items to know are:
1616
Versions here coincide with releases on pypi.
1717

1818
## [master](https://github.com/vsoch/nushell-plugin-python)
19+
- support for sink pipes (0.0.12)
1920
- adding standalone build examples (0.0.11)
2021
- adding base of package and examples (0.0.1)
2122
- skeleton of package while server under development (0.0.0)

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,29 @@ if __name__ == '__main__':
140140
main()
141141
```
142142

143+
### Parameters
144+
145+
Since you can pipe content into a sink, the piped content is parsed into a list
146+
and passed with params as the `_pipe` key. For example, if we do:
147+
148+
```bash
149+
> ls | get name | hello --name Dinosaur
150+
Hello Dinosaur
151+
```
152+
153+
And then look in the output file, we see that the parsed params include a pipe
154+
of all the named of the listed files (that we generated above)
155+
156+
```bash
157+
PARAMS {'name': 'Dinosaur', '_pipe': ['Makefile', 'README.md', 'Dockerfile', 'nu_plugin_hello', 'Dockerfile.standalone']}
158+
```
143159

144160
### Examples
145161

146162
- [pokemon](examples/pokemon) ascii pokemon on demand!
147163
- [hello](examples/hello) say hello using a sink!
148164

165+
149166
## Single Binary
150167

151168
In that you are able to compile your module with [pyinstaller](https://pyinstaller.readthedocs.io/en/stable/operating-mode.html) (e.g., see [examples/len](examples/len)) you can build your python script as a simple binary, and one that doesn't even need nushell installed as a module anymore. Why might you want to do this? It will mean that your plugin is a single file (binary) and you don't need to rely on modules elsewhere in the system. I suspect there are other ways to compile

nushell/plugin.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,12 @@ def getTag(self):
148148
'''
149149
return {"anchor":None, "span":{"end":0,"start":0}}
150150

151-
152151
def parse_params(self, input_params):
153152
'''parse the parameters into an easier to parse object. An example looks
154-
like the following (I'm not sure why an empty list is passed as a second
155-
entry)
153+
like the following. For a sink - this is the first item in a list
154+
under params. For a filter, it's a dict directly under params.
156155
157-
[{'args': {'positional': None,
156+
{'args': {'positional': None,
158157
'named': {'switch': {'tag': {'anchor': None,
159158
'span': {'start': 58, 'end': 64}},
160159
'item': {'Primitive': {'Boolean': True}}},
@@ -163,15 +162,10 @@ def parse_params(self, input_params):
163162
'optional': {'tag': {'anchor': None, 'span': {'start': 44, 'end': 55}},
164163
'item': {'Primitive': {'String': 'OPTIONALARG'}}}}},
165164
'name_tag': {'anchor': None, 'span': {'start': 0, 'end': 7}}},
166-
[]]
167165
'''
168166
if not input_params:
169167
return input_params
170168

171-
# For some reason sink passes a list
172-
if isinstance(input_params, list):
173-
input_params = input_params[0]
174-
175169
if "args" not in input_params:
176170
return input_params
177171

nushell/sink.py

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,59 @@ class SinkPlugin(PluginBase):
2020
and then passing a temporary file as argument (method "sink")
2121
'''
2222
is_filter = False
23+
parse_pipe = True
24+
25+
def get_params(self, input_params):
26+
'''The input params (under ["params"] is a list, with the first entry
27+
being the args dictionary (that we pass to self.parse_params) and
28+
the remaining being entries that are passed if the sink is used as
29+
a pipe. If not, it looks like an empty list, like below:
30+
31+
[{'args': {'positional': None,
32+
'named': {'switch': {'tag': {'anchor': None,
33+
'span': {'start': 58, 'end': 64}},
34+
'item': {'Primitive': {'Boolean': True}}},
35+
'mandatory': {'tag': {'anchor': None, 'span': {'start': 20, 'end': 32}},
36+
'item': {'Primitive': {'String': 'MANDATORYARG'}}},
37+
'optional': {'tag': {'anchor': None, 'span': {'start': 44, 'end': 55}},
38+
'item': {'Primitive': {'String': 'OPTIONALARG'}}}}},
39+
'name_tag': {'anchor': None, 'span': {'start': 0, 'end': 7}}},
40+
[]]
41+
'''
42+
if not input_params:
43+
return input_params
44+
45+
# Args are always the first entry
46+
args = input_params.pop(0)
47+
params = self.parse_params(args)
48+
49+
# The pipe entries are the rest (pass as _pipe)
50+
params["_pipe"] = self._parse_pipe(input_params)
51+
return params
52+
53+
def _parse_pipe(self, pipeList):
54+
'''parse the list of piped input, typically this means string that
55+
have come from the terminal. To disable this, set the client
56+
parse_pipe to False.
57+
58+
Parameters
59+
==========
60+
pipeList: is the second index of the "params" dict from the request
61+
'''
62+
# No pipe will produce empty list
63+
if not pipeList:
64+
return pipeList
65+
66+
pipeList = pipeList.pop(0)
67+
68+
# Return list of values as the pipe content
69+
entries = []
70+
71+
# Each entry has a tag and item. We want the Primitive (type)
72+
for entry in pipeList:
73+
item = entry['item'].get('Primitive')
74+
entries = entries + list(item.values())
75+
return entries
2376

2477
def run(self, sinkFunc):
2578
'''the main run function is required to take a user sinkFunc.
@@ -43,8 +96,8 @@ def run(self, sinkFunc):
4396
# Case 3: A filter must return the item filtered with a tag
4497
elif method == "sink":
4598

46-
# Parse parameters for the calling sink
47-
params = self.parse_params(x['params'])
99+
# Parse parameters for the calling sink, _pipe included
100+
params = self.get_params(x['params'])
48101
self.logger.info("PARAMS %s" % params)
49102

50103
# The only case of not running is if the user asks for help

nushell/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed
66
# with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
77

8-
__version__ = "0.0.11"
8+
__version__ = "0.0.12"
99
AUTHOR = 'Vanessa Sochat'
1010
AUTHOR_EMAIL = 'vsochat@stanford.edu'
1111
NAME = 'nushell'

0 commit comments

Comments
 (0)
0