def avm_obj_from_file(file_path): """ Function to retrieve the XMP packet from a file :param file_path: Path to file :return: A dictionary with AVM data """ xmpfile = libxmp.files.XMPFiles() try: xmpfile.open_file(file_path, open_option=libxmp.files.XMP_OPEN_READ) xmp = xmpfile.get_xmp() xmpfile.close_file() except libxmp.XMPError: return None avm = libavm.AVMMeta(xmp=xmp) return avm
def avm_to_file(file_path, dict={}, replace=False): """ Function to inject AVM into a file. Preserves existing XMP in the file, while replacing fields passed through dict. If a field is an unordered list, then data is appended to existing values :param file_path: Path to file :param dict: A dictionary containing AVM metadata :param xmp: An XMPMeta instance :param replace: Boolean to replace the exisiting XMP in the file. By default it is set to False. :return: Boolean .. todo:: Improve avm_to_file function. Add ability to input an XMP file """ xmpfile = libxmp.files.XMPFiles() try: xmpfile.open_file(file_path, open_forupdate=True) except libxmp.XMPError: return False if replace is True: xmp = libxmp.XMPMeta() else: xmp = xmpfile.get_xmp() if xmp: pass else: xmp = libxmp.XMPMeta() avm = libavm.AVMMeta(xmp=xmp, avm_dict=dict) if xmpfile.can_put_xmp(avm.xmp): xmpfile.put_xmp(avm.xmp) xmpfile.close_file() return True else: return False