Skip to content

Commit e800531

Browse files
committed
minor code refactoring
1 parent db74e61 commit e800531

File tree

7 files changed

+115
-59
lines changed

7 files changed

+115
-59
lines changed

.gitignore

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
*.py[cod]
2+
MANIFEST
23

34
# C extensions
45
*.so
@@ -29,8 +30,3 @@ nosetests.xml
2930

3031
# Translations
3132
*.mo
32-
33-
# Mr Developer
34-
.mr.developer.cfg
35-
.project
36-
.pydevproject

MANIFEST.in

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include README.md

README.md

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# pyownet, a pythonic interface to ownet
22

3-
pyownet is a pure python library that interfaces to an
3+
pyownet is a pure python package that allows to access an
44
[owserver](http://owfs.org/index.php?page=owserver_protocol) via the
55
[owserver network protocol](http://owfs.org/index.php?page=owserver-protocol),
66
in short _ownet_.
@@ -12,11 +12,9 @@ owserver is part of the [OWFS 1-Wire File System](http://owfs.org):
1212
> environment. You can write scripts to read temperature, flash lights, write
1313
> to an LCD, log and graph, ...
1414
15-
This package is composed by modules.
16-
17-
`pyownet.protocol` implements low-level intereaction via the owserver protocol.
18-
A proxy object is created, with methods corresponding to the possible ownet
19-
messages:
15+
The `pyownet.protocol` module is a low-level implementation of the ownet
16+
protocol. Interaction with an owserver takes place via a proxy object whose
17+
methods correspond to ownet messages:
2018

2119
```
2220
>>> owproxy = OwnetProxy(host="owserver.example.com", port=4304)

pyownet/__init__.py

+16-17
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
"""python ownet client"""
22

3-
"""
4-
Copyright 2013, 2014 Stefano Miccoli
5-
6-
7-
This python package is free software: you can redistribute it and/or modify
8-
it under the terms of the GNU General Public License as published by
9-
the Free Software Foundation, either version 3 of the License, or
10-
(at your option) any later version.
11-
12-
This program is distributed in the hope that it will be useful,
13-
but WITHOUT ANY WARRANTY; without even the implied warranty of
14-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15-
GNU General Public License for more details.
16-
17-
You should have received a copy of the GNU General Public License
18-
along with this program. If not, see <http://www.gnu.org/licenses/>.
19-
"""
3+
#
4+
# Copyright 2013, 2014 Stefano Miccoli
5+
#
6+
# This python package is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU General Public License as published by
8+
# the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# This program is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU General Public License
17+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
#

pyownet/protocol.py

+52-29
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
"""ownet protocol implementation
22
3-
This module provides classes to query an owserver via the ownet protocol.
4-
It is a pure python implementation, with no external dependencies.
3+
This module is a pure python, low level implementation of the ownet
4+
protocol.
55
6-
A proxy object is provided that implements methods for the protocol
7-
commands.
6+
OwnetProxy instances are proxy objects whose methods correspond to ownet
7+
protocol messages.
88
99
>>> owproxy = OwnetProxy(host="owserver.example.com", port=4304)
1010
>>> owproxy.ping()
@@ -18,16 +18,33 @@
1818
>>> owproxy.dir()
1919
['/sensA/', '/05.4AEC29CDBAAB/']
2020
21-
The lowlevel OwnetConnection encapsulates all socket operations and
22-
interactions with the server.
21+
The OwnetConnection class encapsulates all socket operations and
22+
interactions with the server and is mean for internal use.
2323
2424
"""
2525

26+
#
27+
# Copyright 2013, 2014 Stefano Miccoli
28+
#
29+
# This python package is free software: you can redistribute it and/or modify
30+
# it under the terms of the GNU General Public License as published by
31+
# the Free Software Foundation, either version 3 of the License, or
32+
# (at your option) any later version.
33+
#
34+
# This program is distributed in the hope that it will be useful,
35+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
36+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37+
# GNU General Public License for more details.
38+
#
39+
# You should have received a copy of the GNU General Public License
40+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
41+
#
42+
2643
from __future__ import print_function
2744

2845
import struct
2946
import socket
30-
import collections
47+
#import collections
3148

3249
# see msg_classification from ow_message.h
3350
MSG_ERROR = 0
@@ -86,12 +103,17 @@
86103

87104
def str2bytez(s):
88105
"transform string to zero-terminated bytes"
89-
return s.encode() + b'\x00'
106+
return s.encode('ascii') + b'\x00'
90107

91-
class _dummy(collections.Sequence):
92-
# dummy list, every item is an empty string
93-
__len__ = lambda self: 0
94-
__getitem__ = lambda self, i: ''
108+
def bytes2str(b):
109+
"transform bytes to string"
110+
return b.decode('ascii')
111+
112+
113+
#class _dummy(collections.Sequence):
114+
# # dummy list, every item is an empty string
115+
# __len__ = lambda self: 0
116+
# __getitem__ = lambda self, i: ''
95117

96118
#
97119
# exceptions
@@ -329,10 +351,11 @@ def __init__(self, host='localhost', port=4304, flags=0,
329351
self.ping()
330352

331353
#self.errmess = _dummy()
354+
332355
# fetch errcodes array from owserver
333356
errcodes = '/settings/return_codes/text.ALL'
334357
assert self.present(errcodes)
335-
self.errmess = self.read(errcodes).decode().split(',')
358+
self.errmess = bytes2str(self.read(errcodes)).split(',')
336359

337360
def sendmess(self, type, payload, flags=0, size=0, offset=0):
338361
""" retcode, data = sendmess(type, payload)
@@ -358,7 +381,7 @@ def ping(self):
358381
raise OwnetError(-ret, self.errmess[-ret])
359382

360383
def present(self, path):
361-
"returns True if there is an entity as path"
384+
"returns True if there is an entity at path"
362385

363386
ret, data = self.sendmess(MSG_PRESENCE, str2bytez(path))
364387
assert ret <= 0
@@ -384,7 +407,7 @@ def dir(self, path='/', slash=True, bus=False):
384407
if ret < 0:
385408
raise OwnetError(-ret, self.errmess[-ret], path)
386409
if data:
387-
return data.decode().split(',')
410+
return bytes2str(data).split(',')
388411
else:
389412
return []
390413

@@ -413,19 +436,19 @@ def write(self, path, data):
413436
if ret < 0:
414437
raise OwnetError(-ret, self.errmess[-ret], path)
415438

416-
def _test():
417-
proxy = OwnetProxy()
418-
proxy.ping()
419-
assert not proxy.present('/nonexistent')
420-
sensors = proxy.dir('/',bus=False)
421-
for j,i in enumerate(sensors):
422-
assert proxy.present(i), i
423-
stype = proxy.read(i + 'type')
424-
if proxy.present(i + 'temperature'):
425-
temp = proxy.read(i + 'temperature')
426-
else:
427-
temp = ''
428-
print(i, stype, temp)
439+
def _main():
440+
# print sensors on localhost owserver
441+
try:
442+
proxy = OwnetProxy()
443+
except ConnError:
444+
print("No owserver on localhost")
445+
return 1
446+
print("owserver directory on localhost:")
447+
print("id".center(17), "type".center(7))
448+
for sensor in proxy.dir(slash=False, bus=False):
449+
stype = proxy.read(sensor + '/type')
450+
print(sensor.ljust(17), stype.ljust(7))
451+
return 0
429452

430453
if __name__ == '__main__':
431-
_test()
454+
_main()

setup.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
from distutils.core import setup
22

33
setup(name = 'pyownet',
4-
version = '0.6.1',
4+
version = '0.6.2.dev0',
5+
packages = ['pyownet', ],
56
description = 'python ownet client library',
67
author = 'Stefano Miccoli',
78
author_email = '[email protected]',
89
url = 'https://github.com/miccoli/pyownet',
9-
packages = ['pyownet', ],
10+
classifiers = [
11+
'Development Status :: 3 - Alpha',
12+
'Environment :: Other Environment',
13+
'Intended Audience :: Developers',
14+
'License :: OSI Approved :: GNU General Public License v3 (GPLv3)',
15+
]
1016
)

tests.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import unittest
2+
from pyownet import protocol
3+
4+
class TestProtocolModule(unittest.TestCase):
5+
6+
@classmethod
7+
def setUpClass(cls):
8+
cls.proxy = protocol.OwnetProxy()
9+
10+
def test_ping(self):
11+
self.assertIsNone(self.proxy.ping())
12+
13+
def test_present(self):
14+
self.assertIs(self.proxy.present('/'), True)
15+
self.assertIs(self.proxy.present('/nonexistent'), False)
16+
17+
def test_dir(self):
18+
for i in self.proxy.dir(bus=False):
19+
self.assertTrue(self.proxy.present(i))
20+
self.assertTrue(self.proxy.present(i + 'type'))
21+
stype = self.proxy.read(i + 'type')
22+
if self.proxy.present(i + 'temperature'):
23+
temp = self.proxy.read(i + 'temperature')
24+
else:
25+
temp = ''
26+
27+
def test_exceptions(self):
28+
self.assertRaises(protocol.OwnetError, self.proxy.dir, '/nonexistent')
29+
self.assertRaises(protocol.OwnetError, self.proxy.read, '/')
30+
31+
if __name__ == '__main__':
32+
unittest.main()
33+

0 commit comments

Comments
 (0)