def show_kernel_error(self, error): """Show kernel initialization errors in infowidget""" # Remove explanation about how to kill the kernel (doesn't apply to us) error = error.split('issues/2049')[-1] # Remove unneeded blank lines at the beginning eol = sourcecode.get_eol_chars(error) if eol: error = error.replace(eol, '<br>') while error.startswith('<br>'): error = error[4:] # Remove connection message if error.startswith('To connect another client') or \ error.startswith('[IPKernelApp] To connect another client'): error = error.split('<br>') error = '<br>'.join(error[2:]) # Don't break lines in hyphens # From http://stackoverflow.com/q/7691569/438386 error = error.replace('-', '‑') message = _("An error ocurred while starting the kernel") kernel_error_template = Template(KERNEL_ERROR) page = kernel_error_template.substitute(css_path=CSS_PATH, message=message, error=error) self.infowidget.setHtml(page)
def set_eol_chars(self, text): """Set widget end-of-line (EOL) characters from text (analyzes text)""" if not is_text_string(text): # testing for QString (PyQt API#1) text = to_text_string(text) eol_chars = sourcecode.get_eol_chars(text) if eol_chars is not None and self.eol_chars is not None: self.document().setModified(True) self.eol_chars = eol_chars
def get_definition_with_regex(source, token, start_line=-1, use_assignment=False): """ Find the definition of an object within a source closest to a given line """ if not token: return None if DEBUG_EDITOR: t0 = time.time() patterns = [ # python / cython keyword definitions '^c?import.*\W{0}{1}', 'from.*\W{0}\W.*c?import ', 'from .* c?import.*\W{0}{1}', 'class\s*{0}{1}', 'c?p?def[^=]*\W{0}{1}', 'cdef.*\[.*\].*\W{0}{1}', # enaml keyword definitions 'enamldef.*\W{0}{1}', 'attr.*\W{0}{1}', 'event.*\W{0}{1}', 'id\s*:.*\W{0}{1}'] if use_assignment: patterns += ['.*\Wself.{0}{1}[^=!<>]*=[^=]', '.*\W{0}{1}[^=!<>]*=[^=]', 'self.{0}{1}[^=!<>]*=[^=]', '{0}{1}[^=!<>]*=[^=]'] patterns = [pattern.format(token, r'[^0-9a-zA-Z.[]') for pattern in patterns] pattern = re.compile('|^'.join(patterns)) # add the trailing space to allow some regexes to match eol = sourcecode.get_eol_chars(source) or '\n' lines = source.split(eol) lines = [line.strip() + ' ' for line in lines] if start_line == -1: start_line = len(lines) matches = [] for (index, line) in enumerate(lines): if re.match(pattern, line): matches.append(index + 1) # find the one closest to the start line (prefer before the start line) if matches: min_dist = len(lines) best_ind = 0 for match in matches: dist = abs(start_line - match) if match <= start_line or not best_ind: if dist < min_dist: min_dist = dist best_ind = match if matches: if DEBUG_EDITOR: log_dt(LOG_FILENAME, 'regex definition match', t0) return best_ind else: if DEBUG_EDITOR: log_dt(LOG_FILENAME, 'regex definition failed match', t0) return None
def set_eol_mode(self, text): """ Set QScintilla widget EOL mode based on *text* EOL characters """ if isinstance(text, QString): text = unicode(text) eol_chars = sourcecode.get_eol_chars(text) if eol_chars is not None: self.setEolMode(self.EOL_MODES[eol_chars])
def _get_definition_location_regex(self, source_code, offset, filename): """ Find the definition for an object within a set of source code This is used to find the path of python-like modules (e.g. cython and enaml) for a goto definition """ token = sourcecode.get_primary_at(source_code, offset) eol = sourcecode.get_eol_chars(source_code) or '\n' lines = source_code[:offset].split(eol) line_nr = None if '.' in token: temp = token.split('.')[-1] line_nr = self.get_definition_with_regex(source_code, temp, len(lines)) if line_nr is None: line_nr = self.get_definition_with_regex(source_code, token, len(lines), True) if line_nr is None and '.' in token: temp = token.split('.')[-1] line_nr = self.get_definition_with_regex(source_code, temp, len(lines), True) if line_nr is None: return None, None line = source_code.split(eol)[line_nr - 1].strip() exts = self.python_like_exts() if not osp.splitext(filename)[-1] in exts: return filename, line_nr if line.startswith('import ') or line.startswith('from '): alt_path = osp.dirname(filename) source_file = self.python_like_mod_finder(line, alt_path=alt_path, stop_token=token) if (not source_file or not osp.splitext(source_file)[-1] in exts): line_nr = self.get_definition_with_regex( source_code, token, line_nr) return filename, line_nr mod_name = osp.basename(source_file).split('.')[0] if mod_name == token or mod_name == '__init__': return source_file, 1 else: line_nr = self.get_definition_from_file(source_file, token) return source_file, line_nr return filename, line_nr
def _get_definition_location_regex(self, source_code, offset, filename): """ Find the definition for an object within a set of source code This is used to find the path of python-like modules (e.g. cython and enaml) for a goto definition """ token = sourcecode.get_primary_at(source_code, offset) eol = sourcecode.get_eol_chars(source_code) or '\n' lines = source_code[:offset].split(eol) line_nr = None if '.' in token: temp = token.split('.')[-1] line_nr = self.get_definition_with_regex(source_code, temp, len(lines)) if line_nr is None: line_nr = self.get_definition_with_regex(source_code, token, len(lines), True) if line_nr is None and '.' in token: temp = token.split('.')[-1] line_nr = self.get_definition_with_regex(source_code, temp, len(lines), True) if line_nr is None: return None, None line = source_code.split(eol)[line_nr - 1].strip() exts = self.python_like_exts() if not osp.splitext(filename)[-1] in exts: return filename, line_nr if line.startswith('import ') or line.startswith('from '): alt_path = osp.dirname(filename) source_file = self.python_like_mod_finder(line, alt_path=alt_path, stop_token=token) if (not source_file or not osp.splitext(source_file)[-1] in exts): line_nr = self.get_definition_with_regex(source_code, token, line_nr) return filename, line_nr mod_name = osp.basename(source_file).split('.')[0] if mod_name == token or mod_name == '__init__': return source_file, 1 else: line_nr = self.get_definition_from_file(source_file, token) return source_file, line_nr return filename, line_nr
def get_definition_with_regex(source, token, start_line=-1, use_assignment=False): """ Find the definition of an object within a source closest to a given line """ if not token: return None if DEBUG_EDITOR: t0 = time.time() patterns = [ # python / cython keyword definitions '^c?import.*\W{0}{1}', 'from.*\W{0}\W.*c?import ', 'from .* c?import.*\W{0}{1}', 'class\s*{0}{1}', 'c?p?def[^=]*\W{0}{1}', 'cdef.*\[.*\].*\W{0}{1}', # enaml keyword definitions 'enamldef.*\W{0}{1}', 'attr.*\W{0}{1}', 'event.*\W{0}{1}', 'id\s*:.*\W{0}{1}' ] if use_assignment: patterns += [ '.*\Wself.{0}{1}[^=!<>]*=[^=]', '.*\W{0}{1}[^=!<>]*=[^=]', 'self.{0}{1}[^=!<>]*=[^=]', '{0}{1}[^=!<>]*=[^=]' ] patterns = [ pattern.format(token, r'[^0-9a-zA-Z.[]') for pattern in patterns ] pattern = re.compile('|^'.join(patterns)) # add the trailing space to allow some regexes to match eol = sourcecode.get_eol_chars(source) or '\n' lines = source.split(eol) lines = [line.strip() + ' ' for line in lines] if start_line == -1: start_line = len(lines) matches = [] for (index, line) in enumerate(lines): if re.match(pattern, line): matches.append(index + 1) # find the one closest to the start line (prefer before the start line) if matches: min_dist = len(lines) best_ind = 0 for match in matches: dist = abs(start_line - match) if match <= start_line or not best_ind: if dist < min_dist: min_dist = dist best_ind = match if matches: if DEBUG_EDITOR: log_dt(LOG_FILENAME, 'regex definition match', t0) return best_ind else: if DEBUG_EDITOR: log_dt(LOG_FILENAME, 'regex definition failed match', t0) return None