16.3.3.2. Printing Header Information with sprintf
In addition to using the Python dictionary syntax, you can use the sprintf method on a header to format data using a syntax exactly the same as the query format tags supported by the rpm command.
Cross Reference
The basic syntax is as follows:
h.sprintf("%{tag_name}")
You can also use special formatting additions to the tag name. For example:
print "Header signature: ", h.sprintf("%{DSAHEADER:pgpsig}")
print "%-20s: %s" % ('Installed on', h.sprintf("%{INSTALLTID:date}") )
You can combine this information into functions that print out header entries with specific formatting. For example:
def nvr(h):
return h.sprintf("%{NAME}-%{VERSION}-%{RELEASE}")
Note that you only really need to use sprintf when you need the format modifiers, such as date on %{INSTALLTID:date}. In most other cases, Python’s string-handling functions will work better.