pax_global_header00006660000000000000000000000064131046476030014516gustar00rootroot0000000000000052 comment=92c5cd0ddbc96ac47107ff25f516e72045d0b9ce python-onewire-0.2/000077500000000000000000000000001310464760300143465ustar00rootroot00000000000000python-onewire-0.2/.gitignore000066400000000000000000000001201310464760300163270ustar00rootroot00000000000000*.pyc bin local lib share include pip-selfcheck.json build *.so dist *.egg-info python-onewire-0.2/.travis.yml000066400000000000000000000005001310464760300164520ustar00rootroot00000000000000language: python python: - "2.7" - "3.4" - "3.5" before_install: - sudo apt-get -qq update - sudo apt-get install -y libow-dev install: - pip install --upgrade pytest pytest-mock pytest-cov coveralls - pip install -e . script: - pytest --cov-config .coveragerc --cov=onewire after_success: - coveralls python-onewire-0.2/CHANGELOG.md000066400000000000000000000002571310464760300161630ustar00rootroot00000000000000# 0.2 (2017-05-10) - Add support for `set` [PR #3](https://github.com/kipe/python-onewire/pull/3) - Fix recursion issue [PR #5](https://github.com/kipe/python-onewire/pull/5) python-onewire-0.2/LICENSE000066400000000000000000000020551310464760300153550ustar00rootroot00000000000000MIT License Copyright (c) 2017 Kimmo Huoman Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. python-onewire-0.2/README.md000066400000000000000000000020201310464760300156170ustar00rootroot00000000000000# python-onewire A wrapper for OWFS C-API, compatible with both Python 2.7 and Python 3.x. ## Requirements Depends on `owcapi` from [OWFS -project](http://owfs.org/index.php?page=owcapi). On Debian-based systems, it can be installed by running ```sh apt-get install libow-dev ``` ## Install `pip install git+https://github.com/kipe/python-onewire.git` ## Usage Usage is fairly similar to the original [owpython](http://owfs.sourceforge.net/owpython.html). However, this library is class-based, so some modifications are required. ```python from onewire import Onewire # Use USB-adapter with Onewire('u') as o: # Find all temperature sensors in the bus, and print family, id and temperature as float. print('\n'.join([ '%s.%s %.02f' % (s.family, s.id, s.read_float('temperature')) for s in o.find(has_all=['temperature']) ])) # Read temperature from specific sensor # (note: when accessing values directly, the return value is always a string). print(o.sensor('28.D035DE060000').temperature) ``` python-onewire-0.2/example.py000066400000000000000000000011051310464760300163500ustar00rootroot00000000000000from onewire import Onewire # Use USB-adapter with Onewire('u') as o: # Find all temperature sensors in the bus, and print family, id and temperature as float. print('\n'.join([ '%s.%s %.02f' % (s.family, s.id, s.read_float('temperature')) for s in o.find(has_all=['temperature']) ])) # Read temperature from specific sensor # (note: when accessing values directly, the return value is always a string). print(o.sensor('28.D035DE060000').temperature) # Find all DS18B20 -sensors print([s for s in o.find(sensor_type='DS18B20')]) python-onewire-0.2/onewire/000077500000000000000000000000001310464760300160165ustar00rootroot00000000000000python-onewire-0.2/onewire/__init__.py000066400000000000000000000065601310464760300201360ustar00rootroot00000000000000# -*- coding: utf-8 -*- from __future__ import unicode_literals from . import _ow import os import re class Onewire(object): _path = '/uncached/' initialized = False SENSOR_REGEX = re.compile(r'[0-9A-F][0-9A-F]\.[0-9A-F]+', re.IGNORECASE | re.DOTALL) def __init__(self, device, cached=False): if _ow.init(str(device)) != 0: raise IOError("Initializing 1-wire failed.") self.initialized = True if cached: self._path = '/' def __enter__(self): return self def __exit__(self, exc_type, exc_val, exc_tb): self.finish() def finish(self): _ow.finish() self.initialized = False def get(self, *path): return _ow.get(str(os.path.join(self._path, *path))) def set(self, path, value): return _ow.set(path, value) def find(self, has_all=[], has_one=[], sensor_type=[]): if not isinstance(has_all, list): has_all = [has_all] if not isinstance(has_one, list): has_one = [has_one] if not isinstance(sensor_type, list): sensor_type = [sensor_type] for p in self.get('').split(','): if not self.SENSOR_REGEX.match(p): continue s = Sensor(self, p) # Check if all required arguments are found if has_all and not set(s.attrs).issuperset(set(has_all)): continue # Check if at least one of has_one arguments is found if has_one and not [x for x in has_one if x in s.attrs]: continue # Check if sensor_type matches if sensor_type and s.sensor_type not in sensor_type: continue yield s def sensor(self, path): return Sensor(self, path) class Sensor(object): def __init__(self, ow, path): self._ow = ow self.path = path.rstrip('/') self._sensor_type = None self._attrs = [] def __str__(self): return '' % (self.path, self.sensor_type) def __unicode__(self): return self.__str__() def __repr__(self): return self.__str__() def __getattr__(self, attr): if attr not in self.attrs: raise AttributeError('Attribute "%s" not found in %s.' % (attr, self.__str__())) return self._ow.get(self.path, attr) def read(self, attr): return self.__getattr__(attr) def read_float(self, attr): return float(self.__getattr__(attr)) def read_int(self, attr): return int(self.__getattr__(attr)) def read_boolean(self, attr): return int(self.__getattr__(attr)) == 1 @property def sensor_type(self): if self._sensor_type: return self._sensor_type self._sensor_type = self._ow.get(self.path, 'type') return self._sensor_type @property def attrs(self): if self._attrs: return self._attrs attributes = self._ow.get(self.path) if not attributes: raise OnewireException("Error fetching object: {}".format(self.path)) self._attrs = attributes.split(',') return self._attrs class OnewireException(Exception): def __init__(self, msg): self.msg = msg def __repr__(self): return self.__str__() def __str__(self): return "OnewireException: {}".format(self.msg) python-onewire-0.2/onewire/_ow.c000066400000000000000000000036641310464760300167570ustar00rootroot00000000000000#include #include static PyObject *OnewireException; static PyObject *init(PyObject *self, PyObject *args) { char *device; if (!PyArg_ParseTuple(args, "s", &device)) return NULL; return Py_BuildValue("i", OW_init(device)); }; static PyObject *get(PyObject *self, PyObject *args) { char *path; char *buffer; size_t buffer_length = 0; if (!PyArg_ParseTuple(args, "s", &path)) return NULL; OW_get(path, &buffer, &buffer_length); PyObject *reval = Py_BuildValue("s", buffer); free(buffer); return reval; }; static PyObject *set(PyObject *self, PyObject *args) { char *path; char *value; int written = 0; if (!PyArg_ParseTuple(args, "ss", &path, &value)) return NULL; written = OW_put(path, value, strlen(value)); if (written > 0) { PyObject *reval = Py_BuildValue("i", written); return reval; } else { PyErr_SetFromErrno(OnewireException); return NULL; } }; static PyObject *finish(PyObject *self, PyObject *args) { OW_finish(); Py_RETURN_NONE; }; static PyMethodDef _ow_methods[] = { {"init", init, METH_VARARGS, "Initialize 1-wire."}, {"get", get, METH_VARARGS, "Get data from 1-wire."}, {"set", set, METH_VARARGS, "Set data on 1-wire."}, {"finish", finish, METH_VARARGS, "Cleanup the library."}, {NULL, NULL, 0, NULL} }; #if PY_MAJOR_VERSION >= 3 static struct PyModuleDef moduledef = { PyModuleDef_HEAD_INIT, "_ow", NULL, -1, _ow_methods, NULL, NULL, NULL, NULL }; PyObject * PyInit__ow(void) #else void init_ow(void) #endif { #if PY_MAJOR_VERSION >= 3 PyObject *module = PyModule_Create(&moduledef); #else Py_InitModule("_ow", _ow_methods); #endif OnewireException = PyErr_NewException("onewire.OnewireException", NULL, NULL); #if PY_MAJOR_VERSION >= 3 return module; #endif } python-onewire-0.2/setup.py000066400000000000000000000007361310464760300160660ustar00rootroot00000000000000from setuptools import setup, Extension setup( name='onewire', version='0.2', description='A wrapper for OWFS C-API, compatible with both Python 2.7 and Python 3.x.', author='Kimmo Huoman', author_email='kipenroskaposti@gmail.com', url='https://github.com/kipe/python-onewire', packages=['onewire'], ext_modules=[ Extension('onewire._ow', libraries=['owcapi'], sources=['onewire/_ow.c'], extra_link_args=['-I', '/usr/include']) ], ) python-onewire-0.2/tests/000077500000000000000000000000001310464760300155105ustar00rootroot00000000000000python-onewire-0.2/tests/test_sensor.py000066400000000000000000000005401310464760300204310ustar00rootroot00000000000000import pytest import onewire def test_sensor_attrs_get_infinite_recursion(mocker): m = mocker.patch('onewire.Onewire.get', return_value=None) i = mocker.patch('onewire._ow.init', return_value=0) o = onewire.Onewire("Testdev") s = o.sensor('/test/path') with pytest.raises(onewire.OnewireException): s.read("temperature")