Completed
Push — master ( ae9e6c...27a45c )
by Lambda
01:46
created

safeget()   A

Complexity

Conditions 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
1
"""Utility module."""
2
from typing import AnyStr, Union
3
from neovim import Nvim
4
5
_cached_encoding = None     # type: str
6
7
8
def get_encoding(nvim: Nvim) -> str:
9
    """Return a Vim's internal encoding.
10
11
    The retrieve encoding is cached to the function instance while encoding
12
    options should not be changed in Vim's live session (see :h encoding) to
13
    enhance performance.
14
15
    Args:
16
        nvim (neovim.Nvim): A ``neovim.Nvim`` instance.
17
18
    Returns:
19
        str: A Vim's internal encoding.
20
    """
21
    global _cached_encoding
22
    if _cached_encoding is None:
23
        _cached_encoding = nvim.options['encoding']
24
    return _cached_encoding
25
26
27
def ensure_bytes(nvim: Nvim, seed: AnyStr) -> bytes:
28
    """Encode `str` to `bytes` if necessary and return.
29
30
    Args:
31
        nvim (neovim.Nvim): A ``neovim.Nvim`` instance.
32
        seed (AnyStr): A str or bytes instance.
33
34
    Example:
35
        >>> from unittest.mock import MagicMock
36
        >>> nvim = MagicMock()
37
        >>> nvim.options = {'encoding': 'utf-8'}
38
        >>> ensure_bytes(nvim, b'a')
39
        b'a'
40
        >>> ensure_bytes(nvim, 'a')
41
        b'a'
42
43
    Returns:
44
        bytes: A bytes represantation of ``seed``.
45
    """
46
    if isinstance(seed, str):
47
        encoding = get_encoding(nvim)
48
        return seed.encode(encoding, 'surrogateescape')
49
    return seed
50
51
52
def ensure_str(nvim: Nvim, seed: AnyStr) -> str:
53
    """Decode `bytes` to `str` if necessary and return.
54
55
    Args:
56
        nvim (neovim.Nvim): A ``neovim.Nvim`` instance.
57
        seed (AnyStr): A str or bytes instance.
58
59
    Example:
60
        >>> from unittest.mock import MagicMock
61
        >>> nvim = MagicMock()
62
        >>> nvim.options = {'encoding': 'utf-8'}
63
        >>> ensure_str(nvim, b'a')
64
        'a'
65
        >>> ensure_str(nvim, 'a')
66
        'a'
67
68
    Returns:
69
        str: A str represantation of ``seed``.
70
    """
71
    if isinstance(seed, bytes):
72
        encoding = get_encoding(nvim)
73
        return seed.decode(encoding, 'surrogateescape')
74
    return seed
75
76
77
def int2chr(nvim: Nvim, code: int) -> str:
78
    """Return a corresponding char of `code`.
79
80
    It uses "nr2char()" in Vim script when 'encoding' option is not utf-8.
81
    Otherwise it uses "chr()" in Python to improve the performance.
82
83
    Args:
84
        nvim (neovim.Nvim): A ``neovim.Nvim`` instance.
85
        code (int): A int which represent a single character.
86
87
    Example:
88
        >>> from unittest.mock import MagicMock
89
        >>> nvim = MagicMock()
90
        >>> nvim.options = {'encoding': 'utf-8'}
91
        >>> int2chr(nvim, 97)
92
        'a'
93
94
    Returns:
95
        str: A str of ``code``.
96
    """
97
    encoding = get_encoding(nvim)
98
    if encoding in ('utf-8', 'utf8'):
99
        return chr(code)
100
    return nvim.call('nr2char', code)
101
102
103
def getchar(nvim: Nvim, *args) -> Union[int, bytes]:
104
    """Call getchar and return int or bytes instance.
105
106
    Args:
107
        nvim (neovim.Nvim): A ``neovim.Nvim`` instance.
108
        *args: Arguments passed to getchar function in Vim.
109
110
    Returns:
111
        Union[int, bytes]: A int or bytes.
112
    """
113
    ret = nvim.call('getchar', *args)
114
    if isinstance(ret, int):
115
        return ret
116
    return ensure_bytes(nvim, ret)
117
118
119
def safeget(l: list, index: int, default=None):
120
    """Return an index item of list or default."""
121
    try:
122
        return l[index]
123
    except IndexError:
124
        return default
125