Add edit_metadata method in collection

This commit is contained in:
Cédric Leporcq 2022-01-29 07:48:33 +01:00
parent e0fc31e543
commit b0969f62af
4 changed files with 47 additions and 34 deletions

View File

@ -17,7 +17,7 @@ import inquirer
from ordigi import LOG
from ordigi.config import Config
from ordigi.database import Sqlite
from ordigi.media import Medias
from ordigi.media import Medias, WriteExif
from ordigi.images import Image, Images
from ordigi import request
from ordigi.summary import Summary
@ -258,7 +258,7 @@ class CollectionDb:
def __init__(self, root):
self.sqlite = Sqlite(root)
def _format_row_data(self, table, metadata):
def _set_row_data(self, table, metadata):
row_data = {}
for title in self.sqlite.tables[table]['header']:
key = utils.camel2snake(title)
@ -268,10 +268,10 @@ class CollectionDb:
def add_file_data(self, metadata):
"""Save metadata informations to db"""
loc_values = self._format_row_data('location', metadata)
loc_values = self._set_row_data('location', metadata)
metadata['location_id'] = self.sqlite.add_row('location', loc_values)
row_data = self._format_row_data('metadata', metadata)
row_data = self._set_row_data('metadata', metadata)
self.sqlite.add_row('metadata', row_data)
@ -797,7 +797,7 @@ class Collection(SortMedias):
metadata['file_path'] = os.path.relpath(file_path, self.root)
self.db.add_file_data(metadata)
self.summary.append('update', file_path)
self.summary.append('update', True, file_path)
return self.summary
@ -1114,7 +1114,7 @@ class Collection(SortMedias):
return self.summary
def fill_metadata(self, path, key, loc=None, overwrite=False):
def edit_metadata(self, path, key, loc=None, overwrite=False):
"""Fill metadata and exif data for given key"""
self._init_check_db()
@ -1135,7 +1135,11 @@ class Collection(SortMedias):
paths = self.paths.get_paths_list(path)
for file_path in paths:
media = self.medias.get_media(file_path, self.root, loc)
media = self.medias.get_media(file_path, self.root)
media.get_metadata(
self.root, loc, self.db.sqlite, False
)
media.metadata['file_path'] = os.path.relpath(file_path, self.root)
print()
value = media.metadata[key]
if overwrite or not value:
@ -1152,20 +1156,27 @@ class Collection(SortMedias):
# Validate value
if key in ('date_original', 'date_created', 'date_modified'):
# Check date format
value = str(media.get_date_format(answer['value']))
value = media.get_date_format(answer['value'])
else:
if not answer[key].isalnum():
print("Invalid entry, use alphanumeric chars")
value = inquirer.prompt(prompt, theme=self.theme)
# print(f"{key}='{value}'")
media.metadata[key] = value
# Update database
self.db.add_file_data(media.metadata)
# Update exif data
media.set_key_values(key, value)
self.summary.append('update', False, file_path)
result = False
if value:
media.metadata[key] = value
# Update database
self.db.add_file_data(media.metadata)
# Update exif data
exif = WriteExif(
file_path,
media.metadata,
ignore_tags=self.opt['Exif']['ignore_tags'],
)
result = exif.set_key_values(key, value)
if result:
self.summary.append('update', True, file_path)
else:
self.summary.append('update', False, file_path)
return self.summary

View File

@ -184,7 +184,7 @@ class WriteExif(ExifMetadata):
:returns: value (str)
"""
# TODO overwrite mode check if fail
return ExifTool(self.file_path).setvalue(tag, value)
return ExifTool(self.file_path, overwrite=True).setvalue(tag, value)
def set_key_values(self, key, value):
"""Set tags values for given key"""

View File

@ -70,7 +70,10 @@ class TestOrdigi:
]
for command in commands:
self.assert_cli(command, ['not_exist'], state=1)
if command.name == 'edit':
self.assert_cli(command, ['-k', 'date_original', 'not_exist'], state=1)
else:
self.assert_cli(command, ['not_exist'], state=1)
self.assert_cli(cli._clone, ['not_exist'], state=2)
@ -202,6 +205,9 @@ class TestOrdigi:
paths = (str(self.src_path),)
# Workaround
self.assert_cli(cli._update, paths)
self.assert_cli(cli._compare, paths)
self.assert_options(cli._compare, bool_options, arg_options, paths)

View File

@ -251,24 +251,20 @@ class TestCollection:
# Summary is created and there is no errors
assert not summary.errors
@pytest.mark.skip()
def test_fill_data(self, tmp_path, monkeypatch):
# @pytest.mark.skip()
def test_edit_metadata(self, tmp_path, monkeypatch):
path = tmp_path / 'collection'
shutil.copytree(self.src_path, path)
collection = Collection(path)
collection = Collection(path, {'cache': False})
# loc = GeoLocation()
# def mockreturn(prompt, theme):
# return {'value': '03-12-2021 08:12:35'}
def mockreturn(prompt, theme):
return {'value': '03-12-2021 08:12:35'}
# monkeypatch.setattr(inquirer, 'prompt', mockreturn)
# collection.fill_data(path, 'date_original')
# # check if db value is set
# import ipdb; ipdb.set_trace()
# date = collection.db.get_metadata_data('test_exif/invalid.invalid',
# 'DateOriginal')
# assert date == '2021-03-12 08:12:35'
# Check if exif value is set
monkeypatch.setattr(inquirer, 'prompt', mockreturn)
collection.fill_data(path, 'date_original', edit=True)
collection.edit_metadata(path, 'date_original', overwrite=True)
# check if db value is set
date = collection.db.sqlite.get_metadata_data('test_exif/photo.rw2',
'DateOriginal')
assert date == '2021-03-12 08:12:35'