Replace Textile with Markdown.
[bloggy.git] / common.py
blob272d4848b7a34b5994f633ad74b7c51df137897c
1 # Copyright (c) 2008, 2009, Simon Morgan <sjm@spamcop.net>
3 # Permission to use, copy, modify, and/or distribute this software for any
4 # purpose with or without fee is hereby granted, provided that the above
5 # copyright notice and this permission notice appear in all copies.
7 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 import sqlite3
16 import sys
18 import config
20 def connect():
21 try:
22 conn = sqlite3.connect(config.DBPATH)
23 conn.execute("CREATE TABLE IF NOT EXISTS entries (id INTEGER PRIMARY KEY, date , title TEXT, text TEXT)")
24 except sqlite3.OperationalError:
25 print 'Failed to connect to database. Check file and directory permissions.'
26 sys.exit(1)
27 return conn
29 def getnumposts(conn, id=None):
30 """Enumerate the number of posts in the database. if an ID is specified
31 then enumerate the number of posts with that ID. The latter should be 0 or
32 1 so essentially this is a check for whether the specified post exists.
33 """
34 if id:
35 numposts = conn.execute("SELECT count(id) FROM entries WHERE id = ?", (id,)).fetchone()
36 else:
37 numposts = conn.execute("SELECT count(id) FROM entries").fetchone()
38 return int(numposts[0])
40 def printheaders(title):
41 print '<head>'
42 print '<title>' + title + '</title>'
43 print '<link href="default.css" rel="stylesheet" type="text/css">'
44 print '</head>'