From dcf83069d9cfbb3c861588e40d62b1e03ca6ea99 Mon Sep 17 00:00:00 2001 From: David Halter Date: Mon, 16 Apr 2012 16:51:01 +0200 Subject: [PATCH] restructuring --- builtin.py | 25 ++++++++++++++++--------- evaluate.py | 37 +++++++++++++++++++------------------ modules.py | 14 ++++++++++---- parsetest.py | 4 ++-- test/run.py | 3 ++- 5 files changed, 49 insertions(+), 34 deletions(-) diff --git a/builtin.py b/builtin.py index 1fb1c3ec..ddd0b2f4 100644 --- a/builtin.py +++ b/builtin.py @@ -21,11 +21,18 @@ class Parser(object): """ This module tries to imitate parsing.Scope """ def __init__(self, name): self.name = name - self.parent = None - self.content = {} - exec 'import %s as module' % name in self.content - self.module = self.content['module'] + self._content = {} self._parser = None + self._module = None + + @property + def module(self): + if not self._module: + print 'import', self.name + exec 'import %s as module' % self.name in self._content + print 'import2', self.name + self._module = self._content['module'] + return self._module @property def parser(self): @@ -33,7 +40,7 @@ class Parser(object): if self._parser: return self._parser else: - code = self.generate_code(self.module) + code = self._generate_code(self.module) try: self._parser = parsing.PyFuzzyParser(code) except: @@ -42,7 +49,7 @@ class Parser(object): raise return self._parser - def generate_code(self, scope, depth=0): + def _generate_code(self, scope, depth=0): """ Generate a string, which uses python syntax as an input to the PyFuzzyParser. @@ -88,7 +95,7 @@ class Parser(object): bases = (c.__name__ for c in cl.__bases__) code += 'class %s(%s):\n' % (name, ','.join(bases)) if depth == 0: - cl_code = self.generate_code(cl, depth + 1) + cl_code = self._generate_code(cl, depth + 1) code += parsing.indent_block(cl_code) code += '\n' @@ -126,8 +133,8 @@ class Parser(object): if depth == 0: #with open('writeout.py', 'w') as f: # f.write(code) - #import sys - #sys.stdout.write(code) + import sys + sys.stdout.write(code) #exit() pass return code diff --git a/evaluate.py b/evaluate.py index d99271be..d72f8598 100644 --- a/evaluate.py +++ b/evaluate.py @@ -55,7 +55,7 @@ def memoize(default=None): class Exec(object): - def __init__(self, base, params): + def __init__(self, base, params=None): self.base = base self.params = params @@ -238,24 +238,25 @@ def get_scopes_for_name(scope, name, search_global=False): # else: # result += filter_name(i) #else: - if [name] == list(scope.names): - if isinstance(scope, ArrayElement): - result.append(scope) - else: - par = scope.parent - if isinstance(par, parsing.Flow): - # TODO get Flow data, which is defined by the loop - # (or with) - pass - elif isinstance(par, parsing.Param): - if isinstance(par.parent.parent, parsing.Class) \ - and par.position == 0: - result.append(Instance(par.parent.parent)) - else: - # TODO get function data - pass + if [name] == list(scope.names): + if isinstance(scope, ArrayElement): + result.append(scope) + else: + par = scope.parent + if isinstance(par, parsing.Flow): + # TODO get Flow data, which is defined by the loop + # (or with) + pass + elif isinstance(par, parsing.Param): + if isinstance(par.parent.parent, parsing.Class) \ + and par.position == 0: + # this is where self is added + result.append(Instance(par.parent.parent)) else: - result.append(scope.parent) + # TODO get function data + pass + else: + result.append(scope.parent) debug.dbg('sfn filter', result) return result diff --git a/modules.py b/modules.py index beafc8e0..3b72cbfe 100644 --- a/modules.py +++ b/modules.py @@ -75,7 +75,8 @@ def find_module(current_module, point_path): path = [ns[1]] else: path = None - debug.dbg('search_module', string, path) + debug.dbg('search_module', string, path, + current_module.module_path) try: i = imp.find_module(string, path) except ImportError: @@ -86,7 +87,7 @@ def find_module(current_module, point_path): raise return i - # TODO handle relative paths - they are included int the import object + # TODO handle relative paths - they are included in the import object current_namespace = None sys.path.insert(0, os.path.dirname(current_module.module_path)) # now execute those paths @@ -105,6 +106,7 @@ def find_module(current_module, point_path): path = current_namespace[1] is_package_directory = current_namespace[2][2] == imp.PKG_DIRECTORY + f = None if is_package_directory or current_namespace[0]: # is a directory module if is_package_directory: @@ -115,7 +117,11 @@ def find_module(current_module, point_path): source = open(path).read() else: source = current_namespace[0].read() - f = File(path, source) - else: + if path.endswith('.py'): + f = File(path, source) + if not f: + print 'lala' f = builtin.Parser(path) + print 'lala2' + return f.parser.top, rest diff --git a/parsetest.py b/parsetest.py index 7627e5c7..6061519e 100644 --- a/parsetest.py +++ b/parsetest.py @@ -138,13 +138,13 @@ class c1(): return c5+'asdf' (c1().c2.\ c, 1, c3()) [0].pop() - c = u"asdf".join([1,2]) matrix_test = [[1,2], [1,3]] c = c1().c3().sleep() asdf = c1; asdf2 = asdf b= asdf2 -#import parsing as test c = b().c3() 1.0.fromhex(); import flask ; flsk = flask.Flask + flask.Request; abc = [1,2+3]; abc[0]. +import pylab +pylab. diff --git a/test/run.py b/test/run.py index 2da1b20f..5d37c042 100755 --- a/test/run.py +++ b/test/run.py @@ -10,6 +10,7 @@ import functions #functions.set_debug_function(functions.debug.print_to_stdout) + def completion_test(source): """ This is the completion test for some cases. The tests are not unit test @@ -56,7 +57,7 @@ def completion_test(source): # completion tests: completion_test_dir = 'completion' summary = [] -for f_name in os.listdir(completion_test_dir ): +for f_name in os.listdir(completion_test_dir): if f_name.endswith(".py"): path = os.path.join(completion_test_dir, f_name) f = open(path)