|
| 1 | +# The MIT License |
| 2 | +# |
| 3 | +# Copyright (c) 2009 Andreas Stuehrk |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | +# |
| 12 | +# The above copyright notice and this permission notice shall be included in |
| 13 | +# all copies or substantial portions of the Software. |
| 14 | +# |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | +# THE SOFTWARE. |
| 22 | + |
| 23 | + |
| 24 | +import imp |
| 25 | +import os |
| 26 | +import sys |
| 27 | + |
| 28 | + |
| 29 | +# The cached list of all known modules |
| 30 | +modules = list() |
| 31 | + |
| 32 | + |
| 33 | +def complete(line, cw): |
| 34 | + """Construct a full list of possibly completions for imports.""" |
| 35 | + tokens = line.split() |
| 36 | + if tokens[0] in ['from', 'import']: |
| 37 | + if tokens[0] == 'from': |
| 38 | + completing_from = True |
| 39 | + if len(tokens) > 3: |
| 40 | + if '.' in cw: |
| 41 | + # This will result in a SyntaxError, so do not return |
| 42 | + # any matches |
| 43 | + return list() |
| 44 | + cw = '%s.%s' % (tokens[1], cw) |
| 45 | + elif len(tokens) == 3: |
| 46 | + return ['import'] |
| 47 | + else: |
| 48 | + completing_from = False |
| 49 | + |
| 50 | + matches = list() |
| 51 | + for name in modules: |
| 52 | + if not (name.startswith(cw) and name.find('.', len(cw)) == -1): |
| 53 | + continue |
| 54 | + if completing_from: |
| 55 | + name = name[len(tokens[1]) + 1:] |
| 56 | + matches.append(name) |
| 57 | + return matches |
| 58 | + else: |
| 59 | + return list() |
| 60 | + |
| 61 | + |
| 62 | +def find_modules(path): |
| 63 | + """Find all modules (and packages) for a given directory.""" |
| 64 | + if not os.path.isdir(path): |
| 65 | + # Perhaps a zip file |
| 66 | + return |
| 67 | + |
| 68 | + for name in os.listdir(path): |
| 69 | + if not any(name.endswith(suffix[0]) for suffix in imp.get_suffixes()): |
| 70 | + # Possibly a package |
| 71 | + if '.' in name: |
| 72 | + continue |
| 73 | + name = os.path.splitext(name)[0] |
| 74 | + try: |
| 75 | + fo, pathname, _ = imp.find_module(name, [path]) |
| 76 | + except ImportError: |
| 77 | + continue |
| 78 | + else: |
| 79 | + if fo is not None: |
| 80 | + fo.close() |
| 81 | + else: |
| 82 | + # Yay, package |
| 83 | + for subname in find_modules(pathname): |
| 84 | + if subname != '__init__': |
| 85 | + yield '%s.%s' % (name, subname) |
| 86 | + yield name |
| 87 | + |
| 88 | + |
| 89 | +def find_all_modules(path=None): |
| 90 | + """Return a list with all modules in `path`, which should be a list of |
| 91 | + directory names. If path is not given, sys.path will be used.""" |
| 92 | + modules = set() |
| 93 | + if path is None: |
| 94 | + modules.update(sys.builtin_module_names) |
| 95 | + path = sys.path |
| 96 | + |
| 97 | + for p in path: |
| 98 | + modules.update(find_modules(p)) |
| 99 | + |
| 100 | + return modules |
| 101 | + |
| 102 | + |
| 103 | +def reload(): |
| 104 | + """Refresh the list of known modules.""" |
| 105 | + global modules |
| 106 | + modules = find_all_modules() |
0 commit comments