Example #1
0
def _extract_def(d, path):
    d_line, d_column = d.start_pos
    # use full name for import type
    if d.type == 'function':
        try:
            params = [p.name for p in d.params]
            name = d.name + '(' + ', '.join(params) + ')'
        except AttributeError:
            name = d.name
    else:
        name = d.name
    definition = Definition(name, d_line - 1, d_column,
                            icon_from_typename(d.name, d.type),
                            file_path=path)
    # check for methods in class or nested methods/classes
    if d.type == "class" or d.type == 'function':
        try:
            sub_definitions = d.defined_names()
            for sub_d in sub_definitions:
                if (d.type == 'function' and sub_d.type == 'function') or \
                        d.type == 'class':
                    definition.add_child(_extract_def(sub_d, path))
        except (AttributeError, IndexError):
            pass
    return definition
Example #2
0
def _extract_def(d, path):
    d_line, d_column = d.start_pos
    # use full name for import type
    if d.type == 'function':
        try:
            params = [p.name for p in d.params]
            name = d.name + '(' + ', '.join(params) + ')'
        except AttributeError:
            name = d.name
    else:
        name = d.name
    definition = Definition(name,
                            d_line - 1,
                            d_column,
                            icon_from_typename(d.name, d.type),
                            file_path=path)
    # check for methods in class or nested methods/classes
    if d.type == "class" or d.type == 'function':
        try:
            sub_definitions = d.defined_names()
            for sub_d in sub_definitions:
                if (d.type == 'function' and sub_d.type == 'function') or \
                        d.type == 'class':
                    definition.add_child(_extract_def(sub_d, path))
        except (AttributeError, IndexError):
            pass
    return definition
Example #3
0
 def flatten(results):
     """
     Flattens the document structure tree as a simple sequential list.
     """
     ret_val = []
     for de in results:
         ret_val.append(de)
         for sub_d in de.children:
             nd = Definition(
                 sub_d.name, sub_d.line, sub_d.column, sub_d.icon)
             nd.name = "    " + nd.name
             ret_val.append(nd)
     return ret_val
 def flatten(results):
     """
     Flattens the document structure tree as a simple sequential list.
     """
     ret_val = []
     for de in results:
         ret_val.append(de)
         for sub_d in de.children:
             nd = Definition(sub_d.name, sub_d.line, sub_d.column,
                             sub_d.icon)
             nd.name = "    " + nd.name
             ret_val.append(nd)
     return ret_val
Example #5
0
 def to_definition(self):
     """
     Converts the name instance to a pyqode.core.share.Definition
     """
     icon = {
         Name.Type.Root: icons.ICON_MIMETYPE,
         Name.Type.Division: icons.ICON_DIVISION,
         Name.Type.Section: icons.ICON_SECTION,
         Name.Type.Variable: icons.ICON_VAR,
         Name.Type.Paragraph: icons.ICON_FUNC
     }[self.node_type]
     d = Definition(self.name, self.line, self.column, icon, self.description)
     for ch in self.children:
         d.add_child(ch.to_definition())
     return d
Example #6
0
 def to_definition(self):
     """
     Converts the name instance to a pyqode.core.share.Definition
     """
     icon = {
         Name.Type.Root: icons.ICON_MIMETYPE,
         Name.Type.Division: icons.ICON_DIVISION,
         Name.Type.Section: icons.ICON_SECTION,
         Name.Type.Variable: icons.ICON_VAR,
         Name.Type.Paragraph: icons.ICON_FUNC
     }[self.node_type]
     d = Definition(self.name, self.line, self.column, icon, self.description)
     for ch in self.children:
         d.add_child(ch.to_definition())
     return d
Example #7
0
 def _on_results_available(self, results):
     if results:
         results = [Definition.from_dict(ddict) for ddict in results]
     self._results = results
     if self._results is not None:
         _logger().log(5, "Document structure changed")
         self.document_changed.emit()
 def _on_results_available(self, results):
     if results:
         results = [Definition.from_dict(ddict) for ddict in results]
     self._results = results
     if self._results is not None:
         _logger().debug("Document structure changed")
         self.document_changed.emit()
Example #9
0
def test_defined_names():
    filename = __file__
    with open(filename, 'r', encoding='utf-8') as file:
        code = file.read()
    results = workers.defined_names({'code': code, 'path': filename})
    assert len(results)
    definitions = []
    for i, definition in enumerate(results):
        d = Definition.from_dict(definition)
        definitions.append(d)
        if i:
            assert d != definitions[i-1]

    # now that the code changed, defined_names should return
    # (True, [xxx, yyy, ...])
    code += "\ndef foo():\n    print('bar')"
    results = workers.defined_names({'code': code, 'path': filename})
    assert len(results)
Example #10
0
def test_defined_names():
    filename = __file__
    with open(filename, 'r', encoding='utf-8') as file:
        code = file.read()
    results = workers.defined_names({'code': code, 'path': filename})
    assert len(results)
    definitions = []
    for i, definition in enumerate(results):
        d = Definition.from_dict(definition)
        definitions.append(d)
        if i:
            assert d != definitions[i-1]

    # now that the code changed, defined_names should return
    # (True, [xxx, yyy, ...])
    code += "\ndef foo():\n    print('bar')"
    results = workers.defined_names({'code': code, 'path': filename})
    assert len(results)
Example #11
0
 def parse(path):
     if path.endswith('_rc.py'):
         return []
     try:
         encoding = Cache().get_file_encoding(path)
     except KeyError:
         encoding = locale.getpreferredencoding()
     try:
         with open(path, encoding=encoding) as f:
             code = f.read()
     except (UnicodeDecodeError, OSError):
         try:
             with open(path, encoding='utf-8') as f:
                 code = f.read()
         except (UnicodeDecodeError, OSError):
             # could not deduce encoding
             return []
     request_data = {
         'path': path,
         'code': code
     }
     results = defined_names(request_data)
     return [Definition.from_dict(ddict) for ddict in results]
Example #12
0
 def parse(self, path):
     return [Definition('spam', 1, 15, icon=("", ""))]