| 1 |
// python-gphoto2 - Python interface to libgphoto2 |
| 2 |
// http://github.com/jim-easterbrook/python-gphoto2 |
| 3 |
// Copyright (C) 2014-23 Jim Easterbrook [email protected] |
| 4 |
// |
| 5 |
// This file is part of python-gphoto2. |
| 6 |
// |
| 7 |
// python-gphoto2 is free software: you can redistribute it and/or modify |
| 8 |
// it under the terms of the GNU Lesser General Public License as published |
| 9 |
// by the Free Software Foundation, either version 3 of the License, or |
| 10 |
// (at your option) any later version. |
| 11 |
// |
| 12 |
// python-gphoto2 is distributed in the hope that it will be useful, |
| 13 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
// GNU Lesser General Public License for more details. |
| 16 |
// |
| 17 |
// You should have received a copy of the GNU Lesser General Public License |
| 18 |
// along with python-gphoto2. If not, see <https://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
%module(package="gphoto2") result |
| 21 |
|
| 22 |
%include "common/preamble.i" |
| 23 |
|
| 24 |
%include "gphoto2/gphoto2-port-result.h" |
| 25 |
%include "gphoto2/gphoto2-result.h" |
| 26 |
|
| 27 |
%pythoncode %{ |
| 28 |
import logging |
| 29 |
|
| 30 |
from gphoto2 import GPhoto2Error |
| 31 |
|
| 32 |
# user adjustable check_result lookup table |
| 33 |
error_severity = { |
| 34 |
GP_ERROR_CANCEL : logging.INFO, |
| 35 |
GP_ERROR_DIRECTORY_EXISTS : logging.WARNING, |
| 36 |
} |
| 37 |
error_exception = logging.ERROR |
| 38 |
|
| 39 |
_return_logger = logging.getLogger('gphoto2.returnvalue') |
| 40 |
|
| 41 |
def check_result(result): |
| 42 |
"""Pops gphoto2 'error' value from 'result' list and checks it. |
| 43 |
|
| 44 |
If there is no error the remaining result is returned. For other |
| 45 |
errors a severity level is taken from the error_severity dict, or |
| 46 |
set to logging.CRITICAL if the error is not in error_severity. |
| 47 |
|
| 48 |
If the severity >= error_exception an exception is raised. |
| 49 |
Otherwise a message is logged at the appropriate severity level. |
| 50 |
""" |
| 51 |
|
| 52 |
if not isinstance(result, (tuple, list)): |
| 53 |
error = result |
| 54 |
elif len(result) == 2: |
| 55 |
error, result = result |
| 56 |
else: |
| 57 |
error = result[0] |
| 58 |
result = result[1:] |
| 59 |
if error >= GP_OK: |
| 60 |
return result |
| 61 |
severity = logging.CRITICAL |
| 62 |
if error in error_severity: |
| 63 |
severity = error_severity[error] |
| 64 |
if severity >= error_exception: |
| 65 |
raise GPhoto2Error(error) |
| 66 |
_return_logger.log(severity, '[%d] %s', error, gp_result_as_string(error)) |
| 67 |
return result |
| 68 |
%} |