]> git.cworth.org Git - obsolete/notmuch-to-html/blobdiff - notmuch-to-html
Revert "Drop the --get-query option"
[obsolete/notmuch-to-html] / notmuch-to-html
index f0146f8ea0c6137d15458609603be10635805fba..712fbb7ee2e85637fd4d6eed2c52817554d6d5c2 100755 (executable)
@@ -1,6 +1,10 @@
 #!/usr/bin/python
 #
+# Generate an HTML page with the result of one or more notmuch
+# searches, (with links to gmane views of each email if available).
+#
 # Copyright (c) 2011-2012 David Bremner <david@tethera.net>
+# Copyright (c) 2014      Carl Worth <cworth@cworth.org
 #
 # dependencies
 #       - python 2.6 for json
@@ -42,6 +46,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):
@@ -62,27 +82,11 @@ if not hasattr(collections, 'OrderedDict'):  # Python 2.6 or earlier
     collections.OrderedDict = _OrderedDict
 
 
-def read_config(path=None, encoding=None):
+def read_config(path, encoding=None):
     "Read config from json file"
     if not encoding:
         encoding = _ENCODING
-    if path:
-        fp = open(path)
-    else:
-        nmbhome = os.getenv('NMBGIT', os.path.expanduser('~/.nmbug'))
-
-        # read only the first line from the pipe
-        sha1_bytes = subprocess.Popen(
-            ['git', '--git-dir', nmbhome, 'show-ref', '-s', 'config'],
-            stdout=subprocess.PIPE).stdout.readline()
-        sha1 = sha1_bytes.decode(encoding).rstrip()
-
-        fp_byte_stream = subprocess.Popen(
-            ['git', '--git-dir', nmbhome, 'cat-file', 'blob',
-             sha1+':status-config.json'],
-            stdout=subprocess.PIPE).stdout
-        fp = codecs.getreader(encoding=encoding)(stream=fp_byte_stream)
-
+    fp = open(path)
     return json.load(fp)
 
 
@@ -201,11 +205,11 @@ class HtmlPage (Page):
             stream.write(view['comment'])
             stream.write('\n')
         for line in [
-                'The view is generated from the following query:',
+                '<p>This view is generated from the following query:',
                 '</p>',
                 '<p>',
                 '  <code>',
-                view['query-string'],
+                'notmuch search ' + view['query-string'],
                 '  </code>',
                 '</p>',
                 ]:
@@ -255,8 +259,11 @@ class HtmlPage (Page):
 parser = argparse.ArgumentParser()
 parser.add_argument('--text', help='output plain text format',
                     action='store_true')
-parser.add_argument('--config', help='load config from given file',
-                    metavar='PATH')
+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')
 parser.add_argument('--get-query', help='get query for view',
@@ -264,7 +271,17 @@ parser.add_argument('--get-query', help='get query for view',
 
 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(
@@ -309,18 +326,19 @@ _PAGES['html'] = HtmlPage(
 </head>
 <body>
 <h2>{title}</h2>
-<p>
-Generated: {date}<br />
 {blurb}
 </p>
 <h3>Views</h3>
-'''.format(date=datetime.datetime.utcnow().date(),
-           title=config['meta']['title'],
+'''.format(title=config['meta']['title'],
            blurb=config['meta']['blurb'],
            encoding=_ENCODING,
            inter_message_padding='0.25em',
            border_radius='0.5em'),
-    footer='</body>\n</html>\n',
+    footer='''<hr>
+<p>Generated: {date} courtesy of <a href="http://notmuchmail.org">notmuch</a> and <a href="http://git.cworth.org/git/notmuch-to-html">notmuch-to-html</a>.
+</body>
+</html>
+'''.format(date=datetime.datetime.utcnow().date())
     )
 
 if args.list_views:
@@ -343,3 +361,12 @@ 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 (note that you can add
+additional views with their own queries):
+
+
+''', file=sys.stderr)
+    print (json.dumps(config, indent=4, separators=(',',':')), file=sys.stderr)