diff options
author | Olivier Tilloy <olivier@tilloy.net> | 2007-01-20 21:32:00 +0100 |
---|---|---|
committer | Olivier Tilloy <olivier@tilloy.net> | 2007-01-20 21:32:00 +0100 |
commit | a0492164d90727aa26f2a1849fb82697dda3d0ab (patch) | |
tree | 207b76a020803b587efc339ebe50eeaba91c5b3c /src | |
parent | 420bd817181d7f674cb765e0452e78557855358b (diff) | |
download | pyexiv2-a0492164d90727aa26f2a1849fb82697dda3d0ab.tar.gz |
Complete switch to C++/Python exceptions: methods do not return empty strings or tuples anymore, they throw an exception instead.
Diffstat (limited to 'src')
-rw-r--r-- | src/libpyexiv2.cpp | 110 | ||||
-rw-r--r-- | src/libpyexiv2.hpp | 9 | ||||
-rw-r--r-- | src/pyexiv2.py | 6 |
3 files changed, 48 insertions, 77 deletions
diff --git a/src/libpyexiv2.cpp b/src/libpyexiv2.cpp index 1206b2a..f77b6c7 100644 --- a/src/libpyexiv2.cpp +++ b/src/libpyexiv2.cpp @@ -26,7 +26,11 @@ #include "libpyexiv2.hpp" -#define METADATA_NOT_READ_CODE 101 +// Custom error codes for Exiv2 exceptions +#define METADATA_NOT_READ 101 +#define KEY_NOT_FOUND 102 +#define THUMB_ACCESS 103 +#define NO_THUMBNAIL 104 namespace LibPyExiv2 { @@ -66,7 +70,7 @@ namespace LibPyExiv2 _image->writeMetadata(); } else - throw Exiv2::Error(METADATA_NOT_READ_CODE); + throw Exiv2::Error(METADATA_NOT_READ); } boost::python::list Image::getAvailableExifTags() @@ -81,7 +85,7 @@ namespace LibPyExiv2 return list; } else - throw Exiv2::Error(METADATA_NOT_READ_CODE); + throw Exiv2::Error(METADATA_NOT_READ); } boost::python::tuple Image::getExifTag(std::string key) @@ -96,14 +100,10 @@ namespace LibPyExiv2 return boost::python::make_tuple(exifDatum.typeName(), exifDatum.toString()); } else - { - // The key was not found - std::cerr << ">>> Image::getExifTag(): tag '" << key << "' not found" << std::endl; - return boost::python::make_tuple(std::string(""), std::string("")); - } + throw Exiv2::Error(KEY_NOT_FOUND, key); } else - throw Exiv2::Error(METADATA_NOT_READ_CODE); + throw Exiv2::Error(METADATA_NOT_READ); } std::string Image::getExifTagToString(std::string key) @@ -120,14 +120,10 @@ namespace LibPyExiv2 return buffer.str(); } else - { - // The key was not found - std::cerr << ">>> Image::getExifTagToString(): tag '" << key << "' not found" << std::endl; - return std::string(""); - } + throw Exiv2::Error(KEY_NOT_FOUND, key); } else - throw Exiv2::Error(METADATA_NOT_READ_CODE); + throw Exiv2::Error(METADATA_NOT_READ); } boost::python::tuple Image::setExifTag(std::string key, std::string value) @@ -149,14 +145,13 @@ namespace LibPyExiv2 else { // The key was not found - std::cerr << ">>> Image::setExifTag(): tag '" << key << "' not found" << std::endl; returnValue = boost::python::make_tuple(std::string(""), std::string("")); } _exifData[key] = value; return returnValue; } else - throw Exiv2::Error(METADATA_NOT_READ_CODE); + throw Exiv2::Error(METADATA_NOT_READ); } boost::python::tuple Image::deleteExifTag(std::string key) @@ -171,17 +166,13 @@ namespace LibPyExiv2 Exiv2::Exifdatum exifDatum = _exifData[key]; returnValue = boost::python::make_tuple(exifDatum.typeName(), exifDatum.toString()); _exifData.erase(i); + return returnValue; } else - { - // The key was not found - std::cerr << ">>> Image::deleteExifTag(): tag '" << key << "' not found" << std::endl; - returnValue = boost::python::make_tuple(std::string(""), std::string("")); - } - return returnValue; + throw Exiv2::Error(KEY_NOT_FOUND, key); } else - throw Exiv2::Error(METADATA_NOT_READ_CODE); + throw Exiv2::Error(METADATA_NOT_READ); } // returns a list containing the keys of all the IPTC tags available in @@ -198,7 +189,7 @@ namespace LibPyExiv2 return list; } else - throw Exiv2::Error(METADATA_NOT_READ_CODE); + throw Exiv2::Error(METADATA_NOT_READ); } boost::python::tuple Image::getIptcTag(std::string key) @@ -213,14 +204,10 @@ namespace LibPyExiv2 return boost::python::make_tuple(iptcDatum.typeName(), iptcDatum.toString()); } else - { - // The key was not found - std::cerr << ">>> Image::getIptcTag(): tag '" << key << "' not found" << std::endl; - return boost::python::make_tuple(std::string(""), std::string("")); - } + throw Exiv2::Error(KEY_NOT_FOUND, key); } else - throw Exiv2::Error(METADATA_NOT_READ_CODE); + throw Exiv2::Error(METADATA_NOT_READ); } boost::python::tuple Image::setIptcTag(std::string key, std::string value) @@ -240,14 +227,13 @@ namespace LibPyExiv2 else { // The key was not found - std::cerr << ">>> Image::setIptcTag(): tag '" << key << "' not found" << std::endl; returnValue = boost::python::make_tuple(std::string(""), std::string("")); } _iptcData[key] = value; return returnValue; } else - throw Exiv2::Error(METADATA_NOT_READ_CODE); + throw Exiv2::Error(METADATA_NOT_READ); } boost::python::tuple Image::deleteIptcTag(std::string key) @@ -262,17 +248,13 @@ namespace LibPyExiv2 Exiv2::Iptcdatum iptcDatum = _iptcData[key]; returnValue = boost::python::make_tuple(iptcDatum.typeName(), iptcDatum.toString()); _iptcData.erase(i); + return returnValue; } else - { - // The key was not found - std::cerr << ">>> Image::deleteIptcTag(): tag '" << key << "' not found" << std::endl; - returnValue = boost::python::make_tuple(std::string(""), std::string("")); - } - return returnValue; + throw Exiv2::Error(KEY_NOT_FOUND, key); } else - throw Exiv2::Error(METADATA_NOT_READ_CODE); + throw Exiv2::Error(METADATA_NOT_READ); } boost::python::tuple Image::getThumbnailData() @@ -302,24 +284,21 @@ namespace LibPyExiv2 return boost::python::make_tuple(format, data); } else - { - std::cerr << ">>> Image::getThumbnailData(): cannot access thumbnail" << std::endl; - return boost::python::make_tuple(std::string(""), std::string("")); - } + throw Exiv2::Error(THUMB_ACCESS); } - throw Exiv2::Error(METADATA_NOT_READ_CODE); + else + throw Exiv2::Error(METADATA_NOT_READ); } - bool Image::setThumbnailData(std::string data) + void Image::setThumbnailData(std::string data) { if(_dataRead) { const Exiv2::byte* dataBuf = (const Exiv2::byte*) data.c_str(); _exifData.setJpegThumbnail(dataBuf, data.size()); - return true; } else - throw Exiv2::Error(METADATA_NOT_READ_CODE); + throw Exiv2::Error(METADATA_NOT_READ); } void Image::deleteThumbnail() @@ -327,37 +306,29 @@ namespace LibPyExiv2 if(_dataRead) _exifData.eraseThumbnail(); else - throw Exiv2::Error(METADATA_NOT_READ_CODE); + throw Exiv2::Error(METADATA_NOT_READ); } - bool Image::dumpThumbnailToFile(const std::string path) + void Image::dumpThumbnailToFile(const std::string path) { if(_dataRead) { int result = _exifData.writeThumbnail(path); - if (result == 0) - { - return true; - } - else if (result == 8) - { - std::cerr << ">>> Image::dumpThumbnailToFile(): the EXIF data does not contain a thumbnail"; - return false; - } + if (result == 8) + throw Exiv2::Error(NO_THUMBNAIL); } else - throw Exiv2::Error(METADATA_NOT_READ_CODE); + throw Exiv2::Error(METADATA_NOT_READ); } - bool Image::setThumbnailFromJpegFile(const std::string path) + void Image::setThumbnailFromJpegFile(const std::string path) { if(_dataRead) { _exifData.setJpegThumbnail(path); - return true; } else - throw Exiv2::Error(METADATA_NOT_READ_CODE); + throw Exiv2::Error(METADATA_NOT_READ); } void translateExiv2Error(Exiv2::Error const& e) @@ -419,10 +390,19 @@ namespace LibPyExiv2 PyErr_SetString(PyExc_MemoryError, message); break; - // custom defined error codes - case METADATA_NOT_READ_CODE: + // custom error codes + case METADATA_NOT_READ: PyErr_SetString(PyExc_IOError, "Image metadata has not been read yet"); break; + case KEY_NOT_FOUND: + PyErr_SetString(PyExc_ValueError, "Tag not set"); + break; + case THUMB_ACCESS: + PyErr_SetString(PyExc_IOError, "Cannot access image thumbnail"); + break; + case NO_THUMBNAIL: + PyErr_SetString(PyExc_IOError, "The EXIF data does not contain a thumbnail"); + break; default: PyErr_SetString(PyExc_RuntimeError, message); diff --git a/src/libpyexiv2.hpp b/src/libpyexiv2.hpp index dff97ce..53cf532 100644 --- a/src/libpyexiv2.hpp +++ b/src/libpyexiv2.hpp @@ -104,8 +104,7 @@ namespace LibPyExiv2 // Sets the thumbnail of the image. The parameter is the thumbnail raw // jpeg data as a string buffer. - // Returns true if successful, false otherwise. - bool setThumbnailData(std::string data); + void setThumbnailData(std::string data); // Deletes the thumbnail embedded in the image. void deleteThumbnail(); @@ -113,13 +112,11 @@ namespace LibPyExiv2 // Writes the thumbnail to an image file. // A filename extension is appended to the given path according to the // image type of the thumbnail, so it should not include an extension. - // Returns true if successful, false otherwise. - bool dumpThumbnailToFile(const std::string path); + void dumpThumbnailToFile(const std::string path); // Sets the image contained in the jpeg file passed as a parameter as // the thumbnail of the image. - // Returns true if successful, false otherwise. - bool setThumbnailFromJpegFile(const std::string path); + void setThumbnailFromJpegFile(const std::string path); private: std::string _filename; diff --git a/src/pyexiv2.py b/src/pyexiv2.py index a7280e4..9f61a69 100644 --- a/src/pyexiv2.py +++ b/src/pyexiv2.py @@ -334,9 +334,6 @@ class Image(libpyexiv2.Image): # call method getExifTagToString() to obtain directly the value as a # human-readable string. return UndefinedToString(tagValue) - else: - # empty type and value - return def setExifTagValue(self, key, value): """ @@ -392,9 +389,6 @@ class Image(libpyexiv2.Image): return StringToTime(tagValue) elif tagType == 'Undefined': return tagValue - else: - # empty type and value - return def setIptcTagValue(self, key, value): """ |