def serialize_to_str(self, padding=0, **kwargs): """ Serializes an XMPMeta object into a string (8-bit, UTF-8 encoded) as RDF and format. :param padding: The number of bytes of padding, useful for modifying embedded XMP in place. :param omit_packet_wrapper: Do not include an XML packet wrapper. :param read_only_packet: Create a read-only XML packet wapper. :param use_compact_format: Use a highly compact RDF syntax and layout. :param include_thumbnail_pad: Include typical space for a JPEG thumbnail in the padding if no xmp:Thumbnails property is present. :param exact_packet_length: The padding parameter provides the overall packet length. :param write_alias_comments: Include XML comments for aliases. :param omit_all_formatting: Omit all formatting whitespace. :return: XMPMeta object serialized into a string as RDF. :rtype: `str` 8-bit string in UTF-8 encoding (ready to e.g. be writtin to a file). """ res_str = None # Ensure padding is an int. padding = int(padding) # Define options bitmask options = options_mask(XMP_SERIAL_OPTIONS, **kwargs) # Serialize xmpstring = _XMPString() res = _exempi.xmp_serialize(self.xmpptr, xmpstring.ptr, options, padding) _check_for_error() # Get string if res: res_str = xmpstring.__str__() del xmpstring return res_str
def parse_from_str(self, xmp_packet_str, xmpmeta_wrap=False, input_encoding=None): """ Parses RDF from a string into a XMP object. The input for parsing may be any valid Unicode encoding. ISO Latin-1 is also recognized, but its use is strongly discouraged. Note RDF string must contain an outermost <x:xmpmeta> object. :param xmp_packet_str: String to parse. :param xmpmeta_wrap: Optional - If True, the string will be wrapped in an <x:xmpmeta> element. :param input_encoding: Optional - If `xmp_packet_str` is a 8-bit string, it will by default be assumed to be UTF-8 encoded. :return: true if :class:`libxmp.core.XMPMeta` object can be written in file. :rtype: bool """ xmp_packet_str = _encode_as_utf8(xmp_packet_str, input_encoding) if xmpmeta_wrap: xmp_packet_str = "<x:xmpmeta xmlns:x='adobe:ns:meta/'>%s</x:xmpmeta>" % xmp_packet_str l = len(xmp_packet_str) res = _exempi.xmp_parse(self.xmpptr, xmp_packet_str, l) _check_for_error() return res
def open_file(self, file_path, **kwargs): """ Open a given file and read XMP from file. File must be closed again with :func:`close_file` :param file_path: Path to file to open. :raises XMPError: in case of errors. .. todo:: Change signature into using kwargs to set option flag """ open_flags = options_mask(XMP_OPEN_OPTIONS, **kwargs) if kwargs else XMP_OPEN_NOOPTION if self._file_path != None: raise XMPError('A file is already open - close it first.') # Ensure file path is UTF-8 encoded (expected by Exempi) file_path = _encode_as_utf8(file_path) if not os.path.exists(file_path): raise XMPError('File does not exists.') if _exempi.xmp_files_open(self.xmpfileptr, file_path, open_flags): self._file_path = file_path else: _check_for_error()
def open_file(self, file_path, **kwargs): """ Open a given file and read XMP from file. File must be closed again with :func:`close_file` :param file_path: Path to file to open. :raises XMPError: in case of errors. .. todo:: Change signature into using kwargs to set option flag """ open_flags = options_mask(XMP_OPEN_OPTIONS, ** kwargs) if kwargs else XMP_OPEN_NOOPTION if self._file_path != None: raise XMPError('A file is already open - close it first.') # Ensure file path is UTF-8 encoded (expected by Exempi) file_path = _encode_as_utf8(file_path) if not os.path.exists(file_path): raise XMPError('File does not exists.') if _exempi.xmp_files_open(self.xmpfileptr, file_path, open_flags): self._file_path = file_path else: _check_for_error()
def serialize_to_str( self, padding = 0, **kwargs ): """ Serializes an XMPMeta object into a string (8-bit, UTF-8 encoded) as RDF and format. :param padding: The number of bytes of padding, useful for modifying embedded XMP in place. :param omit_packet_wrapper: Do not include an XML packet wrapper. :param read_only_packet: Create a read-only XML packet wapper. :param use_compact_format: Use a highly compact RDF syntax and layout. :param include_thumbnail_pad: Include typical space for a JPEG thumbnail in the padding if no xmp:Thumbnails property is present. :param exact_packet_length: The padding parameter provides the overall packet length. :param write_alias_comments: Include XML comments for aliases. :param omit_all_formatting: Omit all formatting whitespace. :return: XMPMeta object serialized into a string as RDF. :rtype: `str` 8-bit string in UTF-8 encoding (ready to e.g. be writtin to a file). """ res_str = None # Ensure padding is an int. padding = int(padding) # Define options bitmask options = options_mask( XMP_SERIAL_OPTIONS, **kwargs ) # Serialize xmpstring = _XMPString() res = _exempi.xmp_serialize( self.xmpptr, xmpstring.ptr, options, padding ) _check_for_error() # Get string if res: res_str = xmpstring.__str__() del xmpstring return res_str
def __init__(self, xmp_obj, schema_ns=None, prop_name=None, **kwargs): self.options = options_mask(consts.XMP_ITERATOR_OPTIONS, ** kwargs) if kwargs else 0 self.xmpiteratorptr = _exempi.xmp_iterator_new(xmp_obj.xmpptr, schema_ns, prop_name, self.options) _check_for_error() self.schema = schema_ns self.prop_name = prop_name
def __del__(self): """ Ensures memory is deallocated when destroying object. """ if self.xmpptr != None: if not _exempi.xmp_free(self.xmpptr): _check_for_error() if self.iterator is not None: del self.iterator
def put_xmp(self, xmp_obj): """ Write XMPMeta object to file. See also :func:`can_put_xmp`. :param xmp_obj: An :class:`libxmp.core.XMPMeta` object """ xmpptr = xmp_obj.xmpptr if xmpptr != None: if not _exempi.xmp_files_put_xmp(self.xmpfileptr, xmpptr): _check_for_error()
def skip(**kwargs ): """ skip() skips some portion of the remaining iterations. :param **kwargs: Optional keyword parameters from XMP_SKIP_OPTIONS to control the iteration :returns: None :rtype: NoneType """ options = options_mask(consts.XMP_SKIP_OPTIONS, **kwargs) if kwargs else 0 _exempi.xmp_iterator_skip( self.xmpiteratorptr, options ); _check_for_error() return None
def skip(**kwargs): """ skip() skips some portion of the remaining iterations. :param **kwargs: Optional keyword parameters from XMP_SKIP_OPTIONS to control the iteration :returns: None :rtype: NoneType """ options = options_mask(consts.XMP_SKIP_OPTIONS, ** kwargs) if kwargs else 0 _exempi.xmp_iterator_skip(self.xmpiteratorptr, options) _check_for_error() return None
def serialize_and_format(self, padding=0, newlinechr='\n', tabchr='\t', indent=0, **kwargs): """ Serializes an XMPMeta object into a string as RDF. Note, normally it is sufficient to use either `serialize_to_str` or `serialize_to_unicode` unless you need high degree of control over the serialization. The specified parameters must be logically consistent, an exception is raised if not. You cannot specify both `omit_packet_wrapper` along with `read_only_packet`, `include_thumbnail_pad`, or `exact_packet_length`. :param padding: The number of bytes of padding, useful for modifying embedded XMP in place. :param newlinechr: The new line character to use. :param tabchr: The indentation character to use. :param indent: The initial indentation level. :param omit_packet_wrapper: Do not include an XML packet wrapper. :param read_only_packet: Create a read-only XML packet wapper. :param use_compact_format: Use a highly compact RDF syntax and layout. :param include_thumbnail_pad: Include typical space for a JPEG thumbnail in the padding if no xmp:Thumbnails property is present. :param exact_packet_length: The padding parameter provides the overall packet length. :param write_alias_comments: Include XML comments for aliases. :param omit_all_formatting: Omit all formatting whitespace. :return: XMPMeta object serialized into a string as RDF. :rtype: `unicode` string. """ res_str = None # Ensure padding is an int. padding = int(padding) indent = int(indent) tabchr = str(tabchr) newlinechr = str(newlinechr) # Define options bitmask options = options_mask(XMP_SERIAL_OPTIONS, **kwargs) # Serialize xmpstring = _XMPString() res = _exempi.xmp_serialize_and_format(self.xmpptr, xmpstring.ptr, options, padding, newlinechr, tabchr, indent) _check_for_error() # Get string if res: res_str = xmpstring.__str__() del xmpstring return res_str
def get_xmp(self): """ Get XMP from file. :return: A new :class:`libxmp.core.XMPMeta` instance. :raises XMPError: in case of errors. """ xmpptr = _exempi.xmp_files_get_new_xmp(self.xmpfileptr) _check_for_error() if xmpptr: return XMPMeta(_xmp_internal_ref=xmpptr) else: return None
def __init__(self, **kwargs): """ :param xmp_str Optional. :param xmp_internal_ref Optional - used for internal purposes. """ if '_xmp_internal_ref' in kwargs: self.xmpptr = kwargs['_xmp_internal_ref'] else: self.xmpptr = _exempi.xmp_new_empty() _check_for_error() if 'xmp_str' in kwargs: self.parse_from_str(kwargs['xmp_str']) self.iterator = None
def __init__( self, **kwargs ): """ :param xmp_str Optional. :param xmp_internal_ref Optional - used for internal purposes. """ if '_xmp_internal_ref' in kwargs: self.xmpptr = kwargs['_xmp_internal_ref'] else: self.xmpptr = _exempi.xmp_new_empty() _check_for_error() if 'xmp_str' in kwargs: self.parse_from_str( kwargs['xmp_str'] ) self.iterator = None
def close_file(self, close_flags=XMP_CLOSE_NOOPTION): """ Close file after use. XMP will not be written to file until this method has been called. :param close_flags: One of the close flags :raises XMPError: in case of errors. .. todo:: Change signature into using kwargs to set option flag """ if not _exempi.xmp_files_close(self.xmpfileptr, close_flags): _check_for_error() else: self._file_path = None
def serialize_and_format( self, padding=0, newlinechr='\n', tabchr = '\t', indent=0, **kwargs ): """ Serializes an XMPMeta object into a string as RDF. Note, normally it is sufficient to use either `serialize_to_str` or `serialize_to_unicode` unless you need high degree of control over the serialization. The specified parameters must be logically consistent, an exception is raised if not. You cannot specify both `omit_packet_wrapper` along with `read_only_packet`, `include_thumbnail_pad`, or `exact_packet_length`. :param padding: The number of bytes of padding, useful for modifying embedded XMP in place. :param newlinechr: The new line character to use. :param tabchr: The indentation character to use. :param indent: The initial indentation level. :param omit_packet_wrapper: Do not include an XML packet wrapper. :param read_only_packet: Create a read-only XML packet wapper. :param use_compact_format: Use a highly compact RDF syntax and layout. :param include_thumbnail_pad: Include typical space for a JPEG thumbnail in the padding if no xmp:Thumbnails property is present. :param exact_packet_length: The padding parameter provides the overall packet length. :param write_alias_comments: Include XML comments for aliases. :param omit_all_formatting: Omit all formatting whitespace. :return: XMPMeta object serialized into a string as RDF. :rtype: `unicode` string. """ res_str = None # Ensure padding is an int. padding = int(padding) indent = int(indent) tabchr = str(tabchr) newlinechr = str(newlinechr) # Define options bitmask options = options_mask( XMP_SERIAL_OPTIONS, **kwargs ) # Serialize xmpstring = _XMPString() res = _exempi.xmp_serialize_and_format( self.xmpptr, xmpstring.ptr, options, padding, newlinechr, tabchr, indent ) _check_for_error() # Get string if res: res_str = xmpstring.__str__() del xmpstring return res_str
def parse_from_str( self, xmp_packet_str, xmpmeta_wrap = False, input_encoding = None ): """ Parses RDF from a string into a XMP object. The input for parsing may be any valid Unicode encoding. ISO Latin-1 is also recognized, but its use is strongly discouraged. Note RDF string must contain an outermost <x:xmpmeta> object. :param xmp_packet_str: String to parse. :param xmpmeta_wrap: Optional - If True, the string will be wrapped in an <x:xmpmeta> element. :param input_encoding: Optional - If `xmp_packet_str` is a 8-bit string, it will by default be assumed to be UTF-8 encoded. :return: true if :class:`libxmp.core.XMPMeta` object can be written in file. :rtype: bool """ xmp_packet_str = _encode_as_utf8( xmp_packet_str, input_encoding ) if xmpmeta_wrap: xmp_packet_str = "<x:xmpmeta xmlns:x='adobe:ns:meta/'>%s</x:xmpmeta>" % xmp_packet_str l = len(xmp_packet_str) res = _exempi.xmp_parse(self.xmpptr, xmp_packet_str, l ) _check_for_error() return res
def __del__(self): _check_for_error()
def __init__( self, xmp_obj, schema_ns=None, prop_name=None, **kwargs ): self.options = options_mask(consts.XMP_ITERATOR_OPTIONS, **kwargs) if kwargs else 0 self.xmpiteratorptr = _exempi.xmp_iterator_new( xmp_obj.xmpptr, schema_ns, prop_name, self.options) _check_for_error() self.schema = schema_ns self.prop_name = prop_name