Re: [PATCH] emacs: wash: make word-wrap bound message width
[notmuch-archives.git] / 2c / 73d1273bbd88449af8a2b4f9f4eb103e107a96
1 Return-Path: <wking@tremily.us>\r
2 X-Original-To: notmuch@notmuchmail.org\r
3 Delivered-To: notmuch@notmuchmail.org\r
4 Received: from localhost (localhost [127.0.0.1])\r
5         by olra.theworths.org (Postfix) with ESMTP id 9808D431FBF\r
6         for <notmuch@notmuchmail.org>; Sat, 10 May 2014 12:17:27 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References"\r
9 X-Spam-Flag: NO\r
10 X-Spam-Score: 0\r
11 X-Spam-Level: \r
12 X-Spam-Status: No, score=0 tagged_above=-999 required=5\r
13         tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001]\r
14         autolearn=disabled\r
15 Received: from olra.theworths.org ([127.0.0.1])\r
16         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
17         with ESMTP id BGMCP752kGtw for <notmuch@notmuchmail.org>;\r
18         Sat, 10 May 2014 12:17:21 -0700 (PDT)\r
19 Received: from QMTA11.westchester.pa.mail.comcast.net\r
20         (qmta11.westchester.pa.mail.comcast.net [76.96.59.211])\r
21         by olra.theworths.org (Postfix) with ESMTP id 4666B431FC0\r
22         for <notmuch@notmuchmail.org>; Sat, 10 May 2014 12:17:21 -0700 (PDT)\r
23 Received: from omta12.westchester.pa.mail.comcast.net ([76.96.62.44])\r
24         by QMTA11.westchester.pa.mail.comcast.net with comcast\r
25         id 0K1n1o0030xGWP85BKHH7N; Sat, 10 May 2014 19:17:17 +0000\r
26 Received: from odin.tremily.us ([24.18.63.50])\r
27         by omta12.westchester.pa.mail.comcast.net with comcast\r
28         id 0KHG1o008152l3L3YKHGXJ; Sat, 10 May 2014 19:17:17 +0000\r
29 Received: from mjolnir.tremily.us (unknown [192.168.0.140])\r
30         by odin.tremily.us (Postfix) with ESMTPS id DD217119F4AD;\r
31         Sat, 10 May 2014 12:17:15 -0700 (PDT)\r
32 Received: (nullmailer pid 7964 invoked by uid 1000);\r
33         Sat, 10 May 2014 19:16:43 -0000\r
34 From: "W. Trevor King" <wking@tremily.us>\r
35 To: notmuch@notmuchmail.org\r
36 Subject: [PATCH 1/2] nmbug-status: Clarify errors for illegible configs\r
37 Date: Sat, 10 May 2014 12:16:38 -0700\r
38 Message-Id:\r
39  <1c786e115a812384a8b397583027cf89316253cb.1399749244.git.wking@tremily.us>\r
40 X-Mailer: git-send-email 1.9.1.353.gc66d89d\r
41 In-Reply-To: <cover.1399749244.git.wking@tremily.us>\r
42 References: <cover.1399749244.git.wking@tremily.us>\r
43 In-Reply-To: <cover.1399749244.git.wking@tremily.us>\r
44 References: <cover.1399749244.git.wking@tremily.us>\r
45 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=comcast.net;\r
46         s=q20140121; t=1399749437;\r
47         bh=Y6e1Qnl8mwd0e87DRQAmuwxtsjiw5fDa2eESfCs8cMU=;\r
48         h=Received:Received:Received:Received:From:To:Subject:Date:\r
49         Message-Id;\r
50         b=R6menPQ+vS6Kjj8UwbryJvUT2aBeW7FPNW3e5Ae/RiK19MKujIKrws2W0cElEeA0b\r
51         8rv75kp2vBJXqvYsLwTpn9OD6gdw0sbdWKe+a+Gwpe+YzKLNzk4P9Ir6z7FDfkhdxr\r
52         gkF2pWYEJEWu7eTYex1s8AImiiGkoqIhPnj7Rexgk5Xj168yt+7SedyltpFXZVxxr1\r
53         0/bDs/Bjufhv7YtBcoivEzScwKSV0rP2sqwTsLoA6aXyAjGGHhw/Xt19EVIe5i4sSQ\r
54         LZ1+79OnvFrnfr1cA5dLsIdgLHPfpLdVqe/YdPVpMpL8VxAaPQUnthbZ1m0H78lP2A\r
55         RC4A62MAYOZAQ==\r
56 X-BeenThere: notmuch@notmuchmail.org\r
57 X-Mailman-Version: 2.1.13\r
58 Precedence: list\r
59 List-Id: "Use and development of the notmuch mail system."\r
60         <notmuch.notmuchmail.org>\r
61 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
62         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
63 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
64 List-Post: <mailto:notmuch@notmuchmail.org>\r
65 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
66 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
67         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
68 X-List-Received-Date: Sat, 10 May 2014 19:17:27 -0000\r
69 \r
70 Carl Worth pointed out that errors like:\r
71 \r
72   $ ./nmbug-status\r
73   fatal: Not a git repository: '/home/cworth/.nmbug'\r
74   fatal: Not a git repository: '/home/cworth/.nmbug'\r
75   Traceback (most recent call last):\r
76     File "./nmbug-status", line 254, in <module>\r
77       config = read_config(path=args.config)\r
78     File "./nmbug-status", line 73, in read_config\r
79       return json.load(fp)\r
80     File "/usr/lib/python2.7/json/__init__.py", line 290, in load\r
81       **kw)\r
82     File "/usr/lib/python2.7/json/__init__.py", line 338, in loads\r
83       return _default_decoder.decode(s)\r
84     File "/usr/lib/python2.7/json/decoder.py", line 366, in decode\r
85       obj, end = self.raw_decode(s, idx=_w(s, 0).end())\r
86     File "/usr/lib/python2.7/json/decoder.py", line 384, in raw_decode\r
87       raise ValueError("No JSON object could be decoded")\r
88   ValueError: No JSON object could be decoded\r
89 \r
90 are not particularly clear.  With this commit, we'll get output like:\r
91 \r
92   $ ./nmbug-status\r
93   fatal: Not a git repository: '/home/wking/.nmbug'\r
94   No local branch 'config' in /home/wking/.nmbug.  Checkout a local\r
95   config branch or explicitly set --config.\r
96 \r
97 which is much more accessible.  I've also added user-friendly messages\r
98 for a number of other config-parsing errors.\r
99 ---\r
100  devel/nmbug/nmbug-status | 54 ++++++++++++++++++++++++++++++++++++++++--------\r
101  1 file changed, 45 insertions(+), 9 deletions(-)\r
102 \r
103 diff --git a/devel/nmbug/nmbug-status b/devel/nmbug/nmbug-status\r
104 index 03621bd..75a6e47 100755\r
105 --- a/devel/nmbug/nmbug-status\r
106 +++ b/devel/nmbug/nmbug-status\r
107 @@ -49,28 +49,60 @@ if not hasattr(collections, 'OrderedDict'):  # Python 2.6 or earlier\r
108      collections.OrderedDict = _OrderedDict\r
109  \r
110  \r
111 +class ConfigError (Exception):\r
112 +    """Errors with config file usage\r
113 +    """\r
114 +    pass\r
115 +\r
116 +\r
117  def read_config(path=None, encoding=None):\r
118      "Read config from json file"\r
119      if not encoding:\r
120          encoding = _ENCODING\r
121      if path:\r
122 -        fp = open(path)\r
123 +        try:\r
124 +            with open(path, 'rb') as f:\r
125 +                config_bytes = f.read()\r
126 +        except IOError as e:\r
127 +            raise ConfigError('Could not read config from {}'.format(path))\r
128      else:\r
129          nmbhome = os.getenv('NMBGIT', os.path.expanduser('~/.nmbug'))\r
130 +        branch = 'config'\r
131 +        filename = 'status-config.json'\r
132  \r
133          # read only the first line from the pipe\r
134          sha1_bytes = subprocess.Popen(\r
135 -            ['git', '--git-dir', nmbhome, 'show-ref', '-s', 'config'],\r
136 +            ['git', '--git-dir', nmbhome, 'show-ref', '-s', branch],\r
137              stdout=subprocess.PIPE).stdout.readline()\r
138          sha1 = sha1_bytes.decode(encoding).rstrip()\r
139 +        if not sha1:\r
140 +            raise ConfigError(\r
141 +                ("No local branch '{branch}' in {nmbgit}.  "\r
142 +                 'Checkout a local {branch} branch or explicitly set --config.'\r
143 +                ).format(branch=branch, nmbgit=nmbhome))\r
144  \r
145 -        fp_byte_stream = subprocess.Popen(\r
146 +        p = subprocess.Popen(\r
147              ['git', '--git-dir', nmbhome, 'cat-file', 'blob',\r
148 -             sha1+':status-config.json'],\r
149 -            stdout=subprocess.PIPE).stdout\r
150 -        fp = codecs.getreader(encoding=encoding)(stream=fp_byte_stream)\r
151 -\r
152 -    return json.load(fp)\r
153 +             '{}:{}'.format(sha1, filename)],\r
154 +            stdout=subprocess.PIPE)\r
155 +        config_bytes, err = p.communicate()\r
156 +        status = p.wait()\r
157 +        if status != 0:\r
158 +            raise ConfigError(\r
159 +                ("Missing status-config.json in branch '{branch}' of"\r
160 +                 '{nmbgit}.  Add the file or explicitly set --config.'\r
161 +                ).format(branch=branch, nmbgit=nmbhome))\r
162 +\r
163 +    config_json = config_bytes.decode(encoding)\r
164 +    try:\r
165 +        return json.loads(config_json)\r
166 +    except ValueError as e:\r
167 +        if not path:\r
168 +            path = "{} in branch '{}' of {}".format(\r
169 +                filename, branch, nmbhome)\r
170 +        raise ConfigError(\r
171 +            'Could not parse JSON from the config file {}:\n{}'.format(\r
172 +                path, e))\r
173  \r
174  \r
175  class Thread (list):\r
176 @@ -254,7 +286,11 @@ parser.add_argument('--get-query', help='get query for view',\r
177  \r
178  args = parser.parse_args()\r
179  \r
180 -config = read_config(path=args.config)\r
181 +try:\r
182 +    config = read_config(path=args.config)\r
183 +except ConfigError as e:\r
184 +    print(e)\r
185 +    sys.exit(1)\r
186  \r
187  _PAGES['text'] = Page()\r
188  _PAGES['html'] = HtmlPage(\r
189 -- \r
190 1.9.1.353.gc66d89d\r
191 \r