mod_util.py

Wed, 31 Dec 2014 15:49:11 -0500

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Wed, 31 Dec 2014 15:49:11 -0500
changeset 114
0d1fd111cb22
parent 113
08e9b1c1b324
child 117
6c0609395889
permissions
-rw-r--r--

- .idgames now works with the page system

73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1 import math
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
2 import urllib2
76
a2fe9ba3041a - various fixes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
3 import json
105
84bd617a2520 - added .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 102
diff changeset
4 import subprocess
84bd617a2520 - added .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 102
diff changeset
5 import re
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
6 from modulecore import command_error
102
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
7 import modulecore as ModuleCore
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
8
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
9 ModuleData = {
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
10 'commands':
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
11 [
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
12 {
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
13 'name': 'convert',
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
14 'description': 'Performs numeric conversion',
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
15 'args': '<value> as <valuetype>',
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
16 'level': 'normal',
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
17 },
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
18
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
19 {
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
20 'name': 'ud',
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
21 'description': 'Looks up a term in urban dictionary',
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
22 'args': '<term...>',
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
23 'level': 'normal',
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
24 },
102
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
25
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
26 {
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
27 'name': 'commands',
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
28 'description': 'Lists commands available to the calling user',
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
29 'args': None,
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
30 'level': 'normal',
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
31 },
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
32
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
33 {
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
34 'name': 'help',
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
35 'description': 'Prints help about a given command',
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
36 'args': '<command>',
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
37 'level': 'normal',
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
38 },
105
84bd617a2520 - added .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 102
diff changeset
39
84bd617a2520 - added .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 102
diff changeset
40 {
84bd617a2520 - added .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 102
diff changeset
41 'name': 'calc',
84bd617a2520 - added .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 102
diff changeset
42 'description': 'Calculates a mathematical expression using apcalc',
84bd617a2520 - added .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 102
diff changeset
43 'args': '<expression...>',
84bd617a2520 - added .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 102
diff changeset
44 'level': 'normal',
84bd617a2520 - added .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 102
diff changeset
45 },
113
08e9b1c1b324 - added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents: 111
diff changeset
46
08e9b1c1b324 - added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents: 111
diff changeset
47 {
08e9b1c1b324 - added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents: 111
diff changeset
48 'name': 'more',
08e9b1c1b324 - added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents: 111
diff changeset
49 'description': 'Prints subsequent command result pages',
08e9b1c1b324 - added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents: 111
diff changeset
50 'args': None,
08e9b1c1b324 - added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents: 111
diff changeset
51 'level': 'normal',
08e9b1c1b324 - added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents: 111
diff changeset
52 },
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
53 ]
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
54 }
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
55
113
08e9b1c1b324 - added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents: 111
diff changeset
56 def cmd_convert (bot, args, reply, **rest):
08e9b1c1b324 - added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents: 111
diff changeset
57 try:
08e9b1c1b324 - added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents: 111
diff changeset
58 value = float (args['value'])
08e9b1c1b324 - added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents: 111
diff changeset
59 except Exception as e:
08e9b1c1b324 - added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents: 111
diff changeset
60 command_error (str (e))
08e9b1c1b324 - added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents: 111
diff changeset
61
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
62 valuetype = args['valuetype']
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
63
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
64 if valuetype in ['radians', 'degrees']:
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
65 if valuetype == 'radians':
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
66 radvalue = value
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
67 degvalue = (value * 180.) / math.pi
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
68 else:
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
69 radvalue = (value * math.pi) / 180.
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
70 degvalue = value
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
71
113
08e9b1c1b324 - added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents: 111
diff changeset
72 reply ('%s radians, %s degrees (%s)' % (radvalue, degvalue, degvalue % 360.))
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
73 return
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
74
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
75 if valuetype in ['celsius', 'fahrenheit']:
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
76 if valuetype == 'celsius':
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
77 celvalue = value
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
78 fahrvalue = value * 1.8 + 32
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
79 else:
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
80 celvalue = (value - 32) / 1.8
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
81 fahrvalue = value
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
82
113
08e9b1c1b324 - added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents: 111
diff changeset
83 reply ('%s degrees celsius, %s degrees fahrenheit' % (celvalue, fahrvalue))
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
84 return
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
85
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
86 command_error ('unknown valuetype %s, expected one of: degrees, radians (angle conversion), ' +
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
87 'celsius, fahrenheit (temperature conversion)' % valuetype)
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
88
113
08e9b1c1b324 - added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents: 111
diff changeset
89 def cmd_ud (bot, args, reply, **rest):
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
90 try:
76
a2fe9ba3041a - various fixes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
91 url = 'http://api.urbandictionary.com/v0/define?term=%s' % (args['term'].replace (' ', '%20'))
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
92 response = urllib2.urlopen (url).read()
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
93 data = json.loads (response)
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
94
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
95 if not 'list' in data \
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
96 or len(data['list']) == 0 \
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
97 or not 'word' in data['list'][0] \
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
98 or not 'definition' in data['list'][0]:
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
99 command_error ("couldn't find a definition of \002%s\002" % args['term'])
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
100
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
101 word = data['list'][0]['word']
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
102 definition = data['list'][0]['definition'].replace ('\r', ' ').replace ('\n', ' ').replace (' ', ' ')
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
103 up = data['list'][0]['thumbs_up']
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
104 down = data['list'][0]['thumbs_down']
113
08e9b1c1b324 - added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents: 111
diff changeset
105 reply ("\002%s\002: %s\0033 %d\003 up,\0035 %d\003 down" % (word, definition, up, down))
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
106 except Exception as e:
102
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
107 command_error ('Urban dictionary lookup failed: %s' % e)
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
108
113
08e9b1c1b324 - added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents: 111
diff changeset
109 def cmd_commands (bot, reply, ident, host, **rest):
102
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
110 commandlist = ModuleCore.get_available_commands (ident, host)
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
111 partitioned=[]
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
112
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
113 while len (commandlist) > 0:
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
114 partitioned.append (commandlist[0:15])
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
115 commandlist = commandlist[15:]
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
116
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
117 for part in partitioned:
113
08e9b1c1b324 - added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents: 111
diff changeset
118 reply ('\002Available commands\002: %s' % (", ".join (part)))
102
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
119
113
08e9b1c1b324 - added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents: 111
diff changeset
120 def cmd_help (bot, reply, ident, host, args, **rest):
102
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
121 cmd = ModuleCore.get_command_by_name (args['command'])
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
122
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
123 if not cmd:
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
124 command_error ('unknown command \'%s\'' % args['command'])
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
125
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
126 if not ModuleCore.is_available (cmd, ident, host):
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
127 command_error ('you may not use %s' % cmd['name'])
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
128
113
08e9b1c1b324 - added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents: 111
diff changeset
129 reply ('%s %s: %s' % (cmd['name'], cmd['args'], cmd['description']))
105
84bd617a2520 - added .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 102
diff changeset
130
106
31583e5b2f49 - added e and phi to .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 105
diff changeset
131 def mathsubstitute (expr, token, value):
31583e5b2f49 - added e and phi to .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 105
diff changeset
132 rex = re.compile (r'^(.*)\b' + token + r'\b(.*)$')
31583e5b2f49 - added e and phi to .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 105
diff changeset
133 match = rex.match (expr)
31583e5b2f49 - added e and phi to .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 105
diff changeset
134
31583e5b2f49 - added e and phi to .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 105
diff changeset
135 while match:
31583e5b2f49 - added e and phi to .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 105
diff changeset
136 expr = match.group(1) + str (value) + match.group(2)
31583e5b2f49 - added e and phi to .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 105
diff changeset
137 match = rex.match (expr)
31583e5b2f49 - added e and phi to .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 105
diff changeset
138
31583e5b2f49 - added e and phi to .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 105
diff changeset
139 return expr
31583e5b2f49 - added e and phi to .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 105
diff changeset
140
113
08e9b1c1b324 - added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents: 111
diff changeset
141 def cmd_calc (bot, reply, args, **rest):
105
84bd617a2520 - added .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 102
diff changeset
142 expr = args['expression']
84bd617a2520 - added .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 102
diff changeset
143
84bd617a2520 - added .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 102
diff changeset
144 try:
106
31583e5b2f49 - added e and phi to .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 105
diff changeset
145 # Substitute some mathematical constants
107
14e1d18d2a2a - add more decimals to the mathematical constants
Teemu Piippo <crimsondusk64@gmail.com>
parents: 106
diff changeset
146 expr = mathsubstitute (expr, 'pi' , 3.14159265358979323846264338327950288419716939937510)
14e1d18d2a2a - add more decimals to the mathematical constants
Teemu Piippo <crimsondusk64@gmail.com>
parents: 106
diff changeset
147 expr = mathsubstitute (expr, 'e' , 2.71828182845904523536028747135266249775724709369995)
14e1d18d2a2a - add more decimals to the mathematical constants
Teemu Piippo <crimsondusk64@gmail.com>
parents: 106
diff changeset
148 expr = mathsubstitute (expr, 'phi', 1.6180339887498948482) # golden ratio
105
84bd617a2520 - added .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 102
diff changeset
149
84bd617a2520 - added .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 102
diff changeset
150 result = subprocess.check_output (['calc', expr], stderr=subprocess.STDOUT) \
84bd617a2520 - added .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 102
diff changeset
151 .replace ('\t', '') \
84bd617a2520 - added .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 102
diff changeset
152 .replace ('\n', '')
84bd617a2520 - added .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 102
diff changeset
153
84bd617a2520 - added .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 102
diff changeset
154 errmatch = re.compile (r'^.*\bError\b.*$').match (result)
84bd617a2520 - added .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 102
diff changeset
155
84bd617a2520 - added .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 102
diff changeset
156 if errmatch:
84bd617a2520 - added .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 102
diff changeset
157 command_error ('math error')
84bd617a2520 - added .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 102
diff changeset
158 return
84bd617a2520 - added .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 102
diff changeset
159
113
08e9b1c1b324 - added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents: 111
diff changeset
160 reply ('Result: %s' % result)
105
84bd617a2520 - added .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 102
diff changeset
161 except subprocess.CalledProcessError as e:
84bd617a2520 - added .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 102
diff changeset
162 command_error (e.output.split('\n')[0])
84bd617a2520 - added .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 102
diff changeset
163 except OSError as e:
84bd617a2520 - added .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 102
diff changeset
164 command_error ('failed to execute calc: ' + e.strerror)
113
08e9b1c1b324 - added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents: 111
diff changeset
165
08e9b1c1b324 - added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents: 111
diff changeset
166 def cmd_more (bot, replyto, **rest):
08e9b1c1b324 - added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents: 111
diff changeset
167 ModuleCore.print_responses (bot, replyto)

mercurial