2008-08-15 23:14:22 +00:00
|
|
|
"""The BPyTextPlugin Module
|
|
|
|
|
|
|
|
Use get_cached_descriptor(txt) to retrieve information about the script held in
|
|
|
|
the txt Text object.
|
|
|
|
|
|
|
|
Use print_cache_for(txt) to print the information to the console.
|
|
|
|
|
|
|
|
Use line, cursor = current_line(txt) to get the logical line and cursor position
|
|
|
|
|
|
|
|
Use get_targets(line, cursor) to find out what precedes the cursor:
|
|
|
|
aaa.bbb.cc|c.ddd -> ['aaa', 'bbb', 'cc']
|
|
|
|
|
|
|
|
Use resolve_targets(txt, targets) to turn a target list into a usable object if
|
|
|
|
one is found to match.
|
|
|
|
"""
|
|
|
|
|
2008-08-10 17:00:25 +00:00
|
|
|
import bpy, sys, os
|
2008-07-15 07:34:46 +00:00
|
|
|
import __builtin__, tokenize
|
2008-07-15 12:55:20 +00:00
|
|
|
from Blender.sys import time
|
2008-07-21 00:38:42 +00:00
|
|
|
from tokenize import generate_tokens, TokenError, \
|
2008-08-15 23:14:22 +00:00
|
|
|
COMMENT, DEDENT, INDENT, NAME, NEWLINE, NL, STRING, NUMBER
|
|
|
|
|
2008-09-10 21:37:22 +00:00
|
|
|
class Definition:
|
2008-08-15 23:14:22 +00:00
|
|
|
"""Describes a definition or defined object through its name, line number
|
|
|
|
and docstring. This is the base class for definition based descriptors.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, name, lineno, doc=''):
|
|
|
|
self.name = name
|
|
|
|
self.lineno = lineno
|
|
|
|
self.doc = doc
|
2008-07-18 11:00:34 +00:00
|
|
|
|
2008-09-10 21:37:22 +00:00
|
|
|
class ScriptDesc:
|
2008-07-26 20:02:10 +00:00
|
|
|
"""Describes a script through lists of further descriptor objects (classes,
|
|
|
|
defs, vars) and dictionaries to built-in types (imports). If a script has
|
|
|
|
not been fully parsed, its incomplete flag will be set. The time of the last
|
|
|
|
parse is held by the time field and the name of the text object from which
|
|
|
|
it was parsed, the name field.
|
|
|
|
"""
|
2008-07-18 11:00:34 +00:00
|
|
|
|
|
|
|
def __init__(self, name, imports, classes, defs, vars, incomplete=False):
|
|
|
|
self.name = name
|
|
|
|
self.imports = imports
|
|
|
|
self.classes = classes
|
|
|
|
self.defs = defs
|
|
|
|
self.vars = vars
|
|
|
|
self.incomplete = incomplete
|
2008-08-15 23:14:22 +00:00
|
|
|
self.parse_due = 0
|
2008-07-18 11:00:34 +00:00
|
|
|
|
2008-08-15 23:14:22 +00:00
|
|
|
def set_delay(self, delay):
|
|
|
|
self.parse_due = time() + delay
|
2008-07-18 11:00:34 +00:00
|
|
|
|
2008-08-15 23:14:22 +00:00
|
|
|
class ClassDesc(Definition):
|
2008-07-26 20:02:10 +00:00
|
|
|
"""Describes a class through lists of further descriptor objects (defs and
|
|
|
|
vars). The name of the class is held by the name field and the line on
|
|
|
|
which it is defined is held in lineno.
|
|
|
|
"""
|
2008-07-21 00:38:42 +00:00
|
|
|
|
2008-08-18 10:01:49 +00:00
|
|
|
def __init__(self, name, parents, defs, vars, lineno, doc=''):
|
2008-08-15 23:14:22 +00:00
|
|
|
Definition.__init__(self, name, lineno, doc)
|
2008-08-18 10:01:49 +00:00
|
|
|
self.parents = parents
|
2008-07-21 00:38:42 +00:00
|
|
|
self.defs = defs
|
|
|
|
self.vars = vars
|
|
|
|
|
2008-08-15 23:14:22 +00:00
|
|
|
class FunctionDesc(Definition):
|
2008-07-26 20:02:10 +00:00
|
|
|
"""Describes a function through its name and list of parameters (name,
|
|
|
|
params) and the line on which it is defined (lineno).
|
|
|
|
"""
|
2008-07-21 00:38:42 +00:00
|
|
|
|
2008-08-15 23:14:22 +00:00
|
|
|
def __init__(self, name, params, lineno, doc=''):
|
|
|
|
Definition.__init__(self, name, lineno, doc)
|
2008-07-21 00:38:42 +00:00
|
|
|
self.params = params
|
|
|
|
|
2008-08-15 23:14:22 +00:00
|
|
|
class VarDesc(Definition):
|
2008-07-26 20:02:10 +00:00
|
|
|
"""Describes a variable through its name and type (if ascertainable) and the
|
|
|
|
line on which it is defined (lineno). If no type can be determined, type
|
|
|
|
will equal None.
|
|
|
|
"""
|
2008-07-21 00:38:42 +00:00
|
|
|
|
|
|
|
def __init__(self, name, type, lineno):
|
2008-08-15 23:14:22 +00:00
|
|
|
Definition.__init__(self, name, lineno)
|
2008-07-21 00:38:42 +00:00
|
|
|
self.type = type # None for unknown (supports: dict/list/str)
|
|
|
|
|
2008-07-15 07:34:46 +00:00
|
|
|
# Context types
|
2008-07-21 00:38:42 +00:00
|
|
|
CTX_UNSET = -1
|
|
|
|
CTX_NORMAL = 0
|
|
|
|
CTX_SINGLE_QUOTE = 1
|
|
|
|
CTX_DOUBLE_QUOTE = 2
|
|
|
|
CTX_COMMENT = 3
|
2008-07-15 07:34:46 +00:00
|
|
|
|
|
|
|
# Python keywords
|
|
|
|
KEYWORDS = ['and', 'del', 'from', 'not', 'while', 'as', 'elif', 'global',
|
|
|
|
'or', 'with', 'assert', 'else', 'if', 'pass', 'yield',
|
|
|
|
'break', 'except', 'import', 'print', 'class', 'exec', 'in',
|
|
|
|
'raise', 'continue', 'finally', 'is', 'return', 'def', 'for',
|
|
|
|
'lambda', 'try' ]
|
|
|
|
|
2008-08-10 17:00:25 +00:00
|
|
|
# Module file extensions
|
|
|
|
MODULE_EXTS = ['.py', '.pyc', '.pyo', '.pyw', '.pyd']
|
|
|
|
|
2008-07-16 10:33:48 +00:00
|
|
|
ModuleType = type(__builtin__)
|
2008-07-18 11:00:34 +00:00
|
|
|
NoneScriptDesc = ScriptDesc('', dict(), dict(), dict(), dict(), True)
|
|
|
|
|
2008-08-10 17:00:25 +00:00
|
|
|
_modules = {}
|
2008-07-16 10:33:48 +00:00
|
|
|
_modules_updated = 0
|
2008-07-18 11:00:34 +00:00
|
|
|
_parse_cache = dict()
|
|
|
|
|
2008-08-10 17:00:25 +00:00
|
|
|
def _load_module_names():
|
|
|
|
"""Searches the sys.path for module files and lists them, along with
|
|
|
|
sys.builtin_module_names, in the global dict _modules.
|
|
|
|
"""
|
|
|
|
|
|
|
|
global _modules
|
|
|
|
|
|
|
|
for n in sys.builtin_module_names:
|
|
|
|
_modules[n] = None
|
|
|
|
for p in sys.path:
|
|
|
|
if p == '': p = os.curdir
|
|
|
|
if not os.path.isdir(p): continue
|
|
|
|
for f in os.listdir(p):
|
|
|
|
for ext in MODULE_EXTS:
|
|
|
|
if f.endswith(ext):
|
|
|
|
_modules[f[:-len(ext)]] = None
|
|
|
|
break
|
|
|
|
|
|
|
|
_load_module_names()
|
|
|
|
|
2008-08-15 23:14:22 +00:00
|
|
|
def _trim_doc(doc):
|
|
|
|
"""Trims the quotes from a quoted STRING token (eg. "'''text'''" -> "text")
|
|
|
|
"""
|
|
|
|
|
|
|
|
l = len(doc)
|
|
|
|
i = 0
|
|
|
|
while i < l/2 and (doc[i] == "'" or doc[i] == '"'):
|
|
|
|
i += 1
|
|
|
|
return doc[i:-i]
|
2008-08-10 17:00:25 +00:00
|
|
|
|
2008-08-15 23:14:22 +00:00
|
|
|
def resolve_targets(txt, targets):
|
|
|
|
"""Attempts to return a useful object for the locally or externally defined
|
|
|
|
entity described by targets. If the object is local (defined in txt), a
|
|
|
|
Definition instance is returned. If the object is external (imported or
|
|
|
|
built in), the object itself is returned. If no object can be found, None is
|
|
|
|
returned.
|
|
|
|
"""
|
|
|
|
|
|
|
|
count = len(targets)
|
|
|
|
if count==0: return None
|
|
|
|
|
|
|
|
obj = None
|
|
|
|
local = None
|
|
|
|
i = 1
|
|
|
|
|
|
|
|
desc = get_cached_descriptor(txt)
|
2008-08-18 10:01:49 +00:00
|
|
|
b = targets[0].find('(')
|
|
|
|
if b==-1: b = None # Trick to let us use [:b] and get the whole string
|
|
|
|
|
|
|
|
if desc.classes.has_key(targets[0][:b]):
|
|
|
|
local = desc.classes[targets[0][:b]]
|
2008-08-15 23:14:22 +00:00
|
|
|
elif desc.defs.has_key(targets[0]):
|
|
|
|
local = desc.defs[targets[0]]
|
|
|
|
elif desc.vars.has_key(targets[0]):
|
|
|
|
obj = desc.vars[targets[0]].type
|
|
|
|
|
|
|
|
if local:
|
|
|
|
while i < count:
|
2008-08-18 10:01:49 +00:00
|
|
|
b = targets[i].find('(')
|
|
|
|
if b==-1: b = None
|
|
|
|
if hasattr(local, 'classes') and local.classes.has_key(targets[i][:b]):
|
|
|
|
local = local.classes[targets[i][:b]]
|
2008-08-15 23:14:22 +00:00
|
|
|
elif hasattr(local, 'defs') and local.defs.has_key(targets[i]):
|
|
|
|
local = local.defs[targets[i]]
|
|
|
|
elif hasattr(local, 'vars') and local.vars.has_key(targets[i]):
|
|
|
|
obj = local.vars[targets[i]].type
|
|
|
|
local = None
|
|
|
|
i += 1
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
local = None
|
|
|
|
break
|
|
|
|
i += 1
|
|
|
|
|
|
|
|
if local: return local
|
|
|
|
|
|
|
|
if not obj:
|
|
|
|
if desc.imports.has_key(targets[0]):
|
|
|
|
obj = desc.imports[targets[0]]
|
|
|
|
else:
|
|
|
|
builtins = get_builtins()
|
|
|
|
if builtins.has_key(targets[0]):
|
|
|
|
obj = builtins[targets[0]]
|
|
|
|
|
|
|
|
while obj and i < count:
|
|
|
|
if hasattr(obj, targets[i]):
|
|
|
|
obj = getattr(obj, targets[i])
|
|
|
|
else:
|
|
|
|
obj = None
|
|
|
|
break
|
|
|
|
i += 1
|
|
|
|
|
|
|
|
return obj
|
|
|
|
|
|
|
|
def get_cached_descriptor(txt, force_parse=0):
|
2008-07-18 11:00:34 +00:00
|
|
|
"""Returns the cached ScriptDesc for the specified Text object 'txt'. If the
|
|
|
|
script has not been parsed in the last 'period' seconds it will be reparsed
|
|
|
|
to obtain this descriptor.
|
|
|
|
|
2008-08-10 17:00:25 +00:00
|
|
|
Specifying TP_AUTO for the period (default) will choose a period based on the
|
2008-07-18 11:00:34 +00:00
|
|
|
size of the Text object. Larger texts are parsed less often.
|
|
|
|
"""
|
|
|
|
|
2008-08-10 17:00:25 +00:00
|
|
|
global _parse_cache
|
2008-07-18 11:00:34 +00:00
|
|
|
|
|
|
|
parse = True
|
2008-07-26 20:02:10 +00:00
|
|
|
key = hash(txt)
|
2008-08-15 23:14:22 +00:00
|
|
|
if not force_parse and _parse_cache.has_key(key):
|
2008-07-18 11:00:34 +00:00
|
|
|
desc = _parse_cache[key]
|
2008-08-15 23:14:22 +00:00
|
|
|
if desc.parse_due > time():
|
2008-07-18 11:00:34 +00:00
|
|
|
parse = desc.incomplete
|
|
|
|
|
|
|
|
if parse:
|
2008-07-26 20:02:10 +00:00
|
|
|
desc = parse_text(txt)
|
2008-07-18 11:00:34 +00:00
|
|
|
|
|
|
|
return desc
|
|
|
|
|
|
|
|
def parse_text(txt):
|
|
|
|
"""Parses an entire script's text and returns a ScriptDesc instance
|
|
|
|
containing information about the script.
|
|
|
|
|
2008-07-21 00:38:42 +00:00
|
|
|
If the text is not a valid Python script (for example if brackets are left
|
|
|
|
open), parsing may fail to complete. However, if this occurs, no exception
|
|
|
|
is thrown. Instead the returned ScriptDesc instance will have its incomplete
|
|
|
|
flag set and information processed up to this point will still be accessible.
|
2008-07-18 11:00:34 +00:00
|
|
|
"""
|
|
|
|
|
2008-08-15 23:14:22 +00:00
|
|
|
start_time = time()
|
2008-07-18 11:00:34 +00:00
|
|
|
txt.reset()
|
|
|
|
tokens = generate_tokens(txt.readline) # Throws TokenError
|
|
|
|
|
|
|
|
curl, cursor = txt.getCursorPos()
|
|
|
|
linen = curl + 1 # Token line numbers are one-based
|
|
|
|
|
|
|
|
imports = dict()
|
|
|
|
imp_step = 0
|
|
|
|
|
|
|
|
classes = dict()
|
|
|
|
cls_step = 0
|
|
|
|
|
|
|
|
defs = dict()
|
|
|
|
def_step = 0
|
|
|
|
|
|
|
|
vars = dict()
|
2008-07-21 00:38:42 +00:00
|
|
|
var1_step = 0
|
|
|
|
var2_step = 0
|
|
|
|
var3_step = 0
|
2008-07-18 11:00:34 +00:00
|
|
|
var_accum = dict()
|
|
|
|
var_forflag = False
|
|
|
|
|
|
|
|
indent = 0
|
|
|
|
prev_type = -1
|
2008-08-15 23:14:22 +00:00
|
|
|
prev_text = ''
|
2008-07-18 11:00:34 +00:00
|
|
|
incomplete = False
|
|
|
|
|
2008-07-26 20:02:10 +00:00
|
|
|
while True:
|
|
|
|
try:
|
2008-08-15 23:14:22 +00:00
|
|
|
type, text, start, end, line = tokens.next()
|
2008-07-26 20:02:10 +00:00
|
|
|
except StopIteration:
|
|
|
|
break
|
2008-08-18 14:16:34 +00:00
|
|
|
except (TokenError, IndentationError):
|
2008-07-26 20:02:10 +00:00
|
|
|
incomplete = True
|
|
|
|
break
|
2008-07-18 11:00:34 +00:00
|
|
|
|
2008-07-21 00:38:42 +00:00
|
|
|
# Skip all comments and line joining characters
|
|
|
|
if type == COMMENT or type == NL:
|
|
|
|
continue
|
|
|
|
|
2008-07-18 11:00:34 +00:00
|
|
|
#################
|
|
|
|
## Indentation ##
|
|
|
|
#################
|
|
|
|
|
2008-07-21 00:38:42 +00:00
|
|
|
if type == INDENT:
|
2008-07-18 11:00:34 +00:00
|
|
|
indent += 1
|
2008-07-21 00:38:42 +00:00
|
|
|
elif type == DEDENT:
|
2008-07-18 11:00:34 +00:00
|
|
|
indent -= 1
|
|
|
|
|
|
|
|
#########################
|
|
|
|
## Module importing... ##
|
|
|
|
#########################
|
|
|
|
|
|
|
|
imp_store = False
|
|
|
|
|
|
|
|
# Default, look for 'from' or 'import' to start
|
|
|
|
if imp_step == 0:
|
2008-08-15 23:14:22 +00:00
|
|
|
if text == 'from':
|
2008-07-18 11:00:34 +00:00
|
|
|
imp_tmp = []
|
|
|
|
imp_step = 1
|
2008-08-15 23:14:22 +00:00
|
|
|
elif text == 'import':
|
2008-07-18 11:00:34 +00:00
|
|
|
imp_from = None
|
|
|
|
imp_tmp = []
|
|
|
|
imp_step = 2
|
|
|
|
|
|
|
|
# Found a 'from', create imp_from in form '???.???...'
|
|
|
|
elif imp_step == 1:
|
2008-08-15 23:14:22 +00:00
|
|
|
if text == 'import':
|
2008-07-18 11:00:34 +00:00
|
|
|
imp_from = '.'.join(imp_tmp)
|
|
|
|
imp_tmp = []
|
|
|
|
imp_step = 2
|
2008-07-21 00:38:42 +00:00
|
|
|
elif type == NAME:
|
2008-08-15 23:14:22 +00:00
|
|
|
imp_tmp.append(text)
|
|
|
|
elif text != '.':
|
2008-07-18 11:00:34 +00:00
|
|
|
imp_step = 0 # Invalid syntax
|
|
|
|
|
|
|
|
# Found 'import', imp_from is populated or None, create imp_name
|
|
|
|
elif imp_step == 2:
|
2008-08-15 23:14:22 +00:00
|
|
|
if text == 'as':
|
2008-07-18 11:00:34 +00:00
|
|
|
imp_name = '.'.join(imp_tmp)
|
|
|
|
imp_step = 3
|
2008-08-15 23:14:22 +00:00
|
|
|
elif type == NAME or text == '*':
|
|
|
|
imp_tmp.append(text)
|
|
|
|
elif text != '.':
|
2008-07-18 11:00:34 +00:00
|
|
|
imp_name = '.'.join(imp_tmp)
|
|
|
|
imp_symb = imp_name
|
|
|
|
imp_store = True
|
|
|
|
|
|
|
|
# Found 'as', change imp_symb to this value and go back to step 2
|
|
|
|
elif imp_step == 3:
|
2008-07-21 00:38:42 +00:00
|
|
|
if type == NAME:
|
2008-08-15 23:14:22 +00:00
|
|
|
imp_symb = text
|
2008-07-18 11:00:34 +00:00
|
|
|
else:
|
|
|
|
imp_store = True
|
|
|
|
|
|
|
|
# Both imp_name and imp_symb have now been populated so we can import
|
|
|
|
if imp_store:
|
|
|
|
|
|
|
|
# Handle special case of 'import *'
|
|
|
|
if imp_name == '*':
|
|
|
|
parent = get_module(imp_from)
|
|
|
|
imports.update(parent.__dict__)
|
|
|
|
|
|
|
|
else:
|
|
|
|
# Try importing the name as a module
|
|
|
|
try:
|
|
|
|
if imp_from:
|
|
|
|
module = get_module(imp_from +'.'+ imp_name)
|
|
|
|
else:
|
|
|
|
module = get_module(imp_name)
|
|
|
|
except (ImportError, ValueError, AttributeError, TypeError):
|
|
|
|
# Try importing name as an attribute of the parent
|
|
|
|
try:
|
|
|
|
module = __import__(imp_from, globals(), locals(), [imp_name])
|
2008-08-08 15:54:04 +00:00
|
|
|
imports[imp_symb] = getattr(module, imp_name)
|
2008-07-18 11:00:34 +00:00
|
|
|
except (ImportError, ValueError, AttributeError, TypeError):
|
|
|
|
pass
|
2008-07-26 20:02:10 +00:00
|
|
|
else:
|
|
|
|
imports[imp_symb] = module
|
2008-07-18 11:00:34 +00:00
|
|
|
|
|
|
|
# More to import from the same module?
|
2008-08-15 23:14:22 +00:00
|
|
|
if text == ',':
|
2008-07-18 11:00:34 +00:00
|
|
|
imp_tmp = []
|
|
|
|
imp_step = 2
|
|
|
|
else:
|
|
|
|
imp_step = 0
|
|
|
|
|
|
|
|
###################
|
|
|
|
## Class parsing ##
|
|
|
|
###################
|
|
|
|
|
|
|
|
# If we are inside a class then def and variable parsing should be done
|
|
|
|
# for the class. Otherwise the definitions are considered global
|
|
|
|
|
|
|
|
# Look for 'class'
|
|
|
|
if cls_step == 0:
|
2008-08-15 23:14:22 +00:00
|
|
|
if text == 'class':
|
2008-07-18 11:00:34 +00:00
|
|
|
cls_name = None
|
2008-07-21 00:38:42 +00:00
|
|
|
cls_lineno = start[0]
|
2008-07-18 11:00:34 +00:00
|
|
|
cls_indent = indent
|
|
|
|
cls_step = 1
|
|
|
|
|
2008-08-18 10:01:49 +00:00
|
|
|
# Found 'class', look for cls_name followed by '(' parents ')'
|
2008-07-18 11:00:34 +00:00
|
|
|
elif cls_step == 1:
|
|
|
|
if not cls_name:
|
2008-07-21 00:38:42 +00:00
|
|
|
if type == NAME:
|
2008-08-15 23:14:22 +00:00
|
|
|
cls_name = text
|
2008-07-18 11:00:34 +00:00
|
|
|
cls_sline = False
|
2008-08-18 10:01:49 +00:00
|
|
|
cls_parents = dict()
|
2008-07-18 11:00:34 +00:00
|
|
|
cls_defs = dict()
|
|
|
|
cls_vars = dict()
|
2008-08-18 10:01:49 +00:00
|
|
|
elif type == NAME:
|
|
|
|
if classes.has_key(text):
|
|
|
|
parent = classes[text]
|
|
|
|
cls_parents[text] = parent
|
|
|
|
cls_defs.update(parent.defs)
|
|
|
|
cls_vars.update(parent.vars)
|
2008-08-15 23:14:22 +00:00
|
|
|
elif text == ':':
|
2008-07-18 11:00:34 +00:00
|
|
|
cls_step = 2
|
|
|
|
|
|
|
|
# Found 'class' name ... ':', now check if it's a single line statement
|
|
|
|
elif cls_step == 2:
|
2008-07-21 00:38:42 +00:00
|
|
|
if type == NEWLINE:
|
2008-07-18 11:00:34 +00:00
|
|
|
cls_sline = False
|
2008-07-21 00:38:42 +00:00
|
|
|
else:
|
2008-07-18 11:00:34 +00:00
|
|
|
cls_sline = True
|
2008-08-15 23:14:22 +00:00
|
|
|
cls_doc = ''
|
|
|
|
cls_step = 3
|
2008-07-18 11:00:34 +00:00
|
|
|
|
|
|
|
elif cls_step == 3:
|
2008-08-15 23:14:22 +00:00
|
|
|
if not cls_doc and type == STRING:
|
|
|
|
cls_doc = _trim_doc(text)
|
2008-07-18 11:00:34 +00:00
|
|
|
if cls_sline:
|
2008-07-21 00:38:42 +00:00
|
|
|
if type == NEWLINE:
|
2008-08-18 10:01:49 +00:00
|
|
|
classes[cls_name] = ClassDesc(cls_name, cls_parents, cls_defs, cls_vars, cls_lineno, cls_doc)
|
2008-07-18 11:00:34 +00:00
|
|
|
cls_step = 0
|
|
|
|
else:
|
2008-07-21 00:38:42 +00:00
|
|
|
if type == DEDENT and indent <= cls_indent:
|
2008-08-18 10:01:49 +00:00
|
|
|
classes[cls_name] = ClassDesc(cls_name, cls_parents, cls_defs, cls_vars, cls_lineno, cls_doc)
|
2008-07-18 11:00:34 +00:00
|
|
|
cls_step = 0
|
|
|
|
|
|
|
|
#################
|
|
|
|
## Def parsing ##
|
|
|
|
#################
|
|
|
|
|
|
|
|
# Look for 'def'
|
|
|
|
if def_step == 0:
|
2008-08-15 23:14:22 +00:00
|
|
|
if text == 'def':
|
2008-07-18 11:00:34 +00:00
|
|
|
def_name = None
|
2008-07-21 00:38:42 +00:00
|
|
|
def_lineno = start[0]
|
2008-07-18 11:00:34 +00:00
|
|
|
def_step = 1
|
|
|
|
|
|
|
|
# Found 'def', look for def_name followed by '('
|
|
|
|
elif def_step == 1:
|
2008-07-21 00:38:42 +00:00
|
|
|
if type == NAME:
|
2008-08-15 23:14:22 +00:00
|
|
|
def_name = text
|
2008-07-18 11:00:34 +00:00
|
|
|
def_params = []
|
2008-08-15 23:14:22 +00:00
|
|
|
elif def_name and text == '(':
|
2008-07-18 11:00:34 +00:00
|
|
|
def_step = 2
|
|
|
|
|
|
|
|
# Found 'def' name '(', now identify the parameters upto ')'
|
|
|
|
# TODO: Handle ellipsis '...'
|
|
|
|
elif def_step == 2:
|
2008-07-21 00:38:42 +00:00
|
|
|
if type == NAME:
|
2008-08-15 23:14:22 +00:00
|
|
|
def_params.append(text)
|
|
|
|
elif text == ':':
|
|
|
|
def_step = 3
|
|
|
|
|
|
|
|
# Found 'def' ... ':', now check if it's a single line statement
|
|
|
|
elif def_step == 3:
|
|
|
|
if type == NEWLINE:
|
|
|
|
def_sline = False
|
|
|
|
else:
|
|
|
|
def_sline = True
|
|
|
|
def_doc = ''
|
|
|
|
def_step = 4
|
|
|
|
|
|
|
|
elif def_step == 4:
|
|
|
|
if type == STRING:
|
|
|
|
def_doc = _trim_doc(text)
|
|
|
|
newdef = None
|
|
|
|
if def_sline:
|
|
|
|
if type == NEWLINE:
|
|
|
|
newdef = FunctionDesc(def_name, def_params, def_lineno, def_doc)
|
|
|
|
else:
|
|
|
|
if type == NAME:
|
|
|
|
newdef = FunctionDesc(def_name, def_params, def_lineno, def_doc)
|
|
|
|
if newdef:
|
2008-07-18 11:00:34 +00:00
|
|
|
if cls_step > 0: # Parsing a class
|
2008-08-15 23:14:22 +00:00
|
|
|
cls_defs[def_name] = newdef
|
2008-07-18 11:00:34 +00:00
|
|
|
else:
|
2008-08-15 23:14:22 +00:00
|
|
|
defs[def_name] = newdef
|
2008-07-18 11:00:34 +00:00
|
|
|
def_step = 0
|
|
|
|
|
|
|
|
##########################
|
|
|
|
## Variable assignation ##
|
|
|
|
##########################
|
|
|
|
|
|
|
|
if cls_step > 0: # Parsing a class
|
|
|
|
# Look for 'self.???'
|
2008-07-21 00:38:42 +00:00
|
|
|
if var1_step == 0:
|
2008-08-15 23:14:22 +00:00
|
|
|
if text == 'self':
|
2008-07-21 00:38:42 +00:00
|
|
|
var1_step = 1
|
|
|
|
elif var1_step == 1:
|
2008-08-15 23:14:22 +00:00
|
|
|
if text == '.':
|
2008-07-18 11:00:34 +00:00
|
|
|
var_name = None
|
2008-07-21 00:38:42 +00:00
|
|
|
var1_step = 2
|
2008-07-18 11:00:34 +00:00
|
|
|
else:
|
2008-07-21 00:38:42 +00:00
|
|
|
var1_step = 0
|
|
|
|
elif var1_step == 2:
|
|
|
|
if type == NAME:
|
2008-08-15 23:14:22 +00:00
|
|
|
var_name = text
|
2008-07-21 00:38:42 +00:00
|
|
|
if cls_vars.has_key(var_name):
|
|
|
|
var_step = 0
|
|
|
|
else:
|
|
|
|
var1_step = 3
|
|
|
|
elif var1_step == 3:
|
2008-08-15 23:14:22 +00:00
|
|
|
if text == '=':
|
2008-07-21 00:38:42 +00:00
|
|
|
var1_step = 4
|
2008-08-30 11:27:27 +00:00
|
|
|
elif text != ',':
|
|
|
|
var1_step = 0
|
2008-07-21 00:38:42 +00:00
|
|
|
elif var1_step == 4:
|
|
|
|
var_type = None
|
2008-08-15 23:14:22 +00:00
|
|
|
if type == NUMBER:
|
2008-08-17 10:08:38 +00:00
|
|
|
close = end[1]
|
2008-08-15 23:14:22 +00:00
|
|
|
if text.find('.') != -1: var_type = float
|
|
|
|
else: var_type = int
|
2008-07-26 20:02:10 +00:00
|
|
|
elif type == STRING:
|
|
|
|
close = end[1]
|
2008-07-21 00:38:42 +00:00
|
|
|
var_type = str
|
2008-08-15 23:14:22 +00:00
|
|
|
elif text == '[':
|
|
|
|
close = line.find(']', end[1])
|
|
|
|
var_type = list
|
|
|
|
elif text == '(':
|
2008-07-21 00:38:42 +00:00
|
|
|
close = line.find(')', end[1])
|
|
|
|
var_type = tuple
|
2008-08-15 23:14:22 +00:00
|
|
|
elif text == '{':
|
2008-07-21 00:38:42 +00:00
|
|
|
close = line.find('}', end[1])
|
|
|
|
var_type = dict
|
2008-08-15 23:14:22 +00:00
|
|
|
elif text == 'dict':
|
2008-07-21 00:38:42 +00:00
|
|
|
close = line.find(')', end[1])
|
|
|
|
var_type = dict
|
|
|
|
if var_type and close+1 < len(line):
|
2008-07-21 11:21:49 +00:00
|
|
|
if line[close+1] != ' ' and line[close+1] != '\t':
|
2008-07-21 00:38:42 +00:00
|
|
|
var_type = None
|
|
|
|
cls_vars[var_name] = VarDesc(var_name, var_type, start[0])
|
|
|
|
var1_step = 0
|
2008-07-18 11:00:34 +00:00
|
|
|
|
|
|
|
elif def_step > 0: # Parsing a def
|
|
|
|
# Look for 'global ???[,???]'
|
2008-07-21 00:38:42 +00:00
|
|
|
if var2_step == 0:
|
2008-08-15 23:14:22 +00:00
|
|
|
if text == 'global':
|
2008-07-21 00:38:42 +00:00
|
|
|
var2_step = 1
|
|
|
|
elif var2_step == 1:
|
|
|
|
if type == NAME:
|
2008-08-15 23:14:22 +00:00
|
|
|
if not vars.has_key(text):
|
|
|
|
vars[text] = VarDesc(text, None, start[0])
|
|
|
|
elif text != ',' and type != NL:
|
2008-07-21 00:38:42 +00:00
|
|
|
var2_step == 0
|
2008-07-18 11:00:34 +00:00
|
|
|
|
|
|
|
else: # In global scope
|
2008-07-21 00:38:42 +00:00
|
|
|
if var3_step == 0:
|
|
|
|
# Look for names
|
2008-08-15 23:14:22 +00:00
|
|
|
if text == 'for':
|
2008-07-21 00:38:42 +00:00
|
|
|
var_accum = dict()
|
|
|
|
var_forflag = True
|
2008-08-15 23:14:22 +00:00
|
|
|
elif text == '=' or (var_forflag and text == 'in'):
|
2008-07-21 00:38:42 +00:00
|
|
|
var_forflag = False
|
|
|
|
var3_step = 1
|
|
|
|
elif type == NAME:
|
2008-08-15 23:14:22 +00:00
|
|
|
if prev_text != '.' and not vars.has_key(text):
|
|
|
|
var_accum[text] = VarDesc(text, None, start[0])
|
|
|
|
elif not text in [',', '(', ')', '[', ']']:
|
2008-07-21 00:38:42 +00:00
|
|
|
var_accum = dict()
|
|
|
|
var_forflag = False
|
|
|
|
elif var3_step == 1:
|
|
|
|
if len(var_accum) != 1:
|
|
|
|
var_type = None
|
|
|
|
vars.update(var_accum)
|
|
|
|
else:
|
|
|
|
var_name = var_accum.keys()[0]
|
|
|
|
var_type = None
|
2008-08-15 23:14:22 +00:00
|
|
|
if type == NUMBER:
|
|
|
|
if text.find('.') != -1: var_type = float
|
|
|
|
else: var_type = int
|
2008-07-26 20:02:10 +00:00
|
|
|
elif type == STRING: var_type = str
|
2008-08-15 23:14:22 +00:00
|
|
|
elif text == '[': var_type = list
|
|
|
|
elif text == '(': var_type = tuple
|
|
|
|
elif text == '{': var_type = dict
|
2008-07-21 00:38:42 +00:00
|
|
|
vars[var_name] = VarDesc(var_name, var_type, start[0])
|
|
|
|
var3_step = 0
|
2008-07-18 11:00:34 +00:00
|
|
|
|
|
|
|
#######################
|
|
|
|
## General utilities ##
|
|
|
|
#######################
|
|
|
|
|
|
|
|
prev_type = type
|
2008-08-15 23:14:22 +00:00
|
|
|
prev_text = text
|
2008-07-18 11:00:34 +00:00
|
|
|
|
|
|
|
desc = ScriptDesc(txt.name, imports, classes, defs, vars, incomplete)
|
2008-08-15 23:14:22 +00:00
|
|
|
desc.set_delay(10 * (time()-start_time) + 0.05)
|
2008-07-18 11:00:34 +00:00
|
|
|
|
|
|
|
global _parse_cache
|
2008-08-12 15:17:08 +00:00
|
|
|
_parse_cache[hash(txt)] = desc
|
2008-07-18 11:00:34 +00:00
|
|
|
return desc
|
2008-07-16 10:33:48 +00:00
|
|
|
|
|
|
|
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()
|
|
|
|
|
2008-07-15 07:34:46 +00:00
|
|
|
def suggest_cmp(x, y):
|
2008-07-15 12:55:20 +00:00
|
|
|
"""Use this method when sorting a list of suggestions.
|
|
|
|
"""
|
2008-07-15 07:34:46 +00:00
|
|
|
|
2008-07-15 17:03:59 +00:00
|
|
|
return cmp(x[0].upper(), y[0].upper())
|
2008-07-15 07:34:46 +00:00
|
|
|
|
|
|
|
def get_module(name):
|
2008-07-15 12:55:20 +00:00
|
|
|
"""Returns the module specified by its name. The module itself is imported
|
|
|
|
by this method and, as such, any initialization code will be executed.
|
|
|
|
"""
|
2008-07-15 07:34:46 +00:00
|
|
|
|
|
|
|
mod = __import__(name)
|
|
|
|
components = name.split('.')
|
|
|
|
for comp in components[1:]:
|
|
|
|
mod = getattr(mod, comp)
|
|
|
|
return mod
|
|
|
|
|
|
|
|
def type_char(v):
|
2008-07-15 12:55:20 +00:00
|
|
|
"""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.
|
|
|
|
|
|
|
|
The following values are returned:
|
|
|
|
'm' if the parameter is a module
|
|
|
|
'f' if the parameter is callable
|
|
|
|
'v' if the parameter is variable or otherwise indeterminable
|
2008-07-18 11:00:34 +00:00
|
|
|
|
2008-07-15 12:55:20 +00:00
|
|
|
"""
|
|
|
|
|
2008-07-16 10:33:48 +00:00
|
|
|
if isinstance(v, ModuleType):
|
2008-07-15 07:34:46 +00:00
|
|
|
return 'm'
|
|
|
|
elif callable(v):
|
|
|
|
return 'f'
|
|
|
|
else:
|
|
|
|
return 'v'
|
|
|
|
|
2008-07-15 12:55:20 +00:00
|
|
|
def get_context(txt):
|
|
|
|
"""Establishes the context of the cursor in the given Blender Text object
|
2008-07-15 07:34:46 +00:00
|
|
|
|
|
|
|
Returns one of:
|
2008-07-21 00:38:42 +00:00
|
|
|
CTX_NORMAL - Cursor is in a normal context
|
|
|
|
CTX_SINGLE_QUOTE - Cursor is inside a single quoted string
|
|
|
|
CTX_DOUBLE_QUOTE - Cursor is inside a double quoted string
|
|
|
|
CTX_COMMENT - Cursor is inside a comment
|
2008-07-15 07:34:46 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
|
2008-07-15 12:55:20 +00:00
|
|
|
l, cursor = txt.getCursorPos()
|
2008-08-12 15:17:08 +00:00
|
|
|
lines = txt.asLines(0, l+1)
|
|
|
|
|
|
|
|
# FIXME: This method is too slow in large files for it to be called as often
|
|
|
|
# as it is. So for lines below the 1000th line we do this... (quorn)
|
|
|
|
if l > 1000: return CTX_NORMAL
|
2008-07-15 12:55:20 +00:00
|
|
|
|
2008-07-15 07:34:46 +00:00
|
|
|
# Detect context (in string or comment)
|
2008-07-21 00:38:42 +00:00
|
|
|
in_str = CTX_NORMAL
|
2008-07-15 12:55:20 +00:00
|
|
|
for line in lines:
|
|
|
|
if l == 0:
|
|
|
|
end = cursor
|
2008-07-15 07:34:46 +00:00
|
|
|
else:
|
2008-07-15 12:55:20 +00:00
|
|
|
end = len(line)
|
|
|
|
l -= 1
|
|
|
|
|
|
|
|
# Comments end at new lines
|
2008-07-21 00:38:42 +00:00
|
|
|
if in_str == CTX_COMMENT:
|
|
|
|
in_str = CTX_NORMAL
|
2008-07-15 12:55:20 +00:00
|
|
|
|
|
|
|
for i in range(end):
|
|
|
|
if in_str == 0:
|
2008-07-21 00:38:42 +00:00
|
|
|
if line[i] == "'": in_str = CTX_SINGLE_QUOTE
|
|
|
|
elif line[i] == '"': in_str = CTX_DOUBLE_QUOTE
|
|
|
|
elif line[i] == '#': in_str = CTX_COMMENT
|
2008-07-15 12:55:20 +00:00
|
|
|
else:
|
2008-07-21 00:38:42 +00:00
|
|
|
if in_str == CTX_SINGLE_QUOTE:
|
2008-07-15 12:55:20 +00:00
|
|
|
if line[i] == "'":
|
2008-07-21 00:38:42 +00:00
|
|
|
in_str = CTX_NORMAL
|
2008-07-15 12:55:20 +00:00
|
|
|
# In again if ' escaped, out again if \ escaped, and so on
|
|
|
|
for a in range(i-1, -1, -1):
|
|
|
|
if line[a] == '\\': in_str = 1-in_str
|
|
|
|
else: break
|
2008-07-21 00:38:42 +00:00
|
|
|
elif in_str == CTX_DOUBLE_QUOTE:
|
2008-07-15 12:55:20 +00:00
|
|
|
if line[i] == '"':
|
2008-07-21 00:38:42 +00:00
|
|
|
in_str = CTX_NORMAL
|
2008-07-15 12:55:20 +00:00
|
|
|
# In again if " escaped, out again if \ escaped, and so on
|
|
|
|
for a in range(i-1, -1, -1):
|
|
|
|
if line[i-a] == '\\': in_str = 2-in_str
|
|
|
|
else: break
|
|
|
|
|
2008-07-15 07:34:46 +00:00
|
|
|
return in_str
|
|
|
|
|
|
|
|
def current_line(txt):
|
|
|
|
"""Extracts the Python script line at the cursor in the Blender Text object
|
|
|
|
provided and cursor position within this line as the tuple pair (line,
|
2008-07-18 11:00:34 +00:00
|
|
|
cursor).
|
|
|
|
"""
|
2008-07-15 07:34:46 +00:00
|
|
|
|
2008-07-21 00:38:42 +00:00
|
|
|
lineindex, cursor = txt.getCursorPos()
|
2008-07-15 07:34:46 +00:00
|
|
|
lines = txt.asLines()
|
|
|
|
line = lines[lineindex]
|
|
|
|
|
|
|
|
# Join previous lines to this line if spanning
|
|
|
|
i = lineindex - 1
|
|
|
|
while i > 0:
|
|
|
|
earlier = lines[i].rstrip()
|
|
|
|
if earlier.endswith('\\'):
|
|
|
|
line = earlier[:-1] + ' ' + line
|
|
|
|
cursor += len(earlier)
|
|
|
|
i -= 1
|
|
|
|
|
|
|
|
# Join later lines while there is an explicit joining character
|
|
|
|
i = lineindex
|
2008-07-15 12:55:20 +00:00
|
|
|
while i < len(lines)-1 and lines[i].rstrip().endswith('\\'):
|
2008-07-15 07:34:46 +00:00
|
|
|
later = lines[i+1].strip()
|
|
|
|
line = line + ' ' + later[:-1]
|
2008-07-15 12:55:20 +00:00
|
|
|
i += 1
|
2008-07-15 07:34:46 +00:00
|
|
|
|
|
|
|
return line, cursor
|
|
|
|
|
|
|
|
def get_targets(line, cursor):
|
|
|
|
"""Parses a period separated string of valid names preceding the cursor and
|
2008-07-18 11:00:34 +00:00
|
|
|
returns them as a list in the same order.
|
|
|
|
"""
|
2008-07-15 07:34:46 +00:00
|
|
|
|
2008-08-18 10:01:49 +00:00
|
|
|
brk = 0
|
|
|
|
targets = []
|
|
|
|
j = cursor
|
|
|
|
i = j-1
|
|
|
|
while i >= 0:
|
|
|
|
if line[i] == ')': brk += 1
|
|
|
|
elif brk:
|
|
|
|
if line[i] == '(': brk -= 1
|
|
|
|
else:
|
|
|
|
if line[i] == '.':
|
|
|
|
targets.insert(0, line[i+1:j]); j=i
|
|
|
|
elif not (line[i].isalnum() or line[i] == '_' or line[i] == '.'):
|
|
|
|
break
|
2008-07-15 07:34:46 +00:00
|
|
|
i -= 1
|
2008-08-18 10:01:49 +00:00
|
|
|
targets.insert(0, line[i+1:j])
|
|
|
|
return targets
|
2008-07-15 07:34:46 +00:00
|
|
|
|
2008-07-18 11:00:34 +00:00
|
|
|
def get_defs(txt):
|
|
|
|
"""Returns a dictionary which maps definition names in the source code to
|
|
|
|
a list of their parameter names.
|
|
|
|
|
|
|
|
The line 'def doit(one, two, three): print one' for example, results in the
|
|
|
|
mapping 'doit' : [ 'one', 'two', 'three' ]
|
|
|
|
"""
|
|
|
|
|
|
|
|
return get_cached_descriptor(txt).defs
|
|
|
|
|
|
|
|
def get_vars(txt):
|
|
|
|
"""Returns a dictionary of variable names found in the specified Text
|
|
|
|
object. This method locates all names followed directly by an equal sign:
|
|
|
|
'a = ???' or indirectly as part of a tuple/list assignment or inside a
|
|
|
|
'for ??? in ???:' block.
|
|
|
|
"""
|
|
|
|
|
|
|
|
return get_cached_descriptor(txt).vars
|
|
|
|
|
2008-07-15 07:34:46 +00:00
|
|
|
def get_imports(txt):
|
|
|
|
"""Returns a dictionary which maps symbol names in the source code to their
|
|
|
|
respective modules.
|
|
|
|
|
|
|
|
The line 'from Blender import Text as BText' for example, results in the
|
|
|
|
mapping 'BText' : <module 'Blender.Text' (built-in)>
|
|
|
|
|
|
|
|
Note that this method imports the modules to provide this mapping as as such
|
|
|
|
will execute any initilization code found within.
|
|
|
|
"""
|
|
|
|
|
2008-07-18 11:00:34 +00:00
|
|
|
return get_cached_descriptor(txt).imports
|
2008-07-15 07:34:46 +00:00
|
|
|
|
|
|
|
def get_builtins():
|
|
|
|
"""Returns a dictionary of built-in modules, functions and variables."""
|
|
|
|
|
|
|
|
return __builtin__.__dict__
|
|
|
|
|
2008-07-15 12:55:20 +00:00
|
|
|
|
2008-07-18 11:00:34 +00:00
|
|
|
#################################
|
|
|
|
## Debugging utility functions ##
|
|
|
|
#################################
|
|
|
|
|
|
|
|
def print_cache_for(txt, period=sys.maxint):
|
|
|
|
"""Prints out the data cached for a given Text object. If no period is
|
|
|
|
given the text will not be reparsed and the cached version will be returned.
|
|
|
|
Otherwise if the period has expired the text will be reparsed.
|
2008-07-15 12:55:20 +00:00
|
|
|
"""
|
|
|
|
|
2008-07-18 11:00:34 +00:00
|
|
|
desc = get_cached_descriptor(txt, period)
|
|
|
|
print '================================================'
|
|
|
|
print 'Name:', desc.name, '('+str(hash(txt))+')'
|
|
|
|
print '------------------------------------------------'
|
|
|
|
print 'Defs:'
|
2008-07-21 00:38:42 +00:00
|
|
|
for name, ddesc in desc.defs.items():
|
|
|
|
print ' ', name, ddesc.params, ddesc.lineno
|
2008-08-15 23:14:22 +00:00
|
|
|
print ' ', ddesc.doc
|
2008-07-18 11:00:34 +00:00
|
|
|
print '------------------------------------------------'
|
|
|
|
print 'Vars:'
|
2008-07-21 00:38:42 +00:00
|
|
|
for name, vdesc in desc.vars.items():
|
|
|
|
print ' ', name, vdesc.type, vdesc.lineno
|
2008-07-18 11:00:34 +00:00
|
|
|
print '------------------------------------------------'
|
|
|
|
print 'Imports:'
|
|
|
|
for name, item in desc.imports.items():
|
|
|
|
print ' ', name.ljust(15), item
|
|
|
|
print '------------------------------------------------'
|
|
|
|
print 'Classes:'
|
|
|
|
for clsnme, clsdsc in desc.classes.items():
|
|
|
|
print ' *********************************'
|
|
|
|
print ' Name:', clsnme
|
2008-08-15 23:14:22 +00:00
|
|
|
print ' ', clsdsc.doc
|
2008-07-18 11:00:34 +00:00
|
|
|
print ' ---------------------------------'
|
|
|
|
print ' Defs:'
|
2008-07-21 00:38:42 +00:00
|
|
|
for name, ddesc in clsdsc.defs.items():
|
|
|
|
print ' ', name, ddesc.params, ddesc.lineno
|
2008-08-15 23:14:22 +00:00
|
|
|
print ' ', ddesc.doc
|
2008-07-18 11:00:34 +00:00
|
|
|
print ' ---------------------------------'
|
|
|
|
print ' Vars:'
|
2008-07-21 00:38:42 +00:00
|
|
|
for name, vdesc in clsdsc.vars.items():
|
|
|
|
print ' ', name, vdesc.type, vdesc.lineno
|
2008-07-18 11:00:34 +00:00
|
|
|
print ' *********************************'
|
|
|
|
print '================================================'
|