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, mv int, workdir text, manfile text, refresh_enabled int, refresh_delay int)''')
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 def hasStudent(student):
00080 global cursor
00081 cursor.execute("select * from owners where student=?", (student,))
00082 return cursor.fetchone() != None
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092 def knowsId(stickid, uuid,tattoo):
00093 global cursor
00094 cursor.execute("select * from owners where stickid=? and uuid=? and tatoo=?", (stickid, uuid,tattoo))
00095 return cursor.fetchone() != None
00096
00097
00098
00099
00100
00101 def tattooList():
00102 global cursor
00103 cursor.execute("select tatoo from owners")
00104 return cursor.fetchmany()
00105
00106
00107
00108
00109
00110
00111 def readStudent(stickid, uuid,tattoo):
00112 global cursor
00113 cursor.execute("select student from owners where stickid=? and uuid=? and tatoo=?", (stickid, uuid,tattoo))
00114 s = cursor.fetchone()
00115 if s != None:
00116 return s[0]
00117 else:
00118 return None
00119
00120
00121
00122
00123
00124
00125 def readPrefs():
00126 global cursor
00127 cursor.execute("select checkable, mv, workdir, manfile, refresh_enabled, refresh_delay from preferences")
00128 s = cursor.fetchone()
00129 if s != None:
00130 checkable = s[0]==1
00131 mv = s[1]==1
00132 workdir = s[2]
00133 manfile = s[3]
00134 refreshEnabled = s[4]==1
00135 refreshDelay = s[5]
00136 return {"checkable" : checkable,
00137 "mv" : mv,
00138 "workdir" : workdir,
00139 "manfile" : manfile,
00140 "refreshEnabled": refreshEnabled,
00141 "refreshDelay" : refreshDelay
00142 }
00143 else:
00144
00145 return {"checkable" : True,
00146 "mv" : False,
00147 "workdir" : "Travail",
00148 "manfile" : "/usr/share/scolasync/help/manualPage_fr_FR.html",
00149 "refreshEnabled": False,
00150 "refreshDelay" : 30
00151 }
00152
00153
00154
00155
00156
00157 def setWd(newDir):
00158 cursor.execute("""update preferences set workdir=?""",
00159 (newDir,))
00160 database.commit()
00161
00162
00163
00164
00165
00166
00167 def writeStudent(stickid, uuid, tattoo, student):
00168 global database, cursor
00169 if knowsId(stickid, uuid, tattoo):
00170 cursor.execute("""update owners
00171 set student=?
00172 where stickid=? and uuid=? and tatoo=?""", (student, stickid, uuid, tattoo))
00173 else:
00174 cursor.execute("""insert into owners
00175 values (?,?,?,?)""", (stickid, uuid, tattoo, student))
00176 database.commit()
00177
00178
00179
00180
00181
00182
00183 def writePrefs(prefs):
00184 global database, cursor
00185 if prefs["checkable"]:
00186 checkable=1
00187 else:
00188 checkable=0
00189 if prefs["mv"]:
00190 mv=1
00191 else:
00192 mv=0
00193 if prefs["refreshEnabled"]:
00194 refreshEnabled=1
00195 else:
00196 refreshEnabled=0
00197 cursor.execute("select checkable from preferences")
00198 s = cursor.fetchone()
00199 newValues=(checkable, mv, prefs["workdir"], prefs["manfile"], refreshEnabled, prefs["refreshDelay"])
00200 if s != None:
00201 cursor.execute("""update preferences
00202 set checkable=?, mv=?, workdir=?, manfile=?, refresh_enabled=?, refresh_delay=?""",
00203 newValues)
00204 else:
00205 cursor.execute("""insert into preferences
00206 values (?,?,?,?,?,?)""",
00207 newValues)
00208 database.commit()
00209
00210
00211 if database == None:
00212 openDb()
00213