def _load_module(evaluator, path=None, source=None, sys_path=None, parent_module=None): def load(source): dotted_path = path and compiled.dotted_from_fs_path(path, sys_path) if path is not None and path.endswith(('.py', '.zip', '.egg')) \ and dotted_path not in settings.auto_import_modules: if source is None: with open(path, 'rb') as f: source = f.read() else: return compiled.load_module(evaluator, path) p = path p = FastParser(evaluator.grammar, source_to_unicode(source), p) save_parser(path, p) return p.module if sys_path is None: sys_path = evaluator.sys_path cached = load_parser(path) module_node = load(source) if cached is None else cached.module if isinstance(module_node, compiled.CompiledObject): return module_node from jedi.evaluate.representation import ModuleContext return ModuleContext(evaluator, module_node)
def _load_module(evaluator, path=None, source=None, sys_path=None, parent_module=None): def load(source): dotted_path = path and compiled.dotted_from_fs_path(path, sys_path) if path is not None and path.endswith(('.py', '.zip', '.egg')) \ and dotted_path not in settings.auto_import_modules: if source is None: with open(path, 'rb') as f: source = f.read() else: return compiled.load_module(evaluator, path) p = path p = fast.FastParser(evaluator.grammar, common.source_to_unicode(source), p) save_parser(path, p) from jedi.evaluate.representation import ModuleWrapper return ModuleWrapper(evaluator, p.module, parent_module) if sys_path is None: sys_path = evaluator.sys_path cached = load_parser(path) module = load(source) if cached is None else cached.module module = evaluator.wrap(module) return module
def _extract_range(self, definition): """Provides the definition range of a given definition For regular symbols it returns the start and end location of the characters making up the symbol. For scoped containers it will return the entire definition of the scope. The scope that jedi provides ends with the first character of the next scope so it's not ideal. For vscode we need the scope to end with the last character of actual code. That's why we extract the lines that make up our scope and trim the trailing whitespace. """ from jedi import common from jedi.parser.utils import load_parser # get the scope range try: if definition.type in ['class', 'function'] and hasattr( definition, '_definition'): scope = definition._definition start_line = scope.start_pos[0] - 1 start_column = scope.start_pos[1] end_line = scope.end_pos[0] - 1 end_column = scope.end_pos[1] # get the lines path = definition._definition.get_parent_until().path parser = load_parser(path) lines = common.splitlines(parser.source) lines[end_line] = lines[end_line][:end_column] # trim the lines lines = lines[start_line:end_line + 1] lines = '\n'.join(lines).rstrip().split('\n') end_line = start_line + len(lines) - 1 end_column = len(lines[-1]) - 1 else: symbol = definition._name start_line = symbol.start_pos[0] - 1 start_column = symbol.start_pos[1] end_line = symbol.end_pos[0] - 1 end_column = symbol.end_pos[1] return { 'start_line': start_line, 'start_column': start_column, 'end_line': end_line, 'end_column': end_column } except Exception as e: return { 'start_line': definition.line - 1, 'start_column': definition.column, 'end_line': definition.line - 1, 'end_column': definition.column }
def _extract_range(self, definition): """Provides the definition range of a given definition For regular symbols it returns the start and end location of the characters making up the symbol. For scoped containers it will return the entire definition of the scope. The scope that jedi provides ends with the first character of the next scope so it's not ideal. For vscode we need the scope to end with the last character of actual code. That's why we extract the lines that make up our scope and trim the trailing whitespace. """ from jedi import common from jedi.parser.utils import load_parser # get the scope range try: if definition.type in ['class', 'function'] and hasattr(definition, '_definition'): scope = definition._definition start_line = scope.start_pos[0] - 1 start_column = scope.start_pos[1] end_line = scope.end_pos[0] - 1 end_column = scope.end_pos[1] # get the lines path = definition._definition.get_parent_until().path parser = load_parser(path) lines = common.splitlines(parser.source) lines[end_line] = lines[end_line][:end_column] # trim the lines lines = lines[start_line:end_line + 1] lines = '\n'.join(lines).rstrip().split('\n') end_line = start_line + len(lines) - 1 end_column = len(lines[-1]) - 1 else: symbol = definition._name start_line = symbol.start_pos[0] - 1 start_column = symbol.start_pos[1] end_line = symbol.end_pos[0] - 1 end_column = symbol.end_pos[1] return { 'start_line': start_line, 'start_column': start_column, 'end_line': end_line, 'end_column': end_column } except Exception as e: return { 'start_line': definition.line - 1, 'start_column': definition.column, 'end_line': definition.line - 1, 'end_column': definition.column }
def _get_paths_from_buildout_script(evaluator, buildout_script): def load(buildout_script): try: with open(buildout_script, 'rb') as f: source = common.source_to_unicode(f.read()) except IOError: debug.dbg('Error trying to read buildout_script: %s', buildout_script) return p = ParserWithRecovery(evaluator.grammar, source, buildout_script) save_parser(buildout_script, p) return p.module cached = load_parser(buildout_script) module = cached and cached.module or load(buildout_script) if not module: return for path in _check_module(evaluator, module): yield path
def get_line_code(self, before=0, after=0): """ Returns the line of code where this object was defined. :param before: Add n lines before the current line to the output. :param after: Add n lines after the current line to the output. :return str: Returns the line(s) of code or an empty string if it's a builtin. """ if self.in_builtin_module(): return '' path = self._name.get_root_context().py__file__() parser = load_parser(path) lines = common.splitlines(parser.source) line_nr = self._name.start_pos[0] start_line_nr = line_nr - before return '\n'.join(lines[start_line_nr:line_nr + after + 1])
def _extract_range_jedi_0_9_0(self, definition): from jedi import common from jedi.parser.utils import load_parser # get the scope range try: if definition.type in ['class', 'function'] and hasattr( definition, '_definition'): scope = definition._definition start_line = scope.start_pos[0] - 1 start_column = scope.start_pos[1] end_line = scope.end_pos[0] - 1 end_column = scope.end_pos[1] # get the lines path = definition._definition.get_parent_until().path parser = load_parser(path) lines = common.splitlines(parser.source) lines[end_line] = lines[end_line][:end_column] # trim the lines lines = lines[start_line:end_line + 1] lines = '\n'.join(lines).rstrip().split('\n') end_line = start_line + len(lines) - 1 end_column = len(lines[-1]) - 1 else: symbol = definition._name start_line = symbol.start_pos[0] - 1 start_column = symbol.start_pos[1] end_line = symbol.end_pos[0] - 1 end_column = symbol.end_pos[1] return { 'start_line': start_line, 'start_column': start_column, 'end_line': end_line, 'end_column': end_column } except Exception as e: return { 'start_line': definition.line - 1, 'start_column': definition.column, 'end_line': definition.line - 1, 'end_column': definition.column }
def _get_paths_from_buildout_script(evaluator, buildout_script): def load(buildout_script): try: with open(buildout_script, 'rb') as f: source = common.source_to_unicode(f.read()) except IOError: debug.dbg('Error trying to read buildout_script: %s', buildout_script) return p = ParserWithRecovery(evaluator.grammar, source, buildout_script) save_parser(buildout_script, p) return p.module cached = load_parser(buildout_script) module_node = cached and cached.module or load(buildout_script) if module_node is None: return from jedi.evaluate.representation import ModuleContext for path in _check_module(ModuleContext(evaluator, module_node)): yield path
def _extract_range_jedi_0_9_0(self, definition): from jedi import common from jedi.parser.utils import load_parser # get the scope range try: if definition.type in ['class', 'function'] and hasattr(definition, '_definition'): scope = definition._definition start_line = scope.start_pos[0] - 1 start_column = scope.start_pos[1] end_line = scope.end_pos[0] - 1 end_column = scope.end_pos[1] # get the lines path = definition._definition.get_parent_until().path parser = load_parser(path) lines = common.splitlines(parser.source) lines[end_line] = lines[end_line][:end_column] # trim the lines lines = lines[start_line:end_line + 1] lines = '\n'.join(lines).rstrip().split('\n') end_line = start_line + len(lines) - 1 end_column = len(lines[-1]) - 1 else: symbol = definition._name start_line = symbol.start_pos[0] - 1 start_column = symbol.start_pos[1] end_line = symbol.end_pos[0] - 1 end_column = symbol.end_pos[1] return { 'start_line': start_line, 'start_column': start_column, 'end_line': end_line, 'end_column': end_column } except Exception as e: return { 'start_line': definition.line - 1, 'start_column': definition.column, 'end_line': definition.line - 1, 'end_column': definition.column }
def _load_module(evaluator, path=None, source=None, sys_path=None): def load(source): dotted_path = path and compiled.dotted_from_fs_path(path, sys_path) if path is not None and path.endswith(('.py', '.zip', '.egg')) \ and dotted_path not in settings.auto_import_modules: if source is None: with open(path, 'rb') as f: source = f.read() else: return compiled.load_module(evaluator, path) p = path p = fast.FastParser(evaluator.grammar, common.source_to_unicode(source), p) save_parser(path, p) return p.module if sys_path is None: sys_path = evaluator.sys_path cached = load_parser(path) module = load(source) if cached is None else cached.module module = evaluator.wrap(module) return module