Completed
Push — master ( 8de643...fa81ea )
by Björn
01:10
created

neovim_gui.UIBridge.apply_updates()   C

Complexity

Conditions 7

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 7
dl 0
loc 21
rs 6.4705
1
"""Bridge for connecting a UI instance to nvim."""
2
import sys
3
from threading import Semaphore, Thread
4
from traceback import format_exc
5
6
7
class UIBridge(object):
8
9
    """UIBridge class. Connects a Nvim instance to a UI class."""
10
11
    def connect(self, nvim, ui, profile=None, notify=False):
12
        """Connect nvim and the ui.
13
14
        This will start loops for handling the UI and nvim events while
15
        also synchronizing both.
16
        """
17
        self._notify = notify
0 ignored issues
show
Coding Style introduced by
The attribute _notify was defined outside __init__.

It is generally a good practice to initialize all attributes to default values in the __init__ method:

class Foo:
    def __init__(self, x=None):
        self.x = x
Loading history...
18
        self._error = None
0 ignored issues
show
Coding Style introduced by
The attribute _error was defined outside __init__.

It is generally a good practice to initialize all attributes to default values in the __init__ method:

class Foo:
    def __init__(self, x=None):
        self.x = x
Loading history...
19
        self._nvim = nvim
0 ignored issues
show
Coding Style introduced by
The attribute _nvim was defined outside __init__.

It is generally a good practice to initialize all attributes to default values in the __init__ method:

class Foo:
    def __init__(self, x=None):
        self.x = x
Loading history...
20
        self._ui = ui
0 ignored issues
show
Coding Style introduced by
The attribute _ui was defined outside __init__.

It is generally a good practice to initialize all attributes to default values in the __init__ method:

class Foo:
    def __init__(self, x=None):
        self.x = x
Loading history...
21
        self._profile = profile
0 ignored issues
show
Coding Style introduced by
The attribute _profile was defined outside __init__.

It is generally a good practice to initialize all attributes to default values in the __init__ method:

class Foo:
    def __init__(self, x=None):
        self.x = x
Loading history...
22
        self._sem = Semaphore(0)
0 ignored issues
show
Coding Style introduced by
The attribute _sem was defined outside __init__.

It is generally a good practice to initialize all attributes to default values in the __init__ method:

class Foo:
    def __init__(self, x=None):
        self.x = x
Loading history...
23
        t = Thread(target=self._nvim_event_loop)
24
        t.daemon = True
25
        t.start()
26
        self._ui_event_loop()
27
        if self._error:
28
            print(self._error)
29
        if self._profile:
30
            print(self._profile)
31
32
    def exit(self):
33
        """Disconnect by exiting nvim."""
34
        self.detach()
35
        self._call(self._nvim.quit)
36
37
    def input(self, input_str):
38
        """Send input to nvim."""
39
        self._call(self._nvim.input, input_str)
40
41
    def resize(self, columns, rows):
42
        """Send a resize request to nvim."""
43
        self._call(self._nvim.ui_try_resize, columns, rows)
44
45
    def attach(self, columns, rows, rgb):
46
        """Attach the UI to nvim."""
47
        self._call(self._nvim.ui_attach, columns, rows, rgb)
48
49
    def detach(self):
50
        """Detach the UI from nvim."""
51
        self._call(self._nvim.ui_detach)
52
53
    def _call(self, fn, *args):
54
        self._nvim.session.threadsafe_call(fn, *args)
55
56
    def _ui_event_loop(self):
57
        self._sem.acquire()
58
        if self._profile:
59
            import StringIO
0 ignored issues
show
Configuration introduced by
The import StringIO could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
60
            import cProfile
61
            import pstats
62
            pr = cProfile.Profile()
63
            pr.enable()
64
        self._ui.start(self)
65
        if self._profile:
66
            pr.disable()
67
            s = StringIO.StringIO()
68
            ps = pstats.Stats(pr, stream=s)
69
            ps.strip_dirs().sort_stats(self._profile).print_stats(30)
70
            self._profile = s.getvalue()
0 ignored issues
show
Coding Style introduced by
The attribute _profile was defined outside __init__.

It is generally a good practice to initialize all attributes to default values in the __init__ method:

class Foo:
    def __init__(self, x=None):
        self.x = x
Loading history...
71
72
    def _nvim_event_loop(self):
73
        def on_setup():
74
            self._sem.release()
75
76
        def on_request(method, args):
0 ignored issues
show
Unused Code introduced by
The argument method seems to be unused.
Loading history...
Unused Code introduced by
The argument args seems to be unused.
Loading history...
77
            raise Exception('Not implemented')
78
79
        def on_notification(method, updates):
80
            def apply_updates():
81
                if self._notify:
82
                    sys.stdout.write('attached\n')
83
                    sys.stdout.flush()
84
                    self._notify = False
0 ignored issues
show
Coding Style introduced by
The attribute _notify was defined outside __init__.

It is generally a good practice to initialize all attributes to default values in the __init__ method:

class Foo:
    def __init__(self, x=None):
        self.x = x
Loading history...
85
                try:
86
                    for update in updates:
87
                        # import sys
88
                        # l = [','.join([str(a) for a in args])
89
                        #      for args in update[1:]]
90
                        # print >> sys.stderr, update[0], ' '.join(l)
91
                        try:
92
                            handler = getattr(self._ui, '_nvim_' + update[0])
93
                        except AttributeError:
94
                            pass
95
                        else:
96
                            for args in update[1:]:
97
                                handler(*args)
98
                except:
0 ignored issues
show
Coding Style Best Practice introduced by
General except handlers without types should be used sparingly.

Typically, you would use general except handlers when you intend to specifically handle all types of errors, f.e. when logging. Otherwise, such general error handlers can mask errors in your application that you want to know of.

Loading history...
99
                    self._error = format_exc()
0 ignored issues
show
Coding Style introduced by
The attribute _error was defined outside __init__.

It is generally a good practice to initialize all attributes to default values in the __init__ method:

class Foo:
    def __init__(self, x=None):
        self.x = x
Loading history...
100
                    self._call(self._nvim.quit)
101
            if method == 'redraw':
102
                self._ui.schedule_screen_update(apply_updates)
103
104
        self._nvim.session.run(on_request, on_notification, on_setup)
105
        self._ui.quit()
106