ScolaSync  5.1
db.py
Aller à la documentation de ce fichier.
1 # $Id: db.py 47 2011-06-13 10:20:14Z georgesk $
2 
3 licence={}
4 licence['en']="""
5  file db.py
6  this file is part of the project scolasync
7 
8  Copyright (C) 2010 Georges Khaznadar <georgesk@ofset.org>
9 
10  This program is free software: you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation, either version3 of the License, or
13  (at your option) any later version.
14 
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with this program. If not, see <http://www.gnu.org/licenses/>.
22 """
23 
24 import os.path, sqlite3, subprocess
25 import version
26 from globaldef import userShareDir
27 
28 database= None # base de données sqlite 3 commune à tous les modules
29 cursor=None
30 
31 
34 
35 def openDb():
36  global database, cursor
37  dir=os.path.expanduser(userShareDir)
38  if not os.path.isdir(dir):
39  subprocess.call("mkdir %s" %dir, shell=True)
40  database = sqlite3.connect(os.path.join(dir,"db"))
41  cursor=database.cursor()
42  cursor.execute('''create table if not exists owners (stickid text, uuid text, tatoo text, student text)''')
43  cursor.execute('''create table if not exists version (major text, minor text)''')
44  cursor.execute('''create table if not exists preferences (checkable int, mv int, schoolfile text, workdir text, manfile text)''')
45  database.commit()
46  checkVersion(version.major(), version.minor())
47 
48 # Vérifie si la base de données reste compatible.
49 # Un changement de version majeur implique une mise à jour en cas de
50 # base de donnée ancienne. Un changmeent de version mineur n'implique
51 # pas de changement de structure de la base de données.
52 #
53 
54 def checkVersion(major, minor):
55  cursor.execute('''select * from version''')
56  values=cursor.fetchone()
57  if values == None:
58  # pas de version existante, on la crée
59  cursor.execute('''insert into version values (?,?)''', (version.major(), version.minor()))
60  else:
61  major, minor = values
62  if major < version.major():
63  raise KeyError("The database version is too old!")
64  elif minor < version.minor():
65  cursor.execute("""update version
66  set minor=?
67  where major=?""", (version.minor(), version.major()))
68  database.commit()
69 
70 
74 
75 def hasStudent(student):
76  global cursor
77  cursor.execute("select * from owners where student=?", (student,))
78  return cursor.fetchone() != None
79 
80 
86 
87 def knowsId(stickid, uuid,tattoo):
88  global cursor
89  cursor.execute("select * from owners where stickid=? and uuid=? and tatoo=?", (stickid, uuid,tattoo))
90  return cursor.fetchone() != None
91 
92 
94 
95 def tattooList():
96  global cursor
97  cursor.execute("select tatoo from owners")
98  return cursor.fetchmany()
99 
100 
103 
104 def readStudent(stickid, uuid, tattoo):
105  global cursor
106  cursor.execute("select student from owners where stickid=? and uuid=? and tatoo=?", (stickid, uuid,tattoo))
107  s = cursor.fetchone()
108  if s != None:
109  return s[0]
110  else:
111  return None
112 
113 
116 
117 def readPrefs():
118  global cursor
119  cursor.execute("select checkable, mv, schoolfile,workdir, manfile from preferences")
120  s = cursor.fetchone()
121  if s != None:
122  checkable = s[0]==1
123  mv = s[1]==1
124  schoolFile = s[2]
125  workdir = s[3]
126  manfile = s[4]
127  return {"checkable" : checkable,
128  "mv" : mv,
129  "schoolFile" : schoolFile,
130  "workdir" : workdir,
131  "manfile" : manfile,
132  }
133  else:
134  # valeur par défaut si la base est vide de préférences
135  return {"checkable" : True,
136  "mv" : False,
137  "schoolFile" : "/usr/share/scolasync/exemple/SCONET_test.xml",
138  "workdir" : "Travail",
139  "manfile" : "/usr/share/scolasync/help/manualPage_fr_FR.html",
140  }
141 
142 
144 
145 def setWd(newDir):
146  cursor.execute("""update preferences set workdir=?""",
147  (newDir,))
148  database.commit()
149 
150 
153 
154 def writeStudent(stickid, uuid, tattoo, student):
155  global database, cursor
156  if knowsId(stickid, uuid, tattoo):
157  cursor.execute("""update owners
158  set student=?
159  where stickid=? and uuid=? and tatoo=?""", (student, stickid, uuid, tattoo))
160  else:
161  cursor.execute("""insert into owners
162  values (?,?,?,?)""", (stickid, uuid, tattoo, student))
163  database.commit()
164 
165 
168 
169 def writePrefs(prefs):
170  global database, cursor
171  checkable=1
172  if prefs["mv"]:
173  mv=1
174  else:
175  mv=0
176  cursor.execute("select checkable from preferences")
177  s = cursor.fetchone()
178  newValues=(1, mv, prefs["schoolFile"], prefs["workdir"], prefs["manfile"])
179  if s != None:
180  cursor.execute("""update preferences
181  set checkable=?, mv=?, schoolfile=?, workdir=?, manfile=?""",
182  newValues)
183  else:
184  cursor.execute("""insert into preferences
185  values (?,?,?,?,?)""",
186  newValues)
187  database.commit()
188 
189 
190 if database == None:
191  openDb()
src.db.checkVersion
def checkVersion(major, minor)
Definition: db.py:54
src.db.hasStudent
def hasStudent(student)
vérifie qu'un étudiant est déjà connu
Definition: db.py:75
src.db.tattooList
def tattooList()
Renvoie la liste des tatouages connus de la base de données.
Definition: db.py:95
src.db.writeStudent
def writeStudent(stickid, uuid, tattoo, student)
inscrit un étudiant comme propriétaire d'une clé USB
Definition: db.py:154
src.db.writePrefs
def writePrefs(prefs)
inscrit les préférences
Definition: db.py:169
src.db.openDb
def openDb()
Ouverture de la base de données de l'application, et création si nécessaire.
Definition: db.py:35
src.db.knowsId
def knowsId(stickid, uuid, tattoo)
dit si une clé USB est déjà connue
Definition: db.py:87
src.db.setWd
def setWd(newDir)
définit le nouveau nom du répertoire de travail préféré.
Definition: db.py:145
src.db.readStudent
def readStudent(stickid, uuid, tattoo)
renvoie l'étudiant qui possède une clé USB
Definition: db.py:104
src.db.readPrefs
def readPrefs()
renvoie les préférences de ScolaSync
Definition: db.py:117