/usr/share/pyshared/ninja_ide/tools/introspection.py is in ninja-ide 2.3-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | # -*- coding: utf-8 -*-
#
# This file is part of NINJA-IDE (http://ninja-ide.org).
#
# NINJA-IDE is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# any later version.
#
# NINJA-IDE is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with NINJA-IDE; If not, see <http://www.gnu.org/licenses/>.
import _ast
import ast
from ninja_ide.tools.completion import analyzer
from ninja_ide.tools.logger import NinjaLogger
logger_imports = NinjaLogger(
'ninja_ide.tools.introspection.obtaining_imports')
logger_symbols = NinjaLogger(
'ninja_ide.tools.introspection.obtainint_symbols')
_map_type = {
_ast.Tuple: 'tuple',
_ast.List: 'list',
_ast.Str: 'str',
_ast.Dict: 'dict',
_ast.Num: 'int',
_ast.Call: 'function()',
}
def _parse_assign(symbol):
assigns = {}
attributes = {}
for var in symbol.targets:
if var.__class__ == ast.Attribute:
attributes[var.attr] = var.lineno
elif var.__class__ == ast.Name:
assigns[var.id] = var.lineno
return (assigns, attributes)
def _parse_class(symbol, with_docstrings):
docstring = {}
attr = {}
func = {}
clazz = {}
name = symbol.name + '('
name += ', '.join([
analyzer.expand_attribute(base) for base in symbol.bases])
name += ')'
for sym in symbol.body:
if sym.__class__ is ast.Assign:
result = _parse_assign(sym)
attr.update(result[0])
attr.update(result[1])
elif sym.__class__ is ast.FunctionDef:
result = _parse_function(sym, with_docstrings)
attr.update(result['attrs'])
if with_docstrings:
docstring.update(result['docstring'])
func[result['name']] = {'lineno': result['lineno'],
'functions': result['functions']}
elif sym.__class__ is ast.ClassDef:
result = _parse_class(sym, with_docstrings)
clazz[result['name']] = {'lineno': result['lineno'],
'members':
{'attributes': result['attributes'],
'functions': result['functions']}}
docstring.update(result['docstring'])
if with_docstrings:
docstring[symbol.lineno] = ast.get_docstring(symbol, clean=True)
lineno = symbol.lineno
for decorator in symbol.decorator_list:
lineno += 1
return {'name': name, 'attributes': attr, 'functions': func,
'lineno': lineno, 'docstring': docstring, 'classes': clazz}
def _parse_function(symbol, with_docstrings):
docstring = {}
attrs = {}
func = {'functions': {}}
func_name = symbol.name + '('
#We store the arguments to compare with default backwards
defaults = []
for value in symbol.args.defaults:
#TODO: In some cases we can have something like: a=os.path
defaults.append(value)
arguments = []
for arg in reversed(symbol.args.args):
if arg.__class__ is not _ast.Name or arg.id == 'self':
continue
argument = arg.id
if defaults:
value = defaults.pop()
arg_default = _map_type.get(value.__class__, None)
if arg_default is None:
if value.__class__ is _ast.Attribute:
arg_default = analyzer.expand_attribute(value)
elif value.__class__ is _ast.Name:
arg_default = value.id
else:
arg_default = 'object'
argument += '=' + arg_default
arguments.append(argument)
func_name += ', '.join(reversed(arguments))
if symbol.args.vararg is not None:
if not func_name.endswith('('):
func_name += ', '
func_name += '*' + symbol.args.vararg
if symbol.args.kwarg is not None:
if not func_name.endswith('('):
func_name += ', '
func_name += '**' + symbol.args.kwarg
func_name += ')'
for sym in symbol.body:
if sym.__class__ is ast.Assign:
result = _parse_assign(sym)
attrs.update(result[1])
elif sym.__class__ is ast.FunctionDef:
result = _parse_function(sym, with_docstrings)
if with_docstrings:
docstring.update(result['docstring'])
func['functions'][result['name']] = {'lineno': result['lineno'],
'functions':
result['functions']}
if with_docstrings:
docstring[symbol.lineno] = ast.get_docstring(symbol, clean=True)
lineno = symbol.lineno
for decorator in symbol.decorator_list:
lineno += 1
return {'name': func_name, 'lineno': lineno,
'attrs': attrs, 'docstring': docstring, 'functions': func}
def obtain_symbols(source, with_docstrings=False, filename=''):
"""Parse a module source code to obtain: Classes, Functions and Assigns."""
try:
module = ast.parse(source)
except:
logger_symbols.debug("The file contains syntax errors: %s" % filename)
return {}
symbols = {}
globalAttributes = {}
globalFunctions = {}
classes = {}
docstrings = {}
for symbol in module.body:
if symbol.__class__ is ast.Assign:
result = _parse_assign(symbol)
globalAttributes.update(result[0])
globalAttributes.update(result[1])
elif symbol.__class__ is ast.FunctionDef:
result = _parse_function(symbol, with_docstrings)
if with_docstrings:
docstrings.update(result['docstring'])
globalFunctions[result['name']] = {'lineno': result['lineno'],
'functions':
result['functions']}
elif symbol.__class__ is ast.ClassDef:
result = _parse_class(symbol, with_docstrings)
classes[result['name']] = {'lineno': result['lineno'],
'members': {'attributes':
result['attributes'],
'functions':
result['functions'],
'classes':
result['classes']}}
docstrings.update(result['docstring'])
if globalAttributes:
symbols['attributes'] = globalAttributes
if globalFunctions:
symbols['functions'] = globalFunctions
if classes:
symbols['classes'] = classes
if docstrings and with_docstrings:
symbols['docstrings'] = docstrings
return symbols
def obtain_imports(source='', body=None):
if source:
try:
module = ast.parse(source)
body = module.body
except:
logger_imports.debug("A file contains syntax errors.")
#Imports{} = {name: asname}, for example = {sys: sysAlias}
imports = {}
#From Imports{} = {name: {module: fromPart, asname: nameAlias}}
fromImports = {}
for sym in body:
if type(sym) is ast.Import:
for item in sym.names:
imports[item.name] = {'asname': item.asname,
'lineno': sym.lineno}
if type(sym) is ast.ImportFrom:
for item in sym.names:
fromImports[item.name] = {'module': sym.module,
'asname': item.asname,
'lineno': sym.lineno}
return {'imports': imports, 'fromImports': fromImports}
|