| OLD | NEW |
| 1 # Copyright 2015 The LUCI Authors. All rights reserved. | 1 # Copyright 2015 The LUCI Authors. All rights reserved. |
| 2 # Use of this source code is governed by the Apache v2.0 license that can be | 2 # Use of this source code is governed under the Apache License, Version 2.0 |
| 3 # found in the LICENSE file. | 3 # that can be found in the LICENSE file. |
| 4 | 4 |
| 5 """Defines `Context` class, convenient for customizable validation. | 5 """Defines `Context` class, convenient for customizable validation. |
| 6 | 6 |
| 7 Usage: | 7 Usage: |
| 8 | 8 |
| 9 def validate(value, ctx=None): | 9 def validate(value, ctx=None): |
| 10 ctx = ctx or Context.raise_on_error() | 10 ctx = ctx or Context.raise_on_error() |
| 11 if value['foo'] < 0: | 11 if value['foo'] < 0: |
| 12 ctx.error('foo is too low: %d', value['foo']) | 12 ctx.error('foo is too low: %d', value['foo']) |
| 13 if len(value['bar']) == 0: | 13 if len(value['bar']) == 0: |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 text = '%s%s' % (prefix, text) | 145 text = '%s%s' % (prefix, text) |
| 146 raise exc_type(text) | 146 raise exc_type(text) |
| 147 return cls(on_message=on_message) | 147 return cls(on_message=on_message) |
| 148 | 148 |
| 149 @classmethod | 149 @classmethod |
| 150 def logging(cls, logger=None): | 150 def logging(cls, logger=None): |
| 151 """Returns a context that logs all messages.""" | 151 """Returns a context that logs all messages.""" |
| 152 logger = logger or logging.getLogger() | 152 logger = logger or logging.getLogger() |
| 153 on_message = lambda msg: logger.log(msg.severity, msg.text) | 153 on_message = lambda msg: logger.log(msg.severity, msg.text) |
| 154 return cls(on_message=on_message) | 154 return cls(on_message=on_message) |
| OLD | NEW |