aboutsummaryrefslogtreecommitdiffhomepage
path: root/custom_libs/subzero/lib/io.py
blob: ed46af2ee378724fdf1030301025d964c495c950 (plain)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# coding=utf-8

from __future__ import absolute_import
import os
import sys

try:
    from os import scandir as _scandir
except ImportError:
    from scandir import scandir as _scandir

# thanks @ plex trakt scrobbler: https://github.com/trakt/Plex-Trakt-Scrobbler/blob/master/Trakttv.bundle/Contents/Libraries/Shared/plugin/core/io.py


class FileIO(object):
    @staticmethod
    def exists(path):
        return os.path.exists(path)

    @staticmethod
    def delete(path):
        os.remove(path)

    @staticmethod
    def read(path, mode='r'):
        with open(path, mode) as fp:
            data = fp.read()

        return data

    @staticmethod
    def write(path, data, mode='w'):
        with open(path, mode) as fp:
            fp.write(data)


VALID_ENCODINGS = ("latin1", "utf-8", "mbcs")


def get_viable_encoding():
    # fixme: bad
    encoding = sys.getfilesystemencoding()
    return "utf-8" if not encoding or encoding.lower() not in VALID_ENCODINGS else encoding


class ScandirListdirEntryStub(object):
    """
    A class which mimics the entries returned by scandir, for fallback purposes when using listdir instead.
    """
    __slots__ = ('name', '_d_type', '_stat', '_lstat', '_scandir_path', '_path', '_inode')

    def __init__(self, scandir_path, name, d_type, inode):
        self._scandir_path = scandir_path
        self.name = name
        self._d_type = d_type
        self._inode = inode
        self._stat = None
        self._lstat = None
        self._path = None

    @property
    def path(self):
        if self._path is None:
            self._path = os.path.join(self._scandir_path, self.name)
        return self._path

    def stat(self, follow_symlinks=True):
        path = self.path
        if follow_symlinks and self.is_symlink():
            path = os.path.realpath(path)

        return os.stat(path)

    def is_dir(self, follow_symlinks=True):
        path = self.path
        if follow_symlinks and self.is_symlink():
            path = os.path.realpath(path)

        return os.path.isdir(path)

    def is_file(self, follow_symlinks=True):
        path = self.path
        if follow_symlinks and self.is_symlink():
            path = os.path.realpath(path)

        return os.path.isfile(path)

    def is_symlink(self):
        return os.path.islink(self.path)


def scandir_listdir_fallback(path):
    for fn in os.listdir(path):
        yield ScandirListdirEntryStub(path, fn, None, None)


def scandir(path):
    try:
        return _scandir(path)

    # fallback for systems where sys.getfilesystemencoding() returns the "wrong" value
    except UnicodeDecodeError:
        return scandir_listdir_fallback(path)