Commits

Roger Pate committed fcf1505

improve encoding handling, add --qa-only option

Comments (0)

Files changed (2)

+import codecs
 import glob
+import locale
 import optparse
 import random
 import re
+import sys
 import textwrap
 from collections import namedtuple
 from os import path
 import yaml
 
 
+if sys.stdout.encoding is None:
+  sys.stdout = codecs.getwriter(locale.getpreferredencoding())(sys.stdout, errors="replace")
+
+
 Quote = namedtuple("Quote", ["text", "attribution", "ref", "tags", "notes"])
 
 
   optparser.add_option("-w", "--width", type="int", default=79)
   optparser.add_option("--max-width", type="int", default=None, help="width = min(width, max_width)")
   optparser.add_option("-v", "--verbose", action="store_true", default=False)
+  optparser.add_option("--qa-only", dest="qa_only", action="store_true", default=False,
+    help="show quote and author/byline only")
   optparser.add_option("--notes", dest="show_notes", action="store_true", default=False)
   optparser.add_option("--debug", action="store_true", default=False)
   optparser.add_option("--grep", nargs=1, default=None)
     help="run N times")
   opts, args = optparser.parse_args(args)
 
+  if opts.qa_only and opts.show_notes:
+    optparser.error("cannot combine --qa-only with --notes")
   if opts.all:
     if opts.n is not None:
       optparser.error("cannot combine --all with -n")
     if filename.startswith("-"):
       return "unexpected option"
     collection = path.splitext(path.basename(filename))[0]
-    for doc in yaml.load_all(open(filename)):
+    for doc in yaml.load_all(codecs.open(filename, encoding="utf-8", errors="replace")):
       q = doc.get("q")
       if "title" in doc:
         a = doc["title"]
       text = textwrap.fill(text, width=opts.width)
 
     attr = q.attribution or "(unknown)"
-    if q.ref:
+    if q.ref and not opts.qa_only:
       attr += "; " + q.ref
 
-    end_text = "-- %s [%s]" % (attr, collection)
+    if opts.qa_only:
+      end_text = "-- " + attr
+    else:
+      end_text = u"-- %s [%s]" % (attr, collection)
 
     sep = " " if opts.oneline else "\n  "
-    print sep.join((text, end_text))
+    print u"%s%s%s" % (text, sep, end_text)
     if opts.show_notes:
       for note in q.notes:
         print "#", note

variety_plugin.py

+#encoding: utf-8
 # http://peterlevi.com/variety/wiki/Writing_plugins
 
 import os
 
 logger = logging.getLogger("variety")
 
+mdash = "—"
+
+def text_replacements(s):
+  s = s.replace(" -- ", mdash)
+  s = s.replace("...", u"…")
+  return s
+
 class FortuneSource(IQuoteSource):
   @classmethod
   def get_info(cls):
 
   def get_random(self):
     try:
-      q = subprocess.check_output(["fortune", "--no-wrap"])
+      lines = subprocess.check_output(["fortune", "--no-wrap", "--qa-only", "-n1000"])
     except subprocess.CalledProcessError:
       return []
 
     # parses my personal fortune's output
-    q, _, author = q.rpartition("\n  -- ")
-    if author:
-      author, _, _ = author.partition(" [")
-    if author == "(unknown)":
-      author = None
+    lines = lines.rstrip().split("\n")
+    results = []
+    while len(lines) >= 2:
+      q, a, lines = lines[0], lines[1][5:], lines[2:]
+      q = text_replacements(q)
+      if a == "(unknown)":
+        a = None
+      else:
+        a = "%s %s" % (mdash, a)
+      results.append({
+        "quote": q,
+        "author": a,
+        })
 
-    return [{
-      "quote": q,
-      "author": author,
-      "sourceName": None,
-      "link": None,
-      }]
+    return results