mod_util.py

Tue, 11 Aug 2015 19:12:30 +0300

author
Teemu Piippo <tsapii@utu.fi>
date
Tue, 11 Aug 2015 19:12:30 +0300
changeset 151
e24793fae424
parent 150
2fd1f6ee05f5
child 152
1b734faab67a
permissions
-rw-r--r--

Yet more fixes

124
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
1 '''
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
2 Copyright 2014-2015 Teemu Piippo
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
3 All rights reserved.
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
4
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
5 Redistribution and use in source and binary forms, with or without
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
6 modification, are permitted provided that the following conditions
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
7 are met:
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
8
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
9 1. Redistributions of source code must retain the above copyright
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
10 notice, this list of conditions and the following disclaimer.
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
11 2. Redistributions in binary form must reproduce the above copyright
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
12 notice, this list of conditions and the following disclaimer in the
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
13 documentation and/or other materials provided with the distribution.
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
14 3. The name of the author may not be used to endorse or promote products
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
15 derived from this software without specific prior written permission.
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
16
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
17 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
18 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
20 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
22 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
26 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
27 '''
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
28
146
c17b82b1f573 Mercurial handling major overhaul. Also get some stuff ready for Python 3
Teemu Piippo <tsapii@utu.fi>
parents: 128
diff changeset
29 from __future__ import print_function
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
30 import math
76
a2fe9ba3041a - various fixes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
31 import json
105
84bd617a2520 - added .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 102
diff changeset
32 import subprocess
84bd617a2520 - added .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 102
diff changeset
33 import re
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
34 from modulecore import command_error
102
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
35 import modulecore as ModuleCore
121
ac07779f788d - reworked mercurial repository handling, removed hardcoded values
Teemu Piippo <crimsondusk64@gmail.com>
parents: 117
diff changeset
36 import utility
123
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents: 121
diff changeset
37 import calc
148
7e225c2659b8 Added .py
Teemu Piippo <tsapii@utu.fi>
parents: 146
diff changeset
38 import urllib
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
39
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
40 ModuleData = {
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
41 'commands':
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
42 [
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
43 {
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
44 'name': 'convert',
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
45 'description': 'Performs numeric conversion',
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
46 'args': '<value> as <valuetype>',
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
47 'level': 'normal',
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
48 },
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
49
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
50 {
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
51 'name': 'ud',
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
52 'description': 'Looks up a term in urban dictionary',
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
53 'args': '<term...>',
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
54 'level': 'normal',
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
55 },
102
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
56
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
57 {
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
58 'name': 'commands',
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
59 'description': 'Lists commands available to the calling user',
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
60 'args': None,
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
61 'level': 'normal',
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
62 },
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
63
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
64 {
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
65 'name': 'help',
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
66 'description': 'Prints help about a given command',
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
67 'args': '<command>',
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
68 'level': 'normal',
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
69 },
105
84bd617a2520 - added .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 102
diff changeset
70
84bd617a2520 - added .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 102
diff changeset
71 {
84bd617a2520 - added .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 102
diff changeset
72 'name': 'calc',
84bd617a2520 - added .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 102
diff changeset
73 'description': 'Calculates a mathematical expression using apcalc',
84bd617a2520 - added .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 102
diff changeset
74 'args': '<expression...>',
84bd617a2520 - added .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 102
diff changeset
75 'level': 'normal',
84bd617a2520 - added .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 102
diff changeset
76 },
113
08e9b1c1b324 - added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents: 111
diff changeset
77
08e9b1c1b324 - added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents: 111
diff changeset
78 {
123
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents: 121
diff changeset
79 'name': 'calcfunctions',
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents: 121
diff changeset
80 'description': 'Lists the functions supported by .calc',
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents: 121
diff changeset
81 'args': None,
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents: 121
diff changeset
82 'level': 'normal',
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents: 121
diff changeset
83 },
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents: 121
diff changeset
84
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents: 121
diff changeset
85 {
113
08e9b1c1b324 - added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents: 111
diff changeset
86 'name': 'more',
08e9b1c1b324 - added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents: 111
diff changeset
87 '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
88 'args': None,
08e9b1c1b324 - added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents: 111
diff changeset
89 'level': 'normal',
08e9b1c1b324 - added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents: 111
diff changeset
90 },
117
6c0609395889 - added a confirm system, probably useful in the future
Teemu Piippo <crimsondusk64@gmail.com>
parents: 113
diff changeset
91
6c0609395889 - added a confirm system, probably useful in the future
Teemu Piippo <crimsondusk64@gmail.com>
parents: 113
diff changeset
92 {
6c0609395889 - added a confirm system, probably useful in the future
Teemu Piippo <crimsondusk64@gmail.com>
parents: 113
diff changeset
93 'name': 'yes',
6c0609395889 - added a confirm system, probably useful in the future
Teemu Piippo <crimsondusk64@gmail.com>
parents: 113
diff changeset
94 'description': 'Confirms the previous command',
6c0609395889 - added a confirm system, probably useful in the future
Teemu Piippo <crimsondusk64@gmail.com>
parents: 113
diff changeset
95 'args': None,
6c0609395889 - added a confirm system, probably useful in the future
Teemu Piippo <crimsondusk64@gmail.com>
parents: 113
diff changeset
96 'level': 'normal',
6c0609395889 - added a confirm system, probably useful in the future
Teemu Piippo <crimsondusk64@gmail.com>
parents: 113
diff changeset
97 },
6c0609395889 - added a confirm system, probably useful in the future
Teemu Piippo <crimsondusk64@gmail.com>
parents: 113
diff changeset
98
6c0609395889 - added a confirm system, probably useful in the future
Teemu Piippo <crimsondusk64@gmail.com>
parents: 113
diff changeset
99 {
6c0609395889 - added a confirm system, probably useful in the future
Teemu Piippo <crimsondusk64@gmail.com>
parents: 113
diff changeset
100 'name': 'no',
6c0609395889 - added a confirm system, probably useful in the future
Teemu Piippo <crimsondusk64@gmail.com>
parents: 113
diff changeset
101 'description': 'Unconfirms the previous command',
6c0609395889 - added a confirm system, probably useful in the future
Teemu Piippo <crimsondusk64@gmail.com>
parents: 113
diff changeset
102 'args': None,
6c0609395889 - added a confirm system, probably useful in the future
Teemu Piippo <crimsondusk64@gmail.com>
parents: 113
diff changeset
103 'level': 'normal',
6c0609395889 - added a confirm system, probably useful in the future
Teemu Piippo <crimsondusk64@gmail.com>
parents: 113
diff changeset
104 },
121
ac07779f788d - reworked mercurial repository handling, removed hardcoded values
Teemu Piippo <crimsondusk64@gmail.com>
parents: 117
diff changeset
105
ac07779f788d - reworked mercurial repository handling, removed hardcoded values
Teemu Piippo <crimsondusk64@gmail.com>
parents: 117
diff changeset
106 {
ac07779f788d - reworked mercurial repository handling, removed hardcoded values
Teemu Piippo <crimsondusk64@gmail.com>
parents: 117
diff changeset
107 'name': 'bitly',
ac07779f788d - reworked mercurial repository handling, removed hardcoded values
Teemu Piippo <crimsondusk64@gmail.com>
parents: 117
diff changeset
108 'description': 'Shortens a link using bit.ly',
ac07779f788d - reworked mercurial repository handling, removed hardcoded values
Teemu Piippo <crimsondusk64@gmail.com>
parents: 117
diff changeset
109 'args': '<link>',
ac07779f788d - reworked mercurial repository handling, removed hardcoded values
Teemu Piippo <crimsondusk64@gmail.com>
parents: 117
diff changeset
110 'level': 'normal'
ac07779f788d - reworked mercurial repository handling, removed hardcoded values
Teemu Piippo <crimsondusk64@gmail.com>
parents: 117
diff changeset
111 },
148
7e225c2659b8 Added .py
Teemu Piippo <tsapii@utu.fi>
parents: 146
diff changeset
112
7e225c2659b8 Added .py
Teemu Piippo <tsapii@utu.fi>
parents: 146
diff changeset
113 {
7e225c2659b8 Added .py
Teemu Piippo <tsapii@utu.fi>
parents: 146
diff changeset
114 'name': 'py',
7e225c2659b8 Added .py
Teemu Piippo <tsapii@utu.fi>
parents: 146
diff changeset
115 'description': 'Evaluates python',
7e225c2659b8 Added .py
Teemu Piippo <tsapii@utu.fi>
parents: 146
diff changeset
116 'args': '<command...>',
7e225c2659b8 Added .py
Teemu Piippo <tsapii@utu.fi>
parents: 146
diff changeset
117 'level': 'normal'
7e225c2659b8 Added .py
Teemu Piippo <tsapii@utu.fi>
parents: 146
diff changeset
118 }
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
119 ]
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
120 }
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
121
113
08e9b1c1b324 - added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents: 111
diff changeset
122 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
123 try:
08e9b1c1b324 - added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents: 111
diff changeset
124 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
125 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
126 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
127
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
128 valuetype = args['valuetype']
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
129
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
130 if valuetype in ['radians', 'degrees']:
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
131 if valuetype == 'radians':
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
132 radvalue = value
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
133 degvalue = (value * 180.) / math.pi
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
134 else:
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
135 radvalue = (value * math.pi) / 180.
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
136 degvalue = value
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
137
113
08e9b1c1b324 - added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents: 111
diff changeset
138 reply ('%s radians, %s degrees (%s)' % (radvalue, degvalue, degvalue % 360.))
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
139 return
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
140
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
141 if valuetype in ['celsius', 'fahrenheit']:
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
142 if valuetype == 'celsius':
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
143 celvalue = value
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
144 fahrvalue = value * 1.8 + 32
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
145 else:
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
146 celvalue = (value - 32) / 1.8
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
147 fahrvalue = value
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
148
113
08e9b1c1b324 - added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents: 111
diff changeset
149 reply ('%s degrees celsius, %s degrees fahrenheit' % (celvalue, fahrvalue))
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
150 return
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
151
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
152 command_error ('unknown valuetype %s, expected one of: degrees, radians (angle conversion), ' +
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
153 'celsius, fahrenheit (temperature conversion)' % valuetype)
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
154
113
08e9b1c1b324 - added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents: 111
diff changeset
155 def cmd_ud (bot, args, reply, **rest):
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
156 try:
76
a2fe9ba3041a - various fixes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
157 url = 'http://api.urbandictionary.com/v0/define?term=%s' % (args['term'].replace (' ', '%20'))
146
c17b82b1f573 Mercurial handling major overhaul. Also get some stuff ready for Python 3
Teemu Piippo <tsapii@utu.fi>
parents: 128
diff changeset
158 response = utility.read_url (url)
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
159 data = json.loads (response)
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
160
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
161 if not 'list' in data \
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
162 or len(data['list']) == 0 \
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
163 or not 'word' in data['list'][0] \
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
164 or not 'definition' in data['list'][0]:
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
165 command_error ("couldn't find a definition of \002%s\002" % args['term'])
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
166
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
167 word = data['list'][0]['word']
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
168 definition = data['list'][0]['definition'].replace ('\r', ' ').replace ('\n', ' ').replace (' ', ' ')
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
169 up = data['list'][0]['thumbs_up']
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
170 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
171 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
172 except Exception as e:
102
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
173 command_error ('Urban dictionary lookup failed: %s' % e)
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
174
113
08e9b1c1b324 - added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents: 111
diff changeset
175 def cmd_commands (bot, reply, ident, host, **rest):
102
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
176 commandlist = ModuleCore.get_available_commands (ident, host)
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
177 partitioned=[]
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
178
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
179 while len (commandlist) > 0:
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
180 partitioned.append (commandlist[0:15])
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
181 commandlist = commandlist[15:]
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
182
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
183 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
184 reply ('\002Available commands\002: %s' % (", ".join (part)))
102
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
185
113
08e9b1c1b324 - added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents: 111
diff changeset
186 def cmd_help (bot, reply, ident, host, args, **rest):
102
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
187 cmd = ModuleCore.get_command_by_name (args['command'])
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
188
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
189 if not cmd:
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
190 command_error ('unknown command \'%s\'' % args['command'])
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
191
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
192 if not ModuleCore.is_available (cmd, ident, host):
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
193 command_error ('you may not use %s' % cmd['name'])
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
194
113
08e9b1c1b324 - added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents: 111
diff changeset
195 reply ('%s %s: %s' % (cmd['name'], cmd['args'], cmd['description']))
105
84bd617a2520 - added .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 102
diff changeset
196
106
31583e5b2f49 - added e and phi to .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 105
diff changeset
197 def mathsubstitute (expr, token, value):
31583e5b2f49 - added e and phi to .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 105
diff changeset
198 rex = re.compile (r'^(.*)\b' + token + r'\b(.*)$')
31583e5b2f49 - added e and phi to .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 105
diff changeset
199 match = rex.match (expr)
31583e5b2f49 - added e and phi to .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 105
diff changeset
200
31583e5b2f49 - added e and phi to .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 105
diff changeset
201 while match:
31583e5b2f49 - added e and phi to .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 105
diff changeset
202 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
203 match = rex.match (expr)
31583e5b2f49 - added e and phi to .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 105
diff changeset
204
31583e5b2f49 - added e and phi to .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 105
diff changeset
205 return expr
31583e5b2f49 - added e and phi to .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 105
diff changeset
206
123
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents: 121
diff changeset
207 def cmd_calcfunctions (bot, reply, **rest):
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents: 121
diff changeset
208 reply ('Available functions for .calc: %s' % \
146
c17b82b1f573 Mercurial handling major overhaul. Also get some stuff ready for Python 3
Teemu Piippo <tsapii@utu.fi>
parents: 128
diff changeset
209 ', '.join (sorted ([name for name, data in calc.Functions.items()])))
105
84bd617a2520 - added .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 102
diff changeset
210
123
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents: 121
diff changeset
211 def cmd_calc (bot, reply, args, **rest):
128
bd949c554dd2 Major refactor: moved calculator routines into a Calculator class.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 124
diff changeset
212 reply (calc.Calculator().calc (args['expression']))
123
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents: 121
diff changeset
213 # expr = args['expression']
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents: 121
diff changeset
214 # try:
106
31583e5b2f49 - added e and phi to .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 105
diff changeset
215 # Substitute some mathematical constants
123
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents: 121
diff changeset
216 # expr = mathsubstitute (expr, 'pi' , 3.14159265358979323846264338327950288419716939937510)
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents: 121
diff changeset
217 # expr = mathsubstitute (expr, 'e' , 2.71828182845904523536028747135266249775724709369995)
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents: 121
diff changeset
218 # expr = mathsubstitute (expr, 'phi', 1.6180339887498948482) # golden ratio
105
84bd617a2520 - added .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 102
diff changeset
219
123
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents: 121
diff changeset
220 # result = subprocess.check_output (['calc', '--', expr], stderr=subprocess.STDOUT) \
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents: 121
diff changeset
221 # .replace ('\t', '') \
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents: 121
diff changeset
222 # .replace ('\n', '')
105
84bd617a2520 - added .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 102
diff changeset
223
123
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents: 121
diff changeset
224 # errmatch = re.compile (r'^.*\bError\b.*$').match (result)
105
84bd617a2520 - added .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 102
diff changeset
225
123
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents: 121
diff changeset
226 # if errmatch:
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents: 121
diff changeset
227 # command_error ('math error')
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents: 121
diff changeset
228 # return
105
84bd617a2520 - added .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents: 102
diff changeset
229
123
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents: 121
diff changeset
230 # reply ('Result: %s' % result)
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents: 121
diff changeset
231 # except subprocess.CalledProcessError as e:
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents: 121
diff changeset
232 # command_error (e.output.split('\n')[0])
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents: 121
diff changeset
233 # except OSError as e:
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents: 121
diff changeset
234 # 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
235
121
ac07779f788d - reworked mercurial repository handling, removed hardcoded values
Teemu Piippo <crimsondusk64@gmail.com>
parents: 117
diff changeset
236 def cmd_more (commandObject, **rest):
ac07779f788d - reworked mercurial repository handling, removed hardcoded values
Teemu Piippo <crimsondusk64@gmail.com>
parents: 117
diff changeset
237 ModuleCore.print_responses (commandObject)
117
6c0609395889 - added a confirm system, probably useful in the future
Teemu Piippo <crimsondusk64@gmail.com>
parents: 113
diff changeset
238
6c0609395889 - added a confirm system, probably useful in the future
Teemu Piippo <crimsondusk64@gmail.com>
parents: 113
diff changeset
239 def cmd_yes (**k):
6c0609395889 - added a confirm system, probably useful in the future
Teemu Piippo <crimsondusk64@gmail.com>
parents: 113
diff changeset
240 ModuleCore.confirm (k, True)
6c0609395889 - added a confirm system, probably useful in the future
Teemu Piippo <crimsondusk64@gmail.com>
parents: 113
diff changeset
241
6c0609395889 - added a confirm system, probably useful in the future
Teemu Piippo <crimsondusk64@gmail.com>
parents: 113
diff changeset
242 def cmd_no (**k):
6c0609395889 - added a confirm system, probably useful in the future
Teemu Piippo <crimsondusk64@gmail.com>
parents: 113
diff changeset
243 ModuleCore.confirm (k, False)
121
ac07779f788d - reworked mercurial repository handling, removed hardcoded values
Teemu Piippo <crimsondusk64@gmail.com>
parents: 117
diff changeset
244
ac07779f788d - reworked mercurial repository handling, removed hardcoded values
Teemu Piippo <crimsondusk64@gmail.com>
parents: 117
diff changeset
245 def cmd_bitly (reply, args, **k):
ac07779f788d - reworked mercurial repository handling, removed hardcoded values
Teemu Piippo <crimsondusk64@gmail.com>
parents: 117
diff changeset
246 reply ('Result: %s' % utility.shorten_link (args['link']))
148
7e225c2659b8 Added .py
Teemu Piippo <tsapii@utu.fi>
parents: 146
diff changeset
247
7e225c2659b8 Added .py
Teemu Piippo <tsapii@utu.fi>
parents: 146
diff changeset
248 def cmd_py (reply, args, **rest):
7e225c2659b8 Added .py
Teemu Piippo <tsapii@utu.fi>
parents: 146
diff changeset
249 url = 'http://eval.appspot.com/eval?statement=' + urllib.quote (args['command'])
7e225c2659b8 Added .py
Teemu Piippo <tsapii@utu.fi>
parents: 146
diff changeset
250 result = utility.read_url (url).splitlines()
7e225c2659b8 Added .py
Teemu Piippo <tsapii@utu.fi>
parents: 146
diff changeset
251
7e225c2659b8 Added .py
Teemu Piippo <tsapii@utu.fi>
parents: 146
diff changeset
252 # \x0f is the 'reset colors' code, prepended to all reply lines to prevent other bots from
7e225c2659b8 Added .py
Teemu Piippo <tsapii@utu.fi>
parents: 146
diff changeset
253 # reacting to this .py call.
7e225c2659b8 Added .py
Teemu Piippo <tsapii@utu.fi>
parents: 146
diff changeset
254 if result[0] == 'Traceback (most recent call last):':
7e225c2659b8 Added .py
Teemu Piippo <tsapii@utu.fi>
parents: 146
diff changeset
255 reply ('\x0f' + result[-1])
7e225c2659b8 Added .py
Teemu Piippo <tsapii@utu.fi>
parents: 146
diff changeset
256 else:
7e225c2659b8 Added .py
Teemu Piippo <tsapii@utu.fi>
parents: 146
diff changeset
257 for line in result:
7e225c2659b8 Added .py
Teemu Piippo <tsapii@utu.fi>
parents: 146
diff changeset
258 reply ('\x0f' + line)

mercurial