[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [15599] branches/soc-2008-quorn/release/ scripts: Previously relying on import to run scripts didn' t work every time and was not the right way to do it.

Ian Thompson quornian at googlemail.com
Wed Jul 16 12:33:51 CEST 2008


Revision: 15599
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=15599
Author:   quorn
Date:     2008-07-16 12:33:48 +0200 (Wed, 16 Jul 2008)

Log Message:
-----------
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.

Modified Paths:
--------------
    branches/soc-2008-quorn/release/scripts/bpymodules/BPyTextPlugin.py
    branches/soc-2008-quorn/release/scripts/textplugin_imports.py
    branches/soc-2008-quorn/release/scripts/textplugin_membersuggest.py
    branches/soc-2008-quorn/release/scripts/textplugin_suggest.py

Modified: branches/soc-2008-quorn/release/scripts/bpymodules/BPyTextPlugin.py
===================================================================
--- branches/soc-2008-quorn/release/scripts/bpymodules/BPyTextPlugin.py	2008-07-16 08:10:23 UTC (rev 15598)
+++ branches/soc-2008-quorn/release/scripts/bpymodules/BPyTextPlugin.py	2008-07-16 10:33:48 UTC (rev 15599)
@@ -1,10 +1,10 @@
-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
 SINGLE_QUOTE = 1
@@ -22,6 +22,23 @@
 _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 @@
 	
 	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 @@
 		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 @@
 	  '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 @@
 			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)

Modified: branches/soc-2008-quorn/release/scripts/textplugin_imports.py
===================================================================
--- branches/soc-2008-quorn/release/scripts/textplugin_imports.py	2008-07-16 08:10:23 UTC (rev 15598)
+++ branches/soc-2008-quorn/release/scripts/textplugin_imports.py	2008-07-16 10:33:48 UTC (rev 15599)
@@ -31,13 +31,13 @@
 		# 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 @@
 			
 			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()

Modified: branches/soc-2008-quorn/release/scripts/textplugin_membersuggest.py
===================================================================
--- branches/soc-2008-quorn/release/scripts/textplugin_membersuggest.py	2008-07-16 08:10:23 UTC (rev 15598)
+++ branches/soc-2008-quorn/release/scripts/textplugin_membersuggest.py	2008-07-16 10:33:48 UTC (rev 15599)
@@ -55,10 +55,7 @@
 	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 @@
 		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()

Modified: branches/soc-2008-quorn/release/scripts/textplugin_suggest.py
===================================================================
--- branches/soc-2008-quorn/release/scripts/textplugin_suggest.py	2008-07-16 08:10:23 UTC (rev 15598)
+++ branches/soc-2008-quorn/release/scripts/textplugin_suggest.py	2008-07-16 10:33:48 UTC (rev 15599)
@@ -43,10 +43,12 @@
 	
 	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 @@
 	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()





More information about the Bf-blender-cvs mailing list