improved many docstrings

This commit is contained in:
David Halter
2012-08-30 15:07:11 +02:00
parent d21044dca2
commit 83a5a4267f
7 changed files with 53 additions and 17 deletions

View File

@@ -17,6 +17,10 @@ module_find_path = sys.path[1:]
class CachedModule(object): class CachedModule(object):
"""
The base type for all modules, which is not to be confused with
`parsing.Module`. Caching happens here.
"""
cache = {} cache = {}
def __init__(self, path=None, name=None): def __init__(self, path=None, name=None):
@@ -103,6 +107,7 @@ class Parser(CachedModule):
if path: if path:
self.sys_path.pop(0) self.sys_path.pop(0)
# module might already be defined
if not self._module: if not self._module:
path = self.path path = self.path
name = self.name name = self.name
@@ -124,6 +129,7 @@ class Parser(CachedModule):
return self._module return self._module
def _get_source(self): def _get_source(self):
""" Override this abstract method """
return self._generate_code(self.module, self._load_mixins()) return self._generate_code(self.module, self._load_mixins())
def _load_mixins(self): def _load_mixins(self):
@@ -208,7 +214,11 @@ class Parser(CachedModule):
return True return True
return False return False
def get_types(names): def get_scope_objects(names):
"""
Looks for the names defined with dir() in an objects and divides
them into different object types.
"""
classes = {} classes = {}
funcs = {} funcs = {}
stmts = {} stmts = {}
@@ -252,7 +262,7 @@ class Parser(CachedModule):
names = set(dir(scope)) - set(['__file__', '__name__', '__doc__', names = set(dir(scope)) - set(['__file__', '__name__', '__doc__',
'__path__', '__package__']) '__path__', '__package__'])
classes, funcs, stmts, members = get_types(names) classes, funcs, stmts, members = get_scope_objects(names)
# classes # classes
for name, cl in classes.items(): for name, cl in classes.items():
@@ -383,6 +393,7 @@ def parse_function_doc(func):
class _Builtin(object): class _Builtin(object):
""" The builtin scope / module """
# Python 3 compatibility # Python 3 compatibility
if is_py3k(): if is_py3k():
name = 'builtins' name = 'builtins'

View File

@@ -1,5 +1,6 @@
import inspect import inspect
try: try:
# Use colorama for nicer console output.
from colorama import Fore, init from colorama import Fore, init
init() init()
except ImportError: except ImportError:
@@ -12,8 +13,13 @@ NOTICE = object()
WARNING = object() WARNING = object()
ERROR = object() ERROR = object()
debug_function = None
#debug_function = print_to_stdout
ignored_modules = []
def dbg(*args): def dbg(*args):
""" Looks at the stack, to see if a debug message should be printed. """
if debug_function: if debug_function:
frm = inspect.stack()[1] frm = inspect.stack()[1]
mod = inspect.getmodule(frm[0]) mod = inspect.getmodule(frm[0])
@@ -35,8 +41,3 @@ def print_to_stdout(level, *args):
""" The default debug function """ """ The default debug function """
msg = (Fore.GREEN + 'dbg: ' if level == NOTICE else Fore.RED + 'warning: ') msg = (Fore.GREEN + 'dbg: ' if level == NOTICE else Fore.RED + 'warning: ')
print(msg + ', '.join(str(a) for a in args) + Fore.RESET) print(msg + ', '.join(str(a) for a in args) + Fore.RESET)
debug_function = None
#debug_function = print_to_stdout
ignored_modules = []

View File

@@ -126,6 +126,7 @@ def check_array_additions(array):
counter = 0 counter = 0
def dec(func): def dec(func):
""" TODO delete this """
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
global counter global counter
element = args[0] element = args[0]
@@ -134,7 +135,7 @@ def dec(func):
else: else:
# must be instance # must be instance
stmt = element.var_args.parent_stmt() stmt = element.var_args.parent_stmt()
print ' '*counter + 'recursion,', stmt print ' ' * counter + 'recursion,', stmt
counter += 1 counter += 1
res = func(*args, **kwargs) res = func(*args, **kwargs)
counter -= 1 counter -= 1
@@ -142,6 +143,7 @@ def dec(func):
return res return res
return wrapper return wrapper
#@dec #@dec
@evaluate.memoize_default([]) @evaluate.memoize_default([])
def _check_array_additions(compare_array, module, is_list): def _check_array_additions(compare_array, module, is_list):
@@ -167,6 +169,10 @@ def _check_array_additions(compare_array, module, is_list):
return result return result
def check_calls(calls, add_name): def check_calls(calls, add_name):
"""
Calls are processed here. The part before the call is searched and
compared with the original Array.
"""
result = [] result = []
for c in calls: for c in calls:
call_path = list(c.generate_call_path()) call_path = list(c.generate_call_path())
@@ -245,6 +251,7 @@ def _check_array_additions(compare_array, module, is_list):
def check_array_instances(instance): def check_array_instances(instance):
""" Used for set() and list() instances. """
if not settings.dynamic_arrays_instances: if not settings.dynamic_arrays_instances:
return instance.var_args return instance.var_args
ai = ArrayInstance(instance) ai = ArrayInstance(instance)

View File

@@ -706,6 +706,7 @@ class Execution(Executable):
class Generator(parsing.Base): class Generator(parsing.Base):
""" Cares for `yield` statements. """ """ Cares for `yield` statements. """
__metaclass__ = CachedMetaClass __metaclass__ = CachedMetaClass
def __init__(self, func, var_args): def __init__(self, func, var_args):
super(Generator, self).__init__() super(Generator, self).__init__()
self.func = func self.func = func
@@ -748,6 +749,7 @@ class Array(parsing.Base):
methods which are important in this module. methods which are important in this module.
""" """
__metaclass__ = CachedMetaClass __metaclass__ = CachedMetaClass
def __init__(self, array): def __init__(self, array):
self._array = array self._array = array
@@ -1194,7 +1196,7 @@ def follow_statement(stmt, seek_name=None):
the result for this name. the result for this name.
:param stmt: A `parsing.Statement`. :param stmt: A `parsing.Statement`.
:param seek_name: :param seek_name: A string.
""" """
statement_path.append(stmt) # important to know for the goto function statement_path.append(stmt) # important to know for the goto function

View File

@@ -10,7 +10,10 @@ import settings
class RecursionDecorator(object): class RecursionDecorator(object):
""" A decorator to detect recursions in statements """ """
A decorator to detect recursions in statements. In a recursion a statement
at the same place, in the same module may not be executed two times.
"""
def __init__(self, func): def __init__(self, func):
self.func = func self.func = func
self.reset() self.reset()
@@ -58,6 +61,7 @@ class RecursionDecorator(object):
class RecursionNode(object): class RecursionNode(object):
""" A node of the RecursionDecorator. """
def __init__(self, stmt, parent): def __init__(self, stmt, parent):
self.script = stmt.get_parent_until() self.script = stmt.get_parent_until()
self.position = stmt.start_pos self.position = stmt.start_pos
@@ -136,7 +140,7 @@ class ExecutionRecursionDecorator(object):
def fast_parent_copy(obj): def fast_parent_copy(obj):
""" """
Much, much faster than deepcopy, but just for the elements in `classes`. Much, much faster than copy.deepcopy, but just for certain elements.
""" """
new_elements = {} new_elements = {}
@@ -179,7 +183,7 @@ def fast_parent_copy(obj):
def generate_param_array(args_tuple, parent_stmt=None): def generate_param_array(args_tuple, parent_stmt=None):
""" This generates an array, that can be used as a param """ """ This generates an array, that can be used as a param. """
values = [] values = []
for arg in args_tuple: for arg in args_tuple:
if arg is None: if arg is None:

View File

@@ -18,6 +18,9 @@ class ModuleNotFound(Exception):
class ImportPath(object): class ImportPath(object):
"""
An ImportPath is the path of a `parsing.Import` object.
"""
class GlobalNamespace(object): class GlobalNamespace(object):
pass pass
@@ -80,6 +83,10 @@ class ImportPath(object):
return names return names
def get_module_names(self, search_path=None): def get_module_names(self, search_path=None):
"""
Get the names of all modules in the search_path. This means file names
and not names defined in the files.
"""
names = [] names = []
for module_loader, name, is_pkg in pkgutil.iter_modules(search_path): for module_loader, name, is_pkg in pkgutil.iter_modules(search_path):
inf = float('inf') inf = float('inf')
@@ -171,7 +178,7 @@ class ImportPath(object):
def strip_imports(scopes): def strip_imports(scopes):
""" """
Here we strip the imports - they don't get resolved necessarily. Here we strip the imports - they don't get resolved necessarily.
Really used anymore? Really used anymore? Merge with remove_star_imports?
""" """
result = [] result = []
for s in scopes: for s in scopes:
@@ -189,6 +196,10 @@ def strip_imports(scopes):
def remove_star_imports(scope): def remove_star_imports(scope):
""" """
Check a module for star imports:
>>> from module import *
and follow these modules.
""" """
modules = strip_imports(i for i in scope.get_imports() if i.star) modules = strip_imports(i for i in scope.get_imports() if i.star)
new = [] new = []

View File

@@ -121,17 +121,17 @@ class ModuleWithCursor(Module):
after = re.search("[\w\d]*", line[self.position[1]:]).group(0) after = re.search("[\w\d]*", line[self.position[1]:]).group(0)
return self.get_path_until_cursor() + after return self.get_path_until_cursor() + after
def get_line(self, line): def get_line(self, line_nr):
if not self._line_cache: if not self._line_cache:
self._line_cache = self.source.split('\n') self._line_cache = self.source.split('\n')
if line == 0: if line_nr == 0:
# This is a fix for the zeroth line. We need a newline there, for # This is a fix for the zeroth line. We need a newline there, for
# the backwards parser. # the backwards parser.
return '' return ''
if line < 0: if line_nr < 0:
raise StopIteration() raise StopIteration()
try: try:
return self._line_cache[line - 1] return self._line_cache[line_nr - 1]
except IndexError: except IndexError:
raise StopIteration() raise StopIteration()