Fix pylint errors in ipython_view

This commit is contained in:
Philipp Hörist 2018-09-21 23:53:50 +02:00
parent 1ca131cdf1
commit fafef38f2c
1 changed files with 43 additions and 38 deletions

View File

@ -40,17 +40,17 @@ available under the terms of the BSD which accompanies this distribution, and
is available at U{http://www.opensource.org/licenses/bsd-license.php} is available at U{http://www.opensource.org/licenses/bsd-license.php}
""" """
import re
import sys
import os
from io import StringIO
from functools import reduce from functools import reduce
from gi.repository import Gtk from gi.repository import Gtk
from gi.repository import Gdk from gi.repository import Gdk
from gi.repository import GObject from gi.repository import GObject
from gi.repository import GLib from gi.repository import GLib
import re
import sys
import os
from gi.repository import Pango from gi.repository import Pango
from io import StringIO
try: try:
import IPython import IPython
@ -92,8 +92,8 @@ class IterableIPShell:
@ivar complete_sep: Seperation delimeters for completion function. @ivar complete_sep: Seperation delimeters for completion function.
@type complete_sep: _sre.SRE_Pattern @type complete_sep: _sre.SRE_Pattern
""" """
def __init__(self,argv=[],user_ns=None,user_global_ns=None, cin=None, def __init__(self, argv=None, user_ns=None, user_global_ns=None, cin=None,
cout=None,cerr=None, input_func=None): cout=None, cerr=None, input_func=None):
""" """
@param argv: Command line options for IPython @param argv: Command line options for IPython
@type argv: list @type argv: list
@ -110,6 +110,9 @@ class IterableIPShell:
@param input_func: Replacement for builtin raw_input() @param input_func: Replacement for builtin raw_input()
@type input_func: function @type input_func: function
""" """
if argv is None:
argv = []
io = IPython.utils.io io = IPython.utils.io
if input_func: if input_func:
if IPython.version_info[0] >= 1: if IPython.version_info[0] >= 1:
@ -161,9 +164,9 @@ class IterableIPShell:
sys.excepthook = excepthook sys.excepthook = excepthook
self.iter_more = 0 self.iter_more = 0
self.history_level = 0 self.history_level = 0
self.complete_sep = re.compile('[\s\{\}\[\]\(\)]') self.complete_sep = re.compile(r'[\s\{\}\[\]\(\)]')
self.updateNamespace({'exit':lambda:None}) self.updateNamespace({'exit':lambda: None})
self.updateNamespace({'quit':lambda:None}) self.updateNamespace({'quit':lambda: None})
#self.IP.readline_startup_hook(self.IP.pre_readline) #self.IP.readline_startup_hook(self.IP.pre_readline)
# Workaround for updating namespace with sys.modules # Workaround for updating namespace with sys.modules
# #
@ -173,7 +176,7 @@ class IterableIPShell:
''' '''
Update self.IP namespace for autocompletion with sys.modules Update self.IP namespace for autocompletion with sys.modules
''' '''
for k,v in sys.modules.items(): for k, v in sys.modules.items():
if not '.' in k: if not '.' in k:
self.IP.user_ns.update({k:v}) self.IP.user_ns.update({k:v})
@ -315,7 +318,7 @@ class IterableIPShell:
possibilities = self.IP.complete(split_line[-1]) possibilities = self.IP.complete(split_line[-1])
else: else:
completed = line completed = line
possibilities = ['',[]] possibilities = ['', []]
if possibilities: if possibilities:
def _commonPrefix(str1, str2): def _commonPrefix(str1, str2):
''' '''
@ -387,7 +390,7 @@ class ConsoleView(Gtk.TextView):
weight=700) weight=700)
self.text_buffer.create_tag('0') self.text_buffer.create_tag('0')
self.text_buffer.create_tag('notouch', editable=False) self.text_buffer.create_tag('notouch', editable=False)
self.color_pat = re.compile('\x01?\x1b\[(.*?)m\x02?') self.color_pat = re.compile(r'\x01?\x1b\[(.*?)m\x02?')
if HAS_IPYTHON5: if HAS_IPYTHON5:
self.style_dict = { self.style_dict = {
Token.Prompt: '0;32', Token.Prompt: '0;32',
@ -401,7 +404,7 @@ class ConsoleView(Gtk.TextView):
self.connect('key-press-event', self.onKeyPress) self.connect('key-press-event', self.onKeyPress)
def write(self, text, editable=False): def write(self, text, editable=False):
if type(text) == str: if isinstance(text, str):
GLib.idle_add(self._write, text, editable) GLib.idle_add(self._write, text, editable)
elif IPython.version_info[0] >= 5: elif IPython.version_info[0] >= 5:
GLib.idle_add(self._write5, text, editable) GLib.idle_add(self._write5, text, editable)
@ -439,7 +442,7 @@ class ConsoleView(Gtk.TextView):
@param editable: If true, added text is editable. @param editable: If true, added text is editable.
@type editable: boolean @type editable: boolean
""" """
if type(text) == list and IPython.version_info[0] >= 5: if isinstance(text, list) and IPython.version_info[0] >= 5:
self._write5(text, editable) self._write5(text, editable)
return return
segments = self.color_pat.split(text) segments = self.color_pat.split(text)
@ -532,7 +535,7 @@ class ConsoleView(Gtk.TextView):
indentation = self.IP.input_splitter.indent_spaces * ' ' indentation = self.IP.input_splitter.indent_spaces * ' '
self.text_buffer.insert_at_cursor(indentation) self.text_buffer.insert_at_cursor(indentation)
def onKeyPress(self, widget, event): def onKeyPress(self, _widget, event):
""" """
Key press callback used for correcting behavior for console-like Key press callback used for correcting behavior for console-like
interfaces. For example 'home' should go to prompt, not to beginning of interfaces. For example 'home' should go to prompt, not to beginning of
@ -555,10 +558,11 @@ class ConsoleView(Gtk.TextView):
if event.get_state() == 0: if event.get_state() == 0:
self.text_buffer.place_cursor(start_iter) self.text_buffer.place_cursor(start_iter)
return True return True
elif event.get_state() == Gdk.ModifierType.SHIFT_MASK: if event.get_state() == Gdk.ModifierType.SHIFT_MASK:
self.text_buffer.move_mark(insert_mark, start_iter) self.text_buffer.move_mark(insert_mark, start_iter)
return True return True
elif event.keyval == Gdk.KEY_Left:
if event.keyval == Gdk.KEY_Left:
insert_iter.backward_cursor_position() insert_iter.backward_cursor_position()
if not insert_iter.editable(True): if not insert_iter.editable(True):
return True return True
@ -611,7 +615,7 @@ class IPythonView(ConsoleView, IterableIPShell):
# IPython 5.0 calls prompt_for_code instead of raw_input # IPython 5.0 calls prompt_for_code instead of raw_input
return self.raw_input(self) return self.raw_input(self)
def raw_input(self, prompt=''): def raw_input(self, _prompt=''):
""" """
Custom raw_input() replacement. Get's current line from console buffer Custom raw_input() replacement. Get's current line from console buffer
@ -643,16 +647,16 @@ class IPythonView(ConsoleView, IterableIPShell):
self.interrupt = True self.interrupt = True
self._processLine() self._processLine()
return True return True
elif event.keyval == Gdk.KEY_Return: if event.keyval == Gdk.KEY_Return:
self._processLine() self._processLine()
return True return True
elif event.keyval == Gdk.KEY_Up: if event.keyval == Gdk.KEY_Up:
self.changeLine(self.historyBack()) self.changeLine(self.historyBack())
return True return True
elif event.keyval == Gdk.KEY_Down: if event.keyval == Gdk.KEY_Down:
self.changeLine(self.historyForward()) self.changeLine(self.historyForward())
return True return True
elif event.keyval == Gdk.KEY_Tab: if event.keyval == Gdk.KEY_Tab:
if not self.getCurrentLine().strip(): if not self.getCurrentLine().strip():
return False return False
completed, possibilities = self.complete(self.getCurrentLine()) completed, possibilities = self.complete(self.getCurrentLine())
@ -672,7 +676,8 @@ class IPythonView(ConsoleView, IterableIPShell):
self.history_pos = 0 self.history_pos = 0
self.execute() self.execute()
rv = self.cout.getvalue() rv = self.cout.getvalue()
if rv: rv = rv.strip('\n') if rv:
rv = rv.strip('\n')
self.showReturned(rv) self.showReturned(rv)
self.cout.truncate(0) self.cout.truncate(0)
self.cout.seek(0) self.cout.seek(0)