Skip to content

Commit

Permalink
Documentation fix
Browse files Browse the repository at this point in the history
* Fixes bug in documentation and converts examples to doctests to ensure
  examples will actually run (closes #34).
  • Loading branch information
lnielsen committed Mar 9, 2014
1 parent 51b34a7 commit 44a3572
Showing 1 changed file with 73 additions and 70 deletions.
143 changes: 73 additions & 70 deletions docs/using.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,90 +21,93 @@ are e.g. the wiki page or the XMP Specification Part 1 available from:

Method 1: Read XMP
------------------
One of the most basic uses of the library is::
One of the most basic uses of the library is:

from libxmp import *
>>> from libxmp.utils import file_to_dict
>>> xmp = file_to_dict( "test/samples/BlueSquare.xmp" )

xmp = file_to_dict( "/path/to/some/file_with_xmp.ext" )

This will read the XMP embedded in the file and return it as a
dictionary. The keys in the dictionary are XMP namespaces so to e.g. get
all Dublin Core properties use::
all Dublin Core properties use:


>>> from libxmp import consts
>>> dc = xmp[consts.XMP_NS_DC]

or to be explicit:

>>> dc = xmp["http://purl.org/dc/elements/1.1/"]

dc = xmp[consts.XMP_NS_DC]
# or to be explicit
dc = xmp["http://purl.org/dc/elements/1.1/"]
This will give you a list of all Dublin Core properties, where each
element in the list is a tuple such as::

(
u'dc:format',
u'application/vnd.adobe.photoshop',
{
'IS_SCHEMA': False,
'IS_ALIAS': False,
'HAS_TYPE': False,
'ARRAY_IS_ALT': False,
'IS_INTERNAL': False,
'IS_DERIVED': False,
'HAS_ALIASES': False,
'HAS_LANG': False,
'VALUE_IS_STRUCT': False,
'HAS_QUALIFIERS': False,
'ARRAY_IS_ALTTEXT': False,
'VALUE_IS_URI': False,
'VALUE_IS_ARRAY': False,
'ARRAY_IS_ORDERED': False,
'IS_QUALIFIER': False,
'IS_STABLE': False
}
)
The first element is the property name, the second element is the value
and the third element is options associated with the element (describing
e.g the type of the property).
element in the list is a tuple. The first element is the property name,
the second element is the value and the third element is options associated
with the element (describing e.g the type of the property):

First tuple element:

>>> print(dc[0][0])
dc:format

Second tuple element:

>>> print(dc[0][1])
application/vnd.adobe.photoshop

Third tuple element is a dict with options:

>>> dc[0][2]['IS_SCHEMA']
False

Method 2: Read/Write XMP
------------------------
Example 1 focused on just extracting the XMP from a file an determine the
value of a property. If you however want to extract the XMP from a file,
update it, *and* write it back again you need to do like the following::


from libxmp import *

# Read file
xmpfile = XMPFiles( file_path="/path/to/some/file", open_forupdate=True )
# Get XMP from file.
xmp = xmpfile.get_xmp()
# Print the property dc:format
print xmp.get_property( libxmp.consts.XMP_NS_DC, 'format' )
# Change the XMP property
xmp.set_property( libxmp.consts.XMP_NS_DC, 'format','application/vnd.adobe.illustrator' )
# Check if XMP document can be written to file and write it.
if xmpfile.can_put_xmp(xmp):
xmpfile.put_xmp(xmp)
# XMP document is not written to the file, before the file
# is closed.
xmpfile.close_file()
update it, *and* write it back again you need to do like the following

Read file:

>>> from libxmp import XMPFiles, consts
>>> xmpfile = XMPFiles( file_path="test/samples/BlueSquare.jpg", open_forupdate=True )

Get XMP from file:

>>> xmp = xmpfile.get_xmp()

Print the property ``dc:format``:

>>> print(xmp.get_property(consts.XMP_NS_DC, 'format' ))
image/jpeg

Change the XMP property:

>>> xmp.set_property(consts.XMP_NS_DC, u'format', u'application/vnd.adobe.illustrator' )
>>> print(xmp.get_property(consts.XMP_NS_DC, 'format' ))
application/vnd.adobe.illustrator

Check if XMP document can be written to file and write it:

>>> xmpfile.can_put_xmp(xmp)
True

XMP document is not written to the file, before the file
is closed:

>>> xmpfile.close_file()

Further Examples
-------------
Append an array item to the XMP packet.::

from libxmp import *
# Read file
xmpfile = XMPFiles( file_path="/path/to/some/file" )
# Get XMP from file
xmp = xmpfile.get_xmp()
# Create a new array item and append a value
xmp.append_array_item(files.XMP_NS_DC, 'creator', 'Your Name Here', {'prop_array_is_ordered': True, 'prop_value_is_array': True})
Read file:

>>> from libxmp import XMPFiles, consts
>>> xmpfile = XMPFiles( file_path="test/samples/BlueSquare.xmp" )

Get XMP from file:

>>> xmp = xmpfile.get_xmp()

Create a new array item and append a value:

>>> xmp.append_array_item(consts.XMP_NS_DC, 'creator', 'Your Name Here', {'prop_array_is_ordered': True, 'prop_value_is_array': True})

0 comments on commit 44a3572

Please sign in to comment.