Refactoring for compilestatic

This commit is contained in:
Antoine Bartuccio 2017-05-02 00:51:43 +02:00
parent 79c769351d
commit 630fdf93a4
2 changed files with 26 additions and 31 deletions

View File

@ -28,45 +28,40 @@ import sass
from django.core.management.base import BaseCommand from django.core.management.base import BaseCommand
class Command(BaseCommand): class Command(BaseCommand):
help = "Comile scss files from static folder" help = "Compile scss files from static folder"
def compilescss(self, folder): def is_compilable(self, file, ext_list):
to_compile = [] path, ext = os.path.splitext(file)
return ext in ext_list
def exec_on_folder(self, folder, func):
to_exec = []
for file in os.listdir(folder): for file in os.listdir(folder):
file = os.path.join(folder, file) file = os.path.join(folder, file)
if os.path.isdir(file): if os.path.isdir(file):
self.compilescss(file) self.exec_on_folder(file, func)
else: elif self.is_compilable(file, ['.scss']):
path, ext = os.path.splitext(file) to_exec.append(file)
if ext == ".scss":
to_compile.append(file)
for f in to_compile: for file in to_exec:
print("compilling %s" % f) func(file)
with open(f, "r") as oldfile:
with open(f.replace('.scss', '.css'), "w") as newfile:
newfile.write(sass.compile(string=oldfile.read()))
def removescss(self, folder): def compilescss(self, file):
to_remove = [] print("compiling %s" % file)
for file in os.listdir(folder): with open(file, "r") as oldfile:
file = os.path.join(folder, file) with open(file.replace('.scss', '.css'), "w") as newfile:
if os.path.isdir(file): newfile.write(sass.compile(string=oldfile.read()))
self.removescss(file)
else: def removescss(self, file):
path, ext = os.path.splitext(file) print("removing %s" % file)
if ext == ".scss": os.remove(file)
to_remove.append(file)
for f in to_remove:
print("removing %s" % f)
os.remove(f)
def handle(self, *args, **options): def handle(self, *args, **options):
if 'static' in os.listdir(): if 'static' in os.listdir():
print("---- Compilling scss files ---") print("---- Compiling scss files ---")
self.compilescss('static') self.exec_on_folder('static', self.compilescss)
print("---- Removing scss files ----") print("---- Removing scss files ----")
self.removescss('static') self.exec_on_folder('static', self.removescss)
else: else:
print("No static folder avaliable, please use collectstatic before compilling scss") print("No static folder avalaible, please use collectstatic before compiling scss")

View File

@ -1,7 +1,7 @@
# -*- coding:utf-8 -* # -*- coding:utf-8 -*
# #
# Copyright 2016,2017 # Copyright 2016,2017
# - Skia <skia@libskia.so> # - Sli <antoine@bartuccio.fr>
# #
# Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM, # Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM,
# http://ae.utbm.fr. # http://ae.utbm.fr.