formating and vscode problems

This commit is contained in:
2022-03-05 17:28:04 -05:00
parent be5858428c
commit cddf809759
6 changed files with 574 additions and 52 deletions
+3 -1
View File
@@ -3,4 +3,6 @@
__pycache__/ __pycache__/
*.pyc *.pyc
*.pyo *.pyo
.flake8
.venv/
test/
+3 -2
View File
@@ -1,5 +1,6 @@
{ {
"python.pythonPath": "/usr/bin/python3", "python.pythonPath": "/usr/bin/python3",
"python.linting.pylintEnabled": true, "python.linting.enabled": true,
"python.linting.enabled": true "python.formatting.provider": "black",
"python.linting.flake8Enabled": true
} }
+19
View File
@@ -0,0 +1,19 @@
To obtain a DNS verified certificate for the websockert server:
certbot certonly -d w02.wrede.ca -d ws.wrede.ca --dns-rfc2136 --dns-rfc2136-credentials /usr/local/etc/letsencrypt/certbot_dns_rfc2136.ini --dns-rfc2136-propagation-seconds 10
and the rfc2136.ini file looks like:
# Target DNS server
dns_rfc2136_server = 192.168.196.248
# Target DNS port
dns_rfc2136_port = 53
# TSIG key name
dns_rfc2136_name = tsig-key
# TSIG key secret
dns_rfc2136_secret = 1KsWP8ZkZxBDKS0RQ2n3bkz1xpVPtz3Tk1y3r/dF+4knwGBzscse8iewaEr/6jUtxaL1taGME6eqSDtV2SD8NQ==
# TSIG key algorithm
dns_rfc2136_algorithm = HMAC-SHA512
+49 -44
View File
@@ -2,11 +2,8 @@
# $Id: hbd,v 1.38 2013/07/14 02:25:05 andreas Exp $ # $Id: hbd,v 1.38 2013/07/14 02:25:05 andreas Exp $
# Wait for heartbeat messages and act on them (or their absence) # Wait for heartbeat messages and act on them (or their absence)
# #
VER = 4.2
import time import time
import os import os
import string
import sys import sys
import socket import socket
import ssl import ssl
@@ -20,7 +17,8 @@ import signal
import pickle import pickle
import smtplib import smtplib
import traceback import traceback
import urllib.request, urllib.parse, urllib.error import urllib.request
import urllib.error
import urllib.parse import urllib.parse
import http.client import http.client
import threading import threading
@@ -38,7 +36,10 @@ from subprocess import Popen, STDOUT, PIPE
# from hbdclass import * # from hbdclass import *
import hbdclass import hbdclass
VER = 4.4
CERT_PATH = "/usr/local/etc/letsencrypt/live/w02.wrede.ca/" CERT_PATH = "/usr/local/etc/letsencrypt/live/w02.wrede.ca/"
CERT_PATH = "./test/"
WSS_PEM = CERT_PATH + "fullchain.pem" WSS_PEM = CERT_PATH + "fullchain.pem"
WSS_KEY = CERT_PATH + "privkey.pem" WSS_KEY = CERT_PATH + "privkey.pem"
@@ -82,18 +83,18 @@ lastfm = ["", "", ""]
tcss = """<script src="http:/sorttable.js"></script> tcss = """<script src="http:/sorttable.js"></script>
<style> <style>
#ntable { #ntable {
border-collapse: collapse; border-collapse: collapse;
} }
#wide-ntable { #wide-ntable {
border-collapse: collapse; border-collapse: collapse;
width: 100%; width: 100%;
} }
#ntable td, #ntable th { #ntable td, #ntable th {
border: 1px solid #ddd; border: 1px solid #ddd;
text-align: left; text-align: left;
padding: 1px; padding: 1px;
} }
#ntable tr:nth-child(even){background-color: #f2f2f2} #ntable tr:nth-child(even){background-color: #f2f2f2}
@@ -101,10 +102,10 @@ tcss = """<script src="http:/sorttable.js"></script>
#ntable tr:hover {background-color: #ddd;} #ntable tr:hover {background-color: #ddd;}
#ntable th { #ntable th {
padding-top: 12px; padding-top: 12px;
padding-bottom: 12px; padding-bottom: 12px;
background-color: #9d9d9d; background-color: #9d9d9d;
color: white; color: white;
} }
</style> """ </style> """
@@ -142,7 +143,7 @@ class LogDevice:
def dicttos(ID, d, compress=False): def dicttos(ID, d, compress=False):
s = [] s = []
for k in d: for k in d:
if type(d[k]) == type(1.2): if isinstance(d[k], float):
s.append("%s=%0.5f" % (k, d[k])) s.append("%s=%0.5f" % (k, d[k]))
else: else:
s.append("%s=%s" % (k, d[k])) s.append("%s=%s" % (k, d[k]))
@@ -207,8 +208,9 @@ def email(s, msg):
except smtplib.SMTPRecipientsRefused as errs: except smtplib.SMTPRecipientsRefused as errs:
log(None, "cannot send email: %s\n" % (errs)) log(None, "cannot send email: %s\n" % (errs))
ret = "Fail" ret = "Fail"
except: except Exception as e:
print(("smtp error: " + traceback.format_exc())) print(f"smtp error: {e}")
ret = "Fail"
saveandrestart() saveandrestart()
try: try:
server.quit() server.quit()
@@ -294,7 +296,7 @@ def pushsignal(msg, title="hbd", recipient=RECIPIENT):
"send", "send",
"-m", "-m",
message, message,
# "-g", GROUP, # "-g", GROUP,
recipient, recipient,
] ]
@@ -313,7 +315,7 @@ def pushsignal(msg, title="hbd", recipient=RECIPIENT):
# nsupdate: set the DNS A record for a fqdn # nsupdate: set the DNS A record for a fqdn
# return: None if ok, else error text # return: None if ok, else error text
def nsupdate(hostname, newip, dyndomain): def nsupdate(hostname, newip, dyndomain):
D = {} D = {}
D["domain"] = dyndomain D["domain"] = dyndomain
@@ -424,8 +426,7 @@ def checkoverdue():
conn.newstate(hbdclass.Connection.overdue, now, grace) conn.newstate(hbdclass.Connection.overdue, now, grace)
pmsg.append(conn.afam) pmsg.append(conn.afam)
if ( if (
conn.state == hbdclass.Connection.overdue conn.state == hbdclass.Connection.overdue and (now - conn.lastbeat) > DROPOVERDUE
and (now - conn.lastbeat) > DROPOVERDUE
): ):
conn.newstate(hbdclass.Connection.unknown, conn.lastbeat) conn.newstate(hbdclass.Connection.unknown, conn.lastbeat)
if pmsg != []: if pmsg != []:
@@ -505,7 +506,7 @@ def readsock(sock):
addr = addrp[0:2] addr = addrp[0:2]
name = shortname(msg.get("name", "unknown")) name = shortname(msg.get("name", "unknown"))
if not name in hbdclass.Host.hosts: # was: hosts.has_key(name): if name not in hbdclass.Host.hosts: # was: hosts.has_key(name):
host = hbdclass.Host(name) host = hbdclass.Host(name)
host.dyn = name in dyndnshosts host.dyn = name in dyndnshosts
if verbose: if verbose:
@@ -550,7 +551,7 @@ def readsock(sock):
email("msg", message) email("msg", message)
pushmsg(message) pushmsg(message)
if conn.getstate() != hbdclass.Connection.up: # XXX and interval > 0: if conn.getstate() != hbdclass.Connection.up: # XXX and interval > 0:
lasts = conn.state lasts = conn.state
d = conn.newstate(hbdclass.Connection.up, now) d = conn.newstate(hbdclass.Connection.up, now)
m = "%s back after being %s for %s" % (conn.afam, lasts, dur(d)) m = "%s back after being %s for %s" % (conn.afam, lasts, dur(d))
@@ -661,7 +662,7 @@ class HttpHandler(http.server.BaseHTTPRequestHandler):
return self.server_version return self.server_version
def handle(self): def handle(self):
# return http.server.BaseHTTPRequestHandler.handle(self) # return http.server.BaseHTTPRequestHandler.handle(self)
try: try:
return http.server.BaseHTTPRequestHandler.handle(self) return http.server.BaseHTTPRequestHandler.handle(self)
except Exception as e: except Exception as e:
@@ -678,8 +679,8 @@ class HttpHandler(http.server.BaseHTTPRequestHandler):
"Last-Modified", "Last-Modified",
time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(now)), time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(now)),
) )
# self.send_header("Accept-Ranges","bytes") # self.send_header("Accept-Ranges","bytes")
# self.send_header("hbdclass.Connection","close") # self.send_header("hbdclass.Connection","close")
for h in headerdict: for h in headerdict:
self.send_header(h, headerdict[h]) self.send_header(h, headerdict[h])
self.end_headers() self.end_headers()
@@ -764,11 +765,11 @@ class HttpHandler(http.server.BaseHTTPRequestHandler):
uname = qa.get("h", [None])[0] uname = qa.get("h", [None])[0]
if not uname: if not uname:
code, res = self.builderror(400, "Argument error", "need h= argument") code, res = self.builderror(400, "Argument error", "need h= argument")
if not uname in hbdclass.Host.hosts: if uname not in hbdclass.Host.hosts:
code, res = self.builderror(400, "Data error", "h=%s not found" % uname) code, res = self.builderror(400, "Data error", "h=%s not found" % uname)
else: else:
log(uname, "dropped") log(uname, "dropped")
# for addr in hbdclass.Host.hosts[uname].0i # for addr in hbdclass.Host.hosts[uname].0i
# TODO: send message to websocket about dropped host # TODO: send message to websocket about dropped host
del hbdclass.Host.hosts[uname] del hbdclass.Host.hosts[uname]
res = self.buildhead() res = self.buildhead()
@@ -778,7 +779,7 @@ class HttpHandler(http.server.BaseHTTPRequestHandler):
uname = qa.get("h", [None])[0] uname = qa.get("h", [None])[0]
if not uname: if not uname:
code, res = self.builderror(400, "Argument error", "need h= argument") code, res = self.builderror(400, "Argument error", "need h= argument")
if not uname in hbdclass.Host.hosts: if uname not in hbdclass.Host.hosts:
code, res = self.builderror(400, "Data error", "h=%s not found" % uname) code, res = self.builderror(400, "Data error", "h=%s not found" % uname)
else: else:
ll = hbdclass.Host.hosts[uname].registerDns() ll = hbdclass.Host.hosts[uname].registerDns()
@@ -814,15 +815,15 @@ class HttpHandler(http.server.BaseHTTPRequestHandler):
elif qr.path == "/api/0/hosts": # api access to host table elif qr.path == "/api/0/hosts": # api access to host table
headerdict = {"Content-Type": "application/json; charset=utf-8"} headerdict = {"Content-Type": "application/json; charset=utf-8"}
l = [] lst = []
for h in hbdclass.Host.hosts: for h in hbdclass.Host.hosts:
l.append(hbdclass.Host.hosts[h].jsons()) lst.append(hbdclass.Host.hosts[h].jsons())
res = ["[" + ",".join(l) + "]"] res = ["[" + ",".join(lst) + "]"]
elif qr.path == "/api/0/messages": # api access to host table elif qr.path == "/api/0/messages": # api access to host table
headerdict = {"Content-Type": "application/json; charset=utf-8"} headerdict = {"Content-Type": "application/json; charset=utf-8"}
l = msgs[len(msgs) - 30 :] lst = msgs[len(msgs) - 30:]
res = [json.dumps(l)] res = [json.dumps(lst)]
elif qr.path == "/r": # restart elif qr.path == "/r": # restart
res = self.buildhead() res = self.buildhead()
@@ -889,7 +890,7 @@ def closeup():
except: except:
pass pass
# signal.signal(signal.SIGTERM, 0) # signal.signal(signal.SIGTERM, 0)
signal.signal(signal.SIGHUP, 0) signal.signal(signal.SIGHUP, 0)
@@ -947,7 +948,7 @@ async def ws_serve(websocket, path):
) )
await websocket.send(jmsg) await websocket.send(jmsg)
# messages in reverse order # messages in reverse order
for m in msgs[len(msgs) - 20 :]: for m in msgs[len(msgs) - 20:]:
jmsg = json.dumps({"type": "message", "data": m}) jmsg = json.dumps({"type": "message", "data": m})
await websocket.send(jmsg) await websocket.send(jmsg)
@@ -1076,17 +1077,17 @@ if f:
ls = f.readline() ls = f.readline()
if len(ls) == 0: if len(ls) == 0:
break break
l = ls[:-1].strip() ln = ls[:-1].strip()
if len(l) == 0 or l[0] == "#": if len(ln) == 0 or ln[0] == "#":
continue continue
if verbose: if verbose:
print((" %s" % l)) print((" %s" % ln))
r = l.split("=") r = ln.split("=")
o = r[0].strip() o = r[0].strip()
try: try:
a = eval(r[1].strip()) a = eval(r[1].strip())
except Exception as e: except Exception as e:
print(("error: %s" % str(r))) print("error: %s %s" % (e, str(r)))
sys.exit(1) sys.exit(1)
if o == "interval": if o == "interval":
interval = a interval = a
@@ -1222,7 +1223,11 @@ asyncio.set_event_loop(loop)
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
wss_pem = pathlib.Path(WSS_PEM) wss_pem = pathlib.Path(WSS_PEM)
wss_key = pathlib.Path(WSS_KEY) wss_key = pathlib.Path(WSS_KEY)
ssl_context.load_cert_chain(wss_pem, keyfile=wss_key) try:
ssl_context.load_cert_chain(wss_pem, keyfile=wss_key)
except FileNotFoundError:
print(("warning: missing %s or %s" % (wss_pem, wss_key)))
sys.exit(1)
wss_start_server = websockets.serve( wss_start_server = websockets.serve(
ws_serve, hbd_host, WSSPORT, ssl=ssl_context, loop=loop, subprotocols=["hbd"] ws_serve, hbd_host, WSSPORT, ssl=ssl_context, loop=loop, subprotocols=["hbd"]
) )
@@ -1281,8 +1286,8 @@ while running:
for fh in sr[0]: for fh in sr[0]:
if fh in [sock, sock6]: if fh in [sock, sock6]:
readsock(fh) readsock(fh)
# elif fh == serv.fileno(): # elif fh == serv.fileno():
# serv.handle_request() # serv.handle_request()
else: else:
sys.stderr.write("what happend just now?\n") sys.stderr.write("what happend just now?\n")
if DEBUG > 3: if DEBUG > 3:
+5 -5
View File
@@ -1,6 +1,6 @@
""" """
host and connection class shared between hbd and host and connection class shared between hbd and
the websit's heartbeat.py the websit's heartbeat.py
""" """
@@ -234,7 +234,7 @@ class Host:
return self.dyn return self.dyn
def isIPv4(self, addr): def isIPv4(self, addr):
if type(addr) == type(()): if isinstance(addr, tuple):
return addr[0].find(".") > 0 return addr[0].find(".") > 0
else: else:
return addr.find(".") > 0 return addr.find(".") > 0
@@ -328,7 +328,7 @@ class Host:
hostfields = Host.hostfields_long hostfields = Host.hostfields_long
h = [] h = []
for f in hostfields: for f in hostfields:
if type(f) == type(()): if isinstance(f, tuple):
h.append(self.gene(tag, hd[f[0]], f[1])) h.append(self.gene(tag, hd[f[0]], f[1]))
else: else:
h.append(self.gene(tag, hd[f])) h.append(self.gene(tag, hd[f]))
@@ -354,7 +354,7 @@ class Host:
res = [] res = []
le = max(40 - len(Host.hosts), 3) le = max(40 - len(Host.hosts), 3)
res.append("<h4>Log of Events</h4>") res.append("<h4>Log of Events</h4>")
for m in msgs[len(msgs) - le :]: for m in msgs[len(msgs) - le:]:
res.append("%s<BR>" % m) res.append("%s<BR>" % m)
return res return res
+495
View File
@@ -0,0 +1,495 @@
/*
SortTable
version 2
7th April 2007
Stuart Langridge, http://www.kryogenix.org/code/browser/sorttable/
Instructions:
Download this file
Add <script src="sorttable.js"></script> to your HTML
Add class="sortable" to any table you'd like to make sortable
Click on the headers to sort
Thanks to many, many people for contributions and suggestions.
Licenced as X11: http://www.kryogenix.org/code/browser/licence.html
This basically means: do what you want with it.
*/
var stIsIE = /*@cc_on!@*/false;
sorttable = {
init: function() {
// quit if this function has already been called
if (arguments.callee.done) return;
// flag this function so we don't do the same thing twice
arguments.callee.done = true;
// kill the timer
if (_timer) clearInterval(_timer);
if (!document.createElement || !document.getElementsByTagName) return;
sorttable.DATE_RE = /^(\d\d?)[\/\.-](\d\d?)[\/\.-]((\d\d)?\d\d)$/;
forEach(document.getElementsByTagName('table'), function(table) {
if (table.className.search(/\bsortable\b/) != -1) {
sorttable.makeSortable(table);
}
});
},
makeSortable: function(table) {
if (table.getElementsByTagName('thead').length == 0) {
// table doesn't have a tHead. Since it should have, create one and
// put the first table row in it.
the = document.createElement('thead');
the.appendChild(table.rows[0]);
table.insertBefore(the,table.firstChild);
}
// Safari doesn't support table.tHead, sigh
if (table.tHead == null) table.tHead = table.getElementsByTagName('thead')[0];
if (table.tHead.rows.length != 1) return; // can't cope with two header rows
// Sorttable v1 put rows with a class of "sortbottom" at the bottom (as
// "total" rows, for example). This is B&R, since what you're supposed
// to do is put them in a tfoot. So, if there are sortbottom rows,
// for backwards compatibility, move them to tfoot (creating it if needed).
sortbottomrows = [];
for (var i=0; i<table.rows.length; i++) {
if (table.rows[i].className.search(/\bsortbottom\b/) != -1) {
sortbottomrows[sortbottomrows.length] = table.rows[i];
}
}
if (sortbottomrows) {
if (table.tFoot == null) {
// table doesn't have a tfoot. Create one.
tfo = document.createElement('tfoot');
table.appendChild(tfo);
}
for (var i=0; i<sortbottomrows.length; i++) {
tfo.appendChild(sortbottomrows[i]);
}
delete sortbottomrows;
}
// work through each column and calculate its type
headrow = table.tHead.rows[0].cells;
for (var i=0; i<headrow.length; i++) {
// manually override the type with a sorttable_type attribute
if (!headrow[i].className.match(/\bsorttable_nosort\b/)) { // skip this col
mtch = headrow[i].className.match(/\bsorttable_([a-z0-9]+)\b/);
if (mtch) { override = mtch[1]; }
if (mtch && typeof sorttable["sort_"+override] == 'function') {
headrow[i].sorttable_sortfunction = sorttable["sort_"+override];
} else {
headrow[i].sorttable_sortfunction = sorttable.guessType(table,i);
}
// make it clickable to sort
headrow[i].sorttable_columnindex = i;
headrow[i].sorttable_tbody = table.tBodies[0];
dean_addEvent(headrow[i],"click", sorttable.innerSortFunction = function(e) {
if (this.className.search(/\bsorttable_sorted\b/) != -1) {
// if we're already sorted by this column, just
// reverse the table, which is quicker
sorttable.reverse(this.sorttable_tbody);
this.className = this.className.replace('sorttable_sorted',
'sorttable_sorted_reverse');
this.removeChild(document.getElementById('sorttable_sortfwdind'));
sortrevind = document.createElement('span');
sortrevind.id = "sorttable_sortrevind";
sortrevind.innerHTML = stIsIE ? '&nbsp<font face="webdings">5</font>' : '&nbsp;&#x25B4;';
this.appendChild(sortrevind);
return;
}
if (this.className.search(/\bsorttable_sorted_reverse\b/) != -1) {
// if we're already sorted by this column in reverse, just
// re-reverse the table, which is quicker
sorttable.reverse(this.sorttable_tbody);
this.className = this.className.replace('sorttable_sorted_reverse',
'sorttable_sorted');
this.removeChild(document.getElementById('sorttable_sortrevind'));
sortfwdind = document.createElement('span');
sortfwdind.id = "sorttable_sortfwdind";
sortfwdind.innerHTML = stIsIE ? '&nbsp<font face="webdings">6</font>' : '&nbsp;&#x25BE;';
this.appendChild(sortfwdind);
return;
}
// remove sorttable_sorted classes
theadrow = this.parentNode;
forEach(theadrow.childNodes, function(cell) {
if (cell.nodeType == 1) { // an element
cell.className = cell.className.replace('sorttable_sorted_reverse','');
cell.className = cell.className.replace('sorttable_sorted','');
}
});
sortfwdind = document.getElementById('sorttable_sortfwdind');
if (sortfwdind) { sortfwdind.parentNode.removeChild(sortfwdind); }
sortrevind = document.getElementById('sorttable_sortrevind');
if (sortrevind) { sortrevind.parentNode.removeChild(sortrevind); }
this.className += ' sorttable_sorted';
sortfwdind = document.createElement('span');
sortfwdind.id = "sorttable_sortfwdind";
sortfwdind.innerHTML = stIsIE ? '&nbsp<font face="webdings">6</font>' : '&nbsp;&#x25BE;';
this.appendChild(sortfwdind);
// build an array to sort. This is a Schwartzian transform thing,
// i.e., we "decorate" each row with the actual sort key,
// sort based on the sort keys, and then put the rows back in order
// which is a lot faster because you only do getInnerText once per row
row_array = [];
col = this.sorttable_columnindex;
rows = this.sorttable_tbody.rows;
for (var j=0; j<rows.length; j++) {
row_array[row_array.length] = [sorttable.getInnerText(rows[j].cells[col]), rows[j]];
}
/* If you want a stable sort, uncomment the following line */
//sorttable.shaker_sort(row_array, this.sorttable_sortfunction);
/* and comment out this one */
row_array.sort(this.sorttable_sortfunction);
tb = this.sorttable_tbody;
for (var j=0; j<row_array.length; j++) {
tb.appendChild(row_array[j][1]);
}
delete row_array;
});
}
}
},
guessType: function(table, column) {
// guess the type of a column based on its first non-blank row
sortfn = sorttable.sort_alpha;
for (var i=0; i<table.tBodies[0].rows.length; i++) {
text = sorttable.getInnerText(table.tBodies[0].rows[i].cells[column]);
if (text != '') {
if (text.match(/^-?[£$¤]?[\d,.]+%?$/)) {
return sorttable.sort_numeric;
}
// check for a date: dd/mm/yyyy or dd/mm/yy
// can have / or . or - as separator
// can be mm/dd as well
possdate = text.match(sorttable.DATE_RE)
if (possdate) {
// looks like a date
first = parseInt(possdate[1]);
second = parseInt(possdate[2]);
if (first > 12) {
// definitely dd/mm
return sorttable.sort_ddmm;
} else if (second > 12) {
return sorttable.sort_mmdd;
} else {
// looks like a date, but we can't tell which, so assume
// that it's dd/mm (English imperialism!) and keep looking
sortfn = sorttable.sort_ddmm;
}
}
}
}
return sortfn;
},
getInnerText: function(node) {
// gets the text we want to use for sorting for a cell.
// strips leading and trailing whitespace.
// this is *not* a generic getInnerText function; it's special to sorttable.
// for example, you can override the cell text with a customkey attribute.
// it also gets .value for <input> fields.
if (!node) return "";
hasInputs = (typeof node.getElementsByTagName == 'function') &&
node.getElementsByTagName('input').length;
if (node.getAttribute("sorttable_customkey") != null) {
return node.getAttribute("sorttable_customkey");
}
else if (typeof node.textContent != 'undefined' && !hasInputs) {
return node.textContent.replace(/^\s+|\s+$/g, '');
}
else if (typeof node.innerText != 'undefined' && !hasInputs) {
return node.innerText.replace(/^\s+|\s+$/g, '');
}
else if (typeof node.text != 'undefined' && !hasInputs) {
return node.text.replace(/^\s+|\s+$/g, '');
}
else {
switch (node.nodeType) {
case 3:
if (node.nodeName.toLowerCase() == 'input') {
return node.value.replace(/^\s+|\s+$/g, '');
}
case 4:
return node.nodeValue.replace(/^\s+|\s+$/g, '');
break;
case 1:
case 11:
var innerText = '';
for (var i = 0; i < node.childNodes.length; i++) {
innerText += sorttable.getInnerText(node.childNodes[i]);
}
return innerText.replace(/^\s+|\s+$/g, '');
break;
default:
return '';
}
}
},
reverse: function(tbody) {
// reverse the rows in a tbody
newrows = [];
for (var i=0; i<tbody.rows.length; i++) {
newrows[newrows.length] = tbody.rows[i];
}
for (var i=newrows.length-1; i>=0; i--) {
tbody.appendChild(newrows[i]);
}
delete newrows;
},
/* sort functions
each sort function takes two parameters, a and b
you are comparing a[0] and b[0] */
sort_numeric: function(a,b) {
aa = parseFloat(a[0].replace(/[^0-9.-]/g,''));
if (isNaN(aa)) aa = 0;
bb = parseFloat(b[0].replace(/[^0-9.-]/g,''));
if (isNaN(bb)) bb = 0;
return aa-bb;
},
sort_alpha: function(a,b) {
if (a[0]==b[0]) return 0;
if (a[0]<b[0]) return -1;
return 1;
},
sort_ddmm: function(a,b) {
mtch = a[0].match(sorttable.DATE_RE);
y = mtch[3]; m = mtch[2]; d = mtch[1];
if (m.length == 1) m = '0'+m;
if (d.length == 1) d = '0'+d;
dt1 = y+m+d;
mtch = b[0].match(sorttable.DATE_RE);
y = mtch[3]; m = mtch[2]; d = mtch[1];
if (m.length == 1) m = '0'+m;
if (d.length == 1) d = '0'+d;
dt2 = y+m+d;
if (dt1==dt2) return 0;
if (dt1<dt2) return -1;
return 1;
},
sort_mmdd: function(a,b) {
mtch = a[0].match(sorttable.DATE_RE);
y = mtch[3]; d = mtch[2]; m = mtch[1];
if (m.length == 1) m = '0'+m;
if (d.length == 1) d = '0'+d;
dt1 = y+m+d;
mtch = b[0].match(sorttable.DATE_RE);
y = mtch[3]; d = mtch[2]; m = mtch[1];
if (m.length == 1) m = '0'+m;
if (d.length == 1) d = '0'+d;
dt2 = y+m+d;
if (dt1==dt2) return 0;
if (dt1<dt2) return -1;
return 1;
},
shaker_sort: function(list, comp_func) {
// A stable sort function to allow multi-level sorting of data
// see: http://en.wikipedia.org/wiki/Cocktail_sort
// thanks to Joseph Nahmias
var b = 0;
var t = list.length - 1;
var swap = true;
while(swap) {
swap = false;
for(var i = b; i < t; ++i) {
if ( comp_func(list[i], list[i+1]) > 0 ) {
var q = list[i]; list[i] = list[i+1]; list[i+1] = q;
swap = true;
}
} // for
t--;
if (!swap) break;
for(var i = t; i > b; --i) {
if ( comp_func(list[i], list[i-1]) < 0 ) {
var q = list[i]; list[i] = list[i-1]; list[i-1] = q;
swap = true;
}
} // for
b++;
} // while(swap)
}
}
/* ******************************************************************
Supporting functions: bundled here to avoid depending on a library
****************************************************************** */
// Dean Edwards/Matthias Miller/John Resig
/* for Mozilla/Opera9 */
if (document.addEventListener) {
document.addEventListener("DOMContentLoaded", sorttable.init, false);
}
/* for Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
var script = document.getElementById("__ie_onload");
script.onreadystatechange = function() {
if (this.readyState == "complete") {
sorttable.init(); // call the onload handler
}
};
/*@end @*/
/* for Safari */
if (/WebKit/i.test(navigator.userAgent)) { // sniff
var _timer = setInterval(function() {
if (/loaded|complete/.test(document.readyState)) {
sorttable.init(); // call the onload handler
}
}, 10);
}
/* for other browsers */
window.onload = sorttable.init;
// written by Dean Edwards, 2005
// with input from Tino Zijdel, Matthias Miller, Diego Perini
// http://dean.edwards.name/weblog/2005/10/add-event/
function dean_addEvent(element, type, handler) {
if (element.addEventListener) {
element.addEventListener(type, handler, false);
} else {
// assign each event handler a unique ID
if (!handler.$$guid) handler.$$guid = dean_addEvent.guid++;
// create a hash table of event types for the element
if (!element.events) element.events = {};
// create a hash table of event handlers for each element/event pair
var handlers = element.events[type];
if (!handlers) {
handlers = element.events[type] = {};
// store the existing event handler (if there is one)
if (element["on" + type]) {
handlers[0] = element["on" + type];
}
}
// store the event handler in the hash table
handlers[handler.$$guid] = handler;
// assign a global event handler to do all the work
element["on" + type] = handleEvent;
}
};
// a counter used to create unique IDs
dean_addEvent.guid = 1;
function removeEvent(element, type, handler) {
if (element.removeEventListener) {
element.removeEventListener(type, handler, false);
} else {
// delete the event handler from the hash table
if (element.events && element.events[type]) {
delete element.events[type][handler.$$guid];
}
}
};
function handleEvent(event) {
var returnValue = true;
// grab the event object (IE uses a global event object)
event = event || fixEvent(((this.ownerDocument || this.document || this).parentWindow || window).event);
// get a reference to the hash table of event handlers
var handlers = this.events[event.type];
// execute each event handler
for (var i in handlers) {
this.$$handleEvent = handlers[i];
if (this.$$handleEvent(event) === false) {
returnValue = false;
}
}
return returnValue;
};
function fixEvent(event) {
// add W3C standard event methods
event.preventDefault = fixEvent.preventDefault;
event.stopPropagation = fixEvent.stopPropagation;
return event;
};
fixEvent.preventDefault = function() {
this.returnValue = false;
};
fixEvent.stopPropagation = function() {
this.cancelBubble = true;
}
// Dean's forEach: http://dean.edwards.name/base/forEach.js
/*
forEach, version 1.0
Copyright 2006, Dean Edwards
License: http://www.opensource.org/licenses/mit-license.php
*/
// array-like enumeration
if (!Array.forEach) { // mozilla already supports this
Array.forEach = function(array, block, context) {
for (var i = 0; i < array.length; i++) {
block.call(context, array[i], i, array);
}
};
}
// generic enumeration
Function.prototype.forEach = function(object, block, context) {
for (var key in object) {
if (typeof this.prototype[key] == "undefined") {
block.call(context, object[key], key, object);
}
}
};
// character enumeration
String.forEach = function(string, block, context) {
Array.forEach(string.split(""), function(chr, index) {
block.call(context, chr, index, string);
});
};
// globally resolve forEach enumeration
var forEach = function(object, block, context) {
if (object) {
var resolve = Object; // default
if (object instanceof Function) {
// functions have a "length" property
resolve = Function;
} else if (object.forEach instanceof Function) {
// the object implements a custom forEach method so use that
object.forEach(block, context);
return;
} else if (typeof object == "string") {
// the object is a string
resolve = String;
} else if (typeof object.length == "number") {
// the object is array-like
resolve = Array;
}
resolve.forEach(object, block, context);
}
};