[#3026] Commande django pour harvest
authorPA Parent <paparent@paparent.me>
Sat, 10 Nov 2012 17:13:10 +0000 (12:13 -0500)
committerPA Parent <paparent@paparent.me>
Sat, 10 Nov 2012 17:13:10 +0000 (12:13 -0500)
auf_savoirs_en_partage/savoirs/management/commands/sep_harvest.py [new file with mode: 0644]

diff --git a/auf_savoirs_en_partage/savoirs/management/commands/sep_harvest.py b/auf_savoirs_en_partage/savoirs/management/commands/sep_harvest.py
new file mode 100644 (file)
index 0000000..8027db7
--- /dev/null
@@ -0,0 +1,72 @@
+# -*- coding: utf-8 -*-
+
+import sys, traceback
+
+from django.core.management.base import BaseCommand
+
+from auf_savoirs_en_partage.backend_config import RESOURCES
+from auf_savoirs_en_partage.savoirs.models import HarvestLog
+from auf_savoirs_en_partage.savoirs.lib.sep import SEP
+from auf_savoirs_en_partage.savoirs.lib.harvest import HarvestStats
+
+
+class Command(BaseCommand):
+
+    def handle(self, *args, **kwargs):
+
+        sep = SEP ()
+        resources = RESOURCES
+
+        try:
+            name = args[0]
+        except:
+            name = None
+
+        if name == 'stats':
+            stats = HarvestStats()
+            stats.wiki()
+            sys.exit(1)
+
+        if name is not None:
+            if RESOURCES.get(name) is not None:
+                resources = {name: RESOURCES.get(name)}
+            else:
+                print "Ressource %s non existante" % name
+                sys.exit(-1)
+
+        for name in resources.keys ():
+            print "Import:", name.encode('utf-8')
+            options = RESOURCES[name]
+            options['server'] = name
+
+            module = 'auf_savoirs_en_partage.savoirs.lib.harvesters.%s.%s' \
+                     % (options['type'], options['acces'])
+            __import__ (module)
+            harvester = sys.modules[module]
+            try:
+                nodes = harvester.harvest (options)
+            except:
+                print >> sys.stderr, "Exception:"
+                print >> sys.stderr, '-'*60
+                traceback.print_exc(file=sys.stderr)
+                print >> sys.stderr, '-'*60
+                nodes = []
+
+            added = updated = 0
+            for node in nodes:
+                node['server'] = name
+                status = sep.add(node)
+
+                if status['added']:
+                    added += 1
+                if status['updated']:
+                    updated += 1
+                message = status
+                message.update({'context':'record', 'name':name, 'processed':1})
+                HarvestLog.add(message)
+
+            message = {'context':'moisson', 'name':name, 'added':added, 'updated':updated, 'processed':len(nodes)}
+            HarvestLog.add(message)
+
+        del (sep)
+