Browse Source

Should now works both on python 2 and 3

saintcrawler 7 years ago
parent
commit
f3ff4ad9ee
4 changed files with 16 additions and 9 deletions
  1. 2 1
      .gitignore
  2. 1 1
      nodemcu_uploader/uploader.py
  3. 9 5
      nodemcu_uploader/utils.py
  4. 4 2
      tox.ini

+ 2 - 1
.gitignore

@@ -12,4 +12,5 @@ env3/
 *.log
 
 # editors
-.vscode/
+.vscode/
+.tox

+ 1 - 1
nodemcu_uploader/uploader.py

@@ -3,7 +3,7 @@
 """Main functionality for nodemcu-uploader"""
 
 # Not sure about it, because UnicodeEncodeError throws anyway
-from __future__ import unicode_literals
+# from __future__ import unicode_literals
 
 
 import time

+ 9 - 5
nodemcu_uploader/utils.py

@@ -5,9 +5,11 @@
 from platform import system
 from os import environ
 from wrapt import ObjectProxy
+from sys import version_info
 
 __all__ = ['default_port', 'system']
 
+PY2 = version_info.major == 2
 
 ENCODING = 'latin1'
 
@@ -26,7 +28,7 @@ def bytefy(x):
 
 
 def to_hex(x):
-    return hex(x) if type(x) == bytes else hex(ord(x))
+    return hex(ord(x))
 
 
 def hexify(byte_arr):
@@ -35,16 +37,18 @@ def hexify(byte_arr):
 
 def from_file(path):
     with open(path, 'rb') as f:
-        content = f.read().decode(ENCODING)
-    return content
+        content = f.read()
+    return content if PY2 else content.decode(ENCODING)
 
 
 class DecoderWrapper(ObjectProxy):
     def read(self, *args, **kwargs):
-        return self.__wrapped__.read(*args, **kwargs).decode(ENCODING)
+        res = self.__wrapped__.read(*args, **kwargs)
+        return res if PY2 else res.decode(ENCODING)
 
     def write(self, data):
-        return self.__wrapped__.write(data.encode(ENCODING))
+        data = data if PY2 else data.encode(ENCODING)
+        return self.__wrapped__.write(data)
 
 
 def wrap(x):

+ 4 - 2
tox.ini

@@ -1,6 +1,8 @@
 [tox]
-envlist = py27, py36
+envlist = py27, py35
 
 [testenv]
 deps = -rtest_requirements.txt
-commands = python -m unittest -v tests.get_tests
+commands = python -m unittest -v tests.get_tests
+setenv =
+  SERIALPORT=/dev/ttyACM0