changed builtin parsing: introduced inspect, __ names are now used, if in mixins defined

This commit is contained in:
David Halter
2012-06-24 02:15:21 +02:00
parent e42a534e38
commit 548af3cc86

View File

@@ -2,6 +2,7 @@ import re
import sys import sys
import os import os
import types import types
import inspect
import debug import debug
import parsing import parsing
@@ -155,18 +156,27 @@ class Parser(CachedModule):
Generate a string, which uses python syntax as an input to the Generate a string, which uses python syntax as an input to the
PyFuzzyParser. PyFuzzyParser.
""" """
def get_doc(obj, indent=False):
doc = inspect.getdoc(obj)
if doc:
doc = ('"""\n%s\n"""\n' % doc)
if indent:
doc = parsing.indent_block(doc)
return doc
return ''
def get_types(names): def get_types(names):
classes = {} classes = {}
funcs = {} funcs = {}
stmts = {} stmts = {}
members = {} members = {}
for n in names: for n in names:
if '__' in n: if '__' in n and n not in mixin_funcs:
continue continue
# this has a builtin_function_or_method # this has a builtin_function_or_method
exe = getattr(scope, n) exe = getattr(scope, n)
if type(exe).__name__ in ['method_descriptor', #print exe, inspect.isbuiltin(exe) or inspect.ismethoddescriptor(exe)
'builtin_function_or_method']: if inspect.isbuiltin(exe) or inspect.ismethoddescriptor(exe):
funcs[n] = exe funcs[n] = exe
elif type(exe) == type: elif type(exe) == type:
classes[n] = exe classes[n] = exe
@@ -177,19 +187,18 @@ class Parser(CachedModule):
return classes, funcs, stmts, members return classes, funcs, stmts, members
code = '' code = ''
try: if inspect.ismodule(scope): # generate comment where the code's from.
try: try:
path = scope.__file__ path = scope.__file__
except AttributeError: except AttributeError:
path = '?' path = '?'
if type(scope) == types.ModuleType: code += '# Generated module %s from %s\n' % (scope.__name__, path)
code += '# Generated module %s from %s\n' % (scope.__name__, path)
except AttributeError: code += get_doc(scope)
pass
code += '"""\n%s\n"""\n' % scope.__doc__
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_types(names)
# classes # classes
@@ -208,7 +217,7 @@ class Parser(CachedModule):
# functions # functions
for name, func in funcs.items(): for name, func in funcs.items():
params, ret = parse_function_doc(func) params, ret = parse_function_doc(func)
doc_str = parsing.indent_block('"""\n%s\n"""\n' % func.__doc__) doc_str = get_doc(func, indent=True)
try: try:
mixin = mixin_funcs[name] mixin = mixin_funcs[name]
except KeyError: except KeyError:
@@ -230,8 +239,7 @@ class Parser(CachedModule):
for name, func in members.items(): for name, func in members.items():
ret = 'pass' ret = 'pass'
code += '@property\ndef %s(self):\n' % (name) code += '@property\ndef %s(self):\n' % (name)
block = '"""\n%s\n"""\n' % func.__doc__ block = get_doc(func, indent=True) + '%s\n\n' % ret
block += '%s\n\n' % ret
code += parsing.indent_block(block) code += parsing.indent_block(block)
# variables # variables
@@ -265,7 +273,7 @@ def parse_function_doc(func):
This is nothing more than a docstring parser. This is nothing more than a docstring parser.
""" """
# TODO: things like utime(path, (atime, mtime)) and a(b [, b]) -> None # TODO: things like utime(path, (atime, mtime)) and a(b [, b]) -> None
doc = func.__doc__ doc = inspect.getdoc(func)
# get full string, parse round parentheses: def func(a, (b,c)) # get full string, parse round parentheses: def func(a, (b,c))
try: try: