File size: 1,107 Bytes
d015b2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""Encoding parameter handling and default."""

import typing

import codecs
import locale

from . import copying

__all__ = ['DEFAULT_ENCODING', 'Encoding']

DEFAULT_ENCODING = 'utf-8'


class Encoding(copying.CopyBase):
    """Encoding used for input and output with ``'utf-8'`` default."""

    _encoding = DEFAULT_ENCODING

    def __init__(self, *, encoding: typing.Optional[str] = DEFAULT_ENCODING,

                 **kwargs) -> None:
        super().__init__(**kwargs)

        self.encoding = encoding

    def _copy_kwargs(self, **kwargs):
        """Return the kwargs to create a copy of the instance."""
        return super()._copy_kwargs(encoding=self._encoding, **kwargs)

    @property
    def encoding(self) -> str:
        """The encoding for the saved source file."""
        return self._encoding

    @encoding.setter
    def encoding(self, encoding: typing.Optional[str]) -> None:
        if encoding is None:
            encoding = locale.getpreferredencoding()

        codecs.lookup(encoding)  # raise early
        self._encoding = encoding