Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions pybash/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def fstring_interpolate(token_string: str, parsed_command: str) -> str:
for sub in subs:
parsed_command = re.sub(pattern, '" + f\"\"\"{' + sub + '}\"\"\" + "', parsed_command, 1)

return parsed_command
return parsed_command.replace('"" + f"""', 'f"""').replace('""" + ""', '"""')

@staticmethod
def direct_interpolate(string: str) -> str:
Expand All @@ -73,7 +73,8 @@ def direct_interpolate(string: str) -> str:
if matches and any(any(bad_char in match for bad_char in invalid_chars) for match in matches):
raise InvalidInterpolation

return re.sub(r'{{(.+?)}}', r'" + \1 + "', string)
interpolated = re.sub(r'{{(.+?)}}', r'" + \1 + "', string)
return interpolated.replace('"" + ', '').replace(' + ""', '')


class Shelled(Processor):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "PyBash"
version = "0.3.2"
version = "0.3.3"
description = ">execute bash commands from python easily"
authors = ["Jay <jay.github0@gmail.com>"]
readme = "README.md"
Expand Down
11 changes: 4 additions & 7 deletions test_pybash.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,8 @@ def test_shell_commands():


def test_direct_interpolate():
assert run_bash("$git {{command}} {{option}}") == 'subprocess.run(["git","" + command + "","" + option + ""])\n'
assert (
run_bash("$git {{command}} {{process(option)}}")
== 'subprocess.run(["git","" + command + "","" + process(option) + ""])\n'
)
assert run_bash("$git {{command}} {{option}}") == 'subprocess.run(["git",command,option])\n'
assert run_bash("$git {{command}} {{process(option)}}") == 'subprocess.run(["git",command,process(option)])\n'
assert (
run_bash("$k get pods --show-{{display_type}}=true")
== 'subprocess.run(["k","get","pods","--show-" + display_type + "=true"])\n'
Expand All @@ -120,9 +117,9 @@ def test_direct_interpolate():
def test_fstring_interpolate():
assert (
run_bash("$kubectl get pods f{\"--\" + \"-\".join(['show', 'labels'])} -n f{ namespace }")
== 'subprocess.run(["kubectl","get","pods","" + f"""{"--" + "-".join([\'show\', \'labels\'])}""" + "","-n","" + f"""{ namespace }""" + ""])\n'
== 'subprocess.run(["kubectl","get","pods",f"""{"--" + "-".join([\'show\', \'labels\'])}""","-n",f"""{ namespace }"""])\n'
)
assert run_bash("$git f{options['h']}") == 'subprocess.run(["git","" + f"""{options[\'h\']}""" + ""])\n'
assert run_bash("$git f{options['h']}") == 'subprocess.run(["git",f"""{options[\'h\']}"""])\n'


def test_invalid_interpolate():
Expand Down