summaryrefslogtreecommitdiff
path: root/lib/taurus/core/evaluation/dev_example.py
blob: 811cdc71287cc3ff5227dedf9d41a5c0ccf6dc11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python
#############################################################################
##
## This file is part of Taurus
## 
## http://taurus-scada.org
##
## Copyright 2011 CELLS / ALBA Synchrotron, Bellaterra, Spain
## 
## Taurus is free software: you can redistribute it and/or modify
## it under the terms of the GNU Lesser General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
## 
## Taurus is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU Lesser General Public License for more details.
## 
## You should have received a copy of the GNU Lesser General Public License
## along with Taurus.  If not, see <http://www.gnu.org/licenses/>.
##
#############################################################################

'''
Examples on using the evaluation scheme for exposing arbitrary non-tango quantities as taurus attributes
'''

__all__ = ['FreeSpaceDevice']

from taurus.core.evaluation import EvaluationDevice
import os, platform, ctypes

class FreeSpaceDevice(EvaluationDevice):
    '''A simple example of usage of the evaluation scheme for 
    creating a device that allows to obtain available space in a dir.
    
    Important: note that only those members listed in `_symbols` will be available
    '''
    _symbols = ['getFreeSpace']
        
    def getFreeSpace(self, dir):
        """ return free space (in bytes). 
        (Recipe adapted from `http://stackoverflow.com/questions/51658`)
        """
        if platform.system() == 'Windows':
            free_bytes = ctypes.c_ulonglong(0)
            ctypes.windll.kernel32.GetDiskFreeSpaceExW(ctypes.c_wchar_p(dir), None, None, ctypes.pointer(free_bytes))
            return free_bytes.value
        else:
            s =  os.statvfs(dir)
            return s.f_bsize * s.f_bavail
        

#===============================================================================
# Just for testing
#===============================================================================

def test1():
    import taurus
    a = taurus.Attribute('eval://dev=taurus.core.evaluation.dev_example.FreeSpaceDevice;getFreeSpace("/")/1024/1024') #calculates free space in Mb
    print "Free space: %iMb"%a.read().value
    
def test2():
    import sys
    from taurus.qt.qtgui.application import TaurusApplication
    from taurus.qt.qtgui.display import TaurusLabel
    app = TaurusApplication()
    
    w = TaurusLabel()
    attrname='eval://dev=taurus.core.evaluation.dev_example.FreeSpaceDevice;getFreeSpace("/")' #calculates free space in Mb
    
    w.setModel(attrname)

    w.show()
    sys.exit(app.exec_())
    
if __name__ == "__main__":
    test2()