Commits

Ronny Pfannschmidt committed 23c5b35

add nonverbose show, better coffescript2js attachments

Comments (0)

Files changed (3)

couchdb_compose/cli.py

 """
 couchdb-compose composes your couchdb  documents
 
-usage: couchdb-compose show [options]
+usage: couchdb-compose show [--non-verbose] [options]
        couchdb-compose push DATABASE [--deploy-views] [options]
        couchdb-compose deploy_views DATABASE [options]
        couchdb-compose drop_viewdata DATABASE [options]
     -h, --help      help
     --path PATH     different path for the default composer [default: ./]
     --deploy-views  deploy views before pushing the ddoc
+    --non-verbose   replace all multiline values with '...' before display
 
 """
 
 
 
 def show(args, composer):
-    json.dump(composer.doc, sys.stdout, indent=1, sort_keys=True)
+    doc = composer.doc
+    if args['--non-verbose']:
+        from .util import ellipsize
+        doc = ellipsize(doc)
+
+    json.dump(doc, sys.stdout, indent=1, sort_keys=True)
 
 
 def push(args, composer):

couchdb_compose/preprocess.py

 
 DOT_COFFEE = '.coffee'
 
+def compile_coffeescript(data):
+    try:
+        data = coffeescript.compile(data, bare=True)
+    except ProgramError as exc:
+        print repr(data)
+        raise SyntaxError(exc.args[0])
+    return data.strip()
+
 
 def commonjs_to_coffeescript(path, name, data):
     if isinstance(data, str) and name.endswith(DOT_COFFEE):
-        try:
-            data = coffeescript.compile(data, bare=True)
-        except ProgramError as exc:
-            print repr(data)
-            raise SyntaxError(exc.args[0])
-        return name[:-len(DOT_COFFEE)], data.strip()
+        return name[:-len(DOT_COFFEE)], compile_coffeescript(data)
 
 
+def attachment_to_coffeescript(path, name, info):
+    if path==('_attachments',) and name.endswith(DOT_COFFEE):
+        info = dict(info,
+            data=compile_coffeescript(info['data']),
+            content_type="application/javascript",
+        )
+        return name.replace(DOT_COFFEE, '.js') , info
+
 
 def preprocess(data, path, processors):
     for k, v in list(data.items()):
                 preprocess(v, path + (k,), processors)
 
 
-preprocessors = [commonjs_to_coffeescript]
+preprocessors = [
+    commonjs_to_coffeescript,
+    attachment_to_coffeescript,
+]
 
 def run_preprocessors(composer):
     preprocess(composer.doc, (), preprocessors)

couchdb_compose/util.py

+
+def ellipsize(doc):
+    if isinstance(doc, (str, unicode)):
+        return '...' if '\n' in doc else doc
+    elif isinstance(doc, list):
+        return [ellipsize(item) for item in doc]
+    elif doc is not None:
+        return dict((key, ellipsize(value)) for key, value in doc.items())