Fix edit metadata

This commit is contained in:
Cédric Leporcq 2022-07-27 03:16:25 +02:00
parent 503b90b631
commit a1ba0663b6
3 changed files with 46 additions and 42 deletions

View File

@ -363,6 +363,13 @@ def _compare(**kwargs):
multiple=True, multiple=True,
help="Select exif tags groups to edit", help="Select exif tags groups to edit",
) )
@click.option(
'--overwrite',
'-O',
default=False,
is_flag=True,
help="Overwrite db and exif value by key value",
)
@click.argument('subdirs', required=False, nargs=-1, type=click.Path()) @click.argument('subdirs', required=False, nargs=-1, type=click.Path())
@click.argument('path', required=True, nargs=1, type=click.Path()) @click.argument('path', required=True, nargs=1, type=click.Path())
def _edit(**kwargs): def _edit(**kwargs):
@ -373,6 +380,8 @@ def _edit(**kwargs):
paths, root = _get_paths(kwargs['subdirs'], kwargs['path']) paths, root = _get_paths(kwargs['subdirs'], kwargs['path'])
overwrite = kwargs['overwrite']
collection = Collection( collection = Collection(
root, root,
{ {
@ -389,13 +398,11 @@ def _edit(**kwargs):
'camera_make', 'camera_make',
'camera_model', 'camera_model',
'city', 'city',
'coordinates',
'country', 'country',
# 'date_created', # 'date_created',
'date_media', 'date_media',
# 'date_modified', # 'date_modified',
'date_original', 'date_original',
'default',
'latitude', 'latitude',
'location', 'location',
'longitude', 'longitude',
@ -410,6 +417,9 @@ def _edit(**kwargs):
keys = set(editable_keys) keys = set(editable_keys)
else: else:
keys = set(kwargs['key']) keys = set(kwargs['key'])
if 'coordinates' in keys:
keys.remove('coordinates')
keys.update(['latitude', 'longitude'])
location = False location = False
for key in keys: for key in keys:
@ -417,10 +427,6 @@ def _edit(**kwargs):
LOG.error(f"key '{key}' is not valid") LOG.error(f"key '{key}' is not valid")
sys.exit(1) sys.exit(1)
if key == 'coordinates':
keys.remove('coordinates')
keys.update(['latitude', 'longitude'])
if key in ( if key in (
'city', 'city',
'latitude', 'latitude',
@ -436,7 +442,7 @@ def _edit(**kwargs):
else: else:
loc = None loc = None
summary = collection.edit_metadata(paths, keys, loc, overwrite=True) summary = collection.edit_metadata(paths, keys, loc, overwrite)
if log_level < 30: if log_level < 30:
summary.print() summary.print()

View File

@ -272,7 +272,7 @@ class CollectionDb:
def __init__(self, root): def __init__(self, root):
self.sqlite = Sqlite(root) self.sqlite = Sqlite(root)
def _set_row_data(self, table, metadata): def _get_row_data(self, table, metadata):
row_data = {} row_data = {}
for title in self.sqlite.tables[table]['header']: for title in self.sqlite.tables[table]['header']:
key = utils.camel2snake(title) key = utils.camel2snake(title)
@ -283,11 +283,11 @@ class CollectionDb:
def add_file_data(self, metadata): def add_file_data(self, metadata):
"""Save metadata informations to db""" """Save metadata informations to db"""
if metadata['latitude'] and metadata['longitude']: if metadata['latitude'] and metadata['longitude']:
loc_values = self._set_row_data('location', metadata) loc_values = self._get_row_data('location', metadata)
metadata['location_id'] = self.sqlite.upsert_location(loc_values) metadata['location_id'] = self.sqlite.upsert_location(loc_values)
if metadata['file_path']: if metadata['file_path']:
row_data = self._set_row_data('metadata', metadata) row_data = self._get_row_data('metadata', metadata)
self.sqlite.upsert_metadata(row_data) self.sqlite.upsert_metadata(row_data)
@ -1144,13 +1144,20 @@ class Collection(SortMedias):
self._init_check_db() self._init_check_db()
for file_path, media in self.medias.get_medias_datas(paths, loc=loc): for file_path, media in self.medias.get_medias_datas(paths, loc=loc):
result = False
media.metadata['file_path'] = os.path.relpath(file_path, self.root) media.metadata['file_path'] = os.path.relpath(file_path, self.root)
exif = WriteExif(
file_path,
media.metadata,
ignore_tags=self.opt['Exif']['ignore_tags'],
)
for key in keys: for key in keys:
print() print()
value = media.metadata[key] value = media.metadata[key]
if overwrite or not value: if overwrite or not value:
print(f"FILE: '{file_path}'") print(f"FILE: '{file_path}'")
if overwrite: if overwrite and value:
print(f"{key}: '{value}'") print(f"{key}: '{value}'")
if overwrite or not value: if overwrite or not value:
# Prompt value for given key for file_path # Prompt value for given key for file_path
@ -1165,11 +1172,11 @@ class Collection(SortMedias):
value = media.get_date_format(answer['value']) value = media.get_date_format(answer['value'])
else: else:
value = answer['value'] value = answer['value']
if not value.isalnum(): while not value.isalnum():
if not value: break
print("Invalid entry, use alphanumeric chars") print("Invalid entry, use alphanumeric chars")
value = inquirer.prompt(prompt, theme=self.theme) value = inquirer.prompt(prompt, theme=self.theme)
result = False
if value: if value:
media.metadata[key] = value media.metadata[key] = value
if key == 'location': if key == 'location':
@ -1179,24 +1186,7 @@ class Collection(SortMedias):
media.metadata['longitude'] = coordinates['longitude'] media.metadata['longitude'] = coordinates['longitude']
media.set_location_from_coordinates(loc) media.set_location_from_coordinates(loc)
# Update database
self.db.add_file_data(media.metadata)
# Update exif data # Update exif data
if key in (
'date_original',
'album',
'title',
'latitude',
'location',
'longitude',
'latitude_ref',
'longitude_ref',
):
exif = WriteExif(
file_path,
media.metadata,
ignore_tags=self.opt['Exif']['ignore_tags'],
)
if key == 'location': if key == 'location':
result = exif.set_key_values( result = exif.set_key_values(
'latitude', media.metadata['latitude'] 'latitude', media.metadata['latitude']
@ -1204,8 +1194,15 @@ class Collection(SortMedias):
result = exif.set_key_values( result = exif.set_key_values(
'longitude', media.metadata['longitude'] 'longitude', media.metadata['longitude']
) )
else: elif key in exif.get_tags().keys():
result = exif.set_key_values(key, value) result = exif.set_key_values(key, value)
# Update checksum
media.metadata['checksum'] = utils.checksum(file_path)
# Update database
self.db.add_file_data(media.metadata)
if result: if result:
self.summary.append('update', True, file_path) self.summary.append('update', True, file_path)
else: else:

View File

@ -96,6 +96,7 @@ class TestOrdigi:
args = ( args = (
'--key', '--key',
'date_original', 'date_original',
'--overwrite',
str(self.src_path.joinpath('test_exif/photo.png')), str(self.src_path.joinpath('test_exif/photo.png')),
str(self.src_path), str(self.src_path),
) )