diff options
author | Ian Thompson <quornian@googlemail.com> | 2008-07-16 14:33:48 +0400 |
---|---|---|
committer | Ian Thompson <quornian@googlemail.com> | 2008-07-16 14:33:48 +0400 |
commit | cc89221a2461e289edb380922d9f53566dc2a9eb (patch) | |
tree | 6102593c71c00f67516aed27f397cb35196fd727 /release | |
parent | 512eec04aa239d49ea655151cdd34ba5d754c466 (diff) |
Previously relying on import to run scripts didn't work every time and was not the right way to do it. Also fixed a problem with 'import *' not working and added the sys.modules list to the import suggestion list with a timed update.
Diffstat (limited to 'release')
-rw-r--r-- | release/scripts/bpymodules/BPyTextPlugin.py | 38 | ||||
-rw-r--r-- | release/scripts/textplugin_imports.py | 12 | ||||
-rw-r--r-- | release/scripts/textplugin_membersuggest.py | 8 | ||||
-rw-r--r-- | release/scripts/textplugin_suggest.py | 5 |
4 files changed, 37 insertions, 26 deletions
diff --git a/release/scripts/bpymodules/BPyTextPlugin.py b/release/scripts/bpymodules/BPyTextPlugin.py index 44038cfc3f6..7ff9db3e5c2 100644 --- a/release/scripts/bpymodules/BPyTextPlugin.py +++ b/release/scripts/bpymodules/BPyTextPlugin.py @@ -1,9 +1,9 @@ -import bpy +import bpy, sys import __builtin__, tokenize from Blender.sys import time from tokenize import generate_tokens, TokenError -# TODO: Remove the dependency for a full Python installation. Currently only the -# tokenize module is required + +# TODO: Remove the dependency for a full Python installation. # Context types NORMAL = 0 @@ -22,6 +22,23 @@ KEYWORDS = ['and', 'del', 'from', 'not', 'while', 'as', 'elif', 'global', _token_cache = None _cache_update = 0 +ModuleType = type(__builtin__) +_modules = dict([(n, None) for n in sys.builtin_module_names]) +_modules_updated = 0 + +def get_modules(since=1): + """Returns the set of built-in modules and any modules that have been + imported into the system upto 'since' seconds ago. + """ + + global _modules, _modules_updated + + t = time() + if _modules_updated < t - since: + _modules.update(sys.modules) + _modules_updated = t + return _modules.keys() + def suggest_cmp(x, y): """Use this method when sorting a list of suggestions. """ @@ -35,10 +52,11 @@ def cached_generate_tokens(txt, since=1): global _token_cache, _cache_update - if _cache_update < time() - since: + t = time() + if _cache_update < t - since: txt.reset() _token_cache = [g for g in generate_tokens(txt.readline)] - _cache_update = time() + _cache_update = t return _token_cache def get_module(name): @@ -52,12 +70,6 @@ def get_module(name): mod = getattr(mod, comp) return mod -def is_module(m): - """Taken from the inspect module of the standard Python installation. - """ - - return isinstance(m, type(bpy)) - def type_char(v): """Returns the character used to signify the type of a variable. Use this method to identify the type character for an item in a suggestion list. @@ -68,7 +80,7 @@ def type_char(v): 'v' if the parameter is variable or otherwise indeterminable """ - if is_module(v): + if isinstance(v, ModuleType): return 'm' elif callable(v): return 'f' @@ -215,7 +227,7 @@ def get_imports(txt): if string == 'as': impname = '.'.join(tmp) step = 3 - elif type == tokenize.NAME: + elif type == tokenize.NAME or string == '*': tmp.append(string) elif string != '.': impname = '.'.join(tmp) diff --git a/release/scripts/textplugin_imports.py b/release/scripts/textplugin_imports.py index 1773427bb01..978efeb2c49 100644 --- a/release/scripts/textplugin_imports.py +++ b/release/scripts/textplugin_imports.py @@ -31,13 +31,13 @@ def main(): # Check instead for straight 'import' pos2 = line.rfind('import ', 0, c) if pos2 != -1 and (pos2 == c-7 or (pos2 < c-7 and line[c-2]==',')): - items = [(m, 'm') for m in sys.builtin_module_names] + items = [(m, 'm') for m in get_modules()] items.sort(cmp = suggest_cmp) txt.suggest(items, '') # Immediate 'from' before cursor elif pos == c-5: - items = [(m, 'm') for m in sys.builtin_module_names] + items = [(m, 'm') for m in get_modules()] items.sort(cmp = suggest_cmp) txt.suggest(items, '') @@ -60,12 +60,10 @@ def main(): items = [('*', 'k')] for (k,v) in mod.__dict__.items(): - if is_module(v): t = 'm' - elif callable(v): t = 'f' - else: t = 'v' - items.append((k, t)) + items.append((k, type_char(v))) items.sort(cmp = suggest_cmp) txt.suggest(items, '') -if OK: +# Check we are running as a script and not imported as a module +if __name__ == "__main__" and OK: main() diff --git a/release/scripts/textplugin_membersuggest.py b/release/scripts/textplugin_membersuggest.py index 57c920c2bf9..61c803e9c0c 100644 --- a/release/scripts/textplugin_membersuggest.py +++ b/release/scripts/textplugin_membersuggest.py @@ -55,10 +55,7 @@ def main(): for k in attr: try: v = getattr(obj, k) - if is_module(v): t = 'm' - elif callable(v): t = 'f' - else: t = 'v' - list.append((k, t)) + list.append((k, type_char(v))) except (AttributeError, TypeError): # Some attributes are not readable pass @@ -66,5 +63,6 @@ def main(): list.sort(cmp = suggest_cmp) txt.suggest(list, pre[-1]) -if OK: +# Check we are running as a script and not imported as a module +if __name__ == "__main__" and OK: main() diff --git a/release/scripts/textplugin_suggest.py b/release/scripts/textplugin_suggest.py index 770d2759bcc..3014aaf381f 100644 --- a/release/scripts/textplugin_suggest.py +++ b/release/scripts/textplugin_suggest.py @@ -43,10 +43,12 @@ def main(): if check_membersuggest(line, c): import textplugin_membersuggest + textplugin_membersuggest.main() return elif check_imports(line, c): import textplugin_imports + textplugin_imports.main() return # Otherwise we suggest globals, keywords, etc. @@ -71,5 +73,6 @@ def main(): list.sort(cmp = suggest_cmp) txt.suggest(list, pre[-1]) -if OK: +# Check we are running as a script and not imported as a module +if __name__ == "__main__" and OK: main() |