diff --git a/index.js b/index.js index caaa0ba..2f3b73d 100644 --- a/index.js +++ b/index.js @@ -323,12 +323,14 @@ const ugj_loadWorkspace = () => { const ugj_pyBeautify = (code) => { let formatted = ''; // // formatted = window.ocogeapi.child_process.spawnSync('python3', ['-m', 'black', '-'], { input: code }).stdout.toString(); - try { - formatted = require('child_process').spawnSync('python3', ['-m', 'black', '-'], { input: code }).stdout.toString(); - } catch (e) { - formatted = code; - console.log('Python formatter "Black" is not found.'); - } + // try { + // formatted = require('child_process').spawnSync('python3', ['-m', 'black', '-'], { input: code }).stdout.toString(); + // } catch (e) { + // formatted = code; + // console.log('Python formatter "Black" is not found.'); + // } + formatted = require('child_process').spawnSync('python3', ['-m', 'black', '-'], { input: code }).stdout.toString(); + if (!formatted) formatted = code; return formatted; } @@ -436,7 +438,7 @@ const ugj_runCode = async () => { // Pyboard ocoge const ugj_spawnPyboard = (code, mode) => { - let script_path = elutil.path.join(elutil.library_path, 'pyboard_ocoge.py'); + let script_path = elutil.path.join(elutil.library_path, 'pybtool.py'); let p = require('child_process').spawn('python3', [script_path, mode]); p.stdin.write(code); p.stdin.end(); diff --git a/index_elutil.js b/index_elutil.js index ca99929..63a63a3 100644 --- a/index_elutil.js +++ b/index_elutil.js @@ -314,6 +314,12 @@ class elUtil { } + // PyBfm を起動 + launchPyBfm() { + let script_path = this.path.join(elutil.library_path, 'pybfm.py'); + require('child_process').spawn('python3', [script_path]); + } + // ファイル名にアプリケーションのドキュメントルートまでのパスをつけて返す getDocPath(filename) { return this.path.join(this.appDocRoot, filename); diff --git a/lib/aaa.py b/lib/aaa.py deleted file mode 100644 index d251286..0000000 --- a/lib/aaa.py +++ /dev/null @@ -1,2 +0,0 @@ -# test -import test diff --git a/lib/main.py b/lib/main.py deleted file mode 100644 index cba2e6d..0000000 --- a/lib/main.py +++ /dev/null @@ -1,16 +0,0 @@ -from machine import Pin -from utime import sleep - -pin = None - - -pin = 25 -_pin = {} -_pin[pin] = Pin(pin, Pin.OUT) -for count2 in range(5): - for count in range(2): - _pin[pin].value(1) - sleep(0.1) - _pin[pin].value(0) - sleep(0.1) - sleep(0.6) diff --git a/lib/pybfm.py b/lib/pybfm.py new file mode 100644 index 0000000..f1c329f --- /dev/null +++ b/lib/pybfm.py @@ -0,0 +1,150 @@ +#!/usr/bin/python3 +# coding: utf-8 + +from tkinter import * +from tkinterdnd2 import * +import os +import sys +import pyboard +import serial.tools.list_ports +from tkinter import messagebox + +##### グローバル変数 ##### +gport = None + +##### イベントハンドラ ##### +# リストボックスにドロップ +def listbox_drop(event): + files = listbox.tk.splitlist(event.data) + for f in files: + if os.path.exists(f): + print('Dropped file: "%s"' % f) + if os.path.isfile(f): + putfile(f) + filelist() + else: + messagebox.showwarning('Warning', 'フォルダは転送できません') + else: + print('Not dropping file "%s": file does not exist.' % f) + +def input_key(ev): + # print(ev) + if ev.keysym == 'F5': + filelist() + elif ev.keysym == 'Delete': + selected = listbox.curselection() + for i in selected: + src = listbox.get(i) + call_pybfunc('fs_rm', src) + print('Deleted %s' % src) + if len(selected): + filelist() + +def on_closing(errmsg=''): + if errmsg: + messagebox.showerror('Error', errmsg) + root.destroy() + sys.exit(0) + +##### pyboard関数 ##### + +def connect(): + global gport + try: + pyb = pyboard.Pyboard(gport) + pyb.enter_raw_repl() + return pyb + except: + on_closing('デバイスに接続できません') + +def disconnect(pyb): + try: + pyb.exit_raw_repl() + pyb.close() + except: + on_closing('切断に失敗しました') + + +# コマンド実行 +def exec_command(cmd): + pyb = connect() + try: + retval = pyb.exec_(cmd) + except: + on_closing('コマンド実行時にエラーが発生しました') + disconnect(pyb) + return retval.decode('utf-8') + +# pyboard.py の関数をコール +def call_pybfunc(funcname, *args): + pyb = connect() + if funcname=='fs_put': + pyb.fs_put(args[0], args[1]) + elif funcname=='fs_rm': + pyb.fs_rm(args[0]) + + disconnect(pyb) + +def initialize(): + global gport + for p in sorted(serial.tools.list_ports.comports()): + if p.hwid.startswith('USB'): + gport = p.device + set_title(gport) + filelist() + break + else: + on_closing('デバイスがみつかりません') + +def list_clear(): + listbox.delete(0, END) + +def filelist(): + list_clear() + files = ls() + for f in files: + listbox.insert(END, f) + +def ls(src='/'): + cmd = ( + "import uos\nfor f in uos.listdir(%s):\n" + " print(f)" + % (("'%s'" % src) if src else "") + ) + retval = exec_command(cmd) + return retval.splitlines() + +def putfile(src): + dest = os.path.basename(src) + call_pybfunc('fs_put', src, dest) + +def set_title(title): + root.title('PyBfm - ' + title) + +##### メイン ##### + +# メインウィンドウの生成 +root = TkinterDnD.Tk() +root.protocol("WM_DELETE_WINDOW", on_closing) +root.title('PyBfm') +root.geometry('400x300') +# root.config(bg='#cccccc') +# Frameウィジェットの生成 +frame = Frame(root) +# Listboxウィジェットの生成 +listbox = Listbox(frame, selectmode=EXTENDED) +listbox.drop_target_register(DND_FILES) +listbox.dnd_bind('<>', listbox_drop) +listbox.bind("", input_key) +# スクロールバーの生成 +scroll = Scrollbar(frame, orient=VERTICAL) +listbox.configure(yscrollcommand=scroll.set) +scroll.config(command=listbox.yview) +# ウィジェットの配置 +frame.pack(expand=True,fill=BOTH) +listbox.pack(expand=True,fill=BOTH, side=LEFT) +scroll.pack(side=RIGHT, fill=Y) + +initialize() + +root.mainloop() \ No newline at end of file diff --git a/lib/pyboard_ocoge.py b/lib/pybtool.py similarity index 100% rename from lib/pyboard_ocoge.py rename to lib/pybtool.py diff --git a/main.js b/main.js index 5320e5d..34f54ad 100644 --- a/main.js +++ b/main.js @@ -217,6 +217,17 @@ let template = [ } ] }, + { + label: "Tools", + submenu: [ + { + label: "Launch PyBfm", + click: (item, focusedWindow) => { + focusedWindow.webContents.executeJavaScript('elutil.launchPyBfm()'); + }, + }, + ] + } ] const menu = Menu.buildFromTemplate(template) Menu.setApplicationMenu(menu)