|
5 | 5 | from textwrap import indent |
6 | 6 | from typing import Any, Dict, Generic, Type, TypeVar |
7 | 7 |
|
8 | | -import black |
9 | 8 | import bqplot |
10 | 9 | import ipywidgets |
11 | 10 | import ipywidgets as widgets # type: ignore |
@@ -106,9 +105,28 @@ def get_element_class(self, cls): |
106 | 105 | def get_ignore_props(self, cls): |
107 | 106 | return self.ignore_props |
108 | 107 |
|
| 108 | + # Replicated from https://gist.github.com/shner-elmo/b2639a4d1e04ceafaad120acfb31213c |
| 109 | + def ruff_format(self, code: str) -> str: |
| 110 | + import subprocess |
| 111 | + from ruff.__main__ import find_ruff_bin |
| 112 | + |
| 113 | + ruff_args = [ |
| 114 | + find_ruff_bin(), |
| 115 | + "format", |
| 116 | + "--stdin-filename", |
| 117 | + "foo.py", # you can pass any random string, it wont use it... |
| 118 | + # these two lines are optional, but this is how you can pass the config for ruff |
| 119 | + "--line-length", |
| 120 | + f"{MAX_LINE_LENGTH}", |
| 121 | + ] |
| 122 | + proc = subprocess.run(ruff_args, input=code, text=True, capture_output=True) |
| 123 | + proc.check_returncode() # raise an Exception if return code is not 0 |
| 124 | + return proc.stdout |
| 125 | + |
109 | 126 | def generate_component(self, cls: Type[widgets.Widget], blacken=True): |
110 | 127 | element_class_name = self.get_element_class(cls).__name__ |
111 | 128 | ignore = self.get_ignore_props(cls) |
| 129 | + |
112 | 130 | traits = {key: value for key, value in cls.class_traits().items() if "output" not in value.metadata and not key.startswith("_") and key not in ignore} |
113 | 131 |
|
114 | 132 | def has_default(trait): |
@@ -293,9 +311,8 @@ def {{ method_name }}(**kwargs): |
293 | 311 | ) |
294 | 312 |
|
295 | 313 | if blacken: |
296 | | - mode = black.Mode(line_length=MAX_LINE_LENGTH) |
297 | 314 | try: |
298 | | - code = black.format_file_contents(code, fast=False, mode=mode) |
| 315 | + code = self.ruff_format(code) |
299 | 316 | except Exception: |
300 | 317 | print("code:\n", code) |
301 | 318 | raise |
@@ -325,10 +342,7 @@ def generate(self, path, blacken=True): |
325 | 342 | raise ValueError(f"Could not find new line after marker: {marker!r}") |
326 | 343 | code_total = current_code[: start + 1] + "\n" + code |
327 | 344 | if blacken: |
328 | | - import black |
329 | | - |
330 | | - mode = black.Mode(line_length=MAX_LINE_LENGTH) |
331 | | - code_total = black.format_file_contents(code_total, fast=False, mode=mode) |
| 345 | + code_total = self.ruff_format(code_total) |
332 | 346 | only_valid = True |
333 | 347 | if only_valid: |
334 | 348 | try: |
|
0 commit comments