src/hdalab/management/commands/export_wpcategory_csv.py
branchdocumentation
changeset 693 09e00f38d177
parent 266 825ff4d6a8ac
equal deleted inserted replaced
692:b7d19cd87fcf 693:09e00f38d177
     1 # -*- coding: utf-8 -*-
     1 # -*- coding: utf-8 -*-
     2 '''
     2 '''
     3 Created on Feb 2, 2012
     3 Exporte en csv les catégories wikipédia utilisées dans HDALab.
     4 
     4 
     5 @author: ymh
     5 **Usage**: ``django-admin export_wpcategory_csv [options] <chemin_vers_le_fichier_csv>``
       
     6 
       
     7 **Options spécifiques:**
       
     8 
       
     9     - *\-\-encoding* : encodage des fichier, le défaut est `latin-1`.
       
    10     - *\-f* : force l'écrasement du fichier csv de sortie.
       
    11 
     6 '''
    12 '''
     7 from django.core.management.base import BaseCommand, CommandError
    13 from django.core.management.base import BaseCommand, CommandError
     8 from optparse import make_option
    14 from optparse import make_option
     9 from hdalab.management.utils import UnicodeWriter
    15 from hdalab.management.utils import UnicodeWriter
    10 from hdabo.wp_utils import normalize_tag
    16 from hdabo.wp_utils import normalize_tag
    21     help = """export csv files for hdabo
    27     help = """export csv files for hdabo
    22 Options:
    28 Options:
    23     -e, --encoding : files encoding. default to latin-1
    29     -e, --encoding : files encoding. default to latin-1
    24     -f, --force : force file overwrite
    30     -f, --force : force file overwrite
    25     """
    31     """
    26     
    32 
    27     option_list = BaseCommand.option_list + (
    33     option_list = BaseCommand.option_list + (
    28         make_option("-e","--encoding",
    34         make_option("-e","--encoding",
    29             action='store',
    35             action='store',
    30             type='string',
    36             type='string',
    31             dest='encoding',
    37             dest='encoding',
    35             action='store_true',
    41             action='store_true',
    36             dest='force',
    42             dest='force',
    37             default=False,
    43             default=False,
    38             help='force file overwrite'),
    44             help='force file overwrite'),
    39         )
    45         )
    40         
    46 
    41     def handle(self, *args, **options):
    47     def handle(self, *args, **options):
    42         
    48 
    43         if len(args) == 0 or not args[0]:
    49         if len(args) == 0 or not args[0]:
    44             raise CommandError("Gives at last one csv file to export")
    50             raise CommandError("Gives at last one csv file to export")
    45 
    51 
    46         self.encoding = options.get("encoding", "utf-8")
    52         self.encoding = options.get("encoding", "utf-8")
    47         self.force = options.get("force", False)
    53         self.force = options.get("force", False)
    48         self.path = args[0].strip()
    54         self.path = args[0].strip()
    49         self.interactive = options.get("interactive",True)
    55         self.interactive = options.get("interactive",True)
    50 
    56 
    51         file = None        
    57         file = None
    52         try:
    58         try:
    53             try:
    59             try:
    54                 file = open(self.path,'r')
    60                 file = open(self.path,'r')
    55                 if (not self.force) and self.interactive:
    61                 if (not self.force) and self.interactive:
    56                     print self.path
    62                     print self.path
    61                     else:
    67                     else:
    62                         return "error"
    68                         return "error"
    63                 elif not self.interactive and not self.force:
    69                 elif not self.interactive and not self.force:
    64                     print "Export file %s already exists. Exit." % (self.path)
    70                     print "Export file %s already exists. Exit." % (self.path)
    65                     return "error"
    71                     return "error"
    66     
    72 
    67                 file.close()
    73                 file.close()
    68                 file = open(self.path,'w')
    74                 file = open(self.path,'w')
    69             except IOError:
    75             except IOError:
    70                 file = open(self.path,'w')
    76                 file = open(self.path,'w')
    71         
    77 
    72             csv = UnicodeWriter(file, doublequote=False, escapechar="\\", encoding=self.encoding)
    78             csv = UnicodeWriter(file, doublequote=False, escapechar="\\", encoding=self.encoding)
    73             writer = None
    79             writer = None
    74             
    80 
    75             qs = WpCategory.objects.filter(tagwpcategory__hidden=False).distinct()
    81             qs = WpCategory.objects.filter(tagwpcategory__hidden=False).distinct()
    76             
    82 
    77             total = qs.count()
    83             total = qs.count()
    78             
    84 
    79             for i,wpcat in enumerate(qs):
    85             for i,wpcat in enumerate(qs):
    80                 writer = show_progress(i+1, total, wpcat.label, 50, writer)
    86                 writer = show_progress(i+1, total, wpcat.label, 50, writer)
    81                 nb_ds = 0
    87                 nb_ds = 0
    82                 for tag in wpcat.tagwpcategory_set.all():
    88                 for tag in wpcat.tagwpcategory_set.all():
    83                     nb_ds += tag.tag.datasheet_set.count()
    89                     nb_ds += tag.tag.datasheet_set.count()
    84                     
    90 
    85                 csv.writerow([wpcat.label, u"http://fr.wikipedia.org/wiki/Catégorie:%s" % urlquote(normalize_tag(wpcat.label)), nb_ds]) 
    91                 csv.writerow([wpcat.label, u"http://fr.wikipedia.org/wiki/Catégorie:%s" % urlquote(normalize_tag(wpcat.label)), nb_ds])
    86 
    92 
    87         finally:
    93         finally:
    88             if file is not None:
    94             if file is not None:
    89                 file.close()
    95                 file.close()