Formatting the import time in the view and paths

Added the %format{} template function to output the time to any format supported by time.strftime()
This commit is contained in:
Lucas Duailibe 2013-05-04 15:40:46 -03:00
parent 0a631bcda2
commit 5c31d3ac15
2 changed files with 21 additions and 1 deletions

View file

@ -37,6 +37,10 @@ import beets
MAX_FILENAME_LENGTH = 200
# This is the default format when printing the import time
# of an object. This needs to be a format accepted by time.strftime()
ITIME_FORMAT = '%Y-%m-%d %H:%M:%S'
# Fields in the "items" database table; all the metadata available for
# items in the library. These are used directly in SQL; they are
# vulnerable to injection if accessible to the user.
@ -398,6 +402,9 @@ class Item(object):
if not sanitize:
mapping['path'] = displayable_path(self.path)
# Convert the import time to human readable
mapping['itime'] = time.strftime(ITIME_FORMAT, time.localtime(getattr(self, 'itime')))
# Use the album artist if the track artist is not set and
# vice-versa.
if not mapping['artist']:
@ -1741,6 +1748,9 @@ class Album(BaseAlbum):
mapping['artpath'] = displayable_path(mapping['artpath'])
mapping['path'] = displayable_path(self.item_dir())
# Convert the import time to human readable format
mapping['itime'] = time.strftime(ITIME_FORMAT, time.localtime(mapping['itime']))
# Get template functions.
funcs = DefaultTemplateFunctions().functions()
funcs.update(plugins.template_funcs())
@ -1829,6 +1839,12 @@ class DefaultTemplateFunctions(object):
"""
return unidecode(s)
@staticmethod
def tmpl_format(s, format):
"""Format the import time to any format according to time.strfime()
"""
return time.strftime(format, time.strptime(s, ITIME_FORMAT))
def tmpl_aunique(self, keys=None, disam=None):
"""Generate a string that is guaranteed to be unique among all
albums in the library who share the same set of keys. A fields

View file

@ -56,7 +56,7 @@ track's artists.
These functions are built in to beets:
* ``%lower{text}``: Convert ``text`` to lowercase.
* ``%lower{text}``: Convert ``text`` to lowercase.
* ``%upper{text}``: Convert ``text`` to UPPERCASE.
* ``%title{text}``: Convert ``text`` to Title Case.
* ``%left{text,n}``: Return the first ``n`` characters of ``text``.
@ -70,8 +70,12 @@ These functions are built in to beets:
`unidecode module`_.
* ``%aunique{identifiers,disambiguators}``: Provides a unique string to
disambiguate similar albums in the database. See :ref:`aunique`, below.
* ``%format{date_time,format}``: Return the date and time in any format accepted
by the `time.strfime() method`_. Should probably be used together with the
``itime`` field (import time).
.. _unidecode module: http://pypi.python.org/pypi/Unidecode
.. _time.strftime() method: http://docs.python.org/2/library/time.html#time.strftime
Plugins can extend beets with more template functions (see
:ref:`writing-plugins`).