Skip to main content

Support running pyfmt outside a venv

ID
630ec3c
date
2024-03-30 18:12:04+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
cd5df99
message
Support running pyfmt outside a venv
changed files
1 file, 23 additions, 2 deletions

Changed files

fish_functions/pyfmt.fish (615) → fish_functions/pyfmt.fish (1072)

diff --git a/fish_functions/pyfmt.fish b/fish_functions/pyfmt.fish
index 2daedbf..d208ebd 100644
--- a/fish_functions/pyfmt.fish
+++ b/fish_functions/pyfmt.fish
@@ -1,3 +1,24 @@
+# Run some basic Python formatting over a directory.
+#
+# This uses black and flake8; if they're not available in the current
+# virtualenv, it will fall back to copies in the scripts repo.
+
+function _run_black
+    if which black
+        black $argv
+    else
+        ~/repos/scripts/.venv/bin/black $argv
+    end
+end
+
+function _run_flake8
+    if which flake8
+        flake8 $argv
+    else
+        ~/repos/scripts/.venv/bin/flake8 $argv
+    end
+end
+
 function pyfmt --description "Run Python formatting over a directory"
     if test (count $argv) -eq 0
         set root $PWD
@@ -5,13 +26,13 @@ function pyfmt --description "Run Python formatting over a directory"
         set root $argv[1]
     end
 
-    black "$root"
+    _run_black "$root"
 
     # E501 = line too long; anything up to 100-ish is fine in my book
     # (the "ish" is intentional; see https://www.youtube.com/watch?v=wf-BqAjZb8M)
     #
     # E203/W503/W504 = this is where black and flake8 conflict, see https://black.readthedocs.io/en/stable/faq.html#why-are-flake8-s-e203-and-w503-violated
-    flake8 \
+    _run_flake8 \
         --ignore=E501,E203,W503 --extend-select=W504 \
         --exclude .venv \
         "$root"