Permalink
Browse files

Added calculator plugin.

  • Loading branch information...
1 parent dd0b137 commit 3476d447cc6d47bf2990670dd11ecd14aba7ce15 @jk0 committed Jun 29, 2012
Showing with 65 additions and 3 deletions.
  1. +3 −1 pyhole/plugin.py
  2. +60 −0 pyhole/plugins/calculator.py
  3. +2 −2 tools/run_pyhole.sh
View
4 pyhole/plugin.py
@@ -1,4 +1,4 @@
-# Copyright 2011 Chris Behrens
+# Copyright 2011-2012 Chris Behrens
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -23,6 +23,8 @@
LOG = log.get_logger()
+_plugin_instances = []
+_plugin_hooks = {}
def _reset_variables():
View
60 pyhole/plugins/calculator.py
@@ -0,0 +1,60 @@
+# Copyright 2012 Josh Kearney
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+"""Pyhole Calculator Plugin"""
+
+import urllib
+
+from pyhole import plugin
+from pyhole import utils
+
+
+class Calculator(plugin.Plugin):
+ """Provide access to Google's calculator"""
+
+ @plugin.hook_add_command("calc")
+ @utils.spawn
+ def calc(self, params=None, **kwargs):
+ """Use Google's calculator (ex: .c <expression>)"""
+ if params:
+ query = urllib.urlencode({"q": params})
+ url = "http://www.google.com/ig/calculator?hl=en&%s" % query
+ response = self.irc.fetch_url(url, self.name)
+ if not response:
+ return
+
+ json = response.read()
+ results = [i.replace("\"", "") for i in json[1:-1].split(",")]
+
+ expression = None
+ answer = None
+
+ for result in results:
+ result = result.split(": ")
+ if result[0] == "lhs":
+ expression = result[1]
+ elif result[0] == "rhs":
+ answer = result[1]
+
+ if expression and answer:
+ self.irc.reply("%s is %s" % (expression, answer))
+ else:
+ self.irc.reply("Unable to calculate '%s'" % params)
+ else:
+ self.irc.reply(self.calc.__doc__)
+
+ @plugin.hook_add_command("c")
+ def alias_c(self, params=None, **kwargs):
+ """Alias of calc"""
+ self.calc(params, **kwargs)
View
4 tools/run_pyhole.sh
@@ -1,6 +1,6 @@
#!/bin/sh
-# Copyright 2011 Josh Kearney
+# Copyright 2011-2012 Josh Kearney
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -14,4 +14,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.
-python -c "from pyhole import irc; irc.main()"
+python -c "from pyhole import irc; irc.main()" $@

0 comments on commit 3476d44

Please sign in to comment.