1 # -*- coding: utf-8 -*- |
|
2 ''' |
|
3 Created on Feb 2, 2012 |
|
4 |
|
5 @author: ymh |
|
6 ''' |
|
7 from django.core.management.base import BaseCommand, CommandError |
|
8 from optparse import make_option |
|
9 from hdalab.management.utils import UnicodeWriter |
|
10 from hdabo.wp_utils import normalize_tag |
|
11 from hdalab.models.categories import WpCategory |
|
12 from django.utils.http import urlquote |
|
13 from hdabo.utils import show_progress |
|
14 |
|
15 class Command(BaseCommand): |
|
16 ''' |
|
17 Command to export tags |
|
18 ''' |
|
19 args = '<path_to_csv_file>' |
|
20 options = '[-e|--encoding csv file encoding]|[-f|--force force file overwrite]' |
|
21 help = """export csv files for hdabo |
|
22 Options: |
|
23 -e, --encoding : files encoding. default to latin-1 |
|
24 -f, --force : force file overwrite |
|
25 """ |
|
26 |
|
27 option_list = BaseCommand.option_list + ( |
|
28 make_option("-e","--encoding", |
|
29 action='store', |
|
30 type='string', |
|
31 dest='encoding', |
|
32 default="utf-8", |
|
33 help='file encoding, default utf-8'), |
|
34 make_option("-f","--force", |
|
35 action='store_true', |
|
36 dest='force', |
|
37 default=False, |
|
38 help='force file overwrite'), |
|
39 ) |
|
40 |
|
41 def handle(self, *args, **options): |
|
42 |
|
43 if len(args) == 0 or not args[0]: |
|
44 raise CommandError("Gives at last one csv file to export") |
|
45 |
|
46 self.encoding = options.get("encoding", "utf-8") |
|
47 self.force = options.get("force", False) |
|
48 self.path = args[0].strip() |
|
49 self.interactive = options.get("interactive",True) |
|
50 |
|
51 file = None |
|
52 try: |
|
53 try: |
|
54 file = open(self.path,'r') |
|
55 if (not self.force) and self.interactive: |
|
56 print self.path |
|
57 resp = raw_input("export file already exists. override ? type yes to continue : ") |
|
58 if resp is not None and (resp.lower() == "yes" or resp.lower() == "y"): |
|
59 self.force = True |
|
60 # clear file |
|
61 else: |
|
62 return "error" |
|
63 elif not self.interactive and not self.force: |
|
64 print "Export file %s already exists. Exit." % (self.path) |
|
65 return "error" |
|
66 |
|
67 file.close() |
|
68 file = open(self.path,'w') |
|
69 except IOError: |
|
70 file = open(self.path,'w') |
|
71 |
|
72 csv = UnicodeWriter(file, doublequote=False, escapechar="\\", encoding=self.encoding) |
|
73 writer = None |
|
74 |
|
75 qs = WpCategory.objects.filter(tagwpcategory__hidden=False).distinct() |
|
76 |
|
77 total = qs.count() |
|
78 |
|
79 for i,wpcat in enumerate(qs): |
|
80 writer = show_progress(i+1, total, wpcat.label, 50, writer) |
|
81 nb_ds = 0 |
|
82 for tag in wpcat.tagwpcategory_set.all(): |
|
83 nb_ds += tag.tag.datasheet_set.count() |
|
84 |
|
85 csv.writerow([wpcat.label, u"http://fr.wikipedia.org/wiki/Catégorie:%s" % urlquote(normalize_tag(wpcat.label)), nb_ds]) |
|
86 |
|
87 finally: |
|
88 if file is not None: |
|
89 file.close() |
|