diff --git a/pybash/transformer.py b/pybash/transformer.py index 3f03810..92df1b3 100644 --- a/pybash/transformer.py +++ b/pybash/transformer.py @@ -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: @@ -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): diff --git a/pyproject.toml b/pyproject.toml index a0f13c8..b32ce83 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 "] readme = "README.md" diff --git a/test_pybash.py b/test_pybash.py index a6b03ac..2b982f8 100644 --- a/test_pybash.py +++ b/test_pybash.py @@ -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' @@ -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():