Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # makedb.py
- # DMC.xml vers SQLite database
- # Pour Full Circle Magazine numero 85
- import apsw
- from xml.etree import ElementTree as ET
- nomtable = "DMC"
- def LireXML():
- global connexion
- global curseur
- nomfic = 'dmc.xml'
- arbre = ET.parse(nomfic)
- racine = arbre.getroot()
- compteur = 0
- for fil in racine.findall('floss'):
- nom = fil.find('name').text
- desc = fil.find('description').text
- for couleur in fil.findall('color'):
- rouge = couleur.find('red').text
- vert = couleur.find('green').text
- bleu = couleur.find('blue').text
- SQL = "INSERT INTO DMC (DMC,Description,Rouge,Vert,Bleu) VALUES \
- ('%s','%s',%s,%s,%s)" % (nom,desc,rouge,vert,bleu)
- curseur.execute(SQL)
- print "Enregistrement en cours : {0}".format(compteur)
- compteur += 1
- def OuvrirBase():
- global connexion
- global curseur
- global ucurseur
- global nombase
- connexion = apsw.Connection("fils.db3")
- curseur = connexion.cursor()
- ucurseur = connexion.cursor()
- def CreerTables():
- sql = '''CREATE TABLE IF NOT EXISTS DMC
- (pkID INTEGER PRIMARY KEY, DMC INTEGER,
- Description TEXT, Rouge INTEGER, Vert INTEGER, Bleu INTEGER,
- HEX TEXT,H INTEGER,S INTEGER,V INTEGER)'''
- curseur.execute(sql)
- def ViderTables():
- sql="DELETE FROM %s" % nomtable
- curseur.execute(sql)
- def rgb2hex(rgb):
- return '%02x%02x%02x' % rgb
- def rgb2hsv(r, g, b):
- r, g, b = r/255.0, g/255.0, b/255.0
- mx = max(r, g, b)
- mn = min(r, g, b)
- df = mx-mn
- if mx == mn:
- h = 0
- elif mx == r:
- h = (60 * ((g-b)/df) + 360) % 360
- elif mx == g:
- h = (60 * ((b-r)/df) + 120) % 360
- elif mx == b:
- h = (60 * ((r-g)/df) + 240) % 360
- if mx == 0:
- s = 0
- else:
- s = df/mx
- v = mx
- return int(round(h,0)), int(round(s*100,0)), int(round(v*100,0))
- def MAJBase():
- global ucurseur
- global curseur
- sql = "SELECT * FROM DMC"
- c = curseur.execute(sql)
- for i in c:
- pkid = i[0]
- r = i[3]
- g = i[4]
- b = i[5]
- hex = rgb2hex((r,g,b))
- h,s,v = rgb2hsv(r,g,b)
- sql2 = "UPDATE DMC SET hex='{0}',h={1},s={2},v={3} WHERE \
- pkid = {4}".format(hex,h,s,v,pkid)
- print ("Maj enregistrement {0}".format(pkid))
- ucurseur.execute(sql2)
- OuvrirBase()
- CreerTables()
- ViderTables() # Juste pour etre sur
- LireXML()
- MAJBase()
- print "Fin"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement