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
|
#! /usr/bin/python
# -*- coding: latin-1 -*-
#Copyright (c) 2011 David Calle <davidc@framli.eu>
#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 3 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, see <http://www.gnu.org/licenses/>.
import sys, os
from gi.repository import GLib, GObject, Gio
from gi.repository import Dee
_m = dir(Dee.SequenceModel)
from gi.repository import Unity
import socket
socket.setdefaulttimeout(5)
BUS_NAME = "net.launchpad.scope.math.calculator"
class Daemon:
def __init__ (self):
self.scope = Unity.Scope.new ("/net/launchpad/scope/math/calculator")
self.scope.search_in_global = False
self.scope.connect ("notify::active-search", self.on_search_changed)
self.scope.connect ("notify::active-global-search", self.on_search_changed)
self.scope.connect ("notify::active", self.on_search_changed)
self.scope.connect ("filters-changed", self.on_search_changed);
self.scope.export()
def get_search_string (self):
search = self.scope.props.active_global_search
if search is None:
search = self.scope.props.active_search
return search.props.search_string if search else None
def on_search_changed (self, scope, param_spec=None):
search = self.get_search_string()
print "Search changed to: '%s'" % search
results_global = self.scope.props.global_results_model
results_global.clear()
self.update_results_model (search, results_global)
results_global.flush_revision_queue ()
def update_results_model(self, search, model):
self.calc(search,model)
def calc(self, search, model):
import commands
print "calc!"
icon = "gnome-calculator"
search = search.replace('(','\(')
search = search.replace(')','\)')
search = search.replace(',','.')
search = search.replace('pi','π')
search = search.replace('"','')
search = search.replace('\'','')
search = search.replace('&','')
search = search.replace('|','')
hint = search
hint = hint.replace('\(','(')
hint = hint.replace('\)',')')
hint = hint.replace('sqrt','√')
calc_result=commands.getoutput('gcalctool -s %s' % str(search))
print search
print calc_result
if search:
if not (calc_result.startswith("Argument")) and not (calc_result.startswith("Error")):
model.append('', icon, 0, "text/html", calc_result, hint, '')
if __name__ == "__main__":
session_bus_connection = Gio.bus_get_sync (Gio.BusType.SESSION, None)
session_bus = Gio.DBusProxy.new_sync (session_bus_connection, 0, None,
'org.freedesktop.DBus',
'/org/freedesktop/DBus',
'org.freedesktop.DBus', None)
result = session_bus.call_sync('RequestName',
GLib.Variant ("(su)", (BUS_NAME, 0x4)),
0, -1, None)
# Unpack variant response with signature "(u)". 1 means we got it.
result = result.unpack()[0]
if result != 1 :
print >> sys.stderr, "Failed to own name %s. Bailing out." % BUS_NAME
raise SystemExit (1)
daemon = Daemon()
GObject.MainLoop().run()
|