URL: https://linuxfr.org/forums/programmation-python/posts/optimisation-programme
Title: Optimisation programme
Authors: JPEC
Date: 2013年01月07日T22:18:19+01:00
License: CC By-SA
Tags: python3, raspyplayer et raspberry_pi
Score: 0
Bonsoir à tous,
Dans le cadre de l'optimisation de [RasPyPlayer](https://linuxfr.org/news/raspyplayer-v1-0-est-sorti) j'aurais besoin d'un peu d'aide. Je cherche des moyens pour accélérer la recherche les fichiers vidéos. **Si vous avez des pistes je suis preneur !**
Le code actuel :
```python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#-------------------------------------------------------------------------#
# MediaScanner.py - Movies scanner for RasPyPlayer
#-------------------------------------------------------------------------#
VERSION = "1.0-devel"
#-------------------------------------------------------------------------#
# Auteur : Julien Pecqueur (JPEC)
# Email : jpec@julienpecqueur.net
# Site : http://raspyplayer.org
# Sources : https://github.com/jpec/RasPyPlayer
# Bugs : https://github.com/jpec/RasPyPlayer/issues
# License :
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see .
#-------------------------------------------------------------------------#
#-------------------------------------------------------------------------#
# IMPORTATION DES MODULES #
#-------------------------------------------------------------------------#
import os
import sqlite3
from sys import argv
from time import time
#-------------------------------------------------------------------------#
# PARAMETRAGE PROGRAMME #
#-------------------------------------------------------------------------#
# DEBUG - Mode debug (0 - off / 1 - on) :
DEBUG = 0
# EXT - Extensions des fichiers vidéos à ajouter dans la librairie :
EXT = [".avi", ".mpg", ".mp4", ".wmv", ".mkv"]
# EXC - Répertoires à exclure de la recherche des vidéos :
EXC = ["Backup",
"Musique",
"Musique_old",
"MacBookPro",
"Temporary Items",
".TemporaryItems",
"MacBook Pro de Julien.sparsebundle",
"A VOIR"]
#-------------------------------------------------------------------------#
# DEFINITION DES CLASSES #
#-------------------------------------------------------------------------#
class Db(object):
"""Classe permettant la gestion de la base de données"""
def __init__(self, db):
"""Constructeur de la classe Db"""
self.db = db
self.con = False
self.cur = False
self.top = False
self.DBCREATE = "CREATE TABLE files (file, path)"
self.DBADD = "INSERT INTO files VALUES (?, ?)"
self.DBDROP = "DROP TABLE files"
def openDb(self):
"""Ouvre la base de donnée, la créé le cas échéant"""
sql = False
bind = False
if os.path.isfile(self.db):
self.top = True
self.con = sqlite3.connect(self.db)
self.cur = self.con.cursor()
return(True)
def initDb(self):
"""Initialisation de la base de données"""
if self.top:
self.execSql(self.DBDROP, False)
self.execSql(self.DBCREATE, False)
self.top = True
return(True)
def closeDb(self):
"""Ferme la base de données"""
self.cur.close()
self.con.close()
self.top = False
return(True)
def execSql(self, sql, bind):
"""Exécute une requête dans la base de données"""
if sql:
if bind:
if DEBUG:
print(sql, bind)
self.cur.execute(sql, bind)
else:
self.cur.execute(sql)
self.con.commit()
return(True)
else:
return(False)
#-------------------------------------------------------------------------#
# FONCTIONS #
#-------------------------------------------------------------------------#
def scanFiles(db, path):
"""Scan les répertoires et alimente la base de données"""
print(path)
for file in os.listdir(path):
filepath = path+"/"+file
if len(file)> 4 and file[-4: len(file)] in EXT \
and file[0:1] != ".":
# Fichier
db.execSql(db.DBADD, (os.path.basename(file), filepath))
elif os.path.isdir(filepath) and not file in EXC \
and file[0:1] != ".":
# Répertoire
scanFiles(db, filepath)
#-------------------------------------------------------------------------#
# PROGRAMME PRINCIPAL #
#-------------------------------------------------------------------------#
if len(argv) == 2:
t1 = time()
path = argv[1]
db = Db("RasPyPlayer.sqlite3")
if db.openDb() and db.initDb():
scanFiles(db, path)
db.closeDb()
t2 = time()
print("Elapsed time : {} sec".format(t2 - t1))
else:
print("Usage: {} /path/to/medias".format(argv[0]))
#-------------------------------------------------------------------------#
# EOF #
#-------------------------------------------------------------------------#
```