Skip to content

Instantly share code, notes, and snippets.

@rafaelmartins
Created February 14, 2009 04:51
Show Gist options
  • Save rafaelmartins/64254 to your computer and use it in GitHub Desktop.
Save rafaelmartins/64254 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# -*- encoding: utf-8 -*-
# MusicLog Daemon
#
# Copyright (c) 2008 Rafael G. Martins <rafael@rafaelmartins.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from os import path, unlink
from base64 import b64encode, b64decode
class txtDb:
table_file = '/home/rafael/workspace/lastfm/src/%s.db'
def __init__(self, tables):
self.tables = tables
for table in self.tables.keys():
if not path.exists(self.table_file % table):
if 'id' not in self.tables[table]:
self.tables[table] = ['id'] + self.tables[table]
db = open(self.table_file % table, 'w')
print >> db, ' '.join(self.tables[table])
db.close()
self.__parseDb(table)
def __parseDb(self, table):
db = open(self.table_file % table, 'r')
lines = [a.strip() for a in db.readlines()]
db.close()
self.keys = {}
self.rows = {}
self.keys[table] = lines[0].split(' ')
if len(lines) > 1:
self.rows[table] = lines[1:]
def __getNewId(self, table):
self.__parseDb(table)
try:
return len(self.rows[table]) + 1
except:
return 1
def addRow(self, table, values):
new_row = []
for key in self.keys[table]:
if key == 'id':
new_row.append(str(self.__getNewId(table)))
elif values.has_key(key):
if values[key] == '':
new_row.append('NULL')
else:
new_row.append(b64encode(str(values[key])))
else:
new_row.append('NULL')
#Add new row to database file
db = open(self.table_file % table, 'a')
print >> db, ' '.join(new_row)
db.close()
def getRow(self, table):
rows = self.getRows(table)
self.cleanTable(table)
if len(rows) > 0:
for row in rows[1:]:
row.pop('id')
self.addRow(table, row)
return rows[0]
else:
return []
def getRows(self, table):
self.__parseDb(table)
result = []
if self.__getNewId(table) > 1:
for row in self.rows[table]:
row = row.split(' ')
aux = {}
for key in range(len(self.keys[table])):
if row[key] != 'NULL' and self.keys[table][key] != 'id':
row[key] = b64decode(row[key])
if row[key] == 'NULL':
row[key] = ''
aux[self.keys[table][key]] = row[key]
result.append(aux)
return result
def cleanTable(self, table):
unlink(self.table_file % table)
self.__init__(self.tables)
if __name__ == '__main__':
a = txtDb({
'teste': ['a','b'],
})
a.addRow('teste', {
'a': '11111',
'b': '',
})
a.addRow('teste', {
'a': '222222',
'b': 'ffff',
})
print a.getRow('teste')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment