Commits

Ronny Pfannschmidt  committed 635496d

use coffeescript

  • Participants
  • Parent commits 130c72e

Comments (0)

Files changed (2)

File glas_process/ddoc.py

+import coffeescript
+
 views = {
     'streams': {
-        'map': """
-            function(doc) {
-                if(doc.doc_type == "Task")
-                    emit([doc._id], null);
-                if(doc.stream)
-                    emit([doc.task, doc.step], doc.stream);
-            }""",
-        'reduce': """
-            function (key, values, rereduce) {
-                var items;
-                if(rereduce) {
+        'map': """(doc) ->
+                if doc.doc_type == "Task"
+                    emit [doc._id] null;
+                if doc.stream
+                    emit [doc.task, doc.step] doc.stream
+                return
+        """,
+        'reduce': """(key, values, rereduce) ->
+                if rereduce
                     items = [];
-                    while(values.length)
-                    items = items.concat(values.pop());
-                }
-                else {
-                    items = values;
-                }
+                    while values.length
+                        items = items.concat values.pop()
+                else
+                    items = values
 
-                var unique = {};
-                while(items.length) {
-                    var entry = items.pop();
-                    if (entry != null)
-                    unique[entry] = true;
-                }
+                unique = {};
+                while items.length
+                    entry = items.pop
+                    if entry != null
+                        unique[entry] = true;
 
-                var result = [];
-                for (var key in unique)
+                result = [];
+                for key in unique
                     result.push(key);
                 return result
-    }"""
+        """
     },
     'lines': {
-        'map':
-            'function (doc) { '
-                'if(doc.stream && doc.line)'
-                    'emit([doc.step, doc.stream, doc.lineno], doc.line);}'
+        'map': '''(doc) ->
+        if doc.stream && doc.line
+            emit [doc.step, doc.stream, doc.lineno] doc.line
+        return
+        '''
     }
 }
 
 
 lists = {
-    "lines": """
-function(head, req) {
-  start({"headers": { "Content-Type": "text/plain"}});
-  while(row = getRow()) {
-    send(row.value);
-  }
-}
+    "lines": """(head, req) ->
+          start headers: { "Content-Type": "text/plain"}
+          while row = getRow()
+            send row.value
+          return
 """
 }
 
 ]
 
 
+def makeviews(data, path = ()):
+    if isinstance(data, str):
+        try:
+            return coffeescript.compile(data, bare=True)
+        except Exception, e:
+            e.args =  (path,) + e.args
+            raise
+    else:
+        return dict((k, makeviews(v, path + (k,))) for k, v in data.items())
+
 ddoc = {
     '_id': '_design/glas_process',
-    'views': views,
-    'lists': lists,
+    'views': makeviews(views),
+    'lists': makeviews(lists),
     'rewrites': rewrites,
 }
     install_requires=[
         'couchdbkit',
         'anyvc',
+        'CoffeeScript',
     ],
     get_version_from_hg=True,
 )