Skip to content

Commit de9e825

Browse files
Improve the string functionality
1 parent 2380123 commit de9e825

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

bashparser/ast.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,35 +38,46 @@ def apply_fn(node):
3838
if node.kind in self.passable_nodes: return CONT
3939
elif node.kind == 'operator': word = node.op
4040
elif node.kind == 'pipe': word = node.pipe
41-
elif node.kind == 'redirect': word = node.type + ' '
41+
elif node.kind == 'redirect':
42+
if type(node.input) == bashlex.ast.node:
43+
word += str(NodeVisitor(node.input))
44+
elif node.input is not None:
45+
word += str(node.input)
46+
word += str(node.type)
47+
if type(node.output) == bashlex.ast.node:
48+
word += str(NodeVisitor(node.output))
49+
elif node.output is not None:
50+
word += str(node.output)
51+
self._string = self._string + ' ' + word
52+
return DONT_DESCEND
4253
elif node.kind == 'compound':
4354
for part in node.list:
44-
word += str(NodeVisitor(part)) + ' '
55+
word += ' ' + str(NodeVisitor(part))
4556
if hasattr(node, 'redirects'):
4657
for part in node.redirects:
47-
word += str(NodeVisitor(part)) + ' '
48-
self._string = self._string + word + ' '
58+
word += ' ' + str(NodeVisitor(part))
59+
self._string = self._string + ' ' + word
4960
return DONT_DESCEND
5061
elif node.kind == 'commandsubstitution':
5162
word = '$('
5263
cmd = node.command
5364
for part in cmd.parts:
5465
word += str(NodeVisitor(part)) + ' '
5566
word = word[:-1] + ')'
56-
self._string = self._string + word + ' '
67+
self._string = self._string + ' ' + word
5768
return DONT_DESCEND
5869

5970
elif hasattr(node, 'word'):
6071
word = node.word
61-
self._string = self._string + word + ' '
72+
self._string = self._string + ' ' + word
6273
return DONT_DESCEND
6374
else:
6475
raise ValueError('Error! Unsupported node kind encountered when converting NodeVisitor to string: ', node.kind)
65-
self._string = self._string + word + ' '
76+
self._string = self._string + ' ' + word
6677
return CONT
6778

6879
self.apply(apply_fn)
69-
return self._string[:-1] # remove trailing space cause wrong
80+
return self._string.strip() # remove surrounding spaces cause wrong
7081

7182

7283
def __type__(self):
@@ -243,8 +254,10 @@ def children(self, root = None):
243254
elif k == 'redirect':
244255
if isinstance(root.output, bashlex.ast.node):
245256
return [ root.output ]
246-
if root.heredoc:
257+
elif root.heredoc:
247258
return [ root.heredoc[child_num] ]
259+
else:
260+
return []
248261
elif hasattr(root, 'list'):
249262
return root.list
250263
else:

bashparser/test/test_ast.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,14 @@ def test_str(self):
3838
rebuilt_commands = str(NodeVisitor(nodes[0]))
3939
expected_result = "for a in $n do wget that ; cd there ; mv here ; a=b ; done"
4040
self.assertTrue(rebuilt_commands == expected_result)
41-
4241
self.assertTrue(str(NodeVisitor(None)) == '')
43-
42+
"""
43+
node_string = 'history -n >/dev/null 2>&1'
44+
node = bashlex.parse(node_string)[0]
45+
rebuilt_command = str(NodeVisitor(node))
46+
expected_result = 'history -n >/dev/null 2>&1'
47+
self.assertTrue(rebuilt_command == expected_result)
48+
"""
4449

4550
def test_apply(self):
4651
# Build ast to test apply on

0 commit comments

Comments
 (0)