Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added python code completion plugin

  • Loading branch information...
commit 97a9ca74b1c90c0b8e136b3ebfbf5d0797b1b62e 1 parent ea963d9
Adam Hitchcock authored
View
145 after/ftplugin/python_pydiction.vim
@@ -0,0 +1,145 @@
+" ============================================================================
+" python_pydiction.vim - Module and Keyword completion for Python
+" ============================================================================
+"
+" Author: Ryan Kulla (rkulla AT gmail DOT com)
+" Version: 1.2, for Vim 7
+" URL: http://www.vim.org/scripts/script.php?script_id=850
+" Last Modified: July 22th, 2009
+" Installation: On Linux, put this file in ~/.vim/after/ftplugin/
+" On Windows, put this file in C:\vim\vimfiles\ftplugin\
+" (assuming you installed vim in C:\vim\).
+" You may install the other files anywhere.
+" In .vimrc, add the following:
+" filetype plugin on
+" let g:pydiction_location = 'path/to/complete-dict'
+" Optionally, you set the completion menu height like:
+" let g:pydiction_menu_height = 20
+" The default menu height is 15
+" To do case-sensitive searches, set noignorecase (:set noic).
+" Usage: Type part of a Python keyword, module name, attribute or method,
+" then hit the TAB key and it will auto-complete (as long as it
+" exists in the complete-dict file.
+" You can also use Shift-Tab to Tab backwards.
+" License: BSD
+" Copyright: Copyright (c) 2003-2009 Ryan Kulla
+" All rights reserved.
+"
+" Redistribution and use in source and binary forms, with or without
+" modification, are permitted provided that the following conditions
+" are met:
+" 1. Redistributions of source code must retain the above copyright
+" notice, this list of conditions and the following disclaimer.
+" 2. Redistributions in binary form must reproduce the above
+" copyright notice, this list of conditions and the following
+" disclaimer in the documentation and/or other materials provided
+" with the distribution.
+" 3. The name of the author may not be used to endorse or promote
+" products derived from this software without specific prior
+" written permission.
+"
+" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
+" OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
+" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
+" GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+" NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+"
+"
+
+if v:version < 700
+ echoerr "Pydiction requires vim version 7 or greater."
+ finish
+endif
+
+
+" Make the Tab key do python code completion:
+inoremap <silent> <buffer> <Tab>
+ \<C-R>=<SID>SetVals()<CR>
+ \<C-R>=<SID>TabComplete('down')<CR>
+ \<C-R>=<SID>RestoreVals()<CR>
+
+" Make Shift+Tab do python code completion in the reverse direction:
+inoremap <silent> <buffer> <S-Tab>
+ \<C-R>=<SID>SetVals()<CR>
+ \<C-R>=<SID>TabComplete('up')<CR>
+ \<C-R>=<SID>RestoreVals()<CR>
+
+
+if !exists("*s:TabComplete")
+ function! s:TabComplete(direction)
+ " Check if the char before the char under the cursor is an
+ " underscore, letter, number, dot or opening parentheses.
+ " If it is, and if the popup menu is not visible, use
+ " I_CTRL-X_CTRL-K ('dictionary' only completion)--otherwise,
+ " use I_CTRL-N to scroll downward through the popup menu or
+ " use I_CTRL-P to scroll upward through the popup menu,
+ " depending on the value of a:direction.
+ " If the char is some other character, insert a normal Tab:
+ if searchpos('[_a-zA-Z0-9.(]\%#', 'nb') != [0, 0]
+ if !pumvisible()
+ return "\<C-X>\<C-K>"
+ else
+ if a:direction == 'down'
+ return "\<C-N>"
+ else
+ return "\<C-P>"
+ endif
+ endif
+ else
+ return "\<Tab>"
+ endif
+ endfunction
+endif
+
+
+if !exists("*s:SetVals")
+ function! s:SetVals()
+ " Save and change any config values we need.
+
+ " Temporarily change isk to treat periods and opening
+ " parenthesis as part of a keyword -- so we can complete
+ " python modules and functions:
+ let s:pydiction_save_isk = &iskeyword
+ setlocal iskeyword +=.,(
+
+ " Save any current dictionaries the user has set:
+ let s:pydiction_save_dictions = &dictionary
+ " Temporarily use only pydiction's dictionary:
+ let &dictionary = g:pydiction_location
+
+ " Save the ins-completion options the user has set:
+ let s:pydiction_save_cot = &completeopt
+ " Have the completion menu show up for one or more matches:
+ let &completeopt = "menu,menuone"
+
+ " Set the popup menu height:
+ let s:pydiction_save_pumheight = &pumheight
+ if !exists('g:pydiction_menu_height')
+ let g:pydiction_menu_height = 15
+ endif
+ let &pumheight = g:pydiction_menu_height
+
+ return ''
+ endfunction
+endif
+
+
+if !exists("*s:RestoreVals")
+ function! s:RestoreVals()
+ " Restore the user's initial values.
+
+ let &dictionary = s:pydiction_save_dictions
+ let &completeopt = s:pydiction_save_cot
+ let &pumheight = s:pydiction_save_pumheight
+ let &iskeyword = s:pydiction_save_isk
+
+ return ''
+ endfunction
+endif
+
View
348 ftplugin/pydiction/README.txt
@@ -0,0 +1,348 @@
+Description
+===========
+Pydiction allows you to Tab-complete Python code in Vim, including keywords, the standard library, and third-party modules.
+
+It consists of three main files:
+
+ python_pydiction.vim -- This is an ftplugin you put in your non-system ftplugin directory (i.e., ~/.vim/after/ftplugin/, on Unix or C:\vim\vimfiles\ftplugin\, on Windows)
+ complete-dict -- This is a vim dictionary file that consists of Python keywords and modules. This is what python_pydiction.vim looks at to know which things are completable.
+ pydiction.py -- (Not required) This is a Python script that was used to generate complete-dict. You can optionally run this script to add more modules to complete-dict.
+
+
+Install Details
+===============
+Unix/Linux: Put python_pydiction.vim in ~/.vim/after/ftplugin/ (If this directory doesn't already exist, create it. Vim will know to look there automatically.)
+Windows: Put python_pydiction.vim in C:\vim\vimfiles\ftplugin (Assuming you installed Vim to C:\vim\).
+
+You may install the other files (complete-dict and pydiction.py) anywhere you want. For this example, we'll assume you put them in "C:\vim\vimfiles\ftplugin\pydiction\" (Do not put any file but python_pydiction.vim in the ftplugin\ directory, only .vim files should go there.)
+
+In your vimrc file, first add the following line to enable filetype plugins:
+
+ filetype plugin on
+
+then make sure you set "g:pydiction_location" to the full path of where you installed complete-dict, i.e.:
+
+ let g:pydiction_location = 'C:/vim/vimfiles/ftplugin/pydiction/complete-dict'
+
+You can optionally set the height of the completion menu by setting "g:pydiction_menu_height" in your vimrc. For example:
+
+ let g:pydiction_menu_height = 20
+
+The default menu height is 15.
+
+Note: If you were using a version of Pydiction less than 1.0, make sure you delete the old pydiction way of doing things from your vimrc. You should ***NOT*** have this in your vimrc anymore:
+
+ if has("autocmd")
+ autocmd FileType python set complete+=k/path/to/pydiction iskeyword+=.,(
+ endif " has("autocmd")
+
+
+Usage
+=====
+Type part of a Python keyword, module name, attribute or method in "insert mode" in Vim, then hit the TAB key and it will auto-complete.
+
+For example, typing:
+
+ raw<Tab>
+
+will bring up a menu of possibilities, such as:
+
+ raw_input(
+ raw_unicode_escape_decode(
+ raw_unicode_escape_encode(
+
+Typing:
+
+ os.p<Tab>
+
+pops up:
+
+ os.pardir
+ os.path
+ os.pathconf(
+ os.pathconf_names
+ os.pathsep
+ os.pipe(
+ ...
+
+Typing:
+
+ co<Tab>
+
+pops up:
+
+ continue
+ coerce(
+ compile(
+ ...
+
+and so on.
+
+As of Pydiction 1.2, there's support for completing modules that were imported via "from module import submodule". For example, you could do:
+
+ from xml.parsers import expat
+ expat.P<Tab>
+
+which expands to:
+
+ expat.ParserCreate(
+
+You can also now use Shift-Tab to Tab backwards through the popup menu.
+
+If you feel you're getting different results in your completion menu, it's probably because you don't have Vim set to ignore case. You can remedy this with ":set noic"
+
+
+Pydiction versus other forms of completion
+==========================================
+Pydiction can complete Python Keywords, as well as Python module names and their attributes and methods. It can also complete both the fully-qualified module names such as "module.method", as well as non-fully qualified names such as simply "method".
+
+Pydiction only uses the "Tab" key to complete, uses a special dictionary file to complete from, and only attempts to complete while editing Python files. This has the advantages of only requiring one keystroke to do completion and of not polluting all of your completion menus that you may be using for other types of completion, such as Vim's regular omni-completion, or other completion scripts that you may be running.
+
+Since pydiction uses a dictionary file of possible completion items, it can complete 3rd party modules much more accurately than other ways. You have full control over what it can and cannot complete. If it's unable to complete anything you can either use pydiction.py, to automatically add a new module's contents to the dictionary, or you can manually add them using a text editor. The dictionary is just a plain text file, which also makes it portable across all platforms. For example, if you're a PyQT user, you can add all the PyQT related modules to the dictionary file (complete-dict) by using pydiction.py. The latest default complete-dict already contains most of the standard library, all Python 2.x keywords, Pygame, OpenGL, wxPython, Twisted, PyQT4, ZSI, LDAP, numarray, PyGTK, MySQLdb, PyGreSQL, pyPgSQL, PythonCard, pyvorbis, bcrypt, openid, GnuPGInterface, OpenSSl, pygments and more.
+
+Also, because pydiction uses a dictionary file, you don't have to import a module before you can complete it, nor do you even have to have the module installed on your machine. This makes completion very fast since it doesn't need to do any type deducing. It also frees you up to use pydiction as a way of looking up what a module or submodule without having to install it first.
+
+If you want to, you can still use Vim 7's built-in omni-completion for Python (pythoncomplete.vim), and other forms of ins-completion, with Pydiction. In fact, they can all make a great team.
+
+Pydiction knows when you're completing a callable method or not and, if you are, it will automatically insert an opening parentheses.
+
+The Tab key will work as normal for everything else. Pydiction will only try to use the Tab key to complete Python code if you're editing a Python file and you first type part of some Python module or keyword.
+
+Pydiction doesn't even require Python support to be compiled into your version of Vim.
+
+
+python_pydiction.vim (filetype plugin)
+======================================
+Pydiction will make it so the Tab key on your keyboard is able to complete python code.
+
+Version 1.0 and greater of Pydiction uses a new file called python_pydiction.vim, which is an ftplugin that only activates when you're editing a python file (e.g., you're editing a file with a ".py" extension or you've manually typed ":set filetype=python"). Past versions of pydiction didn't use a plugin and instead just required you to change the value of "isk" in your vimrc, which was not desirable. Version 1.0 and greater do not require you to manually change the value of isk. It now safely changes isk for you temporarily by only setting it while you're doing Tab-completion of Python code, and it automatically changes it back to its original value whenever Tab-completion isn't being activated. Again, only Tab-completion causes pydiction to activate; not even other forms of ins-completion, such as <Ctrl-x> or <Ctrl-n> completion will activate pydiction, so you're still free to use those other types of completion whenever you want to.
+
+Pydiction works by using Vim's ins-completion functionality by temporarily remapping the Tab key to do the same thing as I_CTRL-X_CTRL_K (dictionary only completion). This means that whenever you're editing a Python file, and you start typing the name of a python keyword or module, you can press the Tab key to complete it. For example, if you type "os.pa" and then press Tab, Pydiction will pop up a completion menu in Vim that will look something like:
+
+ os.pardir
+ os.path
+ os.pathconf(
+ os.pathconf_names
+ os.path.
+ os.path.__all__
+ os.path.__builtins__
+ os.path.__doc__
+ ...
+
+Pressing Tab again while the menu is open will scroll down the menu so you can choose whatever item you want to go with, using the popup-menu keys:
+
+ CTRL-Y Accept the currently selected match and stop completion.
+ <Space> Accept the currently selected match and insert a space.
+ CTRL-E Close the menu and not accept any match.
+ ....
+
+hitting Enter will accept the currently selected match, stop completion, and insert a newline, which is usually not what you want. Use CTRL-Y or <Space>, instead. See ":help popupmenu-keys" for more options.
+
+As of Pydiction 1.3, you can press Shift-Tab to Tab backwards as well.
+
+Pydiction temporarily sets completeopt to "menu,menuone", so that you can complete items that have one or more matches. It will set it back to whatever the original value you have completeopt set to when Tab-completion isn't being activated.
+
+By default, Pydiction ignores case while doing Tab-completion. If you want it to do case-sensitive searches, then set noignorecase (:set noic).
+
+
+pydiction.py
+============
+Note: You can skip this section if you don't plan to add more modules to complete-dict yourself. Check if complete-dict already has the modules you intend to use.
+
+This is the Python script used to create the "complete-dict" Vim dictionary file. I have created and bundled a default complete-dict for your use. I created it using Ubuntu 9.04 Linux, so there won't be any real win32 specific support in it. You're free to run pydiction.py to add, or upgrade, as modules as you want. The dictionary file will still work if you're using windows, but it won't complete win32 related modules unless you tell it to.
+
+Usage: In a command prompt, run:
+
+ $ python pydiction.py <module> ... [-v]
+
+(You need to have python 2.x installed.)
+
+Say you wanted to add a module called "mymodule" to complete-dict, do the following:
+
+ $ python pydiction.py mymodule
+
+You can input more than one module name on the command-line, just separate them by spaces:
+
+ $ python pydiction.py mymodule1 mymodule2 mymodule3
+
+The -v option will just write the results to stdout (standard output) instead of the complete-dict file.
+
+If the backfup file "complete-dict.last" doesn't exist in the current directory, pydiction.py will create it for you. You should always keep a backup of your last working dictionary in case anything goes wrong, as it can get tedious having to recreate the file from scratch.
+
+If complete-dict.last already exists, pydiction will ask you if you want to overwrite your old backup with the new backup.
+
+If you try to add a module that already exists in complete-dict, pydiction will tell you it already exists, so don't worry about adding duplicates. In fact, you can't add duplicates, every time pydiction.py runs it looks for and removes any duplicates in the file.
+
+When pydiction adds new modules to complete-dict, it does so in two phases. First, it adds the fully-qualified name of the module. For example:
+
+ module.attribute
+ module.method(
+
+then it adds the non-fully qualified name:
+
+ attribute
+ method(
+
+this allows you to complete your python code in the way that you imported it. E.g.:
+
+ import module
+
+or:
+
+ from module import method
+
+Say you want to complete "pygame.display.set_mode". If you imported Pygame using "import pygame", then you can Tab-complete using:
+
+ pygame.di<Tab>
+
+to expand to "pygame.display.". Then type:
+
+ se<Tab>
+
+to expand to "pygame.display.set_mode("
+
+Now say you imported using "from pygame import display". To expand to "display.set_mode(" just type:
+
+ display.se<Tab>
+
+And if you imported using "from pygame.display import set_mode" just type:
+
+ se<Tab>
+
+Keep in mind that if you don't use fully-qualified module names then you might get a lot of possible menu options popping up, so you may want to use more than just two letters before you hit Tab, to try to narrow down the list.
+
+As of Pydictoin 1.1, there is also limited support for string type method completion. For example:
+
+ "".jo<Tab>"
+
+will expand to:
+
+ "".join(
+
+make sure you type at least two letters of the method name if this doesn't seem to work.
+
+This only works for quoted strings, ie:
+
+ 'foo bar'.st<Tab>
+
+to get
+
+ 'foo bar'.startswith(
+
+but you can't yet do:
+
+ s = 'foo bar'
+
+ s.st<Tab>
+
+if you want that behavior you can still use Vim 7's omni-completion:
+
+ s.st<Ctrl-x><Ctrl-o>
+
+which will also give you a preview window describing the methods as well as the argument list the methods take, e,g:
+
+ startswith(prefix[, start[, end]])
+ strip([chars])
+
+To Tab-complete your own personal modules, you put your functions in a separate file to be reused, as you normally would. For example, say you put the following function in a file called "myFoo.py":
+
+ def myBar():
+ print "hi"
+
+you would then need to add myFoo to complete-dict by doing:
+
+ ./pydiction.py myFoo
+
+now you can complete myFoo.myBar() by doing:
+
+ myFoo.my<Tab>
+
+You don't have to restart Vim after you update complete-dict.
+
+
+complete-dict
+=============
+This is the Vim dictionary file that python_pydiction.vim reads from and pydiction.py writes to. Without this file, pydiction wouldn't know which Python keywords and modules it can Tab-complete.
+
+complete-dict is only an optional file in the sense that you can create your own complete-dict if you don't want to use the default one that is bundled with Pydiction. The default complete-dict gives you a major head start, as far as what you can Tab-complete, because I did my best to put all of the Python keywords, standard library and even some popular third party modules in it for you.
+
+The default complete-dict currently contains:
+
+ Python keywords:
+
+ and, del, for, is, raise, assert, elif, from, lambda, return, break, else, global, not, try, class, except, if, or, while, continue, exec, import, pass, yield, def, finally, in, print
+
+ Most of the standard library and built ins:
+
+ __builtin__, __future__, os, sys, time, re, sets, string, math, Tkinter, hashlib, urllib, pydoc, etc...
+
+ It also contains some popular third-party libraries:
+
+ Pygame, wxPython, Twisted, ZSI, LDAP, OpenGL, PyGTK, PyQT4, MySQLdb, PyGreSQL, pyPgSQL, SQLite, PythonCard, Numarray, pyvorbis, Bcrypt, OpenID, GnuPGInterface, OpenSSL and Pygments.
+
+ Make sure you download the latest version of Pydiction to get the most up-to-date version of complete-dict. New modules are usually added to it every release.
+
+If you open complete-dict in your text editor you'll see sections in it for each module, such as:
+
+ --- import os ---
+ os.EX_CANTCREAT
+ os.EX_CONFIG
+ os.EX_DATAERR
+ ...
+
+ --- from os import * ---
+ EX_CANTCREAT
+ EX_CONFIG
+ EX_DATAERR
+ ...
+
+If certain attributes seem to be missing, it's probably because pydiction removed them because they were duplicates. This mainly happens with the non-fully qualified module sections. So first try searching the entire file for whatever string you assume is missing before you try adding it. For example, if you don't see:
+
+ __doc__
+
+under:
+
+ --- import sys ---
+
+it's probably because a previous module, such as "os", already has it.
+
+If you try to recreate complete-dict from scratch, you'll need to manually add the Python keywords back to it, as those aren't generated with pydiction.py.
+
+If you don't want certain things to Tab-complete, such as Python keywords or certain modules, simply delete them by hand from complete-dict.
+
+Pydiction doesn't ignore "private" attributes or methods. I.e., those starting (but not ending) with one or two underscores, e.g., "_foo" or "__foo". I have manually deleted things starting with a single underscore from the included complete-dict just to keep it a little more sane--since there were so many. In sticking with the Python tradition of not forcing things to be private, I have left it up to the user to decide how they want to treat their own things. If you want to delete them from your custom complete-dict's, you can use a regex to try to delete them, such as doing:
+
+ :g/\._[a-zA-Z]/d
+ :g/^_[a-zA-Z]/d
+ :g/^\%(_\=[^_]\)*\zs__\%(.\{-}__\)\@!/d
+ etc...
+
+
+Tips
+====
+-Say you create a custom object, called "S" by doing something like:
+
+ S = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+
+you can complete dynamic object methods, such as "S.send()", by using Vim 7's omni-completion ftplugin (a.k.a "pythoncomplete.vim") by doing:
+
+ S.s<Ctrl-x><Ctrl-o>
+
+-You may get unexpected results if you use autocomplpop.vim, supertab.vim or other completion or python plugins. Try disabling them individually to find out the culprit and please don't hesitate to e-mail me any workarounds or suggestions. Thanks.
+
+
+License
+=======
+As of version 1.0, Pydiction is now under a BSD license instead of GPL.
+
+
+Further reading
+===============
+:help ftplugin
+:help 'complete
+:help compl-dictionary
+:help popupmenu-completion
+:help popupmenu-keys
+:help iskeyword
+http://docs.python.org/modindex.html
+
View
93,721 ftplugin/pydiction/complete-dict
93,721 additions, 0 deletions not shown
View
284 ftplugin/pydiction/pydiction.py
@@ -0,0 +1,284 @@
+#!/usr/bin/env python
+# Last modified: July 23rd, 2009
+"""
+
+pydiction.py 1.2 by Ryan Kulla (rkulla AT gmail DOT com).
+
+Description: Creates a Vim dictionary of Python module attributes for Vim's
+ completion feature. The created dictionary file is used by
+ the Vim ftplugin "python_pydiction.vim".
+
+Usage: pydiction.py <module> ... [-v]
+Example: The following will append all the "time" and "math" modules'
+ attributes to a file, in the current directory, called "pydiction"
+ with and without the "time." and "math." prefix:
+ $ python pydiction.py time math
+ To print the output just to stdout, instead of appending to the file,
+ supply the -v option:
+ $ python pydiction.py -v time math
+
+License: BSD.
+"""
+
+
+__author__ = "Ryan Kulla (rkulla AT gmail DOT com)"
+__version__ = "1.2"
+__copyright__ = "Copyright (c) 2003-2009 Ryan Kulla"
+
+
+import os
+import sys
+import types
+import shutil
+
+
+# Path/filename of the vim dictionary file to write to:
+PYDICTION_DICT = r'complete-dict'
+# Path/filename of the vim dictionary backup file:
+PYDICTION_DICT_BACKUP = r'complete-dict.last'
+
+# Sentintal to test if we should only output to stdout:
+STDOUT_ONLY = False
+
+
+def get_submodules(module_name, submodules):
+ """Build a list of all the submodules of modules."""
+
+ # Try to import a given module, so we can dir() it:
+ try:
+ imported_module = my_import(module_name)
+ except ImportError, err:
+ return submodules
+
+ mod_attrs = dir(imported_module)
+
+ for mod_attr in mod_attrs:
+ if type(getattr(imported_module, mod_attr)) is types.ModuleType:
+ submodules.append(module_name + '.' + mod_attr)
+
+ return submodules
+
+
+def write_dictionary(module_name):
+ """Write to module attributes to the vim dictionary file."""
+ prefix_on = '%s.%s'
+ prefix_on_callable = '%s.%s('
+ prefix_off = '%s'
+ prefix_off_callable = '%s('
+
+ try:
+ imported_module = my_import(module_name)
+ except ImportError, err:
+ return
+
+ mod_attrs = dir(imported_module)
+
+ # Generate fully-qualified module names:
+ write_to.write('\n--- import %s ---\n' % module_name)
+ for mod_attr in mod_attrs:
+ if callable(getattr(imported_module, mod_attr)):
+ # If an attribute is callable, show an opening parentheses:
+ format = prefix_on_callable
+ else:
+ format = prefix_on
+ write_to.write(format % (module_name, mod_attr) + '\n')
+
+ # Generate submodule names by themselves, for when someone does
+ # "from foo import bar" and wants to complete bar.baz.
+ # This works the same no matter how many .'s are in the module.
+ if module_name.count('.'):
+ # Get the "from" part of the module. E.g., 'xml.parsers'
+ # if the module name was 'xml.parsers.expat':
+ first_part = module_name[:module_name.rfind('.')]
+ # Get the "import" part of the module. E.g., 'expat'
+ # if the module name was 'xml.parsers.expat'
+ second_part = module_name[module_name.rfind('.') + 1:]
+ write_to.write('\n--- from %s import %s ---\n' %
+ (first_part, second_part))
+ for mod_attr in mod_attrs:
+ if callable(getattr(imported_module, mod_attr)):
+ format = prefix_on_callable
+ else:
+ format = prefix_on
+ write_to.write(format % (second_part, mod_attr) + '\n')
+
+ # Generate non-fully-qualified module names:
+ write_to.write('\n--- from %s import * ---\n' % module_name)
+ for mod_attr in mod_attrs:
+ if callable(getattr(imported_module, mod_attr)):
+ format = prefix_off_callable
+ else:
+ format = prefix_off
+ write_to.write(format % mod_attr + '\n')
+
+
+def my_import(name):
+ """Make __import__ import "package.module" formatted names."""
+ mod = __import__(name)
+ components = name.split('.')
+ for comp in components[1:]:
+ mod = getattr(mod, comp)
+ return mod
+
+
+def remove_duplicates(seq, keep=()):
+ """
+
+ Remove duplicates from a sequence while perserving order.
+
+ The optional tuple argument "keep" can be given to specificy
+ each string you don't want to be removed as a duplicate.
+ """
+ seq2 = []
+ seen = set();
+ for i in seq:
+ if i in (keep):
+ seq2.append(i)
+ continue
+ elif i not in seen:
+ seq2.append(i)
+ seen.add(i)
+ return seq2
+
+
+def get_yesno(msg="[Y/n]?"):
+ """
+
+ Returns True if user inputs 'n', 'Y', "yes", "Yes"...
+ Returns False if user inputs 'n', 'N', "no", "No"...
+ If they enter an invalid option it tells them so and asks again.
+ Hitting Enter is equivalent to answering Yes.
+ Takes an optional message to display, defaults to "[Y/n]?".
+
+ """
+ while True:
+ answer = raw_input(msg)
+ if answer == '':
+ return True
+ elif len(answer):
+ answer = answer.lower()[0]
+ if answer == 'y':
+ return True
+ break
+ elif answer == 'n':
+ return False
+ break
+ else:
+ print "Invalid option. Please try again."
+ continue
+
+
+def main(write_to):
+ """Generate a dictionary for Vim of python module attributes."""
+ submodules = []
+
+ for module_name in sys.argv[1:]:
+ try:
+ imported_module = my_import(module_name)
+ except ImportError, err:
+ print "Couldn't import: %s. %s" % (module_name, err)
+ sys.argv.remove(module_name)
+
+ cli_modules = sys.argv[1:]
+
+ # Step through each command line argument:
+ for module_name in cli_modules:
+ print "Trying module: %s" % module_name
+ submodules = get_submodules(module_name, submodules)
+
+ # Step through the current module's submodules:
+ for submodule_name in submodules:
+ submodules = get_submodules(submodule_name, submodules)
+
+ # Add the top-level modules to the list too:
+ for module_name in cli_modules:
+ submodules.append(module_name)
+
+ submodules.sort()
+
+ # Step through all of the modules and submodules to create the dict file:
+ for submodule_name in submodules:
+ write_dictionary(submodule_name)
+
+ if STDOUT_ONLY:
+ return
+
+ # Close and Reopen the file for reading and remove all duplicate lines:
+ write_to.close()
+ print "Removing duplicates..."
+ f = open(PYDICTION_DICT, 'r')
+ file_lines = f.readlines()
+ file_lines = remove_duplicates(file_lines, ('\n'))
+ f.close()
+
+ # Delete the original file:
+ os.unlink(PYDICTION_DICT)
+
+ # Recreate the file, this time it won't have any duplicates lines:
+ f = open(PYDICTION_DICT, 'w')
+ for attr in file_lines:
+ f.write(attr)
+ f.close()
+ print "Done."
+
+
+if __name__ == '__main__':
+ """Process the command line."""
+
+ if sys.version_info[0:2] < (2, 3):
+ sys.exit("You need a Python 2.x version of at least Python 2.3")
+
+ if len(sys.argv) <= 1:
+ sys.exit("%s requires at least one argument. None given." %
+ sys.argv[0])
+
+ if '-v' in sys.argv:
+ write_to = sys.stdout
+ sys.argv.remove('-v')
+ STDOUT_ONLY = True
+ elif os.path.exists(PYDICTION_DICT):
+ # See if any of the given modules have already been pydiction'd:
+ f = open(PYDICTION_DICT, 'r')
+ file_lines = f.readlines()
+ for module_name in sys.argv[1:]:
+ for line in file_lines:
+ if line.find('--- import %s ' % module_name) != -1:
+ print '"%s" already exists in %s. Skipping...' % \
+ (module_name, PYDICTION_DICT)
+ sys.argv.remove(module_name)
+ break
+ f.close()
+
+ if len(sys.argv) < 2:
+ # Check if there's still enough command-line arguments:
+ sys.exit("Nothing new to do. Aborting.")
+
+ if os.path.exists(PYDICTION_DICT_BACKUP):
+ answer = get_yesno('Overwrite existing backup "%s" [Y/n]? ' % \
+ PYDICTION_DICT_BACKUP)
+ if (answer):
+ print "Backing up old dictionary to: %s" % \
+ PYDICTION_DICT_BACKUP
+ try:
+ shutil.copyfile(PYDICTION_DICT, PYDICTION_DICT_BACKUP)
+ except IOError, err:
+ print "Couldn't back up %s. %s" % (PYDICTION_DICT, err)
+ else:
+ print "Skipping backup..."
+
+ print 'Appending to: "%s"' % PYDICTION_DICT
+ else:
+ print "Backing up current %s to %s" % \
+ (PYDICTION_DICT, PYDICTION_DICT_BACKUP)
+ try:
+ shutil.copyfile(PYDICTION_DICT, PYDICTION_DICT_BACKUP)
+ except IOError, err:
+ print "Couldn't back up %s. %s" % (PYDICTION_DICT, err)
+ else:
+ print 'Creating file: "%s"' % PYDICTION_DICT
+
+
+ if not STDOUT_ONLY:
+ write_to = open(PYDICTION_DICT, 'a')
+
+ main(write_to)
View
86 snippets/python.snippets
@@ -0,0 +1,86 @@
+snippet #!
+ #!/usr/bin/env python
+
+snippet imp
+ import ${1:module}
+# Module Docstring
+snippet docs
+ '''
+ File: ${1:`Filename('$1.py', 'foo.py')`}
+ Author: ${2:`g:snips_author`}
+ Description: ${3}
+ '''
+snippet while
+ while ${1:condition}:
+ ${2:# code...}
+snippet for
+ for ${1:needle} in ${2:haystack}:
+ ${3:# code...}
+# New Class
+snippet class
+ class ${1:ClassName}(${2:object}):
+ """${3:docstring for $1}"""
+ def __init__(self, ${4:arg}):
+ ${5:super($1, self).__init__()}
+ self.$4 = $4
+ ${6}
+# New Function
+snippet def
+ def ${1:fname}(${2:`indent('.') ? 'self' : ''`}):
+ """${3:docstring for $1}"""
+ ${4:pass}
+snippet deff
+ def ${1:fname}(${2:`indent('.') ? 'self' : ''`}):
+ ${3}
+# New Method
+snippet defs
+ def ${1:mname}(self, ${2:arg}):
+ ${3:pass}
+# New Property
+snippet property
+ def ${1:foo}():
+ doc = "${2:The $1 property.}"
+ def fget(self):
+ ${3:return self._$1}
+ def fset(self, value):
+ ${4:self._$1 = value}
+# Lambda
+snippet lambda
+ ${1:var} = lambda ${2:vars} : ${3:action}
+snippet try Try/Except
+ try:
+ ${1:pass}
+ except ${2:Exception}, ${3:e}:
+ ${4:raise $3}
+snippet try Try/Except/Else
+ try:
+ ${1:pass}
+ except ${2:Exception}, ${3:e}:
+ ${4:raise $3}
+ else:
+ ${5:pass}
+snippet try Try/Except/Finally
+ try:
+ ${1:pass}
+ except ${2:Exception}, ${3:e}:
+ ${4:raise $3}
+ finally:
+ ${5:pass}
+snippet try Try/Except/Else/Finally
+ try:
+ ${1:pass}
+ except ${2:Exception}, ${3:e}:
+ ${4:raise $3}
+ else:
+ ${5:pass}
+ finally:
+ ${6:pass}
+# if __name__ == '__main__':
+snippet ifmain
+ if __name__ == '__main__':
+ ${1:main()}
+# __magic__
+snippet _
+ __${1:init}__${2}
+
+
Please sign in to comment.
Something went wrong with that request. Please try again.