mod_util.py

changeset 121
ac07779f788d
parent 117
6c0609395889
child 123
aeb0d0788869
equal deleted inserted replaced
120:9880bb697149 121:ac07779f788d
3 import json 3 import json
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 9
9 ModuleData = { 10 ModuleData = {
10 'commands': 11 'commands':
11 [ 12 [
12 { 13 {
61 { 62 {
62 'name': 'no', 63 'name': 'no',
63 'description': 'Unconfirms the previous command', 64 'description': 'Unconfirms the previous command',
64 'args': None, 65 'args': None,
65 'level': 'normal', 66 'level': 'normal',
67 },
68
69 {
70 'name': 'bitly',
71 'description': 'Shortens a link using bit.ly',
72 'args': '<link>',
73 'level': 'normal'
66 }, 74 },
67 ] 75 ]
68 } 76 }
69 77
70 def cmd_convert (bot, args, reply, **rest): 78 def cmd_convert (bot, args, reply, **rest):
159 # Substitute some mathematical constants 167 # Substitute some mathematical constants
160 expr = mathsubstitute (expr, 'pi' , 3.14159265358979323846264338327950288419716939937510) 168 expr = mathsubstitute (expr, 'pi' , 3.14159265358979323846264338327950288419716939937510)
161 expr = mathsubstitute (expr, 'e' , 2.71828182845904523536028747135266249775724709369995) 169 expr = mathsubstitute (expr, 'e' , 2.71828182845904523536028747135266249775724709369995)
162 expr = mathsubstitute (expr, 'phi', 1.6180339887498948482) # golden ratio 170 expr = mathsubstitute (expr, 'phi', 1.6180339887498948482) # golden ratio
163 171
164 result = subprocess.check_output (['calc', expr], stderr=subprocess.STDOUT) \ 172 result = subprocess.check_output (['calc', '--', expr], stderr=subprocess.STDOUT) \
165 .replace ('\t', '') \ 173 .replace ('\t', '') \
166 .replace ('\n', '') 174 .replace ('\n', '')
167 175
168 errmatch = re.compile (r'^.*\bError\b.*$').match (result) 176 errmatch = re.compile (r'^.*\bError\b.*$').match (result)
169 177
175 except subprocess.CalledProcessError as e: 183 except subprocess.CalledProcessError as e:
176 command_error (e.output.split('\n')[0]) 184 command_error (e.output.split('\n')[0])
177 except OSError as e: 185 except OSError as e:
178 command_error ('failed to execute calc: ' + e.strerror) 186 command_error ('failed to execute calc: ' + e.strerror)
179 187
180 def cmd_more (bot, replyto, **rest): 188 def cmd_more (commandObject, **rest):
181 ModuleCore.print_responses (bot, replyto) 189 ModuleCore.print_responses (commandObject)
182 190
183 def cmd_yes (**k): 191 def cmd_yes (**k):
184 ModuleCore.confirm (k, True) 192 ModuleCore.confirm (k, True)
185 193
186 def cmd_no (**k): 194 def cmd_no (**k):
187 ModuleCore.confirm (k, False) 195 ModuleCore.confirm (k, False)
196
197 def cmd_bitly (reply, args, **k):
198 reply ('Result: %s' % utility.shorten_link (args['link']))

mercurial