Skip to content

CommandLineFormatter: check process exit code #367

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
35 changes: 25 additions & 10 deletions jupyterlab_code_formatter/formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,9 @@ def unescape(self, line: str) -> str:
def handle_line_ending_and_magic(func):
@wraps(func)
def wrapped(self, code: str, notebook: bool, **options) -> str:
if any(code.startswith(f"%{lang}") for lang in INCOMPATIBLE_MAGIC_LANGUAGES) or any(
code.startswith(f"%%{lang}") for lang in INCOMPATIBLE_MAGIC_LANGUAGES
):
if any(
code.startswith(f"%{lang}") for lang in INCOMPATIBLE_MAGIC_LANGUAGES
) or any(code.startswith(f"%%{lang}") for lang in INCOMPATIBLE_MAGIC_LANGUAGES):
logger.info("Non compatible magic language cell block detected, ignoring.")
return code

Expand Down Expand Up @@ -382,7 +382,9 @@ def format_code(self, code: str, notebook: bool, **options) -> str:
from rpy2.robjects import conversion, default_converter

with conversion.localconverter(default_converter):
format_r = rpackages.importr(self.package_name, robject_translations={".env": "env"})
format_r = rpackages.importr(
self.package_name, robject_translations={".env": "env"}
)
formatted_code = format_r.tidy_source(text=code, output=False, **options)
return "\n".join(formatted_code[0])

Expand All @@ -398,7 +400,9 @@ def format_code(self, code: str, notebook: bool, **options) -> str:

with conversion.localconverter(default_converter):
styler_r = rpackages.importr(self.package_name)
formatted_code = styler_r.style_text(code, **self._transform_options(styler_r, options))
formatted_code = styler_r.style_text(
code, **self._transform_options(styler_r, options)
)
return "\n".join(formatted_code)

@staticmethod
Expand All @@ -418,13 +422,17 @@ def _transform_options(styler_r, options):

if "reindention" in transformed_options:
if isinstance(options["reindention"], dict):
transformed_options["reindention"] = rpy2.robjects.ListVector(options["reindention"])
transformed_options["reindention"] = rpy2.robjects.ListVector(
options["reindention"]
)
else:
transformed_options["reindention"] = rpy2.robjects.ListVector(
getattr(styler_r, options["reindention"])()
)
return transformed_options

class FormatterError(Exception):
pass

class CommandLineFormatter(BaseFormatter):
command: List[str]
Expand All @@ -441,7 +449,9 @@ def importable(self) -> bool:
return command_exist(self.command[0])

@handle_line_ending_and_magic
def format_code(self, code: str, notebook: bool, args: List[str] = [], **options) -> str:
def format_code(
self, code: str, notebook: bool, args: List[str] = [], **options
) -> str:
process = subprocess.run(
self.command + args,
input=code,
Expand All @@ -451,9 +461,14 @@ def format_code(self, code: str, notebook: bool, args: List[str] = [], **options
)

if process.stderr:
logger.info(f"An error with {self.command[0]} has occurred:")
logger.info(process.stderr)
return code
raise FormatterError(
f"Error running `{self.command[0]}`:\n" + process.stderr
)
elif process.returncode != 0:
raise FormatterError(
f"Formatter `{self.command[0]}` exited with status {process.returncode}"
+ process.stderr
)
else:
return process.stdout

Expand Down
Loading