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) return handler('quit', {}, null)
}, },
fs: { fs: {
read(filepath) { read(filepath, mode = 'r') {
return handler('fs', { action: 'read', filepath }) 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', { return handler('fs', {
action: 'write', action: 'write',
append: false,
filepath, filepath,
content, content
append })
},
append(filepath, content = '', mode = 'w') {
return handler('fs', {
action: 'write',
append: true,
filepath,
content
}) })
}, },
exists(filepath) { exists(filepath) {
return handler('fs', { action: 'exists', filepath }) return handler('fs', { action: 'exists', filepath })
}, },
list(filepath) {
return handler('fs', { action: 'list', filepath })
},
isfile(filepath) { isfile(filepath) {
return handler('fs', { action: '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') filepath = params.get('filepath')
if params['action'] == 'read': if params['action'] == 'read':
file = open(filepath) with open(filepath, params.get('mode')) as file:
output = file.read() output = file.read()
if params.get('mode').find('b') > -1:
output = list(output)
if params['action'] == 'write': if params['action'] == 'write':
try:
# 调整以支持二进制数据写入 # 调整以支持二进制数据写入
buff = params['content'] with open(filepath, params.get('mode')) as file:
if type(buff) == list: buff = params['content']
buff = bytes(buff)
file = open(filepath, 'ab' if params['append'] else 'wb')
else:
file = open(filepath, 'a' if params['append'] else 'w')
output = file.write(buff) if params.get('mode').find('b') > -1:
finally: buff = bytes(buff)
if file:
file.close() output = file.write(buff)
if params['action'] == 'exists': if params['action'] == 'exists':
output = os.path.exists(filepath) output = os.path.exists(filepath)
if params['action'] == 'list': if params['action'] == 'list':
file = open(filepath) with os.scandir(filepath) as entries:
output = file.read() 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 params['action'] == 'remove':
if os.path.isfile(filepath): if os.path.isfile(filepath):

View File

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