~gdesklets-desklet-team/gdesklets/0.36

« back to all changes in this revision

Viewing changes to libdesklets/hddtemp.py

  • Committer: Robert Pastierovic
  • Date: 2007-10-07 10:08:42 UTC
  • Revision ID: pastierovic@gmail.com-20071007100842-fdvp2vzmqgh1j87k
merged 0.3x branch and basic documentation and some other changes

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import socket
 
2
import re
 
3
import sys
 
4
 
 
5
from utils.Struct import Struct
 
6
from libdesklets import convert
 
7
 
 
8
 
 
9
__HOST = 'localhost'
 
10
__PORT = 7634
 
11
__REGEX = re.compile('\|(?P<device>.+?)\|(?P<name>.+?)\|(?P<value>\d+)\|(?P<unit>[CF])\|')
 
12
 
 
13
 
 
14
def __split(data):
 
15
 
 
16
    # |/dev/hda|TOSHIBA MK6025GAS|50|C|
 
17
    # |/dev/hda|MAXTOR 6L040J2|41|C||/dev/hdh|IC35L040AVVN07-0|39|C|
 
18
 
 
19
    def match_to_struct(m):
 
20
 
 
21
        value = float(m['value'])
 
22
        unit  = m['unit']
 
23
 
 
24
        assert unit in ('C', 'F')
 
25
 
 
26
        if (unit == 'C'):
 
27
            C = value
 
28
        elif (unit == 'F'):
 
29
            C = convert.fahrenheit_to_centigrade(value)
 
30
 
 
31
        F = convert.centigrade_to_fahrenheit(C)
 
32
        K = convert.centigrade_to_kelvin(C)
 
33
 
 
34
        return Struct(device = m['device'],
 
35
                      name   = m['name'],
 
36
                      temp   = Struct(centigrade = C,
 
37
                                      fahrenheit = F,
 
38
                                      kelvin     = K
 
39
                                      )
 
40
                      )
 
41
 
 
42
    return [ match_to_struct(m.groupdict()) for m in __REGEX.finditer(data) ]
 
43
 
 
44
 
 
45
 
 
46
def __socket_read():
 
47
 
 
48
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 
49
    s.connect((__HOST, __PORT))
 
50
    return s.makefile().read()
 
51
 
 
52
 
 
53
 
 
54
def poll_all():
 
55
 
 
56
    try:
 
57
        return __split( __socket_read() )
 
58
    except Exception, exc:
 
59
        log("Cannot retrieve HDD temperature. Error was %s" % (exc,))
 
60
        return []
 
61
 
 
62
 
 
63
 
 
64
def available_devices():
 
65
 
 
66
    return [ s.device for s in poll_all() ]
 
67
 
 
68
 
 
69
 
 
70
def poll(device):
 
71
 
 
72
    for s in poll_all():
 
73
 
 
74
        if (s.device == device):
 
75
            return s
 
76
 
 
77
    else:
 
78
        log("Cannot retrieve HDD temperature for device %s." % (device,))
 
79
        return None
 
80
 
 
81
 
 
82
 
 
83
 
 
84
if __name__ == '__main__':
 
85
 
 
86
    print available_devices()
 
87
    print poll('/dev/hda')
 
88
    print poll_all()
 
89