Initial port to python3
[htsworkflow.git] / htsworkflow / frontend / bcmagic / views.py
index 492d7e58f685ca2d55f4570f79e5cca7e60f2ebd..43b32b5668277eb47b160fac71f5df814a5939b2 100644 (file)
@@ -6,8 +6,14 @@ from django.core.exceptions import ObjectDoesNotExist
 from htsworkflow.frontend.bcmagic import models
 from htsworkflow.frontend.bcmagic.utils import report_error, redirect_to_url
 from htsworkflow.frontend.bcmagic.plugin import bcm_plugin_processor
+from htsworkflow.frontend.bcmagic import plugin
+#from htsworkflow.util.jsonutil import encode_json
+
+try:
+    import json
+except ImportError as e:
+    import simplejson as json
 
-import json
 import re
 
 from htsworkflow.frontend.bcmagic import forms
@@ -22,6 +28,31 @@ def index(request):
                               context_instance=RequestContext(request))
 
 
+def __plugin_search(text):
+    """
+    Runs registered plugins to search for results
+    """
+    
+    hits = []
+    for label, search_func in list(plugin._SEARCH_FUNCTIONS.items()):
+        result = search_func(text)
+        if result is not None:
+            hits.extend(result)
+            
+    n = len(hits)
+    if n == 0:
+        msg = 'No hits found for: %s' % (text)
+        return report_error(msg)
+    elif n == 1:
+        return redirect_to_url(hits[0][1])
+    else:
+        msg = "%d hits found for (%s); multi-hit not implemented yet." % (n, text)
+        return report_error(msg)
+    
+    
+    #return json.dumps(hits)
+    
+
 def __magic_process(text):
     """
     Based on scanned text, check to see if there is map object to use
@@ -32,7 +63,8 @@ def __magic_process(text):
     
     # There should always be at least one | in a valid scan.
     if len(split_text) <= 1:
-        return report_error('Invalid text: %s' % (text))
+        #return report_error('Invalid text: %s' % (text))
+        return __plugin_search(text)
     
     # Keyword is the first element in the list
     keyword = split_text[0]
@@ -40,7 +72,7 @@ def __magic_process(text):
     # Attempt to find a KeywordMap based on keyword
     try:
         keymap = models.KeywordMap.objects.get(keyword=keyword)
-    except ObjectDoesNotExist, e:
+    except ObjectDoesNotExist as e:
         return report_error('Keyword (%s) is not defined' % (keyword))
     
     # Remove keyword and only scan the content
@@ -90,8 +122,8 @@ def magic(request):
     if text is None or text.strip() == '':
         d['mode'] = 'Error'
         d['status'] = 'Did not recieve text'
-        j = json.JSONEncoder()
-        return HttpResponse(j.encode(d), 'text/plain')
+        
+        return HttpResponse(json.dumps(d), 'text/plain')
     
     # Did not receive bcm_mode error
     if bcm_mode is None or bcm_mode.strip() == '':
@@ -116,8 +148,8 @@ def magic(request):
     # Try keyword mapper
     else:
         d = __magic_process(text)
-    j = json.JSONEncoder()
-    return HttpResponse(j.encode(d), 'text/plain')
+    
+    return HttpResponse(json.dumps(d), 'text/plain')
 
 
 
@@ -129,11 +161,11 @@ def json_test(request):
     else:
         text = None
     
-    #return HttpResponse(json.write(request.POST.items()), 'text/plain')
+    #return HttpResponse(json.dumps(request.POST.items()), 'text/plain')
     if text is None or text.strip() == '':
         d['mode'] = 'Error'
         d['status'] = 'Did not recieve text'
-        return HttpResponse(json.write(d), 'text/plain')
+        return HttpResponse(json.dumps(d), 'text/plain')
     
     if text.split('|')[0] == 'url':
         d['mode'] = 'redirect'
@@ -142,5 +174,4 @@ def json_test(request):
         d['msg'] = 'Recieved text: %s' % (text)
         d['mode'] = 'clear'
     
-    j = json.JSONEncoder()
-    return HttpResponse(j.encode(d), 'text/plain')
+    return HttpResponse(json.dumps(d), 'text/plain')