forked from faush01/MissingMovieScanner
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfsutils.py
107 lines (85 loc) · 2.69 KB
/
fsutils.py
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
104
105
106
107
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015 Thomas Amland
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
from collections import namedtuple
try:
import xbmc
import xbmcvfs
except ImportError:
pass
DirEntry = namedtuple('DirEntry', ['name', 'is_dir'])
def _listdir(path):
try:
return xbmcvfs.listdir(path)
except NameError:
pass
# Emulate xbmcvfs.listdir
assert isinstance(path, str)
assert path.endswith(b'/')
assert os.path.isdir(path)
dirs = []
files = []
path = path.rstrip(b'/')
try:
for name in os.listdir(path):
if os.path.isdir(os.path.join(path, name)):
dirs.append(name)
else:
files.append(name)
except OSError:
pass
return dirs, files
def walk(path, filter_dir=lambda *args: True):
assert not isinstance(path, unicode)
return _walk(path, b'', filter_dir)
def listdir(path):
assert not isinstance(path, unicode)
if not path.endswith('/'):
path += '/'
dirs, files = _listdir(path)
for name in dirs:
yield DirEntry(name, True)
for name in files:
yield DirEntry(name, False)
def _walk(path_head, path_tail, filter_dir):
try:
if xbmc.abortRequested:
raise OSError("interrupted")
except NameError:
pass
dirs, files = _listdir(join(path_head, path_tail))
if ".nomedia" in files:
return
for name in files:
yield DirEntry(join(path_tail, name), False)
for name in dirs:
if filter_dir(path_tail, name):
yield DirEntry(join(path_tail, name), True)
for entry in _walk(path_head, join(path_tail, name), filter_dir):
yield entry
def join(base_path, *paths):
"""
Join VFS paths. Uses / as separator if base_path is an url. otherwise os.sep
"""
assert isinstance(base_path, str)
assert base_path != b""
result = base_path
for path in paths:
if result != b"" and not result.endswith('/') and not path.startswith('/'):
result += '/'
result += path
return result