# emacs-mode: -*- python-*-
import os
_dbupdate = 0
_dbdict = {}
_dbname = u''
_debug = 0
try:
    f = open('e:/system/data/debug_set.minidb', 'rb')
    j = f.read()
    f.close()
    if (j == u'debug=on'):
        _debug = 1
    del j
    del f
except:
    _debug = 0

def print_debug(text):
    if _debug:
        f = open('e:/minidb.log', 'a+')
        f.write((text.encode('cp1251') + u'\r\n'))
        f.close()



def Create(name, path = 'e:/system/data/'):
    file = open(((path + name) + '.pydb'), 'w')
    file.close()
    print_debug((((u'create> ' + path) + name) + u'.pydb'))



def Open(name, path = 'e:/system/data/', update = 0):
    global _dbpath
    global _dbname
    global _dbdict
    global _dbupdate
    _dbupdate = update
    _dbdict = {}
    _dbpath = path
    file = open(((_dbpath + name) + '.pydb'), 'rb')
    temp = file.read().decode('utf-8').split(u'</record>')
    file.close()
    _dbname = name
    dblen = (len(temp) - 1)
    for a in temp[0:(len(temp) - 1)]:
        kon = a.find(u'>')
        nazv = a[9:(kon - 1)]
        tit = a[(kon + 1):]
        try:
            nazv1 = int(nazv)
        except:
            nazv1 = nazv
        try:
            tit1 = int(tit)
        except:
            tit1 = tit
        _dbdict[nazv1] = tit1

    print_debug(((((((u'>opendb from ' + path) + name) + u'.pydb with ') + str(len(_dbdict.keys()))) + u' keys, update mode=') + str(update)))



def Read(x = 0):
    if (x == 0):
        print_debug(u'>get all keys')
        return _dbdict
    else:
        try:
            print_debug(((u'>get ' + x) + u' key'))
            return _dbdict[x]
        except:
            print_debug((u'(!)>get invalid key: ' + x))
            return None



def Deldb(name, path = 'e:/system/data/'):
    global _dbdict
    os.remove(((path + name) + '.pydb'))
    _dbdict = {}
    print_debug((((u'>delete db ' + path) + name) + u'.pydb'))



def Write(key, tit):
    _dbdict[key] = tit
    if _dbupdate:
        closedb()
    try:
        print_debug((u'>rec.key ' + str(key)))
    except:
        print_debug((u'>rec.key ' + key))



def Close(d = 0):
    file = open(((_dbpath + _dbname) + '.pydb'), 'w')
    for x in _dbdict.keys():
        file.write(u"<record='")
        try:
            x1 = str(x)
            file.write(x1.encode('utf-8'))
        except:
            file.write(x.encode('utf-8'))
        file.write(u"'>")
        try:
            x2 = str(_dbdict[x])
            file.write(x2.encode('utf-8'))
        except:
            file.write(_dbdict[x].encode('utf-8'))
        file.write('</record>'.encode('utf-8'))

    file.close()
    if (_dbupdate == 0):
        print_debug(((((((u'>update and close db ' + _dbpath) + _dbname) + u'.pydb') + u' ,with ') + str(len(_dbdict.keys()))) + u' keys'))



def Del(name):
    try:
        del _dbdict[name]
        if _dbupdate:
            closedb()
        try:
            print_debug((u'>del key ' + str(name)))
        except:
            print_debug((u'>del key ' + str(name)))
    except:
        try:
            print_debug((u'(!)>error del key ' + str(name)))
        except:
            print_debug((u'(!)>error del key ' + str(name)))



def Debug(x = 0):
    global _debug
    if x:
        mess = u'debug=on'
        _debug = 1
    else:
        mess = u'debug=off'
        _debug = 0
    f = open('e:/system/data/debug_set.minidb', 'w')
    f.write(mess)
    f.close()



# local variables:
# tab-width: 4
