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

Python 3 compatiblity #887

Merged
merged 38 commits into from
Feb 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
fcb3027
Python 3: Fix except syntax
hroncok Feb 11, 2018
f7e45ec
Python 3: Fix the print() function
hroncok Feb 11, 2018
8137882
Python 3: gettext.install() has no more unicode kwarg
hroncok Feb 11, 2018
dc1119d
Python 3: No more tuple unpacking in parameter list
hroncok Feb 11, 2018
44ccbb6
Python 3: Explicit relative imports
hroncok Feb 11, 2018
e042de2
Python 3: Standard library moves
hroncok Feb 11, 2018
879614f
Python 3: No more sys.setdefaultencoding()
hroncok Feb 11, 2018
9bd0a9c
Python 3: map() doesn't return list
hroncok Feb 11, 2018
be7a16b
Python 3: Replace filter with comprehensions
hroncok Feb 12, 2018
1a10d2c
Python 3: Change unicode to str (type name)
hroncok Feb 12, 2018
3f140e4
Python 3: Fix binary STL export bytes/str mishmash
hroncok Feb 12, 2018
b7ce497
Python 3: Fix exec (statement -> function)
hroncok Feb 12, 2018
a56ad6e
Python 3: Use spaces only, no tabs
hroncok Feb 12, 2018
b8ece2b
Python 3: Use // division where needed (incomplete)
hroncok Feb 12, 2018
7222e83
Python 3: Fix xrange and range
hroncok Feb 12, 2018
23ccf76
Python 3: Wrap zip() in list() where necessary
hroncok Feb 12, 2018
2886b92
Python 3: Convert itertools.izip to zip
hroncok Feb 12, 2018
fed5529
Python 3: raw_input -> input
hroncok Feb 12, 2018
d183745
Python 3: next() is a function
hroncok Feb 12, 2018
ea1c838
Python 3: Fix Pronterface logging
hroncok Feb 12, 2018
65ec268
Python 3: Dict iterators fix
hroncok Feb 12, 2018
96f1c16
Python 3: Remove execfile
hroncok Feb 12, 2018
fe4e653
Use isinstance() for type checking
hroncok Feb 12, 2018
25d3541
Python 3: Import reduce
hroncok Feb 12, 2018
5d6b68d
Use set literal where possible instead of set([...])
hroncok Feb 12, 2018
eae3657
Python 3: Remove u from unicode literals (u"...")
hroncok Feb 12, 2018
6e08077
Python 3: With subprocess, always get strs
hroncok Feb 12, 2018
20d3cd3
Python 3: Get rid of explicit object baseclass
hroncok Feb 12, 2018
32e2331
Python 3: Return strs (not bytes) from our cythonized module
hroncok Feb 12, 2018
b2d83e4
Python 3: When talking to the printer, decode early, encode late
hroncok Feb 12, 2018
7161b0d
Python 3: Bytes cannot be decoded with bad baudrate
hroncok Feb 12, 2018
06e3af4
Python 3: Fix shebangs
hroncok Feb 12, 2018
690ecba
Python 3: Fix Python version conditional
hroncok Feb 12, 2018
da6f421
Python 3: Ignore stuff
hroncok Feb 12, 2018
97850dc
Python 3: Get rid of (str, str) checks
hroncok Feb 12, 2018
bd01684
Python 3: Update requirements.txt
hroncok Feb 12, 2018
dab4d94
Python 3: Update README
hroncok Feb 12, 2018
f2943c5
Python 3: Add Python 3 Only classifier
hroncok Feb 12, 2018
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
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ uploads
.vagrant
prontserve-env
printrun/gcoder_line.c
printrun/gcoder_line.so
printrun/gcoder_line*.so
.project
.pydevproject

/build/
/venv/
137 changes: 52 additions & 85 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,16 @@ Note for OSX users: if OSX tells you the file is corrupted, you don't need to re
## Linux
### Ubuntu/Debian

You can install Printrun from official packages (*):
You can install Printrun from official packages (\*):
```
sudo apt install printrun
```

Or you can run Printrun directly from source.

1. Fetch and install the dependencies using (**):
```
sudo apt-get install python-serial python-wxgtk2.8 python-pyglet python-numpy cython python-libxml2 python-gobject python-dbus python-psutil python-cairosvg libpython-dev git
```

2. Clone the repository
```
git clone https://github.com/kliment/Printrun.git
```

3. You can start using Printrun from the Printrun directory created by the git clone command.

(*) Please be aware that the Printrun package available in Ubuntu Yakkety Yak, or later, and Debian Stretch/Sid repositories, is known to be affected by issue [#615](https://github.com/kliment/Printrun/issues/615).

(**) Users of Ubuntu Xenial Xerus or later and Debian Jessie or later won't find the package "python-wxgtk2.8" available, having been replaced by "python-wxgtk3.0". Running Printrun with "python-wxgtk3.0" instead, is possible but it is known to be affected by issue [#615](https://github.com/kliment/Printrun/issues/615).
(\*) Please be aware that the Printrun package available in Ubuntu Yakkety Yak, or later, and Debian Stretch/Sid repositories, is known to be affected by issue [#615](https://github.com/kliment/Printrun/issues/615).

### Chrome OS

You can use Printrun via crouton ( https://github.com/dnschneid/crouton ). Assuming you want Ubuntu Trusty, you used probably `sudo sh -e ~/Downloads/crouton -r trusty -t xfce` to install Ubuntu. Fetch and install dependencies with the line given above for Ubuntu/Debian, and obtain the source via git clone.
You can use Printrun via crouton ( https://github.com/dnschneid/crouton ). Assuming you want Ubuntu Trusty, you used probably `sudo sh -e ~/Downloads/crouton -r trusty -t xfce` to install Ubuntu. Fetch and install printrun with the line given above for Ubuntu/Debian.

By default you have no access to the serial port under Chrome OS crouton, so you cannot connect to your 3D printer. Add yourself to the serial group within the linux environment to fix this

Expand All @@ -70,11 +54,6 @@ Or get only apps you need by

Adding `--enablerepo updates-testing` option to `dnf` might sometimes give you newer packages (but also not very tested).

You can also run Printrun directly from source, if the packages are too old for you. Fetch and install the dependencies using

1. `sudo dnf install pyserial wxPython python-pyglet python-cairosvg`

Optional: `sudo dnf install simarrange`

### Archlinux

Expand All @@ -92,26 +71,43 @@ Run Printrun for source if you want to test out the latest features.

To use pronterface, you need:

* python (ideally 2.6.x or 2.7.x),
* pyserial (or python-serial on ubuntu/debian)
* pyreadline (not needed on Linux) and
* argparse (installed by default with python >= 2.7)
* wxPython (some features such as Tabbed mode work better with wx 2.9)
* Python 3 (ideally 3.6),
* pyserial (or python3-serial on ubuntu/debian)
* pyreadline (not needed on Linux)
* wxPython 4
* pyglet
* numpy (for 3D view)
* pycairo (to use Projector feature)
* cairosvg (to use Projector feature)
* dbus (to inhibit sleep on some Linux systems)
* dbus (to inhibit sleep on some Linux systems)

### Use Python virtual environment

Easiest way to run Printrun from source is to create and use a Python [virtual environment](https://docs.python.org/3/tutorial/venv.html).
The following section assumes Linux. Please see specific instructions for Windows and macOS X below.

Please see specific instructions for Windows and Mac OS X below. Under Linux, you should use your package manager directly (see the "GETTING PRINTRUN" section), or pip:
**Ubuntu/Debian note:** You might need to install `python3-venv` first.

```pip install -r requirements.txt```
**Note:** wxPython4 doesn't have Linux wheels available from the Python Package Index yet. Find a proper wheel for your distro at [extras.wxpython.org](https://extras.wxpython.org/wxPython4/extras/linux/gtk3/) and substitute the link in the bellow example. You might skip the wheel installation, but that results in compiling wxPython4 from source, which can be time and resource consuming and might fail.


```console
$ git clone https://github.com/kliment/Printrun.git # clone the repository
$ python3 -m venv venv # create an virtual environment
$ . venv/bin/activate # activate the virtual environment (notice the space after the dot)
(venv) $ python -m pip install https://extras.wxpython.org/wxPython4/extras/linux/gtk3/fedora-27/wxPython-4.0.1-cp36-cp36m-linux_x86_64.whl # replace the link with yours
(venv) $ python -m pip install -r requirements.txt # intall the rest of dependencies
(venv) $ python pronterface.py # run Pronterface
```

### Cython-based G-Code parser

Printrun default G-Code parser is quite memory hungry, but we also provide a much lighter one which just needs an extra build-time dependency (Cython), plus compiling the extension with:

python setup.py build_ext --inplace
```console
(venv) $ python -m pip install Cython
(venv) $ python setup.py build_ext --inplace
```

The warning message

Expand All @@ -121,54 +117,25 @@ means that this optimized G-Code parser hasn't been compiled. To get rid of it a

### Windows

Download the following, and install in this order:

1. http://python.org/ftp/python/2.7.2/python-2.7.2.msi
2. http://pypi.python.org/packages/any/p/pyserial/pyserial-2.5.win32.exe
3. http://downloads.sourceforge.net/wxpython/wxPython2.8-win32-unicode-2.8.12.0-py27.exe
4. https://pypi.python.org/packages/any/p/pyreadline/pyreadline-1.7.1.win32.exe
5. http://pyglet.googlecode.com/files/pyglet-1.1.4.zip

For the last one, you will need to unpack it, open a command terminal,
go into the the directory you unpacked it in and run
`python setup.py install`

### Mac OS X Lion
Download and install [Python 3.6](https://www.python.org/downloads/) and follow the **Python virtual environment** section above except use the following to create and activate the virtual environment and install dependencies:

1. Ensure that the active Python is the system version. (`brew uninstall python` or other appropriate incantations)
2. Download an install [wxPython2.8-osx-unicode] matching to your python version (most likely 2.7 on Lion,
check with: python --version) from: http://wxpython.org/download.php#stable
Known to work PythonWX: http://superb-sea2.dl.sourceforge.net/project/wxpython/wxPython/2.8.12.1/wxPython2.8-osx-unicode-2.8.12.1-universal-py2.7.dmg
3. Download and unpack pyserial from http://pypi.python.org/packages/source/p/pyserial/pyserial-2.5.tar.gz
4. In a terminal, change to the folder you unzipped to, then type in: `sudo python setup.py install`
5. Repeat 4. with http://http://pyglet.googlecode.com/files/pyglet-1.1.4.zip

The tools will probably run just fine in 64bit on Lion, you don't need to mess
with any of the 32bit settings. In case they don't, try
5. export VERSIONER_PYTHON_PREFER_32_BIT=yes
in a terminal before running Pronterface

### Mac OS X (pre Lion)

A precompiled version is available at http://koti.kapsi.fi/~kliment/printrun/
```cmd
> py -3 -m venv venv
> venv\Scripts\activate
> python -m pip install -r requirements.txt
```

1. Download and install http://downloads.sourceforge.net/wxpython/wxPython2.8-osx-unicode-2.8.12.0-universal-py2.6.dmg
2. Grab the source for pyserial from http://pypi.python.org/packages/source/p/pyserial/pyserial-2.5.tar.gz
3. Unzip pyserial to a folder. Then, in a terminal, change to the folder you unzipped to, then type in:

`defaults write com.apple.versioner.python Prefer-32-Bit -bool yes`

`sudo python setup.py install`

Alternatively, you can run python in 32 bit mode by setting the following environment variable before running the setup.py command:
### macOS X

This alternative approach is confirmed to work on Mac OS X 10.6.8.
Install Python 3, you can use Brew:

`export VERSIONER_PYTHON_PREFER_32_BIT=yes`
```console
$ brew install python3
```

`sudo python setup.py install`
And follow the above **Python virtual environment** section. You don't need to search for wxPython wheel, macOS wheels are available from the Python Package Index.

Then repeat the same with http://http://pyglet.googlecode.com/files/pyglet-1.1.4.zip

# USING PRINTRUN

Expand All @@ -192,8 +159,8 @@ If the Slic3r integration option (_Settings_ > _Options_ > _User interface_ > _E

To use pronsole, you need:

* python (ideally 2.6.x or 2.7.x),
* pyserial (or python-serial on ubuntu/debian) and
* Python 3 (ideally 3.6),
* pyserial (or python3-serial on ubuntu/debian) and
* pyreadline (not needed on Linux)

Start pronsole and you will be greeted with a command prompt. Type help to view the available commands.
Expand All @@ -204,7 +171,7 @@ The "skeinforge" folder must be in the same folder as pronsole.py

## USING PRINTCORE

To use printcore you need python (ideally 2.6.x or 2.7.x) and pyserial (or python-serial on ubuntu/debian)
To use printcore you need Python 3 (ideally 3.6) and pyserial (or python3-serial on ubuntu/debian)
See pronsole for an example of a full-featured host, the bottom of printcore.py for a simple command-line
sender, or the following code example:

Expand Down Expand Up @@ -249,10 +216,10 @@ on localhost port 7978, which provides print progress information.
Here is a sample Python script querying the print status:

```python
import xmlrpclib
import xmlrpc.client

rpc = xmlrpclib.ServerProxy('http://localhost:7978')
print rpc.status()
rpc = xmlrpc.client.ServerProxy('http://localhost:7978')
print(rpc.status())
```

## CONFIGURATION
Expand Down Expand Up @@ -342,7 +309,7 @@ For more powerful macro programming, it is possible to use python code escaping
Note that this python code invocation also works in interactive prompt:

```python
PC> !print "Hello, printer!"
PC> !print("Hello, printer!")
Hello printer!

PC> macro debug_on !self.p.loud = 1
Expand All @@ -363,14 +330,14 @@ Example: swapping two macros to implement toggle:
PC> macro toggle_debug_on
Enter macro using indented lines, end with empty line
..> !self.p.loud = 1
..> !print "Diagnostic information ON"
..> !print("Diagnostic information ON")
..> macro toggle_debug toggle_debug_off
..>
Macro 'toggle_debug_on' defined
PC> macro toggle_debug_off
Enter macro using indented lines, end with empty line
..> !self.p.loud = 0
..> !print "Diagnostic information OFF"
..> !print("Diagnostic information OFF")
..> macro toggle_debug toggle_debug_on
..>
Macro 'toggle_debug_off' defined
Expand All @@ -395,10 +362,10 @@ For example, following macro toggles the diagnostic information similarily to th
```python
!if self.p.loud:
!self.p.loud = 0
!print "Diagnostic information OFF"
!print("Diagnostic information OFF")
!else:
!self.p.loud = 1
!print "Diagnostic information ON"
!print("Diagnostic information ON")
```

Macro parameters are available in '!'-escaped python code as locally defined list variable: arg[0] arg[1] ... arg[N]
Expand Down
44 changes: 22 additions & 22 deletions calibrateextruder.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
# This file is part of the Printrun suite.
#
# Printrun is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -41,7 +41,7 @@
def float_input(prompt=''):
f = None
while f is None:
s = raw_input(prompt)
s = input(prompt)
try:
f = float(s)
except ValueError:
Expand All @@ -56,7 +56,7 @@ def wait(t, m=''):
sys.stdout.write(s)
sys.stdout.flush()
time.sleep(1.0 / 5)
print
print()
def w(s):
sys.stdout.write(s)
sys.stdout.flush()
Expand All @@ -75,9 +75,9 @@ def heatup(p, temp, s = 0):
time.sleep(1.5)
f = True
curtemp = gettemp(p)
if curtemp: w(u"\rHeating extruder up.. %3d \xb0C" % curtemp)
if s: print
else: print "\nReady."
if curtemp: w("\rHeating extruder up.. %3d \xb0C" % curtemp)
if s: print()
else: print("\nReady.")

def gettemp(p):
try: p.logl
Expand All @@ -89,14 +89,14 @@ def gettemp(p):
if 'T:' in line:
try:
setattr(p, 'temp', int(line.split('T:')[1].split()[0]))
except: print line
except: print(line)
p.logl = len(p.log)
return p.temp
if not os.path.exists(port):
port = 0

# Parse options
help = u"""
help = """
%s [ -l DISTANCE ] [ -s STEPS ] [ -t TEMP ] [ -p PORT ]
-l --length Length of filament to extrude for each calibration step (default: %d mm)
-s --steps Initial amount of steps to use (default: %d steps)
Expand All @@ -106,13 +106,13 @@ def gettemp(p):
"""[1:-1].encode('utf-8') % (sys.argv[0], n, k, temp, tempmax, port if port else 'auto')
try:
opts, args = getopt.getopt(sys.argv[1:], "hl:s:t:p:", ["help", "length=", "steps=", "temp=", "port="])
except getopt.GetoptError, err:
print str(err)
print help
except getopt.GetoptError as err:
print(str(err))
print(help)
sys.exit(2)
for o, a in opts:
if o in ('-h', '--help'):
print help
print(help)
sys.exit()
elif o in ('-l', '--length'):
n = float(a)
Expand All @@ -121,17 +121,17 @@ def gettemp(p):
elif o in ('-t', '--temp'):
temp = int(a)
if temp >= tempmax:
print (u'%d \xb0C? Are you insane?'.encode('utf-8') % temp) + (" That's over nine thousand!" if temp > 9000 else '')
print(('%d \xb0C? Are you insane?'.encode('utf-8') % temp) + (" That's over nine thousand!" if temp > 9000 else ''))
sys.exit(255)
elif o in ('-p', '--port'):
port = a

# Show initial parameters
print "Initial parameters"
print "Steps per mm: %3d steps" % k
print "Length extruded: %3d mm" % n
print
print "Serial port: %s" % (port if port else 'auto')
print("Initial parameters")
print("Steps per mm: %3d steps" % k)
print("Length extruded: %3d mm" % n)
print()
print("Serial port: %s" % (port if port else 'auto'))

p = None
try:
Expand All @@ -140,12 +140,12 @@ def gettemp(p):
try:
p = printcore(port, 115200)
except:
print 'Error.'
print('Error.')
raise
while not p.online:
time.sleep(1)
w('.')
print " connected."
print(" connected.")

heatup(p, temp)

Expand All @@ -160,8 +160,8 @@ def gettemp(p):
if n != m:
k = (n / m) * k
p.send_now("M92 E%d" % int(round(k))) # Set new step count
print "Steps per mm: %3d steps" % k # Tell user
print 'Calibration completed.' # Yay!
print("Steps per mm: %3d steps" % k) # Tell user
print('Calibration completed.') # Yay!
except KeyboardInterrupt:
pass
finally:
Expand Down
2 changes: 1 addition & 1 deletion gcodeplater.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3

# This file is part of the Printrun suite.
#
Expand Down
Loading