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.
This commit is contained in:
@@ -1,9 +1,9 @@
|
|||||||
import bpy
|
import bpy, sys
|
||||||
import __builtin__, tokenize
|
import __builtin__, tokenize
|
||||||
from Blender.sys import time
|
from Blender.sys import time
|
||||||
from tokenize import generate_tokens, TokenError
|
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
|
# Context types
|
||||||
NORMAL = 0
|
NORMAL = 0
|
||||||
@@ -22,6 +22,23 @@ KEYWORDS = ['and', 'del', 'from', 'not', 'while', 'as', 'elif', 'global',
|
|||||||
_token_cache = None
|
_token_cache = None
|
||||||
_cache_update = 0
|
_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):
|
def suggest_cmp(x, y):
|
||||||
"""Use this method when sorting a list of suggestions.
|
"""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
|
global _token_cache, _cache_update
|
||||||
|
|
||||||
if _cache_update < time() - since:
|
t = time()
|
||||||
|
if _cache_update < t - since:
|
||||||
txt.reset()
|
txt.reset()
|
||||||
_token_cache = [g for g in generate_tokens(txt.readline)]
|
_token_cache = [g for g in generate_tokens(txt.readline)]
|
||||||
_cache_update = time()
|
_cache_update = t
|
||||||
return _token_cache
|
return _token_cache
|
||||||
|
|
||||||
def get_module(name):
|
def get_module(name):
|
||||||
@@ -52,12 +70,6 @@ def get_module(name):
|
|||||||
mod = getattr(mod, comp)
|
mod = getattr(mod, comp)
|
||||||
return mod
|
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):
|
def type_char(v):
|
||||||
"""Returns the character used to signify the type of a variable. Use this
|
"""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.
|
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
|
'v' if the parameter is variable or otherwise indeterminable
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if is_module(v):
|
if isinstance(v, ModuleType):
|
||||||
return 'm'
|
return 'm'
|
||||||
elif callable(v):
|
elif callable(v):
|
||||||
return 'f'
|
return 'f'
|
||||||
@@ -215,7 +227,7 @@ def get_imports(txt):
|
|||||||
if string == 'as':
|
if string == 'as':
|
||||||
impname = '.'.join(tmp)
|
impname = '.'.join(tmp)
|
||||||
step = 3
|
step = 3
|
||||||
elif type == tokenize.NAME:
|
elif type == tokenize.NAME or string == '*':
|
||||||
tmp.append(string)
|
tmp.append(string)
|
||||||
elif string != '.':
|
elif string != '.':
|
||||||
impname = '.'.join(tmp)
|
impname = '.'.join(tmp)
|
||||||
|
|||||||
@@ -31,13 +31,13 @@ def main():
|
|||||||
# Check instead for straight 'import'
|
# Check instead for straight 'import'
|
||||||
pos2 = line.rfind('import ', 0, c)
|
pos2 = line.rfind('import ', 0, c)
|
||||||
if pos2 != -1 and (pos2 == c-7 or (pos2 < c-7 and line[c-2]==',')):
|
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)
|
items.sort(cmp = suggest_cmp)
|
||||||
txt.suggest(items, '')
|
txt.suggest(items, '')
|
||||||
|
|
||||||
# Immediate 'from' before cursor
|
# Immediate 'from' before cursor
|
||||||
elif pos == c-5:
|
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)
|
items.sort(cmp = suggest_cmp)
|
||||||
txt.suggest(items, '')
|
txt.suggest(items, '')
|
||||||
|
|
||||||
@@ -60,12 +60,10 @@ def main():
|
|||||||
|
|
||||||
items = [('*', 'k')]
|
items = [('*', 'k')]
|
||||||
for (k,v) in mod.__dict__.items():
|
for (k,v) in mod.__dict__.items():
|
||||||
if is_module(v): t = 'm'
|
items.append((k, type_char(v)))
|
||||||
elif callable(v): t = 'f'
|
|
||||||
else: t = 'v'
|
|
||||||
items.append((k, t))
|
|
||||||
items.sort(cmp = suggest_cmp)
|
items.sort(cmp = suggest_cmp)
|
||||||
txt.suggest(items, '')
|
txt.suggest(items, '')
|
||||||
|
|
||||||
if OK:
|
# Check we are running as a script and not imported as a module
|
||||||
|
if __name__ == "__main__" and OK:
|
||||||
main()
|
main()
|
||||||
|
|||||||
@@ -55,10 +55,7 @@ def main():
|
|||||||
for k in attr:
|
for k in attr:
|
||||||
try:
|
try:
|
||||||
v = getattr(obj, k)
|
v = getattr(obj, k)
|
||||||
if is_module(v): t = 'm'
|
list.append((k, type_char(v)))
|
||||||
elif callable(v): t = 'f'
|
|
||||||
else: t = 'v'
|
|
||||||
list.append((k, t))
|
|
||||||
except (AttributeError, TypeError): # Some attributes are not readable
|
except (AttributeError, TypeError): # Some attributes are not readable
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@@ -66,5 +63,6 @@ def main():
|
|||||||
list.sort(cmp = suggest_cmp)
|
list.sort(cmp = suggest_cmp)
|
||||||
txt.suggest(list, pre[-1])
|
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()
|
main()
|
||||||
|
|||||||
@@ -43,10 +43,12 @@ def main():
|
|||||||
|
|
||||||
if check_membersuggest(line, c):
|
if check_membersuggest(line, c):
|
||||||
import textplugin_membersuggest
|
import textplugin_membersuggest
|
||||||
|
textplugin_membersuggest.main()
|
||||||
return
|
return
|
||||||
|
|
||||||
elif check_imports(line, c):
|
elif check_imports(line, c):
|
||||||
import textplugin_imports
|
import textplugin_imports
|
||||||
|
textplugin_imports.main()
|
||||||
return
|
return
|
||||||
|
|
||||||
# Otherwise we suggest globals, keywords, etc.
|
# Otherwise we suggest globals, keywords, etc.
|
||||||
@@ -71,5 +73,6 @@ def main():
|
|||||||
list.sort(cmp = suggest_cmp)
|
list.sort(cmp = suggest_cmp)
|
||||||
txt.suggest(list, pre[-1])
|
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()
|
main()
|
||||||
|
|||||||
Reference in New Issue
Block a user