@@ -183,9 +183,9 @@ def unescape(self, line: str) -> str:
183
183
def handle_line_ending_and_magic (func ):
184
184
@wraps (func )
185
185
def wrapped (self , code : str , notebook : bool , ** options ) -> str :
186
- if any (code . startswith ( f"% { lang } " ) for lang in INCOMPATIBLE_MAGIC_LANGUAGES ) or any (
187
- code .startswith (f"%% { lang } " ) for lang in INCOMPATIBLE_MAGIC_LANGUAGES
188
- ):
186
+ if any (
187
+ code .startswith (f"%{ lang } " ) for lang in INCOMPATIBLE_MAGIC_LANGUAGES
188
+ ) or any ( code . startswith ( f"%% { lang } " ) for lang in INCOMPATIBLE_MAGIC_LANGUAGES ) :
189
189
logger .info ("Non compatible magic language cell block detected, ignoring." )
190
190
return code
191
191
@@ -382,7 +382,9 @@ def format_code(self, code: str, notebook: bool, **options) -> str:
382
382
from rpy2 .robjects import conversion , default_converter
383
383
384
384
with conversion .localconverter (default_converter ):
385
- format_r = rpackages .importr (self .package_name , robject_translations = {".env" : "env" })
385
+ format_r = rpackages .importr (
386
+ self .package_name , robject_translations = {".env" : "env" }
387
+ )
386
388
formatted_code = format_r .tidy_source (text = code , output = False , ** options )
387
389
return "\n " .join (formatted_code [0 ])
388
390
@@ -398,7 +400,9 @@ def format_code(self, code: str, notebook: bool, **options) -> str:
398
400
399
401
with conversion .localconverter (default_converter ):
400
402
styler_r = rpackages .importr (self .package_name )
401
- formatted_code = styler_r .style_text (code , ** self ._transform_options (styler_r , options ))
403
+ formatted_code = styler_r .style_text (
404
+ code , ** self ._transform_options (styler_r , options )
405
+ )
402
406
return "\n " .join (formatted_code )
403
407
404
408
@staticmethod
@@ -418,13 +422,17 @@ def _transform_options(styler_r, options):
418
422
419
423
if "reindention" in transformed_options :
420
424
if isinstance (options ["reindention" ], dict ):
421
- transformed_options ["reindention" ] = rpy2 .robjects .ListVector (options ["reindention" ])
425
+ transformed_options ["reindention" ] = rpy2 .robjects .ListVector (
426
+ options ["reindention" ]
427
+ )
422
428
else :
423
429
transformed_options ["reindention" ] = rpy2 .robjects .ListVector (
424
430
getattr (styler_r , options ["reindention" ])()
425
431
)
426
432
return transformed_options
427
433
434
+ class FormatterError (Exception ):
435
+ pass
428
436
429
437
class CommandLineFormatter (BaseFormatter ):
430
438
command : List [str ]
@@ -441,7 +449,9 @@ def importable(self) -> bool:
441
449
return command_exist (self .command [0 ])
442
450
443
451
@handle_line_ending_and_magic
444
- def format_code (self , code : str , notebook : bool , args : List [str ] = [], ** options ) -> str :
452
+ def format_code (
453
+ self , code : str , notebook : bool , args : List [str ] = [], ** options
454
+ ) -> str :
445
455
process = subprocess .run (
446
456
self .command + args ,
447
457
input = code ,
@@ -451,9 +461,14 @@ def format_code(self, code: str, notebook: bool, args: List[str] = [], **options
451
461
)
452
462
453
463
if process .stderr :
454
- logger .info (f"An error with { self .command [0 ]} has occurred:" )
455
- logger .info (process .stderr )
456
- return code
464
+ raise FormatterError (
465
+ f"Error running `{ self .command [0 ]} `:\n " + process .stderr
466
+ )
467
+ elif process .returncode != 0 :
468
+ raise FormatterError (
469
+ f"Formatter `{ self .command [0 ]} ` exited with status { process .returncode } "
470
+ + process .stderr
471
+ )
457
472
else :
458
473
return process .stdout
459
474
0 commit comments