-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathdocumentFile.py
More file actions
175 lines (157 loc) · 6.21 KB
/
Copy pathdocumentFile.py
File metadata and controls
175 lines (157 loc) · 6.21 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
from base64 import b64encode as encode, b64decode as decode
from .sl4a import Result
from .__init__ import Android
import os
sdcard = os.environ['EXTERNAL_STORAGE']
class documentFile:
def __init__( this, sup ):
for i in dir(this):
if i[0] != '_':
exec(f'sup.documentFile{i}=this.{i}')
this._rpc = sup._rpc
this._write = sup._write
this._flush = sup._flush
this._readline = sup._readline
this._sup = sup
sup.documentFileShowOpen = sup.documentTreeShowOpen = this._TreeShowOpen
sup.documentFileMoveTo = this.RenameTo
sup.documentFileMkdirs = this.Mkdir
sup.documentFileWriteTo = this.OutputStream
sup.documentFileReadFrom = this.InputStream
def Mkdir( self, Dir ):
'''
documentFileMkdir( Dir )
Make New Directory(s)
documentFileMkdirs is the same as documentFileMkdir .
Return True if Success .
'''
return self._rpc("documentFileMkdir", Dir)
def Delete( self, FileOrTree ):
'''
documentFileDelete( FileOrTree )
Return True if Success .
'''
return self._rpc("documentFileDelete", FileOrTree)
def RenameTo( self, Src, Dest ):
'''
documentFileRenameTo( Src, Dest )
documentFileMoveTo is the same as documentFileRenameTo
Move or Rename Tree or File .
Return True if Success .
'''
return self._rpc("documentFileRenameTo", Src, Dest )
def Copy( self, SrcFileOrTree, DestFileOrTree ):
'''
documentFileCopy( SrcFileOrTree, DestFileOrTree )
Return None .
'''
return self._rpc("documentFileCopy", SrcFileOrTree, DestFileOrTree )
def ListFiles( self, Folder ):
'''
documentFileListFiles( Folder )
Return List of Files of the Folder .
'''
return self._rpc("documentFileListFiles", Folder )
def InputStream( self, srcFile, EncodingFormat = "", skip = None, length = None ):
'''
documentFileInputStream( srcFile, EncodingFormat = "", skip = None, length = None )
documentFileReadFrom is the same as documentFileInputStream .
If EncodingFormat is omitted ( empty default ) , this function will return Bytes ;
If EncodingFormat is Base64 , this function will return Base64 String ;
If EncodingFormat is UTF-8 or GBK etc , this function will return Normal String .
If skip is None default, skip == 0 (start of srcFile) , otherwise skip bytes from srcFile's head,
If length is None default, length means rest of srcFile, read length bytes from skip place .
'''
if EncodingFormat:
if EncodingFormat.upper()=='BASE64':
EncodingFormat=''
return self._rpc("documentFileInputStream", srcFile, EncodingFormat, skip, length)
else:
r=self._rpc("documentFileInputStream", srcFile, '', skip, length)
return Result(r.id,decode(r.result),r.error)
def OutputStream( self, destFile, src, EncodingFormat = "", append = None ):
'''
documentFileOutputStream( destFile, src, EncodingFormat = "", append = None )
documentFileWriteTo is the same as documentFileOutputStream .
If EncodingFormat is omitted (empty default) , src will be Bytes ;
If EncodingFormat is Base64 , src will be Base64 String ;
If EncodingFormat is UTF-8 or GBK etc , src will be Normal String ;
When a destFile exists, if append is True, append content to the end of the destFile, if append is False or None, overwrite the original destFile .
This Function always return None .
'''
if EncodingFormat:
if EncodingFormat.upper()=='BASE64':
EncodingFormat=''
return self._rpc("documentFileOutputStream", destFile, src, EncodingFormat, append)
else:
return self._rpc("documentFileOutputStream", destFile, encode( src ).decode(), '', append)
def GetUri(self, path, isDirectory = None):
'''
documentFileGetUri(path, isDirectory = None)
Return $URI according to the specific path .
path (String)
isDirectory (Boolean Optional) :
True - must be a directory , create an empty folder if not exist ;
False - must not be a directory , create an empty file if not exist ;
None - don't know if it is a directory , create nothing and return None if not exist .
'''
return self._rpc("documentFileGetUri", path, isDirectory )
def IsDirectory(self, path):
'''
documentFileIsDirectory(path)
Return a path is a directory ,
if path not exists or not available , return None .
path (String)
'''
return self._rpc("documentFileIsDirectory", path )
def IsFile(self, path):
'''
documentFileIsFile(path)
Return a path is a file ,
if path not exists or not available , return None .
path (String)
'''
return self._rpc("documentFileIsFile", path )
def Exists(self, path):
'''
documentFileExists(path)
Return a path is exists ,
path (String)
'''
return self._rpc("documentFileExists", path )
def LastModified(self, path):
'''
documentFileLastModified(path)
Return a path's Last Modified time,
if path not exists or not available , return 0 .
path (String)
'''
return self._rpc("documentFileLastModified", path )
def Length(self, path):
'''
documentFileLength(path)
Return a path's file size,
if path not exists or not available , return 0 .
path (String)
'''
return self._rpc("documentFileLength", path )
def GetStat(self, path):
'''
documentFileGetStat(path)
Return a dict of file length , last modified and file can read/write ,
if path not exists or not available , return None .
path (String)
'''
return self._rpc("documentFileGetStat", path )
def _TreeShowOpen( self, rootPath = sdcard ):
return self._rpc("documentTreeShowOpen", rootPath )
_i='"content://com.android.externalstorage.documents/<ContentPaths>"'
_d=documentFile.GetUri
_d.__doc__=_d.__doc__.replace('$URI',_i)
documentFile._TreeShowOpen.__doc__=Android.documentTreeShowOpen.__doc__.replace('/sdcard',sdcard).replace('$URI',_i)
_d='Use This Function to Read/Write Media Storage Device or "/sdcard/Android/<subPath>" .'.replace('/sdcard',sdcard)
for _i in dir(documentFile):
if _i[0]!='_':
exec('documentFile.%s.__doc__+=_d'%_i)
del _d,_i
#by 乘着船 2021-2023