3 class DatabaseError(Exception):
7 def __init__(self, db_path):
11 conn = sqlite3.connect(self.db_path)
12 cursor = conn.cursor()
14 # check if table exists, or create one
15 tables = cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='botamusique';").fetchall()
17 cursor.execute("CREATE TABLE botamusique (section text, option text, value text, UNIQUE(section, option))")
22 def get(self, section, option, **kwargs):
23 conn = sqlite3.connect(self.db_path)
24 cursor = conn.cursor()
25 result = cursor.execute("SELECT value FROM botamusique WHERE section=? AND option=?", (section, option)).fetchall()
31 if 'fallback' in kwargs:
32 return kwargs['fallback']
34 raise DatabaseError("Item not found")
36 def getboolean(self, section, option, **kwargs):
37 return bool(int(self.get(section, option, **kwargs)))
39 def getfloat(self, section, option, **kwargs):
40 return float(self.get(section, option, **kwargs))
42 def getint(self, section, option, **kwargs):
43 return int(self.get(section, option, **kwargs))
45 def set(self, section, option, value):
46 conn = sqlite3.connect(self.db_path)
47 cursor = conn.cursor()
49 INSERT OR REPLACE INTO botamusique (section, option, value)
51 ''', (section, option, value))
55 def has_option(self, section, option):
56 conn = sqlite3.connect(self.db_path)
57 cursor = conn.cursor()
58 result = cursor.execute("SELECT value FROM botamusique WHERE section=? AND option=?", (section, option)).fetchall()
65 def remove_option(self, section, option):
66 conn = sqlite3.connect(self.db_path)
67 cursor = conn.cursor()
68 cursor.execute("DELETE FROM botamusique WHERE section=? AND option=?", (section, option))
72 def remove_section(self, section):
73 conn = sqlite3.connect(self.db_path)
74 cursor = conn.cursor()
75 cursor.execute("DELETE FROM botamusique WHERE section=?", (section, ))
79 def items(self, section):
80 conn = sqlite3.connect(self.db_path)
81 cursor = conn.cursor()
82 results = cursor.execute("SELECT option, value FROM botamusique WHERE section=?", (section, )).fetchall()
85 return map(lambda v: (v[0], v[1]), results)
88 conn = sqlite3.connect(self.db_path)
89 cursor = conn.cursor()
90 cursor.execute("DROP TABLE botamusique")