/usr/lib/python2.7/dist-packages/libxmp/exempi.py is in python-libxmp 2.0.1~git20140309.5437b0a-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 | # -*- coding: utf-8 -*-
#
# Copyright (c) 2008-2009, European Space Agency & European Southern
# Observatory (ESA/ESO)
# Copyright (c) 2008-2009, CRS4 - Centre for Advanced Studies, Research and
# Development in Sardinia
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# * Neither the name of the European Space Agency, European Southern
# Observatory, CRS4 nor the names of its contributors may be used to
# endorse or promote products derived from this software without specific
# prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY ESA/ESO AND CRS4 ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
# EVENT SHALL ESA/ESO BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER # IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
"""
Wrapper functions for individual exempi library routines.
"""
import ctypes, ctypes.util
import datetime
import os
import platform
import pytz
from . import XMPError, ExempiLoadError
from .consts import XMP_OPEN_READ, XMP_OPEN_NOOPTION
def _load_exempi():
"""
Loads exempi library.
"""
path = 'libexempi.so.3'
if path is None:
if platform.system().startswith('Darwin'):
if os.path.exists('/opt/local/lib/libexempi.dylib'):
# MacPorts starndard location.
path = '/opt/local/lib/libexempi.dylib'
if path is None:
raise ExempiLoadError('Exempi library not found.')
if os.name != "nt":
EXEMPI = ctypes.CDLL(path)
else:
EXEMPI = ctypes.WinDLL(path)
return EXEMPI
EXEMPI = _load_exempi()
# Error codes defined by libexempi. See "xmperrors.h"
ERROR_MESSAGE = { 0: "unknown error",
-1: "TBD",
-2: "unavailable",
-3: "bad object",
-4: "bad parameter",
-5: "bad value",
-6: "assert failure",
-7: "enforce failure",
-8: "unimplemented",
-9: "internal failure",
-10: "deprecated",
-11: "external failure",
-12: "user abort",
-13: "std exception",
-14: "unknown exception",
-15: "no memory",
-101: "bad schema",
-102: "bad XPath",
-103: "bad options",
-104: "bad index",
-105: "bad iter position",
-106: "bad parse",
-107: "bad serialize",
-108: "bad file format",
-109: "no file handler",
-110: "too large for JPEG",
-201: "bad XML",
-202: "bad RDF",
-203: "bad XMP",
-204: "empty iterator",
-205: "bad unicode",
-206: "bad TIFF",
-207: "bad JPEG",
-208: "bad PSD",
-209: "bad PSIR",
-210: "bad IPTC",
-211: "bad MPEG" }
class XmpDateTime(ctypes.Structure):
"""Corresponds to XmpDateTime type in exempi headers.
"""
_fields_ = [
("year", ctypes.c_int32),
("month", ctypes.c_int32),
("day", ctypes.c_int32),
("hour", ctypes.c_int32),
("minute", ctypes.c_int32),
("second", ctypes.c_int32),
("tzsign", ctypes.c_int32),
("tzhour", ctypes.c_int32),
("tzminute", ctypes.c_int32),
("nanosecond", ctypes.c_int32)]
def append_array_item(xmp, schema, name, array_options, value, option_bits):
"""Append a value to the XMP property array in the XMP packet.
Wrapper for xmp_append_property_item library routine.
Parameters
----------
xmp : pointer
The XMP packet.
schema : str
The schema of the property.
name : str
The name of the property.
array_options : unsigned int
The option bits of the parent array.
value : str
The value of the item to be appended.
option_bits : unsigned int
Mask of options.
Raises
------
XMPError : if the corresponding library routine fails
"""
EXEMPI.xmp_append_array_item.restype = check_error
if value is None:
EXEMPI.xmp_append_array_item.argtypes = [ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_char_p,
ctypes.c_uint32,
ctypes.c_uint32,
ctypes.c_uint32]
EXEMPI.xmp_append_array_item(xmp,
schema.encode('utf-8'),
name.encode('utf-8'),
array_options,
ctypes.c_uint32(0),
option_bits)
else:
EXEMPI.xmp_append_array_item.argtypes = [ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_char_p,
ctypes.c_uint32,
ctypes.c_char_p,
ctypes.c_uint32]
EXEMPI.xmp_append_array_item(xmp,
schema.encode('utf-8'),
name.encode('utf-8'),
array_options,
value.encode('utf-8'),
option_bits)
def copy(xmp):
"""Create a new XMP packet from an existing instance.
Wrapper for xmp_copy library routine.
Parameters
----------
xmp : XmpPtr instance.
The XMP packet object.
Returns
-------
newxmp : XmpPtr instance.
A copy of the XMP packet object.
"""
EXEMPI.xmp_copy.restype = ctypes.c_void_p
EXEMPI.xmp_copy.argtypes = [ctypes.c_void_p]
newxmp = EXEMPI.xmp_copy(xmp)
return newxmp
def files_can_put_xmp(xfptr, xmp):
"""Wrapper for xmp_files_can_put_xmp library routine.
Parameters
----------
xfptr : file pointer
File pointer
xmp : pointer
The XMP packet.
Returns
-------
tf : bool
True if the XMP packet can be written to the file.
"""
EXEMPI.xmp_files_can_put_xmp.restype = ctypes.c_bool
EXEMPI.xmp_files_can_put_xmp.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
value = EXEMPI.xmp_files_can_put_xmp(xfptr, xmp)
return value == 1
def files_check_file_format(filename):
"""Check the file format of a file.
Wrapper for xmp_check_file_format library routine.
Parameters
----------
filename : str
Path to file.
"""
if not os.path.exists(filename):
raise IOError("{0} does not exist.".format(filename))
EXEMPI.xmp_files_check_file_format.restype = ctypes.c_int32
EXEMPI.xmp_files_check_file_format.argtypes = [ctypes.c_char_p]
return EXEMPI.xmp_files_check_file_format(filename.encode('utf-8'))
def delete_localized_text(xmp, schema, name, generic_lang, specific_lang):
"""Remove a property.
Wrapper for xmp_delete_localized_text library routine.
Parameters
----------
xmp : pointer
The XMP packet.
schema : str
The schema of the property.
name : str
The name of the property.
generic_lang : str
The generic language you may want. Can be None.
specific_lang : str
The specific language.
Raises
------
XMPError : if the corresponding library routine fails
.. versionadded:: 2.0.0
"""
EXEMPI.xmp_delete_localized_text.restype = check_error
EXEMPI.xmp_delete_localized_text.argtypes = [ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_char_p,
ctypes.c_char_p,
ctypes.c_char_p]
EXEMPI.xmp_delete_localized_text(xmp,
schema.encode('utf-8'),
name.encode('utf-8'),
generic_lang.encode('utf-8'),
specific_lang.encode('utf-8'))
def delete_property(xmp, schema, name):
"""Delete a property from the XMP packet.
Wrapper for xmp_delete_property library routine.
Parameters
----------
xmp : pointer
The XMP packet.
schema : str
The schema of the property.
name : str
The name of the property.
Raises
------
XMPError : if the corresponding library routine fails
"""
EXEMPI.xmp_delete_property.restype = check_error
EXEMPI.xmp_delete_property.argtypes = [ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_char_p]
EXEMPI.xmp_delete_property(xmp, schema.encode('utf-8'), name.encode('utf-8'))
def files_close(xfptr, options):
"""Close an XMP file, flush the changes.
Wrapper for xmp_files_close library routine.
Parameters
----------
xfptr : file pointer
File pointer
options : int
the options mask for closing the file
Raises
------
XMPError : if the corresponding library routine fails
"""
EXEMPI.xmp_files_close.restype = check_error
EXEMPI.xmp_files_close.argtypes = [ctypes.c_void_p, ctypes.c_int32]
EXEMPI.xmp_files_close(xfptr, options)
def files_free(xfptr):
"""Free an XMP file pointer.
Wrapper for xmp_files_free library routine.
Parameters
----------
xfptr : file pointer
File pointer
Raises
------
XMPError : if the corresponding library routine fails
"""
EXEMPI.xmp_files_free.restype = check_error
EXEMPI.xmp_files_free.argtypes = [ctypes.c_void_p]
EXEMPI.xmp_files_free(xfptr)
def files_get_file_info(xfptr):
"""Get the file info from the open file.
Wrapper for xmp_files_get_file_info library routine.
Parameters
----------
xfptr : file pointer
File pointer
Returns
-------
file_path : str
the file path object to store the path in.
options : OpenFileOptions enum
the options for open.
file_format : XmpFileType
the detected file format.
handler_flags : int
the format options like from files_get_format_info.
Raises
------
XMPError : if the corresponding library routine fails
"""
EXEMPI.xmp_files_get_file_info.restype = check_error
EXEMPI.xmp_files_get_file_info.argtypes = [ctypes.c_void_p,
ctypes.c_void_p,
ctypes.POINTER(ctypes.c_int32),
ctypes.POINTER(ctypes.c_int32),
ctypes.POINTER(ctypes.c_int32)]
_file_path = _string_new()
options = ctypes.c_int32(0)
file_format = ctypes.c_int32(0)
handler_flags = ctypes.c_int32(0)
EXEMPI.xmp_files_get_file_info(xfptr,
_file_path,
ctypes.byref(options),
ctypes.byref(file_format),
ctypes.byref(handler_flags))
file_path = string_cstr(_file_path)
_string_free(_file_path)
return file_path, options.value, file_format.value, handler_flags.value
def files_get_new_xmp(xfptr):
"""Wrapper for xmp_files_get_new_xmp library routine.
Parameters
----------
xfptr : file pointer
File pointer
Returns
-------
xmp_ptr : ctypes pointer
XMP pointer
"""
EXEMPI.xmp_files_get_new_xmp.restype = ctypes.c_void_p
EXEMPI.xmp_files_get_new_xmp.argtypes = [ctypes.c_void_p]
xmp_ptr = EXEMPI.xmp_files_get_new_xmp(xfptr)
return xmp_ptr
def files_get_xmp(xfptr):
"""Wrapper for xmp_files_get_xmp library routine.
Parameters
----------
xfptr : file pointer
File pointer
Returns
-------
xmp_ptr : ctypes pointer
XMP pointer
Raises
------
XMPError : if the corresponding library routine fails
"""
EXEMPI.xmp_files_get_xmp.restype = check_error
EXEMPI.xmp_files_get_xmp.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
xmp = new_empty()
EXEMPI.xmp_files_get_xmp(xfptr, xmp)
return xmp
def files_open(xfptr, filename, options):
"""Wrapper for xmp_files_open library routine.
Parameters
----------
xfptr : object
XMP file object (with no associated file)
filename : str
File to be opened.
options : XmpFileOpenOptions
How the file is to be opened.
Raises
------
XMPError : if the corresponding library routine fails
"""
if (((not os.path.exists(filename)) and
((options == XMP_OPEN_NOOPTION) or (options & XMP_OPEN_READ)))):
raise IOError("{0} does not exist.".format(filename))
EXEMPI.xmp_files_open.restype = check_error
EXEMPI.xmp_files_open.argtypes = [ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_int32]
EXEMPI.xmp_files_open(xfptr, filename.encode('utf-8'), options)
def files_new():
"""Wrapper for xmp_files_new library routine.
Returns
-------
xfptr : ctypes pointer
File pointer.
"""
EXEMPI.xmp_files_new.restype = ctypes.c_void_p
xfptr = EXEMPI.xmp_files_new()
return xfptr
def files_open_new(filename, options):
"""Wrapper for xmp_files_open_new library routine.
Parameters
----------
filename : str
File to be opened.
options : XmpFileOpenOptions
How the file is to be opened.
Returns
-------
xfptr : ctypes pointer
File pointer.
"""
if not os.path.exists(filename) and options & XMP_OPEN_READ:
raise IOError("{0} does not exist.".format(filename))
EXEMPI.xmp_files_open_new.restype = ctypes.c_void_p
EXEMPI.xmp_files_open_new.argtypes = [ctypes.c_void_p, ctypes.c_int32]
xfptr = EXEMPI.xmp_files_open_new(filename.encode('utf-8'), options)
return xfptr
def files_put_xmp(xfptr, xmp):
"""Wrapper for xmp_files_put_xmp library routine.
Parameters
----------
xfptr : ctypes pointer
File pointer.
xmp : pointer
The XMP packet.
Raises
------
XMPError : if the corresponding library routine fails
"""
EXEMPI.xmp_files_put_xmp.restype = check_error
EXEMPI.xmp_files_put_xmp.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
EXEMPI.xmp_files_put_xmp(xfptr, xmp)
def get_array_item(xmp, schema, name, index):
"""Get an item from an array property.
Wrapper for xmp_get_array_item library routine.
Parameters
----------
xmp : pointer
The XMP packet.
schema : str
The schema of the property.
name : str
The name of the property.
index : int
1-based index of the property.
Returns
-------
item : str
The value of the property.
property_bits : unsigned int
The property bits
Raises
------
XMPError : if the corresponding library routine fails
"""
EXEMPI.xmp_get_array_item.restype = check_error
EXEMPI.xmp_get_array_item.argtypes = [ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_char_p,
ctypes.c_int32,
ctypes.c_void_p,
ctypes.POINTER(ctypes.c_uint32)]
_item = _string_new()
property_bits = ctypes.c_uint32(0)
EXEMPI.xmp_get_array_item(xmp,
schema.encode('utf-8'),
name.encode('utf-8'),
ctypes.c_int32(index),
_item,
ctypes.byref(property_bits))
item = string_cstr(_item)
_string_free(_item)
return item, property_bits.value
def get_localized_text(xmp, schema, name, generic_lang, specific_lang):
"""Get a localised text from a localisable property.
Wrapper for xmp_get_localized_text library routine.
Parameters
----------
xmp : pointer
The XMP packet.
schema : str
The schema of the property.
name : str
The name of the property.
generic_lang : str
The generic language you may want. Can be None.
specific_lang : str
The specific language.
Returns
-------
item : str
The localized text.
prop_bits : unsigned int
option bit mask
actual_lang : str
The actual language of the item.
Raises
------
XMPError : if the corresponding library routine fails
"""
EXEMPI.xmp_get_localized_text.restype = check_error
EXEMPI.xmp_get_localized_text.argtypes = [ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_char_p,
ctypes.c_char_p,
ctypes.c_char_p,
ctypes.c_void_p,
ctypes.c_void_p,
ctypes.POINTER(ctypes.c_uint32)]
if generic_lang is not None:
generic_lang = generic_lang.encode('utf-8')
_item = _string_new()
prop_bits = ctypes.c_uint32(0)
_actual_lang = _string_new()
EXEMPI.xmp_get_localized_text(xmp,
schema.encode('utf-8'),
name.encode('utf-8'),
generic_lang,
specific_lang.encode('utf-8'),
_actual_lang, _item,
ctypes.byref(prop_bits))
item = string_cstr(_item)
_string_free(_item)
actual_lang = string_cstr(_actual_lang)
_string_free(_actual_lang)
return item, prop_bits.value, actual_lang
def get_property(xmp, schema, name):
"""Wrapper for xmp_get_property library routine.
Parameters
----------
xmp : pointer
The XMP packet.
schema : str
The schema of the property.
name : str
The name of the property.
Returns
-------
prop : str
Property value as a string.
prop_bits : unsigned int
option bit mask
Raises
------
XMPError : if the corresponding library routine fails
"""
# Use a function callback instead of returning a boolean value.
EXEMPI.xmp_get_property.restype = check_error
EXEMPI.xmp_get_property.argtypes = [ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_char_p,
ctypes.c_void_p,
ctypes.POINTER(ctypes.c_uint32)]
_value = _string_new()
prop_bits = ctypes.c_uint32(0)
EXEMPI.xmp_get_property(xmp,
ctypes.c_char_p(schema.encode('utf-8')),
ctypes.c_char_p(name.encode('utf-8')),
_value, ctypes.byref(prop_bits))
value = string_cstr(_value)
_string_free(_value)
return value, prop_bits.value
def get_property_bool(xmp, schema, name):
"""Wrapper for xmp_get_property_bool library routine.
Parameters
----------
xmp : pointer
The XMP packet.
schema : str
The schema of the property.
name : str
The name of the property.
Returns
-------
value : bool
The value requested.
prop_bits : unsigned int
option bit mask
Raises
------
XMPError : if the corresponding library routine fails
"""
# Use a function callback instead of returning a boolean value.
EXEMPI.xmp_get_property_bool.restype = check_error
EXEMPI.xmp_get_property_bool.argtypes = [ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_char_p,
ctypes.POINTER(ctypes.c_bool),
ctypes.POINTER(ctypes.c_uint32)]
bool_value = ctypes.c_bool(0)
prop_bits = ctypes.c_uint32(0)
EXEMPI.xmp_get_property_bool(xmp,
schema.encode('utf-8'),
name.encode('utf-8'),
ctypes.byref(bool_value),
ctypes.byref(prop_bits))
return bool_value.value, prop_bits.value
def get_property_date(xmp, schema, name):
"""Wrapper for xmp_get_property_date library routine.
Parameters
----------
xmp : pointer
The XMP packet.
schema : str
The schema of the property.
name : str
The name of the property.
Returns
-------
date : datetime.datetime
Date structure.
prop_bits : unsigned int
option bit mask
Raises
------
XMPError : if the corresponding library routine fails
"""
# Use a function callback instead of returning a boolean value.
EXEMPI.xmp_get_property_date.restype = check_error
EXEMPI.xmp_get_property_date.argtypes = [ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_char_p,
ctypes.POINTER(XmpDateTime),
ctypes.POINTER(ctypes.c_uint32)]
xmp_date_time = XmpDateTime()
prop_bits = ctypes.c_uint32(0)
EXEMPI.xmp_get_property_date(xmp,
schema.encode('utf-8'),
name.encode('utf-8'),
ctypes.byref(xmp_date_time),
ctypes.byref(prop_bits))
date1 = datetime.datetime(xmp_date_time.year,
xmp_date_time.month,
xmp_date_time.day,
xmp_date_time.hour,
xmp_date_time.minute,
xmp_date_time.second)
utc = pytz.timezone('utc')
utc_date = utc.localize(date1)
delta = datetime.timedelta(hours=xmp_date_time.tzhour,
minutes=xmp_date_time.tzminute,
microseconds=xmp_date_time.nanosecond * 1000)
if xmp_date_time.tzsign < 0:
the_date = utc_date - delta
else:
the_date = utc_date + delta
return the_date, prop_bits.value
def get_property_int32(xmp, schema, name):
"""Wrapper for xmp_get_property_int32 library routine.
Parameters
----------
xmp : pointer
The XMP packet.
schema : str
The schema of the property.
name : str
The name of the property.
Returns
-------
value : int
The int32 value requested.
prop_bits : unsigned int
option bit mask
Raises
------
XMPError : if the corresponding library routine fails
"""
# Use a function callback instead of returning a boolean value.
EXEMPI.xmp_get_property_int32.restype = check_error
EXEMPI.xmp_get_property_int32.argtypes = [ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_char_p,
ctypes.POINTER(ctypes.c_int32),
ctypes.POINTER(ctypes.c_uint32)]
ivalue = ctypes.c_int32(0)
prop_bits = ctypes.c_uint32(0)
EXEMPI.xmp_get_property_int32(xmp,
schema.encode('utf-8'),
name.encode('utf-8'),
ctypes.byref(ivalue),
ctypes.byref(prop_bits))
return ivalue.value, prop_bits.value
def get_property_int64(xmp, schema, name):
"""Wrapper for xmp_get_property_int64 library routine.
Parameters
----------
xmp : pointer
The XMP packet.
schema : str
The schema of the property.
name : str
The name of the property.
Returns
-------
value : int
The int64 value requested.
prop_bits : unsigned int
option bit mask
Raises
------
XMPError : if the corresponding library routine fails
"""
# Use a function callback instead of returning a boolean value.
EXEMPI.xmp_get_property_int64.restype = check_error
EXEMPI.xmp_get_property_int64.argtypes = [ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_char_p,
ctypes.POINTER(ctypes.c_int64),
ctypes.POINTER(ctypes.c_uint32)]
ivalue = ctypes.c_int64(0)
prop_bits = ctypes.c_uint32(0)
EXEMPI.xmp_get_property_int64(xmp,
schema.encode('utf-8'),
name.encode('utf-8'),
ctypes.byref(ivalue),
ctypes.byref(prop_bits))
return ivalue.value, prop_bits.value
def get_property_float(xmp, schema, name):
"""Wrapper for xmp_get_property_float library routine.
Parameters
----------
xmp : pointer
The XMP packet.
schema : str
The schema of the property.
name : str
The name of the property.
Returns
-------
value : float
The value requested.
prop_bits : unsigned int
option bit mask
"""
# EXEMPI has trouble parsing some floats such as "+1.0", so let python
# do it instead.
value, prop_bits = get_property(xmp, schema, name)
value = float(value)
return value, prop_bits
def free(xmp):
"""Wrapper for xmp_free library routine."""
EXEMPI.xmp_free.argtypes = [ctypes.c_void_p]
EXEMPI.xmp_free.restype = None
EXEMPI.xmp_free(xmp)
def get_error():
"""Wrapper for xmp_get_error library routine."""
EXEMPI.xmp_get_error.restype = ctypes.c_int32
code = EXEMPI.xmp_get_error()
return code
def has_property(xmp, schema, name):
"""Wrapper for xmp_has_property library routine.
Parameters
----------
xmp : pointer
The XMP packet.
schema : str
The schema of the property.
name : str
The name of the property.
"""
EXEMPI.xmp_has_property.restype = ctypes.c_bool
EXEMPI.xmp_has_property.argtypes = [ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_char_p]
ret = EXEMPI.xmp_has_property(xmp,
schema.encode('utf-8'),
name.encode('utf-8'))
if ret == 1:
return True
else:
return False
def init():
"""Wrapper for xmp_init library routine.
Raises
------
XMPError : if the corresponding library routine fails
"""
EXEMPI.xmp_init.restype = check_error
EXEMPI.xmp_init()
def iterator_free(iterator):
"""Wrapper for xmp_iterator_free library routine.
Parameters
----------
iterator : XmpIteratorPtr
iterator for use with iterator_next
Raises
------
XMPError : if the corresponding library routine fails
"""
EXEMPI.xmp_iterator_free.argtypes = [ctypes.c_void_p]
EXEMPI.xmp_iterator_free.restype = check_error
EXEMPI.xmp_iterator_free(iterator)
def iterator_next(iterator):
"""Iterate to the next value.
Wrapper for xmp_iterator_next library routine.
Parameters
----------
iterator : XmpIteratorPtr
iterator for use with iterator_next
Returns
-------
schema, propname, value : str
The schema, name, and value of the property as strings.
options : unsigned integer
The options for the property.
Raises
------
StopIteration : when the library determines that the iteration is finished.
"""
EXEMPI.xmp_iterator_next.argtypes = [ctypes.c_void_p,
ctypes.c_void_p,
ctypes.c_void_p,
ctypes.c_void_p,
ctypes.POINTER(ctypes.c_uint32)]
EXEMPI.xmp_iterator_next.restype = ctypes.c_bool
_schema = _string_new()
_propname = _string_new()
_propvalue = _string_new()
options = ctypes.c_uint32(0)
success = EXEMPI.xmp_iterator_next(iterator, _schema, _propname, _propvalue,
ctypes.byref(options))
if not success:
_string_free(_schema)
_string_free(_propname)
_string_free(_propvalue)
raise StopIteration()
schema = string_cstr(_schema)
_string_free(_schema)
propname = string_cstr(_propname)
_string_free(_propname)
propvalue = string_cstr(_propvalue)
_string_free(_propvalue)
return schema, propname, propvalue, options
def iterator_new(xmp, schema, propname, options):
"""Wrapper for xmp_iterator_new library routine.
Parameters
----------
xmp : pointer
The XMP packet.
schema, propname : str
The schema and name of the property.
options : unsigned integer
Option bit mask for controlling iteration.
Returns
-------
iterator : XmpIteratorPtr
iterator for use with iterator_next
"""
EXEMPI.xmp_iterator_new.argtypes = [ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_char_p,
ctypes.c_int32]
EXEMPI.xmp_iterator_new.restype = ctypes.c_void_p
if schema is not None:
schema = schema.encode('utf-8')
if propname is not None:
propname = propname.encode('utf-8')
iterator = EXEMPI.xmp_iterator_new(xmp, schema, propname, options)
return iterator
def iterator_skip(iterator, options):
"""Wrapper for xmp_iterator_skip library routine.
Parameters
----------
iterator : XmpIteratorPtr
iterator for use with iterator_next
options : int
Mask of options.
Raises
------
XMPError : if the corresponding library routine fails
"""
EXEMPI.xmp_iterator_skip.argtypes = [ctypes.c_void_p,
ctypes.c_int32]
EXEMPI.xmp_iterator_skip.restype = ctypes.c_bool
success = EXEMPI.xmp_iterator_skip(iterator, options)
check_error(success)
def namespace_prefix(namespace):
"""Returns a prefix associated with a namespace.
Wrapper for xmp_namespace_prefix library routine.
Parameters
----------
namespace : str
The namespace associated if registered. May pass None.
TODO: check this
Returns
-------
prefix : str
The prefix associated with the namespace.
Raises
------
XMPError : if the corresponding library routine fails
"""
EXEMPI.xmp_namespace_prefix.restype = check_error
EXEMPI.xmp_namespace_prefix.argtypes = [ctypes.c_char_p, ctypes.c_void_p]
_prefix = _string_new()
EXEMPI.xmp_namespace_prefix(namespace.encode('utf-8'), _prefix)
prefix = string_cstr(_prefix)
_string_free(_prefix)
return prefix
def new_empty():
"""Create a new XMP packet.
Wrapper for xmp_new_empty library routine.
Returns
-------
item : xmp string pointer
Opaque pointer to an XMP string. It is your responsibility to properly
dispose of the string.
"""
EXEMPI.xmp_new_empty.restype = ctypes.c_void_p
xmp = EXEMPI.xmp_new_empty()
return xmp
def parse(xmp, strbuffer):
"""Parse the XML and load it.
Wrapper for xmp_parse library routine.
Parameters
----------
xmp : pointer
The XMP packet.
strbuffer : str
A string of XML to parse.
Raises
------
XMPError : if the corresponding library routine fails
"""
# Use a function callback instead of returning a boolean value.
EXEMPI.xmp_parse.restype = check_error
EXEMPI.xmp_parse.argtypes = [ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_size_t]
strbuffer = strbuffer.encode('utf-8')
EXEMPI.xmp_parse(xmp, strbuffer, len(strbuffer))
def prefix_namespace_uri(prefix):
"""Retrieve namespace associated with a prefix.
Wrapper for xmp_namespace_prefix library routine.
Parameters
----------
prefix : xmp pointer
The prefix to check.
Returns
-------
namespace : str
The namespace associated if registered.
Raises
------
XMPError : if the corresponding library routine fails
"""
EXEMPI.xmp_prefix_namespace_uri.restype = check_error
EXEMPI.xmp_prefix_namespace_uri.argtypes = [ctypes.c_char_p,
ctypes.c_void_p]
_namespace = _string_new()
EXEMPI.xmp_prefix_namespace_uri(prefix.encode('utf-8'), _namespace)
namespace = string_cstr(_namespace)
_string_free(_namespace)
return namespace
def register_namespace(namespace_uri, prefix):
"""Register a new namespace.
Wrapper for xmp_register_namespace library routine.
Parameters
----------
namespace_uri : str
the namespace URI to register
prefix : str
the suggested prefix
Returns
-------
registered_prefix : str
The really registered prefix.
Raises
------
XMPError : if the corresponding library routine fails
"""
EXEMPI.xmp_register_namespace.restype = check_error
EXEMPI.xmp_register_namespace.argtypes = [ctypes.c_char_p,
ctypes.c_char_p,
ctypes.c_void_p]
_registered_prefix = _string_new()
EXEMPI.xmp_register_namespace(namespace_uri.encode('utf-8'),
prefix.encode('utf-8'),
_registered_prefix)
registered_prefix = string_cstr(_registered_prefix)
_string_free(_registered_prefix)
return registered_prefix
def serialize(xmp, options, padding):
"""Serialize the XMP Packet.
Wrapper for xmp_serialize library routine.
Parameters
----------
xmp : pointer
The XMP packet.
options : unsigned integer
Options on how to write the XMP.
padding : int
Number of bytes of padding, useful for modifying embedded XMP in place.
Returns
-------
item : str
The formatted XMP.
Raises
------
XMPError : if the corresponding library routine fails
"""
EXEMPI.xmp_serialize.restype = check_error
EXEMPI.xmp_serialize.argtypes = [ctypes.c_void_p,
ctypes.c_void_p,
ctypes.c_uint32,
ctypes.c_uint32]
_item = _string_new()
EXEMPI.xmp_serialize(xmp, _item, options, padding)
item = string_cstr(_item)
_string_free(_item)
return item
def serialize_and_format(xmp, options, padding, newline, tab, indent):
"""Serialize the XMP Packet with formatting.
Wrapper for xmp_serialize_and_format library routine.
Parameters
----------
xmp : pointer
The XMP packet.
options : unsigned integer
Options on how to write the XMP.
padding : int
Number of bytes of padding, useful for modifying embedded XMP in place.
newline, tab : str
Characters to specify the newline and tabbing.
indent : int
The initial indentation level.
Returns
-------
item : str
The formatted XMP.
Raises
------
XMPError : if the corresponding library routine fails
"""
EXEMPI.xmp_serialize_and_format.restype = check_error
EXEMPI.xmp_serialize_and_format.argtypes = [ctypes.c_void_p,
ctypes.c_void_p,
ctypes.c_uint32,
ctypes.c_uint32,
ctypes.c_char_p,
ctypes.c_char_p,
ctypes.c_int32]
_item = _string_new()
EXEMPI.xmp_serialize_and_format(xmp, _item, options, padding,
newline.encode('utf-8'), tab.encode('utf-8'), indent)
item = string_cstr(_item)
_string_free(_item)
return item
def set_array_item(xmp, schema, name, index, value, option_bits):
"""Wrapper for xmp_set_array_item library routine.
Parameters
----------
xmp : pointer
The XMP packet.
schema : str
The schema of the property.
name : str
The name of the property.
index : int
1-based index of the property.
value : str
The value of the property.
Raises
------
XMPError : if the corresponding library routine fails
"""
EXEMPI.xmp_set_array_item.restype = check_error
if value is None:
EXEMPI.xmp_set_array_item.argtypes = [ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_char_p,
ctypes.c_int32,
ctypes.c_uint32,
ctypes.c_uint32]
EXEMPI.xmp_set_array_item(xmp,
schema.encode('utf-8'),
name.encode('utf-8'),
ctypes.c_int32(index),
ctypes.c_uint32(0),
option_bits)
else:
EXEMPI.xmp_set_array_item.argtypes = [ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_char_p,
ctypes.c_int32,
ctypes.c_char_p,
ctypes.c_uint32]
EXEMPI.xmp_set_array_item(xmp,
schema.encode('utf-8'),
name.encode('utf-8'),
ctypes.c_int32(index),
value.encode('utf-8'),
option_bits)
def set_localized_text(xmp, schema, name, generic_lang, specific_lang, value,
mask=0):
"""Set a localized text from a localizable property.
Wrapper for xmp_set_localized_txt library routine.
Parameters
----------
xmp : pointer
The XMP packet.
schema : str
The schema of the property.
name : str
The name of the property.
specific_lang : str
The specific language.
generic_lang : str
The generic language you may want as a fall back.
value : xmp string pointer
Opaque pointer to an XMP string.
mask : unsigned int
option bit mask
Raises
------
XMPError : if the corresponding library routine fails
"""
EXEMPI.xmp_set_localized_text.restype = check_error
EXEMPI.xmp_set_localized_text.argtypes = [ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_char_p,
ctypes.c_char_p,
ctypes.c_char_p,
ctypes.c_char_p,
ctypes.c_uint32]
if generic_lang is not None:
generic_lang = generic_lang.encode('utf-8')
mask = ctypes.c_uint32(mask)
EXEMPI.xmp_set_localized_text(xmp,
schema.encode('utf-8'),
name.encode('utf-8'),
generic_lang,
specific_lang.encode('utf-8'),
value.encode('utf-8'),
mask)
def set_property(xmp, schema, name, value, option_bits=0):
"""Set an XMP property in the XMP packet.
Wrapper for xmp_set_property library routine.
Parameters
----------
xmp : pointer
The XMP packet.
schema : str
The schema of the property.
name : str
The name of the property.
value : str
The name of the property.
option_bits : unsigned int
Mask of options.
Raises
------
XMPError : if the corresponding library routine fails
"""
# Use a function callback instead of returning a boolean value.
EXEMPI.xmp_set_property.restype = check_error
EXEMPI.xmp_set_property.argtypes = [ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_char_p,
ctypes.c_char_p,
ctypes.c_uint32]
EXEMPI.xmp_set_property(xmp,
schema.encode('utf-8'),
name.encode('utf-8'),
value.encode('utf-8'),
ctypes.c_uint32(option_bits))
def set_property_bool(xmp, schema, name, value, option_bits=0):
"""Set a bool XMP property in the XMP packet.
Wrapper for xmp_set_property_bool library routine.
Parameters
----------
xmp : pointer
The XMP packet.
schema : str
The schema of the property.
name : str
The name of the property.
value : bool
The boolean value
option_bits : unsigned int
Mask of options.
Raises
------
XMPError : if the corresponding library routine fails
"""
# Use a function callback instead of returning a boolean value.
EXEMPI.xmp_set_property_bool.restype = check_error
EXEMPI.xmp_set_property_bool.argtypes = [ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_char_p,
ctypes.c_bool,
ctypes.c_uint32]
bvalue = ctypes.c_bool(value)
EXEMPI.xmp_set_property_bool(xmp,
schema.encode('utf-8'),
name.encode('utf-8'),
bvalue,
ctypes.c_uint32(option_bits))
def set_property_date(xmp, schema, name, the_date, option_bits=0):
"""Set a date XMP property in the XMP packet.
Wrapper for xmp_set_property_date library routine.
Parameters
----------
xmp : pointer
The XMP packet.
schema : str
The schema of the property.
name : str
The name of the property.
the_date : datetime.datetime
The date and time
option_bits : unsigned int
Mask of options.
Raises
------
XMPError : if the corresponding library routine fails
"""
# Use a function callback instead of returning a boolean value.
EXEMPI.xmp_set_property_date.restype = check_error
EXEMPI.xmp_set_property_date.argtypes = [ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_char_p,
ctypes.POINTER(XmpDateTime),
ctypes.c_uint32]
if the_date.tzinfo is not None:
the_date = the_date.astimezone(pytz.utc)
xmp_date = XmpDateTime()
xmp_date.year = the_date.year
xmp_date.month = the_date.month
xmp_date.day = the_date.day
xmp_date.hour = the_date.hour
xmp_date.minute = the_date.minute
xmp_date.second = the_date.second
xmp_date.tzsign = 0
xmp_date.tzhour = 0
xmp_date.tzminute = 0
xmp_date.nanosecond = 0
EXEMPI.xmp_set_property_date(xmp,
ctypes.c_char_p(schema.encode('utf-8')),
ctypes.c_char_p(name.encode('utf-8')),
ctypes.byref(xmp_date),
ctypes.c_uint32(option_bits))
def set_property_int32(xmp, schema, name, value, option_bits=0):
"""Set an int32 XMP property in the XMP packet.
Wrapper for xmp_set_property_int32 library routine.
Parameters
----------
xmp : pointer
The XMP packet.
schema : str
The schema of the property.
name : str
The name of the property.
value : int64
The int32 value
option_bits : unsigned int
Mask of options.
Raises
------
XMPError : if the corresponding library routine fails
"""
# Use a function callback instead of returning a boolean value.
EXEMPI.xmp_set_property_int32.restype = check_error
EXEMPI.xmp_set_property_int32.argtypes = [ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_char_p,
ctypes.c_int32,
ctypes.c_uint32]
ivalue = ctypes.c_int32(value)
EXEMPI.xmp_set_property_int32(xmp,
schema.encode('utf-8'),
name.encode('utf-8'),
ivalue,
ctypes.c_uint32(option_bits))
def set_property_int64(xmp, schema, name, value, option_bits=0):
"""Set an int64 XMP property in the XMP packet.
Wrapper for xmp_set_property_int32 library routine.
Parameters
----------
xmp : pointer
The XMP packet.
schema : str
The schema of the property.
name : str
The name of the property.
value : int
The int64 value
option_bits : unsigned int
Mask of options.
Raises
------
XMPError : if the corresponding library routine fails
"""
# Use a function callback instead of returning a boolean value.
EXEMPI.xmp_set_property_int64.restype = check_error
EXEMPI.xmp_set_property_int64.argtypes = [ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_char_p,
ctypes.c_int64,
ctypes.c_uint32]
ivalue = ctypes.c_int64(value)
EXEMPI.xmp_set_property_int64(xmp,
schema.encode('utf-8'),
name.encode('utf-8'),
ivalue,
ctypes.c_uint32(option_bits))
def set_property_float(xmp, schema, name, value, option_bits=0):
"""Set a float XMP property in the XMP packet.
Wrapper for xmp_set_property_float library routine.
Parameters
----------
xmp : pointer
The XMP packet.
schema : str
The schema of the property.
name : str
The name of the property.
value : float
The float value
option_bits : unsigned int
Mask of options.
Raises
------
XMPError : if the corresponding library routine fails
"""
# Use a function callback instead of returning a boolean value.
EXEMPI.xmp_set_property_float.restype = check_error
EXEMPI.xmp_set_property_float.argtypes = [ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_char_p,
ctypes.c_double,
ctypes.c_uint32]
dvalue = ctypes.c_double(value)
EXEMPI.xmp_set_property_float(xmp,
schema.encode('utf-8'),
name.encode('utf-8'),
dvalue,
ctypes.c_uint32(option_bits))
def string_cstr(xmpstr):
"""Wrapper for xmp_string_cstr library routine.
Parameters
----------
xmpstr : Opaque pointer (XmpStringPtr)
Exempi string type.
Returns
-------
pystr : UTF-8 str
Python string
"""
EXEMPI.xmp_string_cstr.restype = ctypes.c_char_p
EXEMPI.xmp_string_cstr.argtypes = [ctypes.c_void_p]
cstr = EXEMPI.xmp_string_cstr(xmpstr)
cstr = cstr.decode('utf-8')
return cstr
def _string_free(xmp_string):
"""Free an XmpStringPtr.
Wrapper for xmp_string_free library routine. You should not need to call
this function.
Parameters
----------
xmp_string : exempi XmpStringPtr
The resource to free.
"""
EXEMPI.xmp_string_free.argtypes = [ctypes.c_void_p]
EXEMPI.xmp_string_free(xmp_string)
def _string_new():
"""Wrapper for xmp_string_new library routine. You should not need to call
this function.
Returns
-------
ptr : pointer
Opaque pointer to a string.
"""
# This is an opaque type that we should not peek into!
EXEMPI.xmp_string_new.restype = ctypes.c_void_p
return EXEMPI.xmp_string_new()
def terminate():
"""Wrapper for xmp_terminate library routine"""
EXEMPI.xmp_terminate.restype = ctypes.c_void_p
EXEMPI.xmp_terminate()
def check_error(success):
"""Set a generic function as the restype attribute of all exempi
functions that return a boolean value. This way we do not have to check
for error status in each wrapping function and an exception will always be
appropriately raised.
Parameters
----------
success : bool
Return value from library function indicating success or failure.
"""
# Unfortunately the success parameter does not seem to always be reliable
# so we supplement it by explicitly checking the error code.
ecode = EXEMPI.xmp_get_error()
if not success or ecode != 0:
error_msg = ERROR_MESSAGE[ecode]
msg = 'Exempi function failure ("{0}").'.format(error_msg)
raise XMPError(msg)
|