diff -r b7d19cd87fcf -r 09e00f38d177 src/hdalab/management/commands/export_wpcategory_csv.py --- a/src/hdalab/management/commands/export_wpcategory_csv.py Thu Apr 12 01:27:16 2018 +0200 +++ b/src/hdalab/management/commands/export_wpcategory_csv.py Wed Apr 11 12:19:47 2018 +0200 @@ -1,8 +1,14 @@ # -*- coding: utf-8 -*- ''' -Created on Feb 2, 2012 +Exporte en csv les catégories wikipédia utilisées dans HDALab. + +**Usage**: ``django-admin export_wpcategory_csv [options] `` -@author: ymh +**Options spécifiques:** + + - *\-\-encoding* : encodage des fichier, le défaut est `latin-1`. + - *\-f* : force l'écrasement du fichier csv de sortie. + ''' from django.core.management.base import BaseCommand, CommandError from optparse import make_option @@ -23,7 +29,7 @@ -e, --encoding : files encoding. default to latin-1 -f, --force : force file overwrite """ - + option_list = BaseCommand.option_list + ( make_option("-e","--encoding", action='store', @@ -37,9 +43,9 @@ default=False, help='force file overwrite'), ) - + def handle(self, *args, **options): - + if len(args) == 0 or not args[0]: raise CommandError("Gives at last one csv file to export") @@ -48,7 +54,7 @@ self.path = args[0].strip() self.interactive = options.get("interactive",True) - file = None + file = None try: try: file = open(self.path,'r') @@ -63,27 +69,27 @@ elif not self.interactive and not self.force: print "Export file %s already exists. Exit." % (self.path) return "error" - + file.close() file = open(self.path,'w') except IOError: file = open(self.path,'w') - + csv = UnicodeWriter(file, doublequote=False, escapechar="\\", encoding=self.encoding) writer = None - + qs = WpCategory.objects.filter(tagwpcategory__hidden=False).distinct() - + total = qs.count() - + for i,wpcat in enumerate(qs): writer = show_progress(i+1, total, wpcat.label, 50, writer) nb_ds = 0 for tag in wpcat.tagwpcategory_set.all(): nb_ds += tag.tag.datasheet_set.count() - - csv.writerow([wpcat.label, u"http://fr.wikipedia.org/wiki/Catégorie:%s" % urlquote(normalize_tag(wpcat.label)), nb_ds]) + + csv.writerow([wpcat.label, u"http://fr.wikipedia.org/wiki/Catégorie:%s" % urlquote(normalize_tag(wpcat.label)), nb_ds]) finally: if file is not None: - file.close() \ No newline at end of file + file.close()