How to have automated headers for python files
A proper header format in python is described here .
Using either VIM o开发者_如何学Pythonr a shell script, I would like to have the usual metadata (like __author__, __authors__, __contact__, __copyright__, __license__, __deprecated__, __date__ and __version__
) added to the file header. SVN keywords would also be nice. Adding it to new files is the most relevant. Adding it to existing files is a bonus.
Ruslan's Blog has a solution for Emacs. But I was unable to find a solution for Python.
Where has this been done for python without Emacs? VIM can copy text from one file to another like so , but maybe there is a nicer way.
I highly recommend the snipMate plugin. You can easily add new snips per file type, and they're triggered by just typing a keyword and hitting tab, so for example you could just hit
header<TAB>
and all the fields would be added, and you can easily tab through any that require being filled out on a per-file basis. There is even already a snippet called docs in the built-in python snippets (vimfiles/snippets/python.snippets) that looks like it fills out similar metadata for docstrings which you could easily modify for your purposes, here it is as an example of the snip format:
# Module Docstring
snippet docs
'''
File: ${1:`Filename('$1.py', 'foo.py')`}
Author: ${2:`g:snips_author`}
Description: ${3}
'''
Dynamic entries are supported using backticks in your snip (Filename and g:snips_author as the default for tabbable entries 1 and 2 above). The date could be added with:
`system("date +%Y-%m-%d")`
(from the snipMate help doc).
Here is my C++ files template:
/*****************************************************************************
* @file <file_name>
*
* @date <date>
* @author John Doe
* @email jdoe@yourcompany.com
*
* @brief
*
* @detail
*
*****************************************************************************/
And here is what I have inside ~/.vimrc
:
" Reads the template file replacing the tags by the actual
" information and insert the result at the beginning of the buffer. At
" the end, creates two blank lines at the end of the file and
" position the cursor at the first one.
function! s:insert_description()
let template = $HOME . "/.vim/template/cpp.template"
let file_name = expand("%:t") " Get file name without path
let date = strftime("%Y") " Get the current year in format YYYY
let i = 0
for line in readfile(template)
let line = substitute(line, "<file_name>", file_name, "ge")
let line = substitute(line, "<date>", date, "ge")
call append(i, line)
let i += 1
endfor
execute "normal! Go\<Esc>k"
endfunction
autocmd BufNewFile *.{c++,cpp,cc,c,h,hpp} call <SID>insert_description()
Basically, I read the template file replacing the tags and with the actual information and insert the result at the beggining of the newly created file. The function s:insert_description()
is called whenever vim created a new file. This is set by the autocmd
at the last line.
You can base yourself at this code and create the equivalent for python.
Just go over to http://www.vim.org and search there for "new file template" and you'll get tons of plugins that solve this kind of problem, so look at them and pick one.
精彩评论