Skip to content

Commit

Permalink
Add a helper which opens a python path's actual file for editing
Browse files Browse the repository at this point in the history
  • Loading branch information
my8bird committed May 10, 2013
1 parent 08cc517 commit 49b6a64
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 9 deletions.
37 changes: 37 additions & 0 deletions find_python_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import os, sys
import imp

# Using common_env is way to slow so manually add the paths
pj = os.path.join
abspath = os.path.abspath

taccs_base = os.environ['TACCS_BASE_DIR']
add_path = lambda sub: sys.path.append(abspath(pj(taccs_base, sub)))

add_path('taccs/packages')
add_path('plugins/p5_common/packages/')
add_path('plugins/glpcc4/packages/')
add_path('plugins/cots/packages/')
add_path('plugins/demo/packages/')
add_path('plugins/amtrak/packages/')
add_path('plugins/actic/packages/')
add_path('plugins/isave/packages/')
add_path('plugins/kdas/packages/')

import_path = sys.argv[1]
parts = import_path.split('.', 1)
root = parts[0]
sub = ''
if len(parts) > 1:
sub = parts[1]

_, dirpath, _ = imp.find_module(root)

sub_path = sub.replace('.', '/')
mod_path = abspath(pj(dirpath, sub_path))
if os.path.isdir(mod_path):
mod_path = pj(mod_path, '__init__.py')
else:
mod_path = mod_path + '.py'

sys.stdout.write(mod_path)
43 changes: 34 additions & 9 deletions init.el
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,17 @@
;; Python
(defun flymake-create-temp-intemp (file-name prefix)
"Return file name in temporary directory for checking FILE-NAME.
This is a replacement for `flymake-create-temp-inplace'. The
difference is that it gives a file name in
`temporary-file-directory' instead of the same directory as
FILE-NAME.
This is a replacement for `flymake-create-temp-inplace'. The
difference is that it gives a file name in
`temporary-file-directory' instead of the same directory as
FILE-NAME.
For the use of PREFIX see that function.
For the use of PREFIX see that function.
Note that not making the temporary file in another directory
\(like here) will not if the file you are checking depends on
relative paths to other files \(for the type of checks flymake
makes)."
Note that not making the temporary file in another directory
\(like here) will not if the file you are checking depends on
relative paths to other files \(for the type of checks flymake
makes)."
(unless (stringp file-name)
(error "Invalid file-name"))
(or prefix
Expand All @@ -84,6 +84,13 @@ makes)."
(flymake-log 3 "create-temp-intemp: file=%s temp=%s" file-name temp-name)
temp-name))

(eval-after-load "flymake"
'(progn
(defun flymake-after-change-function (start stop len)
"Start syntax check for current buffer if it isn't already running."
;; Do nothing, don't want to run checks until I save.
)))

(when (load "flymake" t)
(defun flymake-pyflakes-init ()
(let* ((temp-file (flymake-init-create-temp-buffer-copy
Expand Down Expand Up @@ -161,3 +168,21 @@ makes)."
(setq indent-tabs-mode nil) ; always replace tabs with spaces
(setq tab-width 3) ; set tab width to 3 for all buffers
(setq c-basic-offset 3)


(defun open-selected-py-import-file ()
"Locates the file for the current import path and opens its buffer"
(interactive)
(open-py-import-file (buffer-substring (region-beginning ) (region-end ))))

(defvar find-py-script-path (concat emacs-root "/find_python_file.py"))
(defun open-py-import-file (import-path)
"Opens the supplied import path as a buffer"
(interactive)
(message import-path)
(let ((py-out (shell-command-to-string (concat "python " find-py-script-path " " import-path))))
(message py-out)
(if (file-exists-p py-out)
(find-file py-out))))

(global-set-key (kbd "C-c p") 'open-selected-py-import-file)

0 comments on commit 49b6a64

Please sign in to comment.