summaryrefslogtreecommitdiff
path: root/CompizTools.py
blob: 8a613aab7de83a4f3baa9840174b9fde30e8b2ef (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#
# Copyright Eyemagnet Limited
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program 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
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.
#
# Author: Kristian F. Erikson <kristian@erikson.dk>
#

import dbus
from CompizError import CompizPluginTypeArgError

def convertFromDbusToTypes(arg):
    if type(arg) == type(dbus.Boolean()):
        return type(True)                   # "<type 'bool'>"
    elif type(arg) == type(dbus.Int32()):
        return type(1)                      # "<type 'int'>"
    elif type(arg) == type(dbus.Double()):
        return type(1.012)                  # "<type 'float'>"
    elif type(arg) == type(dbus.String()):
        return type("Hello World ;)")       # "<type 'str'>"
    elif type(arg) == type(dbus.Array()):
        signature = arg.signature           # If we get an array see what's inside
        if signature == dbus.Signature('b'):
            return [type(True)]                   # "<type 'bool'>" in an arrray
        elif signature == dbus.Signature('i'):
            return [type(1)]                      # "<type 'int'>" in an arrray
        elif signature == dbus.Signature('d'):
            return [type(1.012)]                  # "<type 'float'> in an arrray
        elif signature == dbus.Signature('s'):
            return [type("Hello World ;)")]       # "<type 'str'>" in an arrray
    else:
        return arg     # If we couldn't convert return original


def convertFromStringToTypes(arg):
    result = {}
    for key in arg.keys():
        if arg[key] == 'bool':
            result[key] = type(True)                   # "<type 'bool'>"
        elif arg[key] == 'int':
            result[key] = type(1)                      # "<type 'int'>"
        elif arg[key] == 'float':
            result[key] = type(1.012)                  # "<type 'float'>"
        elif arg[key] == 'double':
            result[key] = type(1.012)                  # "<type 'float'>"
        elif arg[key] == 'string':
            result[key] = type("Hello World ;)")       # "<type 'str'>"
        else:
            result[key] = None
    return result


def convertFromSimpleDbusToPython(arg):
    if type(arg) == type(dbus.Boolean()):
        return bool(arg)
    elif type(arg) == type(dbus.Int32()):
        return int(arg)
    elif type(arg) == type(dbus.Double()):
        return float(arg)
    elif type(arg) == type(dbus.String()):
        return str(arg)


def convertFromDbusToPython(arg):
    if type(arg) == type(dbus.Boolean()):
        return bool(arg)
    elif type(arg) == type(dbus.Int32()):
        return int(arg)
    elif type(arg) == type(dbus.Double()):
        return float(arg)
    elif type(arg) == type(dbus.String()):
        return unicode(arg)
    elif type(arg) == type(dbus.Array()):
        signature = arg.signature # If we get an array convert what's inside
        arg = list(arg) # First convert to a list
        newlist = []
        if signature == dbus.Signature('b'): # Convert each item on the list
            for item in arg:
                newlist.append(bool(item))
        elif signature == dbus.Signature('i'):
            for item in arg:
                newlist.append(int(item))
        elif signature == dbus.Signature('d'):
            for item in arg:
                newlist.append(float(item))
        elif signature == dbus.Signature('s'):
            for item in arg:
                newlist.append(unicode(item))
        return newlist # Return newly built list
    elif type(arg) == type((1,2)):
        newlist = []
        for item in arg:
            newlist.append(convertFromSimpleDbusToPython(item))
        return newlist # Return newly built list
    else:
        return arg    # If we couldn't convert return original


def convertPythonToDbusSignature(arg):
    if type(arg) == type(True):
        return dbus.Signature('b')
    elif type(arg) == type(234):
        return dbus.Signature('i')
    elif type(arg) == type(44.44):
        return dbus.Signature('d')
    elif type(arg) == type("s") or type(arg) == type('s') or type(arg) == type(unicode('s')):
        return dbus.Signature('s')
    else:
        raise CompizPluginTypeArgError


def convertFromSimplePythonToDbus(arg):
    if type(arg) == type(True):
        return dbus.Boolean(arg)
    elif type(arg) == type(234):
        return dbus.Int32(arg)
    elif type(arg) == type(44.44):
        return dbus.Double(arg)
    elif type(arg) == type("s") or type(arg) == type('s') or type(arg) == type(unicode('s')):
        return dbus.String(unicode(arg))
    else:
        raise CompizPluginTypeArgError


def convertFromPythonToDbus(arg):
    if type(arg) == type(True):
        return dbus.Boolean(arg)
    elif type(arg) == type(234):
        return dbus.Int32(arg)
    elif type(arg) == type(44.44):
        return dbus.Double(arg)
    elif type(arg) == (type("s") or type('s') or type(unicode('s'))):
        return dbus.String(unicode(arg))
    elif type(arg) == type([]) or type(arg) == type((1,2)):
        signature = convertPythonToDbusSignature(arg[0])
        newlist = []
        if signature == dbus.Signature('b'): # Convert each item on the list
            for item in arg:
                if type(item) == type(True):    # Type check each item as well
                    newlist.append(convertFromSimplePythonToDbus(item))
                else:
                    raise CompizPluginTypeArgError
        elif signature == dbus.Signature('i'):
            for item in arg:
                if type(item) == type(123):
                    newlist.append(convertFromSimplePythonToDbus(item))
                else:
                    raise CompizPluginTypeArgError
        elif signature == dbus.Signature('d'):
            for item in arg:
                if type(item) == type(23.12):
                    newlist.append(convertFromSimplePythonToDbus(item))
                else:
                    raise CompizPluginTypeArgError
        elif signature == dbus.Signature('s'):
            for item in arg:
                if type(item) == type("s") or type(item) == type('s') or type(item) == type(unicode('s')):
                    newlist.append(convertFromSimplePythonToDbus(item))
                else:
                    raise CompizPluginTypeArgError
        return dbus.Array(newlist, signature)
    else:
        raise CompizPluginTypeArgError   # Unsupported type