Skip to content

Commit 07b6c14

Browse files
authored
Encoder: added sysfs module
1 parent f8c730e commit 07b6c14

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

Adafruit_BBIO/sysfs.py

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
4+
# Copyright (c) 2014 MIT OpenCourseWare
5+
#
6+
# Permission is hereby granted, free of charge, to any person obtaining a copy
7+
# of this software and associated documentation files (the "Software"), to deal
8+
# in the Software without restriction, including without limitation the rights
9+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
# copies of the Software, and to permit persons to whom the Software is
11+
# furnished to do so, subject to the following conditions:
12+
#
13+
# The above copyright notice and this permission notice shall be included in
14+
# all copies or substantial portions of the Software.
15+
#
16+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
# SOFTWARE.
23+
#
24+
# Code originally published at http://stackoverflow.com/questions/4648792/ and
25+
# subsequently forked at https://github.com/ponycloud/python-sysfs
26+
#
27+
# Original author: Benedikt Reinartz <filmor@gmail.com>
28+
# Contributors:
29+
# - Jan Dvořák <mordae@anilinux.org>
30+
# - Jonathon Reinhart https://github.com/JonathonReinhart
31+
# - Ondřej Koch <o.koch@zerusnet.com>
32+
# - David Planella <david.planella@ubuntu.com>
33+
34+
"""
35+
Simplistic Python SysFS interface.
36+
37+
Usage examples::
38+
from sysfs import sys
39+
40+
# Print all block devices in /sys, with their sizes
41+
for block_dev in sys.block:
42+
print block_dev, str(int(block_dev.size) / 1048576) + ' M'
43+
44+
>>> import sysfs
45+
>>> # Read/write Beaglebone Black's eQEP module attributes
46+
>>> eqep0 = sysfs.Node("/sys/devices/platform/ocp/48300000.epwmss/48300180.eqep")
47+
>>> # Read eqep attributes
48+
>>> eqep0.enabled
49+
'1'
50+
>>> eqep0.mode
51+
'0'
52+
>>> eqep0.period
53+
'1000000000'
54+
>>> eqep0.position
55+
'0'
56+
>>> # Write eqep attributes. They should be strings.
57+
>>> eqep0.position = str(2)
58+
>>> eqep0.position
59+
'2'
60+
"""
61+
62+
from os import listdir
63+
from os.path import isdir, isfile, join, realpath, basename
64+
65+
__all__ = ['sys', 'Node']
66+
67+
68+
class Node(object):
69+
__slots__ = ['_path_', '__dict__']
70+
71+
def __init__(self, path='/sys'):
72+
self._path_ = realpath(path)
73+
if not self._path_.startswith('/sys/') and not '/sys' == self._path_:
74+
raise RuntimeError('Using this on non-sysfs files is dangerous!')
75+
76+
self.__dict__.update(dict.fromkeys(listdir(self._path_)))
77+
78+
def __repr__(self):
79+
return '<sysfs.Node "%s">' % self._path_
80+
81+
def __str__(self):
82+
return basename(self._path_)
83+
84+
def __setattr__(self, name, val):
85+
if name.startswith('_'):
86+
return object.__setattr__(self, name, val)
87+
88+
path = realpath(join(self._path_, name))
89+
if isfile(path):
90+
with open(path, 'w') as fp:
91+
fp.write(val)
92+
else:
93+
raise RuntimeError('Cannot write to non-files.')
94+
95+
def __getattribute__(self, name):
96+
if name.startswith('_'):
97+
return object.__getattribute__(self, name)
98+
99+
path = realpath(join(self._path_, name))
100+
if isfile(path):
101+
with open(path, 'r') as fp:
102+
return fp.read().strip()
103+
elif isdir(path):
104+
return Node(path)
105+
106+
def __setitem__(self, name, val):
107+
return setattr(self, name, val)
108+
109+
def __getitem__(self, name):
110+
return getattr(self, name)
111+
112+
def __iter__(self):
113+
return iter(getattr(self, name) for name in listdir(self._path_))
114+
115+
116+
sys = Node()

0 commit comments

Comments
 (0)