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-07-31 17:09:27 +08:00
parent 018c18a1ed
commit 98023484cf
4 changed files with 106 additions and 14 deletions

3
.gitignore vendored
View File

@ -1 +1,2 @@
__pycache__ __pycache__
*.txt

View File

@ -186,7 +186,35 @@ Object.assign(native, {
return handler('quit', {}, null) return handler('quit', {}, null)
}, },
fs: { fs: {
read(filepath) {} read(filepath) {
return handler('fs', { action: 'read', filepath })
},
write(filepath, content = '', append = false) {
return handler('fs', {
action: 'write',
filepath,
content,
append
})
},
exists(filepath) {
return handler('fs', { action: 'exists', filepath })
},
isfile(filepath) {
return handler('fs', { action: 'isfile', filepath })
},
isdir(filepath) {
return handler('fs', { action: 'isdir', filepath })
},
remove(filepath) {
return handler('fs', { action: 'remove', filepath })
},
rename(filepath, target) {
return handler('fs', { action: 'rename', filepath, target })
},
copy(filepath, target) {
return handler('fs', { action: 'copy', filepath, target })
}
}, },
image(filepath) { image(filepath) {
return handler('image', { value: filepath }).then(r => new NativeImage(r)) return handler('image', { value: filepath }).then(r => new NativeImage(r))

53
main.py
View File

@ -1,6 +1,6 @@
#!/usr/bin/python3 #!/usr/bin/python3
import gi, json, os import gi, json, os, shutil
gi.require_version("Gtk", "3.0") gi.require_version("Gtk", "3.0")
@ -173,7 +173,48 @@ class WebKitWindow(Gtk.Window):
match event: match event:
case 'fs': case 'fs':
pass filepath = params['filepath']
if params['action'] == 'read':
file = open(filepath)
output = file.read()
if params['action'] == 'write':
try:
file = open(filepath, 'a' if params['append'] else 'w')
output = file.write(params['content'])
finally:
if file:
file.close()
if params['action'] == 'exists':
output = os.path.exists(filepath)
if params['action'] == 'list':
file = open(filepath)
output = file.read()
if params['action'] == 'remove':
if os.path.isfile(filepath):
output = os.remove(filepath)
elif os.path.isdir(filepath):
output = os.removedirs(filename)
if params['action'] == 'rename':
if os.path.exists(filepath):
output = shutil.move(filepath, params['target'])
if params['action'] == 'copy':
if os.path.exists(filepath):
output = shutil.copy2(filepath, params['target'])
if params['action'] == 'isfile':
output = os.path.isfile(filepath)
if params['action'] == 'isdir':
output = os.path.isdir(filepath)
case 'clipboard': case 'clipboard':
# 读文本 # 读文本
@ -304,14 +345,6 @@ class WebKitWindow(Gtk.Window):
output = self.is_visible() output = self.is_visible()
case _:
output = {"foo": 123, "bar": (11,22,33)}
# 有回调则返回结果 # 有回调则返回结果
if callback : if callback :
self.call_js(callback, output) self.call_js(callback, output)

View File

@ -10,7 +10,8 @@ import { html, css, Component } from 'wkit'
class App extends Component { class App extends Component {
static props = { static props = {
input: '', input: '',
img: '' img: '',
content: ''
} }
static styles = css` static styles = css`
@ -28,7 +29,7 @@ class App extends Component {
<a target="_blank" href="about:blank">打开控制台</a> <a target="_blank" href="about:blank">打开控制台</a>
<div class="output">loading...</div> <div class="output">${this.content || 'loading...'}</div>
<input value=${this.input} /> <input value=${this.input} />
@ -50,6 +51,35 @@ class App extends Component {
> >
unregisterall unregisterall
</button> </button>
<button
@click=${async function () {
let data = await native.fs.read('/code/gtk/webkit/demo.txt')
console.log(data)
this.content = data
}}
>
cat file
</button>
<button
@click=${async function () {
await native.fs.write(
'/code/gtk/webkit/demo.txt',
'hello world 222\n'
)
}}
>
echo >> file
</button>
<button
@click=${async function () {
await native.fs.copy(
'/code/gtk/webkit/demo3.txt',
'/code/gtk/webkit/demo2.txt'
)
}}
>
rename
</button>
<img style="width:100px;border:1px solid #09f;" src=${this.img} /> <img style="width:100px;border:1px solid #09f;" src=${this.img} />
<textarea @paste=${this.pasteImg}></textarea> <textarea @paste=${this.pasteImg}></textarea>
` `