Mercurial > public > mercurial-scm > python-hglib
comparison hglib/util.py @ 49:3d7e0325ba1c
util: introduce a generic error handler that is aware of return codes
author | Idan Kamara <idankk86@gmail.com> |
---|---|
date | Thu, 18 Aug 2011 16:20:22 +0300 |
parents | 19d2c55c3928 |
children | d1f57f162274 |
comparison
equal
deleted
inserted
replaced
48:82d927ac1329 | 49:3d7e0325ba1c |
---|---|
1 import itertools, cStringIO | 1 import itertools, cStringIO, error |
2 | 2 |
3 def grouper(n, iterable): | 3 def grouper(n, iterable): |
4 ''' list(grouper(2, range(4))) -> [(0, 1), (2, 3)] ''' | 4 ''' list(grouper(2, range(4))) -> [(0, 1), (2, 3)] ''' |
5 args = [iter(iterable)] * n | 5 args = [iter(iterable)] * n |
6 return itertools.izip(*args) | 6 return itertools.izip(*args) |
96 for a in args: | 96 for a in args: |
97 if a is not None: | 97 if a is not None: |
98 cmd.append(a) | 98 cmd.append(a) |
99 | 99 |
100 return cmd | 100 return cmd |
101 | |
102 class reterrorhandler(object): | |
103 """ | |
104 This class is meant to be used with rawcommand() error handler argument. | |
105 It remembers the return value the command returned if it's one of allowed | |
106 values, which is only 1 if none are given. Otherwise it raises a CommandError. | |
107 | |
108 >>> e = reterrorhandler('') | |
109 >>> bool(e) | |
110 True | |
111 >>> e(1, 'a', '') | |
112 'a' | |
113 >>> bool(e) | |
114 False | |
115 """ | |
116 def __init__(self, args, allowed=None): | |
117 self.args = args | |
118 self.ret = 0 | |
119 if allowed is None: | |
120 self.allowed = [1] | |
121 else: | |
122 self.allowed = allowed | |
123 | |
124 def __call__(self, ret, out, err): | |
125 self.ret = ret | |
126 if ret not in self.allowed: | |
127 raise error.CommandError(self.args, ret, out, err) | |
128 return out | |
129 | |
130 def __nonzero__(self): | |
131 """ Returns True if the return code was 0, False otherwise """ | |
132 return self.ret == 0 |