Skip to content

Commit

Permalink
New version: now loads + saves files.
Browse files Browse the repository at this point in the history
  • Loading branch information
tmontes committed Nov 8, 2020
1 parent 0c4a8bb commit 90489f8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/puppy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Puppy - A minimal GUI app to test pup.
"""

__version__ = '1.3.0'
__version__ = '1.4.0'

__title__ = 'puppy'
__description__ = 'Puppy, a minimal GUI app to test pup'
Expand Down
46 changes: 45 additions & 1 deletion src/puppy/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import sys
import tkinter
import tkinter.ttk
import tkinter.filedialog

import importlib_metadata as ilm
import importlib_resources as ilr
Expand Down Expand Up @@ -85,9 +86,15 @@ def _bottom_frame_widget(window, log_lines):
state='readonly',
)
serial_select.set('[select serial port]')
serial_select.pack(side='left')
serial_select.pack(side='left', padx=(10, 0))
window.after(1000, lambda: _populate_serial_ports(window, serial_select, log_lines))

load_button = tkinter.Button(frame, text='Load', padx=10, command=lambda: _load_file(log_lines))
load_button.pack(side='left', padx=(10, 0))

save_button = tkinter.Button(frame, text='Save', padx=10, command=lambda: _save_file(log_lines))
save_button.pack(side='left')

return frame


Expand Down Expand Up @@ -158,6 +165,43 @@ def _read_serial_port(window, log):



def _load_file(log_lines):

log = lambda s: log_lines([s])

filename = tkinter.filedialog.askopenfilename()
log(f'Opening {filename!r} in read mode...')
try:
with open(filename, 'rb') as f:
log(f'Reading 128 bytes...')
payload = f.read(128)
except OSError as e:
log(f'Failed: {e}.')
else:
log(f'Read {payload!r}.')
finally:
log('')


def _save_file(log_lines):

log = lambda s: log_lines([s])

filename = tkinter.filedialog.asksaveasfilename()
log(f'Opening {filename!r} in write mode...')
payload = b'puppy wrote this file!\n'
try:
with open(filename, 'wb') as f:
log(f'Writing payload...')
f.write(payload)
except OSError as e:
log(f'Failed: {e}.')
else:
log(f'Wrote {payload!r}.')
finally:
log('')


def _add_widgets(window):

_top_frame_widget(window).pack(
Expand Down

0 comments on commit 90489f8

Please sign in to comment.