-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathsfile.py
More file actions
253 lines (170 loc) · 5.15 KB
/
sfile.py
File metadata and controls
253 lines (170 loc) · 5.15 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# Copyright (C) 2015
# Sean Poyser (seanpoyser@gmail.com)
#
# 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 2, or (at your option)
# any later version.
#
# This Progr`am 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 XBMC; see the file COPYING. If not, write to
# the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
# http://www.gnu.org/copyleft/gpl.html
#
# this module provides a wrapper around the Kodi xbmcvfs class
import xbmcvfs
import os
def exists(filename):
if xbmcvfs.exists(filename):
return True
if xbmcvfs.exists(filename+os.sep):
return True
return os.path.exists(filename)
def isfile(filename):
if not exists(filename):
#raise Exception('sfile.isfile error %s does not exists' % filename)
return False
import stat
return stat.S_ISREG(xbmcvfs.Stat(filename).st_mode())
def isdir(folder):
import utils
if folder.endswith('\\') or folder.endswith('/'):
folder = folder[:-1]
import stat
if stat.S_ISDIR(xbmcvfs.Stat(folder).st_mode()):
return True
import xbmc
folder = xbmc.translatePath(folder)
if folder.endswith('\\') or folder.endswith('/'):
folder = folder[:-1]
return stat.S_ISDIR(xbmcvfs.Stat(xbmc.translatePath(folder)).st_mode())
def file(filename, type):
return xbmcvfs.File(filename, type)
def size(filename):
return xbmcvfs.File(filename).size()
def read(filename):
f = file(filename, 'r')
content = f.read()
f.close()
return content
def write(filename, content):
f = file(filename, 'wb')
f.write(content)
f.close()
def readlines(filename):
lines = read(filename)
lines = lines.replace('\r', '')
lines = lines.split('\n')
return lines
def writelines(filename, lines):
f = file(filename, 'w')
first = True
for line in lines:
if not first:
f.write('\n')
else:
first = False
f.write(line)
f.close()
def walk(folder):
list = xbmcvfs.listdir(folder)
return folder, list[0], list[1]
def glob(folder):
import os
current, dirs, files = walk(folder)
full = []
for file in files:
full.append(os.path.join(current, file))
return full
def makedirs(path):
xbmcvfs.mkdirs(path)
def delete(filename):
return remove(filename)
def remove(filename):
if isdir(filename):
return rmtree(filename)
return xbmcvfs.delete(filename)
def rmtree(folder):
import os
current, dirs, files = walk(folder)
for file in files:
remove(os.path.join(current, file))
for dir in dirs:
rmtree(os.path.join(current, dir))
xbmcvfs.rmdir(folder)
def copytree(src, dst):
import os
#if exists(dst):
# rmtree(dst)
makedirs(dst)
current, dirs, files = walk(src)
for file in files:
copy(os.path.join(current, file), os.path.join(dst, file))
for dir in dirs:
copytree(os.path.join(src, dir), os.path.join(dst, dir))
def copy(src, dst, overWrite=True):
if not overWrite and exists(dst):
return False
if isdir(src):
return copytree(src, dst)
return xbmcvfs.copy(src, dst)
def rename(src, dst):
if src == dst:
return
if not exists(src):
return
if isdir(src):
if src.lower() == dst.lower():
newSrc = src +'sfile_temp_name'
rename(src, newSrc)
src = newSrc
copytree(src, dst)
rmtree(src)
return
return xbmcvfs.rename(src, dst)
def mtime(filename):
if not exists(filename):
raise Exception('sfile.mtime error %s does not exists')
status = xbmcvfs.Stat(filename)
return status.st_mtime()
def ctime(filename):
if not exists(filename):
raise Exception('sfile.ctime error %s does not exists')
status = xbmcvfs.Stat(filename)
return status.st_ctime()
#def status(filename):
# if not exists(filename):
# raise Exception('sfile.status error %s does not exists' % filename)
#
# status = xbmcvfs.Stat(filename)
# return status
def getfolder(path):
import os
path = path.replace('/', os.sep)
if path.endswith(os.sep):
path += 'filename'
try: return path.rsplit(os.sep, 1)[0]
except: return ''
def getfilename(path):
import os
path = path.replace('/', os.sep)
try: return path.rsplit(os.sep, 1)[-1]
except: return ''
def removeextension(path):
try: return path.rsplit('.', 1)[0]
except: path
def getextension(path):
try: return path.rsplit('.')[-1]
except: return ''
def isempty(folder):
current, dirs, files = walk(folder)
if len(dirs) > 0:
return False
if len(files) > 0:
return False
return True