From: "W. Trevor King" <wking@tremily.us>
To: notmuch@notmuchmail.org
Subject: [PATCH 1/2] nmbug-status: Clarify errors for illegible configs
Date: Sat, 10 May 2014 12:16:38 -0700 [thread overview]
Message-ID: <1c786e115a812384a8b397583027cf89316253cb.1399749244.git.wking@tremily.us> (raw)
In-Reply-To: <cover.1399749244.git.wking@tremily.us>
In-Reply-To: <cover.1399749244.git.wking@tremily.us>
Carl Worth pointed out that errors like:
$ ./nmbug-status
fatal: Not a git repository: '/home/cworth/.nmbug'
fatal: Not a git repository: '/home/cworth/.nmbug'
Traceback (most recent call last):
File "./nmbug-status", line 254, in <module>
config = read_config(path=args.config)
File "./nmbug-status", line 73, in read_config
return json.load(fp)
File "/usr/lib/python2.7/json/__init__.py", line 290, in load
**kw)
File "/usr/lib/python2.7/json/__init__.py", line 338, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python2.7/json/decoder.py", line 384, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
are not particularly clear. With this commit, we'll get output like:
$ ./nmbug-status
fatal: Not a git repository: '/home/wking/.nmbug'
No local branch 'config' in /home/wking/.nmbug. Checkout a local
config branch or explicitly set --config.
which is much more accessible. I've also added user-friendly messages
for a number of other config-parsing errors.
---
devel/nmbug/nmbug-status | 54 ++++++++++++++++++++++++++++++++++++++++--------
1 file changed, 45 insertions(+), 9 deletions(-)
diff --git a/devel/nmbug/nmbug-status b/devel/nmbug/nmbug-status
index 03621bd..75a6e47 100755
--- a/devel/nmbug/nmbug-status
+++ b/devel/nmbug/nmbug-status
@@ -49,28 +49,60 @@ if not hasattr(collections, 'OrderedDict'): # Python 2.6 or earlier
collections.OrderedDict = _OrderedDict
+class ConfigError (Exception):
+ """Errors with config file usage
+ """
+ pass
+
+
def read_config(path=None, encoding=None):
"Read config from json file"
if not encoding:
encoding = _ENCODING
if path:
- fp = open(path)
+ try:
+ with open(path, 'rb') as f:
+ config_bytes = f.read()
+ except IOError as e:
+ raise ConfigError('Could not read config from {}'.format(path))
else:
nmbhome = os.getenv('NMBGIT', os.path.expanduser('~/.nmbug'))
+ branch = 'config'
+ filename = 'status-config.json'
# read only the first line from the pipe
sha1_bytes = subprocess.Popen(
- ['git', '--git-dir', nmbhome, 'show-ref', '-s', 'config'],
+ ['git', '--git-dir', nmbhome, 'show-ref', '-s', branch],
stdout=subprocess.PIPE).stdout.readline()
sha1 = sha1_bytes.decode(encoding).rstrip()
+ if not sha1:
+ raise ConfigError(
+ ("No local branch '{branch}' in {nmbgit}. "
+ 'Checkout a local {branch} branch or explicitly set --config.'
+ ).format(branch=branch, nmbgit=nmbhome))
- fp_byte_stream = subprocess.Popen(
+ p = 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)
-
- return json.load(fp)
+ '{}:{}'.format(sha1, filename)],
+ stdout=subprocess.PIPE)
+ config_bytes, err = p.communicate()
+ status = p.wait()
+ if status != 0:
+ raise ConfigError(
+ ("Missing status-config.json in branch '{branch}' of"
+ '{nmbgit}. Add the file or explicitly set --config.'
+ ).format(branch=branch, nmbgit=nmbhome))
+
+ config_json = config_bytes.decode(encoding)
+ try:
+ return json.loads(config_json)
+ except ValueError as e:
+ if not path:
+ path = "{} in branch '{}' of {}".format(
+ filename, branch, nmbhome)
+ raise ConfigError(
+ 'Could not parse JSON from the config file {}:\n{}'.format(
+ path, e))
class Thread (list):
@@ -254,7 +286,11 @@ parser.add_argument('--get-query', help='get query for view',
args = parser.parse_args()
-config = read_config(path=args.config)
+try:
+ config = read_config(path=args.config)
+except ConfigError as e:
+ print(e)
+ sys.exit(1)
_PAGES['text'] = Page()
_PAGES['html'] = HtmlPage(
--
1.9.1.353.gc66d89d
next prev parent reply other threads:[~2014-05-10 19:17 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-05-10 19:16 [PATCH 0/2] nmbug-config errors for illegible configs W. Trevor King
2014-05-10 19:16 ` W. Trevor King [this message]
2014-07-13 12:28 ` [PATCH 1/2] nmbug-status: Clarify " David Bremner
2015-03-15 8:32 ` David Bremner
2015-03-15 9:34 ` W. Trevor King
2014-05-10 19:16 ` [PATCH 2/2] nmbug-status: Use 'show-ref --heads' for loading configs W. Trevor King
2014-07-13 12:30 ` David Bremner
2014-07-13 18:13 ` W. Trevor King
2014-07-14 9:51 ` David Bremner
2014-07-14 17:52 ` W. Trevor King
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://notmuchmail.org/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1c786e115a812384a8b397583027cf89316253cb.1399749244.git.wking@tremily.us \
--to=wking@tremily.us \
--cc=notmuch@notmuchmail.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://yhetil.org/notmuch.git/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).