Commit 74863506 authored by Lev Lamberov's avatar Lev Lamberov

New upstream version 2016.1

parents
[bumpversion]
current_version = 2016.1
files = py-isort.el
commit = True
tag = True
parse = (?P<major>\d+)\.(?P<minor>\d+)
serialize = {major}.{minor}
buftra.el
language: python
python:
- "3.4"
install:
- pip install isort
script: ./tests.sh
This diff is collapsed.
help:
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
test: ## Run the tests.
./tests.sh
* py-isort.el
[[https://travis-ci.org/paetzke/py-isort.el][https://travis-ci.org/paetzke/py-isort.el.svg?branch=master]]
[[http://melpa.org/#/py-isort][http://melpa.org/packages/py-isort-badge.svg]]
Provides commands, which use the external =isort= tool to tidy up the imports in the current buffer.
Install =isort= and download =py-isort=
#+BEGIN_SRC bash
$ pip install isort
$ wget https://raw.githubusercontent.com/paetzke/py-isort.el/master/py-isort.el \
-O /your/path/py-isort.el
#+END_SRC
You can also install =py-isort= with /MELPA/:
#+BEGIN_SRC lisp
M-x package-install RET
py-isort RET
#+END_SRC
Add the =before-save-hook= to your =~/.emacs=
#+BEGIN_SRC lisp
(add-to-list 'load-path "/your/path/")
(require 'py-isort)
(add-hook 'before-save-hook 'py-isort-before-save)
#+END_SRC
Now every time you save your Python file =isort= will be executed on the current buffer.
To customize the behaviour of =isort= you can set the =py-isort-options= e.g.
#+BEGIN_SRC lisp
(setq py-isort-options '("--lines=100"))
#+END_SRC
** Functions
- =M-x py-isort-buffer RET=: Uses the =isort= tool to reformat the current buffer.
- =M-x py-isort-region RET=: Uses the =isort= tool to reformat the current region.
** Bugs and improvements
Feel free to open tickets or send pull requests with improvements.
These [[https://github.com/paetzke/py-isort.el/graphs/contributors][contributors]] have done so.
#!/bin/bash -eu
main() {
local lead='^;; BEGIN GENERATED -----------------$'
local tail='^;; END GENERATED -------------------$'
wget https://raw.githubusercontent.com/paetzke/buftra.el/master/buftra.el -O buftra.el
sed -i 's/buftra/py-isort-bf/g' buftra.el
sed -i '0,/py-isort-bf/s//buftra/g' buftra.el
sed -i '0,/py-isort-bf/s//buftra/g' buftra.el
sed -i '1i;; !!! This file is generated !!!' buftra.el
sed -i -e "/$lead/,/$tail/{ /$lead/{p; r buftra.el
}; /$tail/p; d }" py-isort.el
}
main
;;; py-isort.el --- Use isort to sort the imports in a Python buffer
;; Copyright (C) 2014, Friedrich Paetzke <paetzke@fastmail.fm>
;; Author: Friedrich Paetzke <paetzke@fastmail.fm>
;; URL: http://paetzke.me/project/py-isort.el
;; Version: 2016.1
;;; Commentary:
;; Provides commands, which use the external "isort" tool
;; to tidy up the imports in the current buffer.
;; To automatically sort imports when saving a python file, use the
;; following code:
;; (add-hook 'before-save-hook 'py-isort-before-save)
;; To customize the behaviour of "isort" you can set the
;; py-isort-options e.g.
;; (setq py-isort-options '("--lines=100"))
;;; Code:
(defgroup py-isort nil
"Use isort to sort the imports in a Python buffer."
:group 'convenience
:prefix "py-isort-")
(defcustom py-isort-options nil
"Options used for isort."
:group 'py-isort
:type '(repeat (string :tag "option")))
(defun py-isort--find-settings-path ()
(expand-file-name
(or (locate-dominating-file buffer-file-name ".isort.cfg")
(file-name-directory buffer-file-name))))
(defun py-isort--call-executable (errbuf file)
(let ((default-directory (py-isort--find-settings-path)))
(zerop (apply 'call-process "isort" nil errbuf nil
(append `(" " , file, " ",
(concat "--settings-path=" default-directory))
py-isort-options)))))
(defun py-isort--call (only-on-region)
(py-isort-bf--apply-executable-to-buffer "isort"
'py-isort--call-executable
only-on-region
"py"))
;;;###autoload
(defun py-isort-region ()
"Uses the \"isort\" tool to reformat the current region."
(interactive)
(py-isort--call t))
;;;###autoload
(defun py-isort-buffer ()
"Uses the \"isort\" tool to reformat the current buffer."
(interactive)
(py-isort--call nil))
;;;###autoload
(defun py-isort-before-save ()
(interactive)
(when (eq major-mode 'python-mode)
(condition-case err (py-isort-buffer)
(error (message "%s" (error-message-string err))))))
;; BEGIN GENERATED -----------------
;; !!! This file is generated !!!
;; buftra.el
;; Copyright (C) 2015, Friedrich Paetzke <paetzke@fastmail.fm>
;; Author: Friedrich Paetzke <paetzke@fastmail.fm>
;; URL: https://github.com/paetzke/buftra.el
;; Version: 0.4
(defun py-isort-bf--apply-rcs-patch (patch-buffer)
"Apply an RCS-formatted diff from PATCH-BUFFER to the current buffer."
(let ((target-buffer (current-buffer))
(line-offset 0))
(save-excursion
(with-current-buffer patch-buffer
(goto-char (point-min))
(while (not (eobp))
(unless (looking-at "^\\([ad]\\)\\([0-9]+\\) \\([0-9]+\\)")
(error "invalid rcs patch or internal error in py-isort-bf--apply-rcs-patch"))
(forward-line)
(let ((action (match-string 1))
(from (string-to-number (match-string 2)))
(len (string-to-number (match-string 3))))
(cond
((equal action "a")
(let ((start (point)))
(forward-line len)
(let ((text (buffer-substring start (point))))
(with-current-buffer target-buffer
(setq line-offset (- line-offset len))
(goto-char (point-min))
(forward-line (- from len line-offset))
(insert text)))))
((equal action "d")
(with-current-buffer target-buffer
(goto-char (point-min))
(forward-line (- from line-offset 1))
(setq line-offset (+ line-offset len))
(kill-whole-line len)))
(t
(error "invalid rcs patch or internal error in py-isort-bf-apply--rcs-patch")))))))))
(defun py-isort-bf--replace-region (filename)
(delete-region (region-beginning) (region-end))
(insert-file-contents filename))
(defun py-isort-bf--apply-executable-to-buffer (executable-name
executable-call
only-on-region
file-extension)
"Formats the current buffer according to the executable"
(when (not (executable-find executable-name))
(error (format "%s command not found." executable-name)))
(let ((tmpfile (make-temp-file executable-name nil (concat "." file-extension)))
(patchbuf (get-buffer-create (format "*%s patch*" executable-name)))
(errbuf (get-buffer-create (format "*%s Errors*" executable-name)))
(coding-system-for-read buffer-file-coding-system)
(coding-system-for-write buffer-file-coding-system))
(with-current-buffer errbuf
(setq buffer-read-only nil)
(erase-buffer))
(with-current-buffer patchbuf
(erase-buffer))
(if (and only-on-region (use-region-p))
(write-region (region-beginning) (region-end) tmpfile)
(write-region nil nil tmpfile))
(if (funcall executable-call errbuf tmpfile)
(if (zerop (call-process-region (point-min) (point-max) "diff" nil
patchbuf nil "-n" "-" tmpfile))
(progn
(kill-buffer errbuf)
(message (format "Buffer is already %sed" executable-name)))
(if only-on-region
(py-isort-bf--replace-region tmpfile)
(py-isort-bf--apply-rcs-patch patchbuf))
(kill-buffer errbuf)
(message (format "Applied %s" executable-name)))
(error (format "Could not apply %s. Check *%s Errors* for details"
executable-name executable-name)))
(kill-buffer patchbuf)
(delete-file tmpfile)))
;; py-isort-bf.el ends here
;; END GENERATED -------------------
(provide 'py-isort)
;;; py-isort.el ends here
#!/bin/bash -e
TEST_FILE=/tmp/py-test-file.py
install_emacs24() {
sudo add-apt-repository ppa:cassou/emacs -y
sudo apt-get update -y
sudo apt-get install emacs24 -y
}
test_01() {
echo $FUNCNAME
rm $TEST_FILE || true
emacs --no-init-file -nw \
--load ./tests/tests.el \
--load py-isort.el \
./tests/01/before.py \
-f py-isort-before-save \
-f write-test-file \
-f kill-emacs
diff $TEST_FILE ./tests/01/after.py
}
test_02() {
echo $FUNCNAME
rm $TEST_FILE || true
emacs --no-init-file -nw \
--load ./tests/tests.el \
--load py-isort.el \
./tests/02/before.py \
-f py-isort-before-save \
-f write-test-file \
-f kill-emacs
diff $TEST_FILE ./tests/02/after.py
}
test_03() {
echo $FUNCNAME
rm $TEST_FILE || true
emacs --no-init-file -nw \
--load ./tests/tests.el \
--load py-isort.el \
./tests/03/before.py \
-f mark-whole-buffer \
-f py-isort-region \
-f write-test-file \
-f kill-emacs
diff $TEST_FILE ./tests/03/after.py
}
test_04() {
echo $FUNCNAME
rm $TEST_FILE || true
emacs --no-init-file -nw \
--load ./tests/tests.el \
--load py-isort.el \
./tests/04/before.py \
-f py-isort-buffer \
-f write-test-file \
-f kill-emacs
diff $TEST_FILE ./tests/04/after.py
}
test_05() {
echo $FUNCNAME
rm $TEST_FILE || true
emacs --no-init-file -nw \
--load ./tests/tests.el \
--load py-isort.el \
./tests/05/files/before.py \
-f py-isort-buffer \
-f write-test-file \
-f kill-emacs
diff $TEST_FILE ./tests/05/files/after.py
}
test_install_package() {
echo $FUNCNAME
emacs --no-init-file -nw \
py-isort.el \
-f package-install-from-buffer \
-f kill-emacs
}
main() {
if [ "$TRAVIS" = "true" ]; then
install_emacs24
test_install_package
fi
test_01
test_02
test_03
test_04
test_05
}
main
import os
import re
[settings]
line_length=30
from x import (a, b, c, e, f,
g, h, i, m, n,
o, p, q, r, s,
t, u, v, w, x,
y, z)
from x import a, b, c, e, f, g, h, i, m, n, o, p, q, r, s, t, u, v, w, x, y, z
import os
import re
import os
import re
[settings]
import_heading_stdlib=STDLIB
# STDLIB
import re
(defun write-test-file ()
(write-file "/tmp/py-test-file.py"))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment