]> git.notmuchmail.org Git - obsolete/notmuch-to-html/commitdiff
Add support for a --query option (along with a default configuration)
authorCarl Worth <cworth@cworth.org>
Tue, 22 Apr 2014 20:48:23 +0000 (13:48 -0700)
committerCarl Worth <cworth@cworth.org>
Tue, 22 Apr 2014 20:48:23 +0000 (13:48 -0700)
Since I don't like programs that require configuration, here we make
this script self-configuring. When run with no options, it will print
an example usage for a simple query:

notmuch-to-html --query=tag:inbox

Then, if run with that query, it will use a very simple, built-in
configuration to run the query. Finally, it will also print out,
(to stderr, to avoid breaking redirection), a sample configuration
file so that the user can customize the output.

notmuch-to-html

index 5aba2a5fe67e6f13782a37590ff55411e7daf295..f5a6b42827f0498c1822eb7245ea5990482a2738 100755 (executable)
@@ -45,6 +45,22 @@ import xml.sax.saxutils
 _ENCODING = 'UTF-8'
 _PAGES = {}
 
+DEFAULT_CONFIG='''
+{{
+    "meta": {{
+        "title": "Page title",
+        "blurb": "Page description"
+    }},
+
+    "views": [
+       {{
+           "title": "View title",
+           "comment": "View description",
+           "query": [ "{query}" ]
+       }}
+    ]
+}}'''
+
 
 if not hasattr(collections, 'OrderedDict'):  # Python 2.6 or earlier
     class _OrderedDict (dict):
@@ -240,15 +256,29 @@ class HtmlPage (Page):
         return self._slug_regexp.sub('-', string)
 
 parser = argparse.ArgumentParser()
-parser.add_argument('config', help='path to configuration file', metavar='CONFIG_FILE')
 parser.add_argument('--text', help='output plain text format',
                     action='store_true')
+group = parser.add_mutually_exclusive_group()
+group.add_argument('--config', help='path to configuration file',
+                   metavar='PATH')
+group.add_argument('--query', help='path to configuration file',
+                   metavar='PATH')
 parser.add_argument('--list-views', help='list views',
                     action='store_true')
 
 args = parser.parse_args()
 
-config = read_config(path=args.config)
+if (args.config):
+    config = read_config(path=args.config)
+elif (args.query):
+    config = json.loads(DEFAULT_CONFIG.format(query=args.query))
+else:
+    print ('''To use notmuch-to-html, you need to provide a notmuch query. Try:
+
+       notmuch-to-html --query=tag:inbox
+
+Or 'notmuch-to-html --help' for additional options.''')
+    exit (0)
 
 _PAGES['text'] = Page()
 _PAGES['html'] = HtmlPage(
@@ -319,3 +349,10 @@ else:
 
 db = notmuch.Database(mode=notmuch.Database.MODE.READ_ONLY)
 page.write(database=db, views=config['views'])
+
+if (args.query):
+    print ('''To customize the output use 'notmuch-to-html --config=CONFIG_FILE' after
+placing the following content into CONFIG_FILE:
+
+''', file=sys.stderr)
+    print (json.dumps(config, indent=4, separators=(',',':')), file=sys.stderr)