#! /usr/bin/env python # -*- coding: utf-8 -*- # vim:fenc=utf-8 # # Copyright © 2015 lafrite # """ Parcours tous les dossiers et ajoute un fichier notes_***.rst quand il n'existe pas """ #import optparse import argparse from path import path import jinja2 import copy parser = argparse.ArgumentParser() parser.add_argument("directory", help="Directory") option = parser.parse_args() templateLoader = jinja2.FileSystemLoader( searchpath="." ) templateEnv = jinja2.Environment( loader=templateLoader ) tpl_file = "./tools/Other/tpl_notes.rst" tpl = templateEnv.get_template(tpl_file) cwd = path("./").abspath() directory = path(option.directory) info = {"date" : "2013-07-01", "modified" : "2013-07-01", "summary": "Pas de résumé, note créée automatiquement parce que je ne l'avais pas bien fait...", "authors" : "Benjamin Bertrand", "tags": set(), "category": set(), "title" : "", "telechargeable" : [], } ext_telechargeable = [".ipynb", ".ps", ".tex", ".pdf", ".csv", ".jpg", ".xlsx", ".xls", ".odt", ".ods", ".png", ".py", ".cls", ".sty", ".ggb", ".odc"] non_exam_type = ["Cours", "Exo", "TD"] exam_type = ["DS", "DM"] def underline(string, cara = "#"): return string + "\n" + cara * len(string) for i in directory.walkdirs(): #if i.files("notes_*.rst"): if not i.files("index.rst"): cara = i.split('/') try: cara.remove('Archive') except ValueError: pass if cara[-1] not in ["fig", "Bilan", "Conn"]: folder_info = copy.deepcopy(info) folder_info["title"] = underline("Notes sur " + cara[-1].replace('_', ' ')) print(folder_info['title']) folder_info["category"].add(cara[1]) try: tag = cara[2].replace('_', ' ') folder_info["tags"].add(tag) except IndexError: pass folder_info['tags'] = folder_info['tags'].union(set([i for i in exam_type if i in cara])) folder_info['tags'] = folder_info['tags'].union(set([i for i in non_exam_type if i.lower() in [c.lower() for c in cara]])) for f in i.files(): if f.ext in ext_telechargeable: print("\t ",f.basename()) folder_info["telechargeable"] += [f.basename()] if (i/'fig').exists(): for f in (i/'fig').files(): if f.ext in ext_telechargeable: print("\t ",f.basename()) folder_info["telechargeable"] += ['fig/'+f.basename()] if folder_info["telechargeable"]: cible = i / "index.rst" with open(cible,'w') as f: f.write(tpl.render(folder_info)) # ----------------------------- # Reglages pour 'vim' # vim:set autoindent expandtab tabstop=4 shiftwidth=4: # cursor: 16 del