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

Refactor printcore.py #1346

Merged
merged 11 commits into from
Dec 25, 2023
Merged

Conversation

rockstorm101
Copy link
Collaborator

Split printcore.py into two. All the functionality related to directly communicating with serial ports or TCP sockets is moved into device.py. This way this logic is abstracted from printcore.py, which is now independent of the underlying connection type.

This reduces the complexity of printcore.py and increases code modularity. Plus it has the advantage of making it simple to add more connection types in the future (if any).

Notes:

  • This refactored code passes all tests on test_printcore.py (with a very slight adjustment), so (in theory) merging this PR this does not change/break current API.
  • I've tested it on my printer through a serial connection and works fine so far, however, I could not test the TCP connection. The goal has been to keep the code logic the same so expectation is no regression issues occur.
  • I've named it device.py and Device as the main class but I'm open to suggestions since I'm not entirely happy with the naming.

As always, any comments/suggestions are appreciated and I'll be happy to address them.

Split `printcore.py` into two. All the functionality related to
directly communicating with serial ports or TCP sockets is moved into
`device.py`. This way this logic is abstracted from `printcore.py`,
which is now independent of the underlying connection type.

This reduces the complexity of `printcore.py` and increases code
modularity. Plus it has the advantage of making it simple to add more
connection types in the future (if any).
Serial.readline() is blocking in itself, but since Serial was opened
with timeout=0.25 it won't block anyway. Plus it already returns b''
when timed out. The use of Serial.in_waiting was counterproductive
here.
@volconst
Copy link
Collaborator

  • I could not test the TCP connection.

Try to connect to https://github.com/kliment/Printrun/blob/master/testtools/mock-printer.py

@rockstorm101
Copy link
Collaborator Author

Try to connect to https://github.com/kliment/Printrun/blob/master/testtools/mock-printer.py

Of course, thanks! I forgot about that. It communicates without issues with that mock-up on my machine, yey! \o/

 * connect function no longer accepts a `dtr` argument. Is is now an
   instance-wide configurable option called `force_dtr`.
 * Added `parity_workaround` option to instance to allow switching it on
   and off.
 * Device.connect does no longer accept a "dtr" argument

 * When `parity_workaround` is enabled, Serial.open and Serial.close are
   called twice on each connection attempt instead of just once.
   Therefore the previous test code failed on platforms where
   `parity_workaround` is enabled by default.
@rockstorm101 rockstorm101 merged commit 4f0a15d into kliment:master Dec 25, 2023
21 checks passed
@rockstorm101 rockstorm101 deleted the pr-refactor-printcore branch January 5, 2024 10:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants