Completed
Pull Request — master (#305)
by Bart
01:41
created

fuel.utils.open_()   A

Complexity

Conditions 2

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 2
dl 0
loc 23
rs 9.0856
1
"""Low-level utilities for reading a variety of source formats."""
2
import codecs
3
import gzip
4
import tarfile
5
import six
6
7
8
def open_(filename, mode='r', encoding='utf-8'):
9
    """Open a text file with UTF-8 and optional gzip compression.
10
11
    This function is useful when dealing with text files.
12
13
    Parameters
14
    ----------
15
    filename : str
16
        The filename to read.
17
    mode : str, optional
18
        The mode with which to open the file. Defaults to `r`.
19
    encoding : str, optional
20
        The encoding to use (see the codecs documentation_ for supported
21
        values). Defaults to `utf-8`.
22
23
    .. _documentation:
24
    https://docs.python.org/3/library/codecs.html#standard-encodings
25
26
    """
27
    if filename.endswith('.gz.'):
28
        zf = gzip.open(filename, mode)
29
        return codecs.getreader(encoding)(zf)
30
    return codecs.open(filename, mode, encoding=encoding)
31
32
33
def tar_open(f):
34
    """Open either a filename or a file-like object as a TarFile.
35
36
    Parameters
37
    ----------
38
    f : str or file-like object
39
        The filename or file-like object from which to read.
40
41
    Returns
42
    -------
43
    TarFile
44
        A `TarFile` instance.
45
46
    """
47
    if isinstance(f, six.string_types):
48
        return tarfile.open(name=f)
49
    else:
50
        return tarfile.open(fileobj=f)
51