00001
00002
00003
00004 licence={}
00005 licence['en']="""
00006 file db.py
00007 this file is part of the project scolasync
00008
00009 Copyright (C) 2010 Georges Khaznadar <georgesk@ofset.org>
00010
00011 This program is free software: you can redistribute it and/or modify
00012 it under the terms of the GNU General Public License as published by
00013 the Free Software Foundation, either version3 of the License, or
00014 (at your option) any later version.
00015
00016 This program is distributed in the hope that it will be useful,
00017 but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00019 GNU General Public License for more details.
00020
00021 You should have received a copy of the GNU General Public License
00022 along with this program. If not, see <http://www.gnu.org/licenses/>.
00023 """
00024
00025 import os.path, sqlite3, subprocess
00026 import version
00027 from globaldef import userShareDir
00028
00029 database= None
00030 cursor=None
00031
00032
00033
00034
00035
00036
00037 def openDb():
00038 global database, cursor
00039 dir=os.path.expanduser(userShareDir)
00040 if not os.path.isdir(dir):
00041 subprocess.call("mkdir %s" %dir, shell=True)
00042 database = sqlite3.connect(os.path.join(dir,"db"))
00043 cursor=database.cursor()
00044 cursor.execute('''create table if not exists owners (stickid text, uuid text, tatoo text, student text)''')
00045 cursor.execute('''create table if not exists version (major text, minor text)''')
00046 cursor.execute('''create table if not exists preferences (checkable int, workdir text)''')
00047 database.commit()
00048 checkVersion(version.major(), version.minor())
00049
00050
00051
00052
00053
00054
00055
00056
00057 def checkVersion(major, minor):
00058 cursor.execute('''select * from version''')
00059 values=cursor.fetchone()
00060 if values == None:
00061
00062 cursor.execute('''insert into version values (?,?)''', (version.major(), version.minor()))
00063 else:
00064 major, minor = values
00065 if major < version.major():
00066 raise KeyError, "The database version is too old!"
00067 elif minor < version.minor():
00068 cursor.execute("""update version
00069 set minor=?
00070 where major=?""", (version.minor(), version.major()))
00071 database.commit()
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081 def knowsId(stickid, uuid,tattoo):
00082 global cursor
00083 cursor.execute("select * from owners where stickid=? and uuid=? and tatoo=?", (stickid, uuid,tattoo))
00084 return cursor.fetchone() != None
00085
00086
00087
00088
00089
00090 def tattooList():
00091 global cursor
00092 cursor.execute("select tatoo from owners")
00093 return cursor.fetchmany()
00094
00095
00096
00097
00098
00099
00100 def readStudent(stickid, uuid,tattoo):
00101 global cursor
00102 cursor.execute("select student from owners where stickid=? and uuid=? and tatoo=?", (stickid, uuid,tattoo))
00103 s = cursor.fetchone()
00104 if s != None:
00105 return s[0]
00106 else:
00107 return None
00108
00109
00110
00111
00112
00113
00114 def readPrefs():
00115 global cursor
00116 cursor.execute("select checkable, workdir from preferences")
00117 s = cursor.fetchone()
00118 if s != None:
00119 checkable = s[0]==1
00120 workdir = s[1]
00121 return {"checkable": checkable, "workdir": workdir}
00122 else:
00123
00124 return {"checkable": True, "workdir": "Travail"}
00125
00126
00127
00128
00129
00130 def setWd(newDir):
00131 cursor.execute("""update preferences set workdir=?""",
00132 (newDir,))
00133 database.commit()
00134
00135
00136
00137
00138
00139
00140 def writeStudent(stickid, uuid, tattoo, student):
00141 global database, cursor
00142 if knowsId(stickid, uuid, tattoo):
00143 cursor.execute("""update owners
00144 set student=?
00145 where stickid=? and uuid=? and tatoo=?""", (student, stickid, uuid, tattoo))
00146 else:
00147 cursor.execute("""insert into owners
00148 values (?,?,?,?)""", (stickid, uuid, tattoo, student))
00149 database.commit()
00150
00151
00152
00153
00154
00155
00156 def writePrefs(prefs):
00157 global database, cursor
00158 if prefs["checkable"]:
00159 checkable=1
00160 else:
00161 checkable=0
00162 cursor.execute("select checkable, workdir from preferences")
00163 s = cursor.fetchone()
00164 if s != None:
00165 cursor.execute("""update preferences
00166 set checkable=?, workdir=?""",
00167 (checkable, prefs["workdir"]))
00168 else:
00169 cursor.execute("""insert into preferences
00170 values (?,?)""", (checkable, prefs["workdir"]))
00171 database.commit()
00172
00173
00174 if database == None:
00175 openDb()
00176