mod_util.py

changeset 123
aeb0d0788869
parent 121
ac07779f788d
child 124
7b2cd8b1ba86
equal deleted inserted replaced
122:f899af683bbe 123:aeb0d0788869
4 import subprocess 4 import subprocess
5 import re 5 import re
6 from modulecore import command_error 6 from modulecore import command_error
7 import modulecore as ModuleCore 7 import modulecore as ModuleCore
8 import utility 8 import utility
9 import calc
9 10
10 ModuleData = { 11 ModuleData = {
11 'commands': 12 'commands':
12 [ 13 [
13 { 14 {
40 41
41 { 42 {
42 'name': 'calc', 43 'name': 'calc',
43 'description': 'Calculates a mathematical expression using apcalc', 44 'description': 'Calculates a mathematical expression using apcalc',
44 'args': '<expression...>', 45 'args': '<expression...>',
46 'level': 'normal',
47 },
48
49 {
50 'name': 'calcfunctions',
51 'description': 'Lists the functions supported by .calc',
52 'args': None,
45 'level': 'normal', 53 'level': 'normal',
46 }, 54 },
47 55
48 { 56 {
49 'name': 'more', 57 'name': 'more',
158 expr = match.group(1) + str (value) + match.group(2) 166 expr = match.group(1) + str (value) + match.group(2)
159 match = rex.match (expr) 167 match = rex.match (expr)
160 168
161 return expr 169 return expr
162 170
171 def cmd_calcfunctions (bot, reply, **rest):
172 reply ('Available functions for .calc: %s' % \
173 ', '.join (sorted ([name for name, data in calc.Functions.iteritems()])))
174
163 def cmd_calc (bot, reply, args, **rest): 175 def cmd_calc (bot, reply, args, **rest):
164 expr = args['expression'] 176 reply (calc.calc (args['expression']))
165 177 # expr = args['expression']
166 try: 178 # try:
167 # Substitute some mathematical constants 179 # Substitute some mathematical constants
168 expr = mathsubstitute (expr, 'pi' , 3.14159265358979323846264338327950288419716939937510) 180 # expr = mathsubstitute (expr, 'pi' , 3.14159265358979323846264338327950288419716939937510)
169 expr = mathsubstitute (expr, 'e' , 2.71828182845904523536028747135266249775724709369995) 181 # expr = mathsubstitute (expr, 'e' , 2.71828182845904523536028747135266249775724709369995)
170 expr = mathsubstitute (expr, 'phi', 1.6180339887498948482) # golden ratio 182 # expr = mathsubstitute (expr, 'phi', 1.6180339887498948482) # golden ratio
171 183
172 result = subprocess.check_output (['calc', '--', expr], stderr=subprocess.STDOUT) \ 184 # result = subprocess.check_output (['calc', '--', expr], stderr=subprocess.STDOUT) \
173 .replace ('\t', '') \ 185 # .replace ('\t', '') \
174 .replace ('\n', '') 186 # .replace ('\n', '')
175 187
176 errmatch = re.compile (r'^.*\bError\b.*$').match (result) 188 # errmatch = re.compile (r'^.*\bError\b.*$').match (result)
177 189
178 if errmatch: 190 # if errmatch:
179 command_error ('math error') 191 # command_error ('math error')
180 return 192 # return
181 193
182 reply ('Result: %s' % result) 194 # reply ('Result: %s' % result)
183 except subprocess.CalledProcessError as e: 195 # except subprocess.CalledProcessError as e:
184 command_error (e.output.split('\n')[0]) 196 # command_error (e.output.split('\n')[0])
185 except OSError as e: 197 # except OSError as e:
186 command_error ('failed to execute calc: ' + e.strerror) 198 # command_error ('failed to execute calc: ' + e.strerror)
187 199
188 def cmd_more (commandObject, **rest): 200 def cmd_more (commandObject, **rest):
189 ModuleCore.print_responses (commandObject) 201 ModuleCore.print_responses (commandObject)
190 202
191 def cmd_yes (**k): 203 def cmd_yes (**k):

mercurial