|
107 | 107 | "### Optional export processors" |
108 | 108 | ] |
109 | 109 | }, |
110 | | - { |
111 | | - "cell_type": "code", |
112 | | - "execution_count": null, |
113 | | - "metadata": {}, |
114 | | - "outputs": [ |
115 | | - { |
116 | | - "name": "stdout", |
117 | | - "output_type": "stream", |
118 | | - "text": [ |
119 | | - "False False False\n" |
120 | | - ] |
121 | | - } |
122 | | - ], |
123 | | - "source": [ |
124 | | - "print(\n", |
125 | | - " get_config().black_formatting,\n", |
126 | | - " get_config().ruff_formatting,\n", |
127 | | - " get_config().ruff_fixing,\n", |
128 | | - ")" |
129 | | - ] |
130 | | - }, |
131 | 110 | { |
132 | 111 | "cell_type": "code", |
133 | 112 | "execution_count": null, |
|
160 | 139 | "test_eq(_cell.source, 'import ast\\n\\nj = [1, 2, 3]\\nfor i in j:\\n str(1)')" |
161 | 140 | ] |
162 | 141 | }, |
163 | | - { |
164 | | - "cell_type": "code", |
165 | | - "execution_count": null, |
166 | | - "metadata": {}, |
167 | | - "outputs": [], |
168 | | - "source": [ |
169 | | - "#| export\n", |
170 | | - "def ruff_format_cell(cell, # Cell to format\n", |
171 | | - " force=False): # Turn ruff formatting on regardless of settings.ini\n", |
172 | | - " \"Processor to format code with `ruff format`\"\n", |
173 | | - " try: cfg = get_config()\n", |
174 | | - " except FileNotFoundError: return\n", |
175 | | - " if (not cfg.ruff_formatting and not force) or cell.cell_type != 'code': return\n", |
176 | | - " \n", |
177 | | - " try: import subprocess\n", |
178 | | - " except: raise ImportError(\"subprocess is required\")\n", |
179 | | - " \n", |
180 | | - " try:\n", |
181 | | - " subprocess.run(['ruff', '--version'], capture_output=True, check=True)\n", |
182 | | - " except (subprocess.CalledProcessError, FileNotFoundError):\n", |
183 | | - " raise ImportError(\"You must install ruff: `pip install ruff` if you wish to use ruff formatting with nbdev\")\n", |
184 | | - " else:\n", |
185 | | - " try:\n", |
186 | | - " result = subprocess.run(\n", |
187 | | - " ['ruff', 'format', '-'],\n", |
188 | | - " input=cell.source,\n", |
189 | | - " capture_output=True,\n", |
190 | | - " text=True,\n", |
191 | | - " check=False\n", |
192 | | - " )\n", |
193 | | - " if result.returncode == 0:\n", |
194 | | - " cell.source = result.stdout.strip()\n", |
195 | | - " except:\n", |
196 | | - " pass" |
197 | | - ] |
198 | | - }, |
199 | | - { |
200 | | - "cell_type": "code", |
201 | | - "execution_count": null, |
202 | | - "metadata": {}, |
203 | | - "outputs": [], |
204 | | - "source": [ |
205 | | - "_cell = read_nb('../../tests/export_procs.ipynb')['cells'][0]\n", |
206 | | - "ruff_format_cell(_cell, force=True)\n", |
207 | | - "test_eq(_cell.source, 'import ast\\n\\nj = [1, 2, 3]\\nfor i in j:\\n str(1)')" |
208 | | - ] |
209 | | - }, |
210 | | - { |
211 | | - "cell_type": "code", |
212 | | - "execution_count": null, |
213 | | - "metadata": {}, |
214 | | - "outputs": [], |
215 | | - "source": [ |
216 | | - "#| export\n", |
217 | | - "def ruff_fix_cell(cell, # Cell to lint and fix\n", |
218 | | - " force=False): # Turn ruff fixing on regardless of settings.ini\n", |
219 | | - " \"Processor to lint and auto-fix code with `ruff check --fix`\"\n", |
220 | | - " try: cfg = get_config()\n", |
221 | | - " except FileNotFoundError: return\n", |
222 | | - " if (not cfg.ruff_fixing and not force) or cell.cell_type != 'code': return\n", |
223 | | - " \n", |
224 | | - " try: import subprocess\n", |
225 | | - " except: raise ImportError(\"subprocess is required\")\n", |
226 | | - "\n", |
227 | | - " try:\n", |
228 | | - " subprocess.run(['ruff', '--version'], capture_output=True, check=True)\n", |
229 | | - " except (subprocess.CalledProcessError, FileNotFoundError):\n", |
230 | | - " raise ImportError(\"You must install ruff: `pip install ruff` if you wish to use ruff formatting with nbdev\")\n", |
231 | | - " else:\n", |
232 | | - " try:\n", |
233 | | - " result = subprocess.run(\n", |
234 | | - " ['ruff', 'check', '--fix', '-'],\n", |
235 | | - " input=cell.source,\n", |
236 | | - " capture_output=True,\n", |
237 | | - " text=True,\n", |
238 | | - " check=False\n", |
239 | | - " )\n", |
240 | | - " if result.returncode == 0 or result.returncode == 1: # 1 means fixes were applied\n", |
241 | | - " cell.source = result.stdout.strip() if result.stdout else cell.source\n", |
242 | | - " except:\n", |
243 | | - " pass" |
244 | | - ] |
245 | | - }, |
246 | | - { |
247 | | - "cell_type": "code", |
248 | | - "execution_count": null, |
249 | | - "metadata": {}, |
250 | | - "outputs": [], |
251 | | - "source": [ |
252 | | - "_cell = read_nb('../../tests/export_procs.ipynb')['cells'][0]\n", |
253 | | - "ruff_fix_cell(_cell, force=True)\n", |
254 | | - "test_eq(_cell.source, 'j = [1,\\n 2,\\n 3\\n]\\nfor i in j:\\n str(1)')" |
255 | | - ] |
256 | | - }, |
257 | 142 | { |
258 | 143 | "cell_type": "code", |
259 | 144 | "execution_count": null, |
|
336 | 221 | "outputs": [], |
337 | 222 | "source": [ |
338 | 223 | "# every optional processor should be explicitly listed here\n", |
339 | | - "test_eq(optional_procs(), ['black_format', 'ruff_format_cell', 'ruff_fix_cell', 'scrub_magics'])" |
| 224 | + "test_eq(optional_procs(), ['black_format', 'scrub_magics'])" |
340 | 225 | ] |
341 | 226 | }, |
342 | 227 | { |
|
0 commit comments