Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add circuitpython run support #485

Open
wants to merge 67 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
d997173
add get_view/get_pathname support
fmorton Apr 20, 2018
4407b0b
add circuit python "run" support
fmorton Jun 14, 2018
7f8b076
Merge branch 'master' of git://github.com/mu-editor/mu
fmorton Jun 14, 2018
9d276dc
add actions_dynamic support
fmorton Jun 14, 2018
acb6d31
Merge branch 'master' of git://github.com/mu-editor/mu
fmorton Jun 14, 2018
9308548
add adafruit run support
fmorton Jun 14, 2018
ff4ff33
Merge branch 'master' of git://github.com/mu-editor/mu
fmorton Jun 20, 2018
bd3f476
Merge branch 'master' of git://github.com/mu-editor/mu
fmorton Jun 20, 2018
c1a6c93
add adafruit run support without actions_dynamic support
fmorton Jun 20, 2018
17d9b25
add adafruit "run" admin support
fmorton Jun 25, 2018
1af1117
fix typo on test_adafruit.py
fmorton Jun 26, 2018
f7bae4c
add adafruit "run" admin support/fix test
fmorton Jun 26, 2018
fe4de5d
add adafruit "run" admin support/fix test
fmorton Jun 26, 2018
ef4ee5a
add adafruit run support
fmorton Sep 20, 2018
e143189
Merge branch 'master' of git://github.com/mu-editor/mu
fmorton Sep 20, 2018
9ee45c9
add adafruit run support
fmorton Sep 20, 2018
a9c8856
add adafruit run support
fmorton Sep 20, 2018
63d2d2e
show/hide adafruit "run" button after admin changes
fmorton Sep 20, 2018
4eb37a4
Merge branch 'master' of git://github.com/mu-editor/mu
fmorton Oct 4, 2018
639e08a
add adafruit library support
fmorton Oct 16, 2018
5ade290
Merge branch 'master' of git://github.com/mu-editor/mu
fmorton Oct 18, 2018
ba220e5
add run_adafruit_lib_copy support
fmorton Oct 18, 2018
020e4a9
add run_adafruit_lib_copy support
fmorton Nov 3, 2018
b0e0e70
add adafruit copy lib source file support
fmorton Dec 11, 2018
96cda2a
shorten one line for pylint
fmorton Dec 11, 2018
97dd534
catch up from june 2018
fmorton Nov 14, 2019
dd3646d
catch up from june 2018
fmorton Nov 14, 2019
093a606
Merge branch 'master' of git://github.com/mu-editor/mu
fmorton Nov 14, 2019
f1f0ca1
add circuitpython dialog
fmorton Nov 14, 2019
7273caa
add circuitpython dialog
fmorton Nov 14, 2019
ad18689
catch up from june 2018--needs more test coverage
fmorton Nov 14, 2019
1458d12
improve circuitpython test coverage
fmorton Nov 14, 2019
4523de0
improve test coverage
fmorton Nov 14, 2019
a7e23a4
improve test coverage
fmorton Nov 14, 2019
5c6056d
modify comment
fmorton Feb 4, 2020
50354f2
Merge branch 'master' of git://github.com/mu-editor/mu
fmorton Feb 9, 2020
56df957
merge recent changes
fmorton Sep 8, 2020
ac070bd
guess at test_get_pathname
fmorton Sep 8, 2020
52563ca
catch up to 1.1.0.beta.1
fmorton Feb 11, 2021
fbe976b
simplify circuitpython run button/remove dialog support
fmorton Mar 19, 2021
0cd3cf7
shorten two long lines for make check
fmorton Mar 19, 2021
21cfb10
simplify circuitpython/remove unused methods
fmorton Mar 19, 2021
520a949
Merge branch 'master' of git://github.com/mu-editor/mu
fmorton Mar 22, 2021
4e51068
Merge branch 'master' of git://github.com/mu-editor/mu
fmorton Mar 22, 2021
d478fb3
Merge branch 'master' of git://github.com/mu-editor/mu
fmorton Mar 23, 2021
2d8ef9e
Merge branch 'master' of git://github.com/mu-editor/mu
fmorton Mar 24, 2021
a73a939
Merge branch 'master' of git://github.com/mu-editor/mu
fmorton Mar 29, 2021
936bd30
Merge branch 'master' of git://github.com/mu-editor/mu
fmorton Apr 10, 2021
26eeb14
merge most recent changes
fmorton Apr 19, 2021
11bb103
Merge branch 'master' of git://github.com/mu-editor/mu
fmorton Apr 30, 2021
c89e6e8
Merge branch 'master' of git://github.com/mu-editor/mu
fmorton Jun 11, 2021
77c7b13
Merge branch 'master' of git://github.com/mu-editor/mu
fmorton Jun 22, 2021
f6bc81d
Merge branch 'master' of git://github.com/mu-editor/mu
fmorton Jul 22, 2021
d1548b6
Merge branch 'master' of git://github.com/mu-editor/mu
fmorton Sep 13, 2021
c86c32e
Merge branch 'master' of git://github.com/mu-editor/mu
fmorton Sep 22, 2021
a5a38b3
Merge branch 'master' of git://github.com/mu-editor/mu
fmorton Oct 5, 2021
a359c2c
Merge branch 'master' of git://github.com/mu-editor/mu
fmorton Nov 7, 2021
102df87
Merge branch 'master' of git://github.com/mu-editor/mu
fmorton Dec 28, 2021
160b825
Merge branch 'master' of git://github.com/mu-editor/mu
fmorton Jan 3, 2022
cdc0e35
Merge branch 'master' of git://github.com/mu-editor/mu
fmorton Feb 14, 2022
06a8313
Merge branch 'master' of git://github.com/mu-editor/mu
fmorton Feb 18, 2022
b92c0e8
Merge branch 'master' of git://github.com/mu-editor/mu
fmorton Feb 28, 2022
f7d99d3
Merge branch 'master' of https://github.com/mu-editor/mu
fmorton Sep 2, 2022
b4eb0d8
Merge branch 'master' of https://github.com/mu-editor/mu
fmorton Nov 22, 2022
9536874
Merge branch 'master' of https://github.com/mu-editor/mu
fmorton May 16, 2023
71f37d0
Merge branch 'master' of https://github.com/mu-editor/mu
fmorton Dec 27, 2023
e9ca77b
Merge branch 'master' of https://github.com/mu-editor/mu
fmorton Feb 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions mu/logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,13 @@ def read_and_decode(filepath):
return text, newline


def get_pathname(self):
"""
Returns the pathname of the currently edited file
"""
return self.view.current_tab.path


def extract_envars(raw):
"""
Returns a list of environment variables given a string containing
Expand Down
84 changes: 78 additions & 6 deletions mu/modes/circuitpython.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import os
import time
import ctypes
import logging
from shutil import copyfile
from subprocess import check_output
from mu.modes.base import MicroPythonMode
from mu.modes.api import ADAFRUIT_APIS, SHARED_APIS
from mu.interface.panes import CHARTS
from mu.logic import Device
from mu.logic import get_pathname
from adafruit_board_toolkit import circuitpython_serial

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -137,13 +140,22 @@ def actions(self):
is a name (also used to identify the icon) , description, and handler.
"""
buttons = [
{
"name": "run",
"display_name": _("Run"),
"description": _(
"Save and run your current file on CIRCUITPY."
),
"handler": self.run,
"shortcut": "CTRL+Shift+R",
},
{
"name": "serial",
"display_name": _("Serial"),
"description": _("Open a serial connection to your device."),
"handler": self.toggle_repl,
"shortcut": "CTRL+Shift+U",
}
},
]
if CHARTS:
buttons.append(
Expand Down Expand Up @@ -246,11 +258,9 @@ def get_volume_name(disk_name):
try:
for disk in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
path = "{}:\\".format(disk)
if (
os.path.exists(path)
and get_volume_name(path) == "CIRCUITPY"
):
return path
if os.path.exists(path):
if get_volume_name(path) == "CIRCUITPY":
return path
finally:
ctypes.windll.kernel32.SetErrorMode(old_mode)
else:
Expand Down Expand Up @@ -279,6 +289,68 @@ def get_volume_name(disk_name):
self.connected = False
return wd

def workspace_dir_cp(self):
"""
Is the file currently being edited located on CIRCUITPY.
"""
return "CIRCUITPY" in str(get_pathname(self))

def workspace_cp_avail(self):
"""
Is CIRCUITPY available.
"""
return "CIRCUITPY" in str(self.workspace_dir())

def run_circuitpython_lib_copy(self, pathname, dst_dir):
"""
Optionally copy lib files to CIRCUITPY.
"""
lib_dir = os.path.dirname(pathname) + "/lib"
if not os.path.isdir(lib_dir):
return
replace_cnt = 0
for root, dirs, files in os.walk(lib_dir):
for filename in files:
src_lib = lib_dir + "/" + filename
dst_lib_dir = dst_dir + "/lib"
dst_lib = dst_lib_dir + "/" + filename
if not os.path.exists(dst_lib):
replace_lib = True
else:
src_tm = time.ctime(os.path.getmtime(src_lib))
dst_tm = time.ctime(os.path.getmtime(dst_lib))
replace_lib = src_tm > dst_tm
if replace_lib:
if replace_cnt == 0:
if not os.path.exists(dst_lib_dir):
os.makedirs(dst_lib_dir)
copyfile(src_lib, dst_lib)
replace_cnt = replace_cnt + 1
# let libraries load before copying source main source file
if replace_cnt > 0:
time.sleep(4)

def run(self, event):
"""
Save the file and copy to CIRCUITPY if not already there and available.
"""
self.editor.save()

if not self.workspace_dir_cp() and self.workspace_cp_avail():
pathname = get_pathname(self)
if pathname:
dst_dir = self.workspace_dir()
if pathname.find("/lib/") == -1:
dst = dst_dir + "/code.py"
else:
dst = dst_dir + "/lib/" + os.path.basename(pathname)

# copy library files on to device if not working on the device
# self.run_circuitpython_lib_copy(pathname, dst_dir)

# copy edited source file on to device
copyfile(pathname, dst)

def compatible_board(self, port):
"""Use adafruit_board_toolkit to find out whether a board is running
CircuitPython. The toolkit sees if the CDC Interface name is appropriate.
Expand Down
20 changes: 12 additions & 8 deletions tests/modes/test_circuitpython.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ def test_circuitpython_mode():

with mock.patch("mu.modes.circuitpython.CHARTS", True):
actions = am.actions()
assert len(actions) == 2
assert actions[0]["name"] == "serial"
assert actions[0]["handler"] == am.toggle_repl
assert actions[1]["name"] == "plotter"
assert actions[1]["handler"] == am.toggle_plotter
assert len(actions) == 3
assert actions[0]["name"] == "run"
assert actions[0]["handler"] == am.run
assert actions[1]["name"] == "serial"
assert actions[1]["handler"] == am.toggle_repl
assert actions[2]["name"] == "plotter"
assert actions[2]["handler"] == am.toggle_plotter
assert "code" not in am.module_names


Expand All @@ -41,9 +43,11 @@ def test_circuitpython_mode_no_charts():
am = CircuitPythonMode(editor, view)
with mock.patch("mu.modes.circuitpython.CHARTS", False):
actions = am.actions()
assert len(actions) == 1
assert actions[0]["name"] == "serial"
assert actions[0]["handler"] == am.toggle_repl
assert len(actions) == 2
assert actions[0]["name"] == "run"
assert actions[0]["handler"] == am.run
assert actions[1]["name"] == "serial"
assert actions[1]["handler"] == am.toggle_repl


def test_workspace_dir_posix_exists():
Expand Down
7 changes: 7 additions & 0 deletions tests/test_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,13 @@
mock_func.assert_called_once_with("settings.json")


def test_get_pathname():
mock_func = mock.MagicMock(return_value="CIRCUITPY")
with mock.patch("mu.logic.get_pathname", mock_func):
assert mu.logic.get_pathname() == "CIRCUITPY"

Check failure

Code scanning / CodeQL

Wrong number of arguments in a call Error test

Call to
function get_pathname
with too few arguments; should be no fewer than 1.
mock_func.assert_called_once_with()


def test_extract_envars():
"""
Given a correct textual representation, get the expected list
Expand Down
Loading