added mem and url validator plugins

This commit is contained in:
ipsum 2010-03-12 01:17:43 -05:00
parent 20fdb82b2d
commit 5115c0ad1d
3 changed files with 111 additions and 47 deletions

View File

@ -1,30 +1,61 @@
import os
import re import os
import re
from util import hook
from util import hook
@hook.command @hook.command
def mem(inp): def mem(bot):
".mem -- returns bot's current memory usage -- linux/windows only" ".mem -- returns bot's current memory usage"
if os.name == 'posix': if os.name=='posix':
status_file = open("/proc/%d/status" % os.getpid()).read() return posixmem()
line_pairs = re.findall(r"^(\w+):\s*(.*)\s*$", status_file, re.M) if os.name=='nt':
status = dict(line_pairs) return ntmem()
keys = 'VmSize VmLib VmData VmExe VmRSS VmStk'.split()
return ', '.join(key + ':' + status[key] for key in keys) return mem.__doc__
elif os.name == 'nt':
cmd = "tasklist /FI \"PID eq %s\" /FO CSV /NH" % os.getpid() def posixmem():
out = os.popen(cmd).read() return _VmB('Size: ') + ' ' + _VmB('Resident:') + ' ' + _VmB('Stack:')
total = 0 def ntmem():
for amount in re.findall(r'([,0-9]+) K', out): pid = os.getpid()
total += int(amount.replace(',', '')) total = 0
return 'memory usage: %d kB' % total cmd = "tasklist /FI \"PID eq {0}\" /FO CSV /NH".format(pid)
e = re.compile("[,0-9]+ K")
return mem.__doc__
meml = re.findall(e,os.popen(cmd).read())
print mem('')
if not mem1[0]:
return 'This os does not have the tasklist command installed'
for mem in meml:
total += int(mem.rstrip(" K").replace(",",""))
return 'Bot mem usage: ' + str(total) + ' K'
def _VmB(Key):
'''Private'''
_proc_status = '/proc/%d/status' % os.getpid()
_scale = {'kB': 1024.0, 'mB': 1024.0*1024.0,
'KB': 1024.0, 'MB': 1024.0*1024.0}
# get pseudo file /proc/<pid>/status
try:
t = open(_proc_status)
v = t.read()
t.close()
except:
return 0.0
# get Key line e.g. 'VmRSS: 9999 kB\n ...'
i = v.index(Key)
v = v[i:].split(None, 3)
if len(v) < 3:
return 0.0
return float(v[1]) * _scale[v[2]]

View File

@ -1,23 +1,23 @@
''' '''
regular.py regular.py
skybot plugin for testing regular expressions skybot plugin for testing regular expressions
by Ipsum by Ipsum
''' '''
import re import re
from util import hook from util import hook
@hook.command('re') @hook.command('re')
def reg(inp): def reg(inp):
".re <regex> <string> -- matches regular expression in given <string> "\ ".re <regex> <string> -- matches regular expression in given <string> "\
"(leave 2 spaces between)" "(leave 2 spaces between)"
query = inp.split(" ", 1) query = inp.split(" ", 1)
if not inp or len(query) != 2: if not inp or len(query) != 2:
return reg.__doc__ return reg.__doc__
return '|'.join(re.findall(query[0], query[1])) return '|'.join(re.findall(query[0], query[1]))

33
plugins/validate.py Normal file
View File

@ -0,0 +1,33 @@
'''
Runs a given url through the w3c validator and queries
the result header for information
'''
import urllib
from util import hook
path = 'http://validator.w3.org/check?uri=%s'
@hook.command('val')
@hook.command
def validate(inp):
'''.val/.validate <url> -- runs url through the w3c markup validator'''
if not inp:
return validate.__doc__
if not inp.startswith('http://'):
inp = 'http://' + inp
url = path % (urllib.quote(inp))
temp = urllib.urlopen(url).info()
status = temp.getheader('X-W3C-Validator-Status')
if (status == "Valid" or status == "Invalid"):
errorcount = temp.getheader('X-W3C-Validator-Errors')
warningcount = temp.getheader('X-W3C-Validator-Warnings')
return "%s was validated as %s with %s errors and %s warnings. See: %s" \
% (inp, status.lower(), errorcount, warningcount, url)
else:
return "Something went wrong while validating %s" % (inp)