From filmor's answer, but with the int() casting removed:
from os import listdir
from os.path import isdir, isfile, islink, join, realpath, normpath
from keyword import iskeyword
_norm = lambda name: name + ('_' if iskeyword(name) else '')
def _denorm(name):
if name.endswith('_') and iskeyword(name[:-1]):
return name[:-1]
else:
return name
def _norm_path(path):
return normpath(realpath(path))
class SysFsObject(object):
__slots__ = ['_path', '__dict__']
@staticmethod
def __id_args__(path='/sys'):
return _norm_path(path)
def __init__(self, path='/sys'):
self._path = _norm_path(path)
if not self._path.startswith('/sys'):
raise RuntimeError("Using this on non-sysfs files is dangerous!")
self.__dict__.update(dict.fromkeys(_norm(i) for i in listdir(self._path)))
def __repr__(self):
return "<SysFsObject %s>" % self._path
def __setattr__(self, name, val):
if name.startswith('_'):
return object.__setattr__(self, name, val)
name = _denorm(name)
p = realpath(join(self._path, name))
if isfile(p):
file(p, 'w').write(val)
else:
raise RuntimeError
def __getattribute__(self, name):
if name.startswith('_'):
return object.__getattribute__(self, name)
name = _denorm(name)
p = realpath(join(self._path, name))
if isfile(p):
return open(p, 'r').read()[:-1]
elif isdir(p):
return SysFsObject(p)
Arbitrarily casting to int is unexpected and even dangerous. For example, if you were to use that code on any of the cpulist files prevalent in sysfs, a string such as "0-7" would always be returned on multi-processor systems. Then someday, someone uses your code on a single-core system and reading the exact same sysfs file that now contains "0" returns an int.
In other words, any function that calls that code and expects to receive the native data type of sysfs (strings) must explicitly cast to str().