Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
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
21 changes: 15 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
language: python
env:
- USE_MICROPYTHON=false
- USE_MICROPYTHON=true
python:
- 3.4
cache: pip
cache:
pip: true
directories:
- ~/micropython
- ~/.micropython
sudo: false
git:
depth: 100
install:
- pip install Pillow Sphinx sphinx_bootstrap_theme recommonmark evdev==1.0.0
- pip install -r ./docs/requirements.txt
- if [ $USE_MICROPYTHON = false ]; then pip install Pillow Sphinx sphinx_bootstrap_theme recommonmark evdev==1.0.0 && pip install -r ./docs/requirements.txt; fi
- if [ $USE_MICROPYTHON = true ]; then ./.travis/install-micropython.sh && PYTHON=~/micropython/ports/unix/micropython; else PYTHON=python3; fi
script:
- chmod -R g+rw ./tests/fake-sys/devices/**/*
- ./tests/api_tests.py
- sphinx-build -nW -b html ./docs/ ./docs/_build/html
- $PYTHON tests/api_tests.py
- if [ $USE_MICROPYTHON = false ]; then sphinx-build -nW -b html ./docs/ ./docs/_build/html; fi
deploy:
provider: pypi
user: Denis.Demidov
password:
secure: cTDN4WMJEjxZ0zwFwkLPpOIOSG/1JlHbQsHzd/tV9LfjBMR0nJR5HrmyiIO1TE5Wo6AFSFd7S3JmKdEnFO1ByvN/EFtsKGRUR9L6Yst4KfFi4nHwdZ7vgPTV0nNdvgC1YM/3bx8W8PcjFSm/k8awx7aicwXj09yDA8ENTf5XedaX22Z+9OKhb1mKola1cqEoc0GwaYzd8UX0Ruwh9/6RRbvTt7zn8BCZc9vIVqNd6mZgBWY9zAU40ZZsjYORbiZmDNCygEI+RViZ51M58WYkPPhoivzcG9em8DMRS7SC3CjWGapiOaXUHa3Fhnn+IQ+Q8Xv9YU5+qmj65MWQy3SSnMnvuxmrLf4aLoOlSJUMhDarjni4tdBOTX5PdOkdmhskyQt1DqDrw0+WhPItYfGe5zQfQwqW+YOpGbOipAeU+844arPo5jvZG/IOBX2qVUwdSxo8Y/98ocjqoZOq8b5xkWtJef0Kh1RCkp1bR2XELQVe76qeWqQxWz3OPqq+wK3xeNvj5kMQmytl3dCEB//D6UcES7Qr8YxD+LWoaIf32JIj/4LaCXXuxMVH+PJ68Oc72/ox0qLmXK0qhbea2QvaqXyGDrO+a2X6VbMdH32D2xHzH4Mg75xLnXnSaFvGovhYl1zEVcYUDioxrXZEuDmymGf9nH2mivJ24Fon6u+C3QQ=
on:
tags: true
condition: $TRAVIS_TAG != ev3dev-stretch/*
condition:
- $TRAVIS_TAG != ev3dev-stretch/*

@WasabiFan WasabiFan Sep 11, 2018 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect that this (the list for condition) isn't right. I need to take another look and figure out how to do it correctly.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never mind... although the documentation says

condition: deploy when a single bash condition evaluates to true. This must be a string value, and is equivalent to if [[ <condition> ]]; then <deploy>; fi.

It emits the correct script:

  if [[ ($TRAVIS_REPO_SLUG = "ev3dev/ev3dev-lang-python") && ($TRAVIS_TAG != ev3dev-stretch/*) && ($USE_MICROPYTHON = false) && ("$TRAVIS_TAG" != "") ]]; then

- $USE_MICROPYTHON = false
repo: ev3dev/ev3dev-lang-python
25 changes: 25 additions & 0 deletions .travis/install-micropython.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/sh
set -ex

if [ -e ~/micropython/ports/unix/micropython ]; then
# the micropython binary already exists, which means that the cached folder was restored
exit 0
fi

cd ~/

# Build micropython from source
# TODO: cache micropython build output

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is how I did it:

Adding a path to travis cache:

https://github.com/ddemidov/amgcl/blob/master/.travis.yml#L39-L42

Checking if path exists (that is, if cache was unpacked successfully):

https://github.com/ddemidov/amgcl/blob/master/.travis/install_boost.sh#L5

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Thanks for the reference!

git clone --recurse-submodules https://github.com/micropython/micropython.git --depth 1 --branch v1.9.4 --quiet
cd ./micropython/ports/unix
make axtls
make

# Install upip
~/micropython/tools/bootstrap_upip.sh

# Install micropython library modules
~/micropython/ports/unix/micropython -m upip install micropython-unittest micropython-os micropython-os.path micropython-shutil micropython-io micropython-fnmatch micropython-numbers micropython-struct micropython-time micropython-logging micropython-select
# Make unittest module show error output; will run until failure then print first error
# See https://github.com/micropython/micropython-lib/blob/f20d89c6aad9443a696561ca2a01f7ef0c8fb302/unittest/unittest.py#L203
sed -i 's/#raise/raise/g' ~/.micropython/lib/unittest.py
34 changes: 23 additions & 11 deletions ev3dev2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@
if sys.version_info < (3,4):
raise SystemError('Must be using Python 3.4 or higher')

def is_micropython():
return sys.implementation.name == "micropython"

def chain_exception(exception, cause):

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if I like how I did this... Micropython doesn't support exception chaining, so we have to suppress the from when running on that interpreter. But now chain_exception shows up in the call stack.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we could implement our own Exception subclass, that would carry its parent exception around, but I am not sure if that would be any better than what you did.

if is_micropython():
raise exception
else:
raise exception from cause

import os
import io
import fnmatch
Expand Down Expand Up @@ -188,7 +197,8 @@ def get_index(file):
except StopIteration:
self._path = None
self._device_index = None
raise DeviceNotFound("%s is not connected." % self) from None

chain_exception(DeviceNotFound("%s is not connected." % self), None)

def __str__(self):
if 'address' in self.kwargs:
Expand All @@ -206,13 +216,13 @@ def _attribute_file_open(self, name):
w_ok = mode & stat.S_IWGRP

if r_ok and w_ok:
mode = 'r+'
mode_str = 'r+'
elif w_ok:
mode = 'w'
mode_str = 'w'
else:
mode = 'r'
mode_str = 'r'

return io.FileIO(path, mode)
return io.FileIO(path, mode_str)

def _get_attribute(self, attribute, name):
"""Device attribute getter"""
Expand Down Expand Up @@ -245,20 +255,22 @@ def _raise_friendly_access_error(self, driver_error, attribute):
if not isinstance(driver_error, OSError):
raise driver_error

if driver_error.errno == errno.EINVAL:
driver_errorno = driver_error.args[0] if is_micropython() else driver_error.errno

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would feel nicer if this would be implemented as a function (driver_errno()) but I guess it is OK as is, since this is the only place we need it.


if driver_errorno == errno.EINVAL:
if attribute == "speed_sp":
try:
max_speed = self.max_speed
except (AttributeError, Exception):
raise ValueError("The given speed value was out of range") from driver_error
chain_exception(ValueError("The given speed value was out of range"), driver_error)
else:
raise ValueError("The given speed value was out of range. Max speed: +/-" + str(max_speed)) from driver_error
raise ValueError("One or more arguments were out of range or invalid") from driver_error
elif driver_error.errno == errno.ENODEV or driver_error.errno == errno.ENOENT:
chain_exception(ValueError("The given speed value was out of range. Max speed: +/-" + str(max_speed)), driver_error)
chain_exception(ValueError("One or more arguments were out of range or invalid"), driver_error)
elif driver_errorno == errno.ENODEV or driver_errorno == errno.ENOENT:
# We will assume that a file-not-found error is the result of a disconnected device
# rather than a library error. If that isn't the case, at a minimum the underlying
# error info will be printed for debugging.
raise DeviceNotFound("%s is no longer connected" % self) from driver_error
chain_exception(DeviceNotFound("%s is no longer connected" % self), driver_error)
raise driver_error

def get_attr_int(self, attribute, name):
Expand Down
6 changes: 3 additions & 3 deletions ev3dev2/motor.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import select
import time
from logging import getLogger
from math import atan2, degrees as math_degrees, hypot
from math import atan2, degrees as math_degrees, sqrt
from os.path import abspath
from ev3dev2 import get_current_platform, Device, list_device_names

Expand Down Expand Up @@ -1945,7 +1945,7 @@ def get_speed_steering(self, steering, speed):
"""

assert steering >= -100 and steering <= 100,\
"%{} is an invalid steering, must be between -100 and 100 (inclusive)".format(steering)
"{} is an invalid steering, must be between -100 and 100 (inclusive)".format(steering)

# We don't have a good way to make this generic for the pair... so we
# assume that the left motor's speed stats are the same as the right
Expand Down Expand Up @@ -1997,7 +1997,7 @@ def on(self, x, y, max_speed=100.0, radius=100.0):
MoveTank.off()
return

vector_length = hypot(x, y)
vector_length = sqrt(x*x + y*y)
angle = math_degrees(atan2(y, x))

if angle < 0:
Expand Down
5 changes: 1 addition & 4 deletions ev3dev2/sensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,7 @@ def value(self, n=0):
an error. The values are fixed point numbers, so check decimals to see
if you need to divide to get the actual value.
"""
if isinstance(n, numbers.Real):
n = int(n)
elif isinstance(n, str):
n = int(n)
n = int(n)

self._value[n], value = self.get_attr_int(self._value[n], 'value'+str(n))
return value
Expand Down
22 changes: 14 additions & 8 deletions tests/api_tests.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env python3
import unittest, sys, os
import unittest, sys, os.path

FAKE_SYS = os.path.join(os.path.dirname(__file__), 'fake-sys')

Expand All @@ -17,15 +17,22 @@

_internal_set_attribute = ev3dev2.Device._set_attribute
def _set_attribute(self, attribute, name, value):
# After writing, clear the rest of the file to remove any residual text from
# the last write. On the real device we're writing to sysfs attributes where
# there isn't any persistent buffer, but in the test environment they're
# normal files on disk.
# Follow the text with a newline to separate new content from stuff that
# already existed in the buffer. On the real device we're writing to sysfs
# attributes where there isn't any persistent buffer, but in the test
# environment they're normal files on disk which retain previous data.
attribute = _internal_set_attribute(self, attribute, name, value)
attribute.truncate()

attribute.write(b'\n')
return attribute
ev3dev2.Device._set_attribute = _set_attribute

_internal_get_attribute = ev3dev2.Device._get_attribute
def _get_attribute(self, attribute, name):
# Split on newline delimiter; see _set_attribute above
attribute, value = _internal_get_attribute(self, attribute, name)
return attribute, value.split('\n', 1)[0]
ev3dev2.Device._get_attribute = _get_attribute

def dummy_wait(self, cond, timeout=None):
pass

Expand Down Expand Up @@ -110,7 +117,6 @@ def test_infrared_sensor(self):
self.assertEqual(s.mode, "IR-REMOTE")

val = s.proximity
# Our test environment writes to actual files on disk, so while "seek(0) write(...)" works on the real device, it leaves trailing characters from previous writes in tests. "s.mode" returns "IR-PROXTE" here.
self.assertEqual(s.mode, "IR-PROX")
self.assertEqual(val, 16)

Expand Down
2 changes: 1 addition & 1 deletion tests/fake-sys