From 5387f8e2f970ff7fa1967ccad87b45a4f7e50d32 Mon Sep 17 00:00:00 2001 From: Rhet Turnbull Date: Sat, 13 Jun 2020 09:17:34 -0700 Subject: [PATCH] Added hour, min, sec to template system, issue #158 --- osxphotos/_version.py | 2 +- osxphotos/phototemplate.py | 36 ++++++++++++++++++++++++++++++++++++ tests/test_template.py | 14 ++++++++++++-- 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/osxphotos/_version.py b/osxphotos/_version.py index cb42f9a8..c39cba68 100644 --- a/osxphotos/_version.py +++ b/osxphotos/_version.py @@ -1,3 +1,3 @@ """ version info """ -__version__ = "0.29.14" +__version__ = "0.29.15" diff --git a/osxphotos/phototemplate.py b/osxphotos/phototemplate.py index 0cc8b5a8..233f2402 100644 --- a/osxphotos/phototemplate.py +++ b/osxphotos/phototemplate.py @@ -35,6 +35,9 @@ TEMPLATE_SUBSTITUTIONS = { "{created.dd}": "2-digit day of the month (zero padded) of file creation time", "{created.dow}": "Day of week in user's locale of the file creation time", "{created.doy}": "3-digit day of year (e.g Julian day) of file creation time, starting from 1 (zero padded)", + "{created.hour}": "2-digit hour of the file creation time", + "{created.min}": "2-digit minute of the file creation time", + "{created.sec}": "2-digit second of the file creation time", "{modified.date}": "Photo's modification date in ISO format, e.g. '2020-03-22'", "{modified.year}": "4-digit year of file modification time", "{modified.yy}": "2-digit year of file modification time", @@ -43,6 +46,9 @@ TEMPLATE_SUBSTITUTIONS = { "{modified.mon}": "Month abbreviation in the user's locale of the file modification time", "{modified.dd}": "2-digit day of the month (zero padded) of the file modification time", "{modified.doy}": "3-digit day of year (e.g Julian day) of file modification time, starting from 1 (zero padded)", + "{modified.hour}": "2-digit hour of the file modification time", + "{modified.min}": "2-digit minute of the file modification time", + "{modified.sec}": "2-digit second of the file modification time", "{place.name}": "Place name from the photo's reverse geolocation data, as displayed in Photos", "{place.country_code}": "The ISO country code from the photo's reverse geolocation data", "{place.name.country}": "Country name from the photo's reverse geolocation data", @@ -273,6 +279,15 @@ class PhotoTemplate: if field == "created.doy": return DateTimeFormatter(self.photo.date).doy + if field == "created.hour": + return DateTimeFormatter(self.photo.date).hour + + if field == "created.min": + return DateTimeFormatter(self.photo.date).min + + if field == "created.sec": + return DateTimeFormatter(self.photo.date).sec + if field == "modified.date": return ( DateTimeFormatter(self.photo.date_modified).date @@ -329,6 +344,27 @@ class PhotoTemplate: else None ) + if field == "modified.hour": + return ( + DateTimeFormatter(self.photo.date_modified).hour + if self.photo.date_modified + else None + ) + + if field == "modified.min": + return ( + DateTimeFormatter(self.photo.date_modified).min + if self.photo.date_modified + else None + ) + + if field == "modified.sec": + return ( + DateTimeFormatter(self.photo.date_modified).sec + if self.photo.date_modified + else None + ) + if field == "place.name": return self.photo.place.name if self.photo.place else None diff --git a/tests/test_template.py b/tests/test_template.py index ec4bcc6a..9633ccf9 100644 --- a/tests/test_template.py +++ b/tests/test_template.py @@ -32,6 +32,9 @@ TEMPLATE_VALUES = { "{created.dd}": "04", "{created.dow}": "Tuesday", "{created.doy}": "035", + "{created.hour}": "19", + "{created.min}": "07", + "{created.sec}": "38", "{modified.date}": "2020-03-21", "{modified.year}": "2020", "{modified.yy}": "20", @@ -40,6 +43,9 @@ TEMPLATE_VALUES = { "{modified.mon}": "Mar", "{modified.dd}": "21", "{modified.doy}": "081", + "{modified.hour}": "01", + "{modified.min}": "33", + "{modified.sec}": "08", "{place.name}": "Washington, District of Columbia, United States", "{place.country_code}": "US", "{place.name.country}": "United States", @@ -115,7 +121,10 @@ def test_lookup_multi(): import os import re import osxphotos - from osxphotos.phototemplate import TEMPLATE_SUBSTITUTIONS_MULTI_VALUED, PhotoTemplate + from osxphotos.phototemplate import ( + TEMPLATE_SUBSTITUTIONS_MULTI_VALUED, + PhotoTemplate, + ) photosdb = osxphotos.PhotosDB(dbfile=PHOTOS_DB_PLACES) photo = photosdb.photos(uuid=[UUID_DICT["place_dc"]])[0] @@ -123,10 +132,11 @@ def test_lookup_multi(): for subst in TEMPLATE_SUBSTITUTIONS_MULTI_VALUED: lookup_str = re.match(r"\{([^\\,}]+)\}", subst).group(1) - lookup = template.get_template_value_multi(lookup_str,path_sep=os.path.sep) + lookup = template.get_template_value_multi(lookup_str, path_sep=os.path.sep) assert isinstance(lookup, list) assert len(lookup) >= 1 + def test_subst(): """ Test that substitutions are correct """ import locale