Archive for April, 2008
Latex: Table converter
Posted by JohannesTheDeveloper in Happy Hacking on April 16th, 2008
If you copy-paste a table out of open-office, it is usually one line per cell.
This Python script converts it into a LaTeX-table (without \begin and \end):
table2latex.py:
#!/usr/bin/env python
import sys
f = sys.stdin
if(len(sys.argv) < 2):
print "First Argument is number of columns"
sys.exit(1)
numcolumns = int(sys.argv[1])
borders = False
if len(sys.argv) > 2 and sys.argv[2] == "--with-borders":
borders = True
i = 0
while True:
for j in range(numcolumns):
l = f.readline()
if l == "":
sys.exit(0)
if i == 0:
print "\\tablehdr{%s}" % l.strip(),
else:
print "%s" % l.strip(),
if j == numcolumns - 1:
print '\\\\'
if borders:
print '\hline'
else:
print '&',
i = i + 1
Use it like this (pipe in from stdin, first arg is column count):
[user@thiscomputer ~]$ seq 30 | python table2latex.py 3
\tablehdr{1} & \tablehdr{2} & \tablehdr{3} \\
4 & 5 & 6 \\
7 & 8 & 9 \\
10 & 11 & 12 \\
13 & 14 & 15 \\
16 & 17 & 18 \\
19 & 20 & 21 \\
22 & 23 & 24 \\
25 & 26 & 27 \\
28 & 29 & 30 \\
[user@thiscomputer ~]$
The second argument can be if borders should be used, i.e. \hline after each row.
TU: TIL Tutors helper
Posted by JohannesTheDeveloper in Happy Hacking on April 12th, 2008
This is a script only of interest to tutors in TIL, who use sbt.logic.at for entering students data. It sets the edited selection when one enters gradings sbt-logic-at.user (js, 2 KB).
Install Greasemonkey first, then click the link or download and add manually.
run Firefox as another user
Posted by JohannesTheDeveloper in fun with Linux on April 4th, 2008
Firefox is a big application and thus has security-related issues. NoScript can help a little, but not all bugs are necessarily script related.
Here I let firefox run as another user “kiosk”:
I access the normal user’s Xauthority file to be able to write to the X server. Note that firefox will not be able to write to your users home directory (which is the whole point), but to kiosk’s home, from which you can grab the files.
File /home/user/bin/firefox.sh
#!/bin/bash
cp -f /home/user/.Xauthority /home/kiosk/
chown kiosk:kiosk /home/kiosk/.Xauthority # you can also do this with xhost.
su -c "XAUTHORITY=/home/kiosk/.Xauthority DISPLAY=:0.0 /opt/firefox/firefox" kiosk
Then, as user, call
sudo bash bin/firefox.sh
And firefox should pop up.
There is also a howto for creating a chroot (for various distros).
However, su didn’t compile, so I didn’t use it. Jailkit might also be something to look into.
CVS log analysing for summarising spent hours
Posted by JohannesTheDeveloper in Happy Hacking on April 2nd, 2008
CVS is based on RCS, which is file based. That means, you don’t really have revisions over multiple files. The “cvs log” command reflects that problem. This outputs a more useful summary: date and commit comment per line.
cvs log > cvslog # puts log in this file
cat cvslog |
grep '^date: ' -A3 | # we assume max 3 lines of comments
while read line; do echo -n "$line |"; done | # all in one line, seperated by |
sed 's/date: /\ndate: /g'| # a line per date
sed 's/[-=]\{2,\}/\n/g' | # removing line seperators
sed 's/^[- |]*//g'| # remove useless - and |
grep -v '^revision '| # there exist some more boring lines
sed 's/; author:.*; |/;/g'| # we aren't interested in author and id etc.
sed 's/ |$//g'| # remove ending |
sort -u | # sort (by date)
python guniq.py | # show only uniq lines
cat > cvslog.1
You need guniq.py which does the same as the unix command uniq, except that it removes duplicates found in the whole input.
guniq.py:
#!/usr/bin/python
import sys
a = []
while True:
l = sys.stdin.readline()
if l == '':
break
if not a.__contains__(l):
print l,
a.append(l)
The whole thing got me nearly to boot into Windows, because TortoiseCVS/TortoiseSVN is a really cool awesome thingy!
Python: HTML database descriptions from create statements
Posted by JohannesTheDeveloper in Happy Hacking on April 2nd, 2008
This code produces HTML database descriptions from create statements.
Feed into stdin, await from stdout.
For example: python dbdesc.py < DATASCHEME > dbdesc.html
It isn’t perfect nor meant to be, but gives a good starting point for a documentation.
#!/usr/bin/python
import sys
import re
a = ''
for i in sys.stdin.readlines():
a = a + " " + re.sub('--.*', '', i).strip()
a = a.strip().lower()
a = re.sub('\/\*[^*]*\*\/', '', a)
print """
<html>
<head>
<link rel="stylesheet" href="dbdesc.css" />
</head>
<body>
"""
for m in re.findall('create table ([^ (]*)[ (]*([^;]*)[ )]*;', a):
tablename = m[0]
content = m[1]
print "<h2>%s</h2>" % tablename
print """<table class="dbdesc"><thead><tr>
<th class="name">Feldname</th>
<th class="type">Typ</th>
<th class="option">Option</th>
<th class="comment">Bemerkung</th>
</tr></thead>"""
atts = {}
for l in content.split(','):
l = l.strip()
if l == '':
continue
if l.startswith('constraint') or l.startswith('foreign key'):
continue
l = l.split(None, 2)
if len(l)<2:
continue
name = l[0]
if name == 'constraint' or name.__contains__('(') or name.__contains__(')'):
continue
type = l[1]
if len(l)<3:
l.append('')
op = ""
if 'not null' in l[2]: op = op + '!'
if 'primary key' in l[2]: op = op + '1+P'
elif 'unique' in l[2]: op = op + '1'
print """ <tr>
<td class="name">%s</td>
<td class="type">%s</td>
<td class="option">%s</td>
<td class="comment"><!-- TODO: Bemerkungen --></td>
</tr>""" % (name, type, op)
print """ </table> """
print """</html> """
CSS file dbdesc.css
table.dbdesc{ width: 40em; border: 1px solid #080; border-width: 1px 0;}
table.dbdesc th{ border-bottom: 1px solid #080;}
table.dbdesc td{ border: none;}
table.dbdesc td, table.dbdesc th{ width: 15em;}
table.dbdesc th{ font-style: italic; font-weight: normal; }
table.dbdesc td.option{ text-align: center;}
table.dbdesc td.type{ text-transform: uppercase;}
Recent Comments