Commits

Ali Gholami Rudi  committed bbdcd88

Using a new textual object form

  • Participants
  • Parent commits a409273

Comments (0)

Files changed (8)

File docs/dev/workingon.txt

-Automatic SOI
-=============
+New Hierarchical Textual Form
+=============================
 
-- ``automatic_soi`` project config
+- Using a new textual form
 
 * Adding ``indentation_size`` config
 * Adding configs for specifying long imports?

File rope/base/builtins.py

     def get_pyname(self, name):
         if self.args:
             args = self.args.get_pynames(self.argnames)
-            return args[self.argnames.index(name)]
+            if name in self.argnames:
+                return args[self.argnames.index(name)]
 
     def get_arguments(self, argnames):
         if self.args:
         if self.keys is not None:
             return
         new_dict = context.get_pynames(['self', 'd'])[1]
-        if isinstance(new_dict.get_object().get_type(), Dict):
+        if new_dict and isinstance(new_dict.get_object().get_type(), Dict):
             args = evaluate.ObjectArguments([new_dict])
             items = new_dict.get_object().get_attribute('popitem').\
                     get_object().get_returned_object(args)

File rope/base/history.py

     history_file = property(_get_history_file)
 
     def do(self, changes):
+        changes.do()
         if self._is_change_interesting(changes):
             self.undo_list.append(changes)
             if len(self.undo_list) > self.max_undos:
                 del self.undo_list[0]
-        changes.do()
 
     def _is_change_interesting(self, changes):
         for resource in changes.get_changed_resources():

File rope/base/oi/objectinfo.py

         self.project = project
         self.to_textual = rope.base.oi.transform.PyObjectToTextual(project)
         self.to_pyobject = rope.base.oi.transform.TextualToPyObject(project)
+        self.doi_to_pyobject = rope.base.oi.transform.DOITextualToPyObject(project)
         preferred = project.get_prefs().get('objectdb_type', 'memory')
         validation = TextualValidation(self.to_pyobject)
         if preferred == 'memory' or project.ropefolder is None:
                     for parameter in parameters]
 
     def doi_data_received(self, data):
-        self._save_data(data[0], data[1], data[2])
+        def doi_to_normal(textual):
+            pyobject = self.doi_to_pyobject.transform(textual)
+            return self.to_textual.transform(pyobject)
+        function = doi_to_normal(data[0])
+        args = tuple([doi_to_normal(textual) for textual in data[1]])
+        returned = doi_to_normal(data[2])
+        if function[0] == 'defined' and len(function) == 3:
+            self._save_data(function, args, returned)
 
     def function_called(self, pyfunction, params, returned=None):
         function_text = self.to_textual.transform(pyfunction)
         resource = pyobject.get_module().get_resource()
         if resource is None:
             return memorydb.NullScopeInfo(error_on_write=False)
-        path = os.path.abspath(resource.real_path)
-        lineno = pyobject.get_ast().lineno
-        return self.objectdb.get_scope_info(path, str(lineno),
-                                            readonly=readonly)
+        textual = self.to_textual.transform(pyobject)
+        if textual[0] == 'defined':
+            path = textual[1]
+            if len(textual) == 3:
+                key = textual[2]
+            else:
+                key = ''
+            return self.objectdb.get_scope_info(path, key, readonly=readonly)
 
     def sync(self):
         self.objectdb.sync()
         return self.to_pyobject.file_to_resource(path) is not None
 
     def is_scope_valid(self, path, key):
-        return self.to_pyobject.transform(('defined', path, key)) is not None
+        if key == '':
+            textual = ('defined', path)
+        else:
+            textual = ('defined', path, key)
+        return self.to_pyobject.transform(textual) is not None
 
 
 class _FileListObserver(object):

File rope/base/oi/transform.py

         return ('unknown',)
 
     def PyFunction_to_textual(self, pyobject):
-        return ('defined', self._get_pymodule_path(pyobject.get_module()),
-                str(pyobject.get_ast().lineno))
+        return self._defined_to_textual(pyobject)
 
     def PyClass_to_textual(self, pyobject):
+        return self._defined_to_textual(pyobject)
+
+    def _defined_to_textual(self, pyobject):
+        address = []
+        while pyobject.parent is not None:
+            address.insert(0, pyobject.get_name())
+            pyobject = pyobject.parent
         return ('defined', self._get_pymodule_path(pyobject.get_module()),
-                pyobject.get_name())
+                '.'.join(address))
 
     def PyModule_to_textual(self, pyobject):
         return ('defined', self._get_pymodule_path(pyobject))
 
     def _get_pymodule_path(self, pymodule):
         resource = pymodule.get_resource()
-        resource_path = resource.path
-        if os.path.isabs(resource_path):
-            return resource_path
-        return os.path.abspath(
-            os.path.normpath(os.path.join(self.project.address,
-                                          resource_path)))
+        return resource.real_path
 
 
 class TextualToPyObject(object):
         path = textual[1]
         return self._get_pymodule(path)
 
-    def _function_to_pyobject(self, textual):
+    def _hierarchical_defined_to_pyobject(self, textual):
         path = textual[1]
-        lineno = int(textual[2])
+        names = textual[2].split('.')
         pymodule = self._get_pymodule(path)
-        if pymodule is not None:
-            scope = pymodule.get_scope()
-            inner_scope = scope.get_inner_scope_for_line(lineno)
-            return inner_scope.pyobject
-
-    def _class_to_pyobject(self, textual):
-        path, name = textual[1:]
-        pymodule = self._get_pymodule(path)
-        if pymodule is None:
-            return None
-        module_scope = pymodule.get_scope()
-        suspected = None
-        if name in module_scope.get_names():
-            suspected = module_scope.get_name(name).get_object()
-        if suspected is not None and isinstance(suspected, rope.base.pyobjects.PyClass):
-            return suspected
-        else:
-            lineno = self._find_occurrence(name, pymodule.get_resource().read())
-            if lineno is not None:
-                inner_scope = module_scope.get_inner_scope_for_line(lineno)
-                return inner_scope.pyobject
+        pyobject = pymodule
+        for name in names:
+            if pyobject is None:
+                return None
+            if isinstance(pyobject, rope.base.pyobjects.PyDefinedObject):
+                try:
+                    pyobject = pyobject.get_scope().get_name(name).get_object()
+                except exceptions.NameNotFoundError:
+                    return None
+            else:
+                return None
+        return pyobject
 
     def defined_to_pyobject(self, textual):
-        if len(textual) == 2:
+        if len(textual) == 2 or textual[2] == '':
             return self._module_to_pyobject(textual)
         else:
-            if textual[2].isdigit():
-                result = self._function_to_pyobject(textual)
-            else:
-                result = self._class_to_pyobject(textual)
-            if not isinstance(result, rope.base.pyobjects.PyModule):
-                return result
+            return self._hierarchical_defined_to_pyobject(textual)
 
     def instance_to_pyobject(self, textual):
         type = self.transform(textual[1])
         if type is not None:
             return rope.base.pyobjects.PyObject(type)
 
-    def _find_occurrence(self, name, source):
-        pattern = re.compile(r'^\s*class\s*' + name + r'\b')
-        lines = source.split('\n')
-        for i in range(len(lines)):
-            if pattern.match(lines[i]):
-                return i + 1
-
     def _get_pymodule(self, path):
         resource = self.file_to_resource(path)
         if resource is not None:
             return resource
         except exceptions.RopeError:
             return None
+
+
+class DOITextualToPyObject(TextualToPyObject):
+
+    def _function_to_pyobject(self, textual):
+        path = textual[1]
+        lineno = int(textual[2])
+        pymodule = self._get_pymodule(path)
+        if pymodule is not None:
+            scope = pymodule.get_scope()
+            inner_scope = scope.get_inner_scope_for_line(lineno)
+            return inner_scope.pyobject
+
+    def _class_to_pyobject(self, textual):
+        path, name = textual[1:]
+        pymodule = self._get_pymodule(path)
+        if pymodule is None:
+            return None
+        module_scope = pymodule.get_scope()
+        suspected = None
+        if name in module_scope.get_names():
+            suspected = module_scope.get_name(name).get_object()
+        if suspected is not None and \
+           isinstance(suspected, rope.base.pyobjects.PyClass):
+            return suspected
+        else:
+            lineno = self._find_occurrence(name, pymodule.get_resource().read())
+            if lineno is not None:
+                inner_scope = module_scope.get_inner_scope_for_line(lineno)
+                return inner_scope.pyobject
+
+    def defined_to_pyobject(self, textual):
+        if len(textual) == 2:
+            return self._module_to_pyobject(textual)
+        else:
+            if textual[2].isdigit():
+                result = self._function_to_pyobject(textual)
+            else:
+                result = self._class_to_pyobject(textual)
+            if not isinstance(result, rope.base.pyobjects.PyModule):
+                return result
+
+    def _find_occurrence(self, name, source):
+        pattern = re.compile(r'^\s*class\s*' + name + r'\b')
+        lines = source.split('\n')
+        for i in range(len(lines)):
+            if pattern.match(lines[i]):
+                return i + 1

File rope/base/pycore.py

 
     def _file_changed(self, resource, new_resource=None):
         if resource.exists() and self.is_python_file(resource):
-            self.analyze_module(resource)
-        elif new_resource is not None and self.is_python_file(new_resource):
-            self.analyze_module(new_resource)
+            try:
+                self.analyze_module(resource)
+            except SyntaxError:
+                pass
 
     def is_python_file(self, resource):
         return not resource.is_folder() and resource.name.endswith('.py')

File rope/base/pyobjects.py

         return self.parameter_pynames
 
     def get_parameter(self, index):
-        return self.parameter_pyobjects.get()[index]
+        if index < len(self.parameter_pyobjects.get()):
+            return self.parameter_pyobjects.get()[index]
 
     def get_returned_object(self, args):
         return self.returned.get(args)

File ropetest/objectinfertest.py

                'a_var = a_func(a_func)\n'
         mod.write(code)
         self.pycore.run_module(mod).wait_process()
-        mod.write('\n' + code)
+        mod.write(code.replace('a_func', 'newfunc'))
         mod.write(code)
         pymod = self.pycore.resource_to_pyobject(mod)
         self.assertNotEquals(pymod.get_attribute('a_func').get_object(),