Commits

gleb_zhulik committed 07896dc

Python3 and Python2.7 now fully supported

Comments (0)

Files changed (5)

sensors/__init__.py

 #!/usr/bin/env python
 # -*- coding: utf-8 -*-
-from common import *
+from .common import *
 
-if MAJOR_VERSION < 3:
-    from api3 import *
-else:
-    from api4 import *
+from .api4 import *
 
 
 __version__ = '0.0.2'

sensors/api3.py

-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
-from ctypes import *
-
-import common
-import stdc
-from common import *
-
-__all__ = ['API_VERSION', 'DEFAULT_CONFIG_FILENAME', 'iter_detected_chips']
-
-API_VERSION = 3
-common.DEFAULT_CONFIG_FILENAME = DEFAULT_CONFIG_FILENAME = '/etc/sensors.conf'
-
-
-class Feature(Structure):
-    _fields_ = [
-        ('number', c_int),
-        ('name', c_char_p),
-        ('mapping', c_int),
-        ('compute_mapping', c_int),
-        ('mode', c_int),
-    ]
-    
-    NO_MAPPING = -1
-    
-    def __repr__(self):
-        return '<%s number=%d name=%r mapping=%d compute_mapping=%d mode=%d' % (
-            self.__class__.__name__,
-            self.number,
-            self.name,
-            self.mapping,
-            self.compute_mapping,
-            self.mode
-        )
-    
-    def __iter__(self):
-        return self.chip._iter_features(self.number)
-    
-    @property
-    def label(self):
-        result_p = c_char_p()
-        _get_label(self.chip, self.number, byref(result_p))
-        result = result_p.value
-        stdc.free(result_p)
-        return result
-    
-    def get_value(self):
-        result = c_double()
-        _get_feature(self.chip, self.number, byref(result))
-        return result.value
-
-FEATURE_P = POINTER(Feature)
-
-
-class Chip(Structure):
-    # 
-    # TODO Implement a `__str__()` method.
-    # TODO Move common stuff into `AbstractChip` class.
-    # 
-    _fields_ = [
-        ('prefix', c_char_p),
-        ('bus', c_int),
-        ('addr', c_int),
-        ('busname', c_char_p),
-    ]
-    
-    def __new__(cls, *args):
-        result = super(Chip, cls).__new__(cls)
-        if args:
-            _parse_chip_name(args[0], byref(result))
-        return result
-    
-    def __init__(self, *_args):
-        Structure.__init__(self)
-    
-    def __repr__(self):
-        return '<%s prefix=%r bus=%r addr=%r busname=%r>' % (
-            (
-                self.__class__.__name__,
-                self.prefix,
-                self.bus,
-                self.addr,
-                self.busname
-            )
-        )
-    
-    def __iter__(self):
-        return self._iter_features()
-    
-    @property
-    def has_wildcards(self):
-        return bool(_chip_name_has_wildcards(self))
-    
-    @property
-    def adapter_name(self):
-        return _get_adapter_name(self.bus)
-    
-    def match(self, other):
-        return bool(_match_chip(self, other))
-    
-    def _iter_features(self, parent=Feature.NO_MAPPING):
-        nr1, nr2 = c_int(0), c_int(0)
-        while True:
-            result_p = _get_all_features(self, byref(nr1), byref(nr2))
-            if not result_p:
-                break
-            result = result_p.contents
-            if result.mapping == parent:
-                result.chip = self
-                result.subfeatures = dict()
-                yield result
-
-CHIP_P = POINTER(Chip)
-
-
-# 
-# TODO Implement a handler for at least `sensors_parse_error`.
-# 
-
-_parse_chip_name = SENSORS_LIB.sensors_parse_chip_name
-_parse_chip_name.argtypes = [c_char_p, CHIP_P]
-_parse_chip_name.restype = c_int
-_parse_chip_name.errcheck = _error_check
-
-_match_chip = SENSORS_LIB.sensors_match_chip
-_match_chip.argtypes = [Chip, Chip]
-_match_chip.restype = c_int
-
-_chip_name_has_wildcards = SENSORS_LIB.sensors_chip_name_has_wildcards
-_chip_name_has_wildcards.argtypes = [Chip]
-_chip_name_has_wildcards.restype = c_int
-
-_get_adapter_name = SENSORS_LIB.sensors_get_adapter_name
-_get_adapter_name.argtypes = [c_int]
-_get_adapter_name.restype = c_char_p
-
-_get_label = SENSORS_LIB.sensors_get_label
-_get_label.argtypes = [Chip, c_int, POINTER(c_char_p)]
-_get_label.restype = c_int
-_parse_chip_name.errcheck = _error_check
-
-# 
-# TODO sensors_get_ignored()
-# 
-
-_get_feature = SENSORS_LIB.sensors_get_feature
-_get_feature.argtypes = [Chip, c_int, POINTER(c_double)]
-_get_feature.restype = c_int
-_parse_chip_name.errcheck = _error_check
-
-# 
-# TODO sensors_set_feature()
-# TODO sensors_do_chip_sets()
-# TODO sensors_do_all_sets()    (common function)
-# 
-
-_get_detected_chips = SENSORS_LIB.sensors_get_detected_chips
-_get_detected_chips.argtypes = [POINTER(c_int)]
-_get_detected_chips.restype = CHIP_P
-
-_get_all_features = SENSORS_LIB.sensors_get_all_features
-_get_all_features.argtypes = [Chip, POINTER(c_int), POINTER(c_int)]
-_get_all_features.restype = FEATURE_P
-
-
-def iter_detected_chips(chip_name='*-*'):
-    chip = Chip(chip_name)
-    number = c_int(0)
-    while True:
-        result_p = _get_detected_chips(byref(number))
-        if not result_p:
-            break
-        result = result_p.contents
-        # 
-        # TODO Add ignore check.
-        # 
-        if chip.match(result):
-            yield result
 # -*- coding: utf-8 -*-
 from ctypes import *
 
-import common
-import stdc
-from common import *
+from . import common
+from . import stdc
+from .common import *
 
 __all__ = ['API_VERSION', 'DEFAULT_CONFIG_FILENAME', 'iter_detected_chips']
 
         # 
         # TODO Maybe this is a memory leak!
         # 
-        return _get_label(byref(self.chip), byref(self))
+        return _get_label(byref(self.chip), byref(self)).decode('utf-8')
     
     def get_value(self):
         # 
         # TODO Is the first always the correct one for all feature types?
         # 
-        return iter(self).next().get_value()
+        return next(iter(self)).get_value()
 
 FEATURE_P = POINTER(Feature)
 
         return (
             '*' if self.type == self.TYPE_ANY
                 else _get_adapter_name(byref(self))
-        )
+        ).decode('utf-8')
     
     def __repr__(self):
         return '%s(%r, %r)' % (self.__class__.__name__, self.type, self.nr)
     def __new__(cls, *args):
         result = super(Chip, cls).__new__(cls)
         if args:
-            _parse_chip_name(args[0], byref(result))
+            _parse_chip_name(args[0].encode('utf-8'), byref(result))
         return result
         
     def __init__(self, *_args):
         buffer_size = 200
         result = create_string_buffer(buffer_size)
         used = _snprintf_chip_name(result, len(result), byref(self))
-        assert used < buffer_size
-        return result.value
+        return result.value.decode('utf-8')
     
     def __iter__(self):
         number = c_int(0)

sensors/cli.py

-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
-import sensors
-
-
-def main():
-    sensors.init()
-    try:
-        for chip in sensors.iter_detected_chips():
-            print chip
-            print 'Adapter:', chip.adapter_name
-            for feature in chip:
-                print '%s (%r): %.1f' % (
-                    feature.name, feature.label, feature.get_value()
-                )
-                for subfeature in feature:
-                    print '  %s: %.1f' % (
-                        subfeature.name, subfeature.get_value()
-                    )
-            print
-    finally:
-        sensors.cleanup()
-
-
-if __name__ == '__main__':
-    main()

sensors/common.py

 from ctypes import *
 from ctypes.util import find_library
 
-import stdc
+from . import stdc
 
 __all__ = [
     'SENSORS_LIB', 'VERSION', 'MAJOR_VERSION', 'SensorsError', '_error_check',
 
 LIB_FILENAME = os.environ.get('SENSORS_LIB') or find_library('sensors')
 SENSORS_LIB = CDLL(LIB_FILENAME)
-VERSION = c_char_p.in_dll(SENSORS_LIB, 'libsensors_version').value
+VERSION = c_char_p.in_dll(SENSORS_LIB, 'libsensors_version').value.decode('utf-8')
 MAJOR_VERSION = int(VERSION.split('.', 1)[0])
 
 
 def init(config_filename=None):
     if config_filename is None:
         config_filename = DEFAULT_CONFIG_FILENAME
-    file_p = stdc.fopen(config_filename, 'r')
+    file_p = stdc.fopen(config_filename.encode('utf-8'), b'r')
     if file_p is None:
         error_number = get_errno()
         raise OSError(error_number, os.strerror(error_number), config_filename)