refactor code
[auf_savoirs_en_partage_django.git] / auf_savoirs_en_partage / savoirs / lib / harvest.py
1 # -*- encoding: utf-8 -*-
2 import sys, os, time, traceback
3 from auf_savoirs_en_partage.backend_config import RESOURCES
4 from savoirs.models import HarvestLog, Record
5 from sep import SEP
6
7 class HarvestStats:
8
9 libelles = {'site':50, 'url':50, 'type':10, 'acces':10, 'ref_importees':20, 'date':30, 'obs':10}
10 separateur = " "
11
12 def dernier_logs_moisson(self, site):
13 return HarvestLog.objects.filter(name=site, context='moisson').order_by('-date')
14
15 def date(self, site):
16 logs = self.dernier_logs_moisson(site)
17 if len(logs) > 0:
18 return str(logs[0].date)
19 else:
20 return "pas d'import"
21
22 def ref_importees(self, site):
23 records = Record.objects.filter(server=site)
24 return str(len(records))
25
26 def stats(self,):
27 stats = []
28 tableau = []
29 for site, options in RESOURCES.items():
30 options['site'] = site
31 tableau.append(options)
32
33 # libelles
34 libelles_formates = []
35 for l, largeur in self.libelles.items():
36 l = "*%s*" % l
37 libelles_formates.append(l.ljust(largeur, self.separateur))
38 stats.append(libelles_formates)
39
40 # lignes
41 for ligne in tableau:
42 ligne_ordonnee = []
43 for l,largeur in self.libelles.items():
44 method = getattr(self, l, None)
45 if method is not None:
46 value = method(ligne['site'])
47 elif ligne.has_key(l):
48 value = ligne[l]
49 else:
50 value = ""
51 value = value.ljust(largeur, self.separateur)
52 ligne_ordonnee.append(value)
53 stats.append(ligne_ordonnee)
54 return stats
55
56 def wiki(self,):
57 for s in self.stats():
58 print "|%s|" % "|".join(s)
59
60 def import_all ():
61 """Cette méthode effectue l'importation des données pour toutes les
62 sources définies dans `conf.py`, et les ajoute dans le système de stockage
63 en passant par SEP (:doc:`../sep/index`)
64 """
65 sep = SEP ()
66
67 resources = RESOURCES
68 if len(sys.argv) == 2:
69 name = sys.argv[1]
70
71 if name == 'stats':
72 stats = HarvestStats()
73 stats.wiki()
74 sys.exit(1)
75
76 if RESOURCES.get(name) is not None:
77 resources = {name: RESOURCES.get(name)}
78 else:
79 print "Ressource %s non existante" % name
80 sys.exit(-1)
81
82 for name in resources.keys ():
83 print "Import:", name
84 options = RESOURCES[name]
85 options['server'] = name
86
87 module = 'harvesters.%s.%s' \
88 % (options['type'], options['acces'])
89 __import__ (module)
90 harvester = sys.modules[module]
91 try:
92 nodes = harvester.harvest (options)
93 except:
94 print "Exception:"
95 print '-'*60
96 traceback.print_exc(file=sys.stdout)
97 print '-'*60
98 nodes = []
99
100 added = updated = 0
101 for node in nodes:
102 node['server'] = name
103
104 try:
105 status = sep.add (node)
106 except:
107 message.update({'context':'error', 'name':name, 'processed':0})
108 HarvestLog.add(message)
109 continue
110
111 if status['added']:
112 added += 1
113 if status['updated']:
114 updated += 1
115 message = status
116 message.update({'context':'record', 'name':name, 'processed':1})
117 HarvestLog.add(message)
118
119 message = {'context':'moisson', 'name':name, 'added':added, 'updated':updated, 'processed':len(nodes)}
120 HarvestLog.add(message)
121
122 del (sep)
123
124 if __name__ == '__main__':
125 import_all()