This repository has been archived on 2023-09-06. You can view files and clone it, but cannot push or open issues/pull-requests.
yutent
/
py-gtk-notes
Archived
1
0
Fork 0

增加遍历目录的接口

master
yutent 2023-08-01 15:25:13 +08:00
parent 031255857d
commit 511302f72d
3 changed files with 52 additions and 23 deletions

View File

@ -186,20 +186,33 @@ Object.assign(native, {
return handler('quit', {}, null)
},
fs: {
read(filepath) {
return handler('fs', { action: 'read', filepath })
read(filepath, mode = 'r') {
return handler('fs', { action: 'read', mode, filepath }).then(r =>
mode.includes('b') ? new Uint8Array(r) : r
)
},
write(filepath, content = '', append = false) {
write(filepath, content = '', mode = 'w') {
return handler('fs', {
action: 'write',
append: false,
filepath,
content,
append
content
})
},
append(filepath, content = '', mode = 'w') {
return handler('fs', {
action: 'write',
append: true,
filepath,
content
})
},
exists(filepath) {
return handler('fs', { action: 'exists', filepath })
},
list(filepath) {
return handler('fs', { action: 'list', filepath })
},
isfile(filepath) {
return handler('fs', { action: 'isfile', filepath })
},

35
main.py
View File

@ -197,30 +197,35 @@ class WebKitWindow(Gtk.Window):
filepath = params.get('filepath')
if params['action'] == 'read':
file = open(filepath)
output = file.read()
with open(filepath, params.get('mode')) as file:
output = file.read()
if params.get('mode').find('b') > -1:
output = list(output)
if params['action'] == 'write':
try:
# 调整以支持二进制数据写入
buff = params['content']
if type(buff) == list:
buff = bytes(buff)
file = open(filepath, 'ab' if params['append'] else 'wb')
else:
file = open(filepath, 'a' if params['append'] else 'w')
with open(filepath, params.get('mode')) as file:
buff = params['content']
output = file.write(buff)
finally:
if file:
file.close()
if params.get('mode').find('b') > -1:
buff = bytes(buff)
output = file.write(buff)
if params['action'] == 'exists':
output = os.path.exists(filepath)
if params['action'] == 'list':
file = open(filepath)
output = file.read()
with os.scandir(filepath) as entries:
output = [{
"name": it.name,
"path": os.path.join(filepath, it.name),
"is_dir": it.is_dir(),
"size": it.stat().st_size,
"atime": int(it.stat().st_atime),
"mtime": int(it.stat().st_mtime),
} for it in entries]
if params['action'] == 'remove':
if os.path.isfile(filepath):

View File

@ -53,13 +53,24 @@ class App extends Component {
</button>
<button
@click=${async function () {
let data = await native.fs.read('/code/gtk/webkit/demo.txt')
let data = await native.fs.read('/code/gtk/webkit/red.png', 'rb')
console.log(data)
this.content = data
// this.content = data
this.img = URL.createObjectURL(
new Blob([data], { type: 'image/png' })
)
}}
>
cat file
</button>
<button
@click=${async function () {
let data = await native.fs.list('/code/gtk/webkit/')
console.log(data)
}}
>
list dir
</button>
<button
@click=${async function () {
await native.fs.write(
@ -107,7 +118,7 @@ class App extends Component {
await native.fs.write(
'/code/gtk/webkit/demo.png',
Array.from(u8.slice(i, i + 2048)),
true
'wb'
)
}
}}