Skip to content

Commit

Permalink
PICARD-2729: Allow disabling date sanitazation for APE and Vorbis tags
Browse files Browse the repository at this point in the history
  • Loading branch information
ShubhamBhut committed Apr 23, 2024
1 parent 05ddd5f commit 51aaa38
Show file tree
Hide file tree
Showing 10 changed files with 190 additions and 44 deletions.
6 changes: 5 additions & 1 deletion picard/formats/apev2.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ class APEv2File(File):
'replaygain_reference_loudness': 'REPLAYGAIN_REFERENCE_LOUDNESS',
}
__rtranslate = {v.lower(): k for k, v in __translate.items()}
sanitize_date = sanitize_date

def __init__(self, filename):
super().__init__(filename)
Expand All @@ -136,6 +137,8 @@ def _load(self, filename):
file = self._File(encode_filename(filename))
metadata = Metadata()
if file.tags:
config = get_config()
date_sanitize = not self.NAME in config.setting['formats_to_disable_date_sanitize']

Check notice on line 141 in picard/formats/apev2.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

picard/formats/apev2.py#L141

test for membership should be 'not in' (E713)
for origname, values in file.tags.items():
name_lower = origname.lower()
if (values.kind == mutagen.apev2.BINARY
Expand All @@ -160,7 +163,8 @@ def _load(self, filename):
name = name_lower
if name == 'year':
name = 'date'
value = sanitize_date(value)
if date_sanitize:
value = sanitize_date(value)
elif name == 'track':
name = 'tracknumber'
track = value.split('/')
Expand Down
11 changes: 8 additions & 3 deletions picard/formats/id3.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ class ID3File(File):
__lrc_line_re_parse = re.compile(r'(\[\d\d:\d\d\.\d\d\d\])')
__lrc_syllable_re_parse = re.compile(r'(<\d\d:\d\d\.\d\d\d>)')
__lrc_both_re_parse = re.compile(r'(\[\d\d:\d\d\.\d\d\d\]|<\d\d:\d\d\.\d\d\d>)')
sanitize_date = sanitize_date

def __init__(self, filename):
super().__init__(filename)
Expand Down Expand Up @@ -278,6 +279,7 @@ def _load(self, filename):
f = tags.pop(old)
tags.add(getattr(id3, new)(encoding=f.encoding, text=f.text))
metadata = Metadata()
date_sanitize = not self.NAME in config.setting['formats_to_disable_date_sanitize']

Check notice on line 282 in picard/formats/id3.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

picard/formats/id3.py#L282

test for membership should be 'not in' (E713)
for frame in tags.values():
frameid = frame.FrameID
if frameid in self.__translate:
Expand Down Expand Up @@ -395,9 +397,12 @@ def _load(self, filename):
metadata.add('~rating', rating)

if 'date' in metadata:
sanitized = sanitize_date(metadata.getall('date')[0])
if sanitized:
metadata['date'] = sanitized
if date_sanitize:
sanitized = sanitize_date(metadata.getall('date')[0])
if sanitized:
metadata['date'] = sanitized
else:
metadata['date'] = metadata.getall('date')[0]

self._info(metadata, file)
return metadata
Expand Down
6 changes: 6 additions & 0 deletions picard/formats/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ def supported_formats():
return [(file_format.EXTENSIONS, file_format.NAME) for file_format in _formats]


def formats_with_sanitize_date():
for fmt in _formats:
if hasattr(fmt, 'sanitize_date'):
yield fmt


def supported_extensions():
"""Returns list of supported extensions."""
return [ext for exts, name in supported_formats() for ext in exts]
Expand Down
9 changes: 7 additions & 2 deletions picard/formats/vorbis.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,20 +129,24 @@ class VCommentFile(File):
'waveformatextensible_channel_mask': '~waveformatextensible_channel_mask',
}
__rtranslate = {v: k for k, v in __translate.items()}
sanitize_date = sanitize_date

def _load(self, filename):
log.debug("Loading file %r", filename)
config = get_config()
file = self._File(encode_filename(filename))
file.tags = file.tags or {}
metadata = Metadata()
config = get_config()
date_sanitize = not self.NAME in config.setting['formats_to_disable_date_sanitize']

Check notice on line 141 in picard/formats/vorbis.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

picard/formats/vorbis.py#L141

test for membership should be 'not in' (E713)
for origname, values in file.tags.items():
for value in values:
value = value.rstrip('\0')
name = origname
if name in {'date', 'originaldate', 'releasedate'}:
# YYYY-00-00 => YYYY
value = sanitize_date(value)
if date_sanitize:
value = sanitize_date(value)
elif name == 'performer' or name == 'comment':
# transform "performer=Joe Barr (Piano)" to "performer:Piano=Joe Barr"
name += ':'
Expand Down Expand Up @@ -280,7 +284,8 @@ def _save(self, filename, metadata):
name = 'lyrics'
elif name in {'date', 'originaldate', 'releasedate'}:
# YYYY-00-00 => YYYY
value = sanitize_date(value)
if not self.NAME in config.setting['formats_to_disable_date_sanitize']:

Check notice on line 287 in picard/formats/vorbis.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

picard/formats/vorbis.py#L287

test for membership should be 'not in' (E713)
value = sanitize_date(value)
elif name.startswith('performer:') or name.startswith('comment:'):
# transform "performer:Piano=Joe Barr" to "performer=Joe Barr (Piano)"
name, desc = name.split(':', 1)
Expand Down
2 changes: 2 additions & 0 deletions picard/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ class UserProfileGroups():
SettingDesc('convert_punctuation', ['convert_punctuation']),
SettingDesc('release_ars', ['release_ars']),
SettingDesc('track_ars', ['track_ars']),
SettingDesc('disable_date_sanitize', ['disable_date_sanitize']),
SettingDesc('formats_to_disable_date_sanitize', ['selected_formats']),
SettingDesc('guess_tracknumber_and_title', ['guess_tracknumber_and_title']),
SettingDesc('va_name', ['va_name']),
SettingDesc('nat_name', ['nat_name']),
Expand Down
12 changes: 12 additions & 0 deletions picard/ui/options/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
from picard.ui.ui_multi_locale_selector import Ui_MultiLocaleSelector
from picard.ui.ui_options_metadata import Ui_MetadataOptionsPage
from picard.ui.util import qlistwidget_items
from picard.formats.util import formats_with_sanitize_date


def iter_sorted_locales(locales):
Expand Down Expand Up @@ -92,6 +93,8 @@ class MetadataOptionsPage(OptionsPage):
ListOption('setting', 'script_exceptions', [], title=N_("Translation script exceptions")),
BoolOption('setting', 'release_ars', True, title=N_("Use release relationships")),
BoolOption('setting', 'track_ars', False, title=N_("Use track and release relationships")),
BoolOption('setting', 'disable_date_sanitize', False, title=N_("Disable date sanitization for APE and Vorbis tags")),
Option('setting', 'formats_to_disable_date_sanitize', set(), title=N_("Formats to disable date sanitize")),
BoolOption('setting', 'convert_punctuation', False, title=N_("Convert Unicode punctuation characters to ASCII")),
BoolOption('setting', 'standardize_artists', False, title=N_("Use standardized artist names")),
BoolOption('setting', 'standardize_instruments', True, title=N_("Use standardized instrument and vocal credits")),
Expand All @@ -108,6 +111,7 @@ def __init__(self, parent=None):
self.ui.select_scripts.clicked.connect(self.open_script_selector)
self.ui.translate_artist_names.stateChanged.connect(self.set_enabled_states)
self.ui.translate_artist_names_script_exception.stateChanged.connect(self.set_enabled_states)
self.ui.disable_date_sanitize.stateChanged.connect(self.set_enabled_states)

def load(self):
config = get_config()
Expand All @@ -117,6 +121,10 @@ def load(self):
self.current_scripts = config.setting['script_exceptions']
self.make_scripts_text()
self.ui.translate_artist_names_script_exception.setChecked(config.setting['translate_artist_names_script_exception'])
self.ui.disable_date_sanitize.setChecked(config.setting['disable_date_sanitize'])
self.current_formats = config.setting['formats_to_disable_date_sanitize']
fmt_names = sorted(fmt.NAME for fmt in formats_with_sanitize_date())
self.ui.selected_formats.addItems(fmt_names)

self.ui.convert_punctuation.setChecked(config.setting['convert_punctuation'])
self.ui.release_ars.setChecked(config.setting['release_ars'])
Expand Down Expand Up @@ -152,6 +160,8 @@ def save(self):
config.setting['convert_punctuation'] = self.ui.convert_punctuation.isChecked()
config.setting['release_ars'] = self.ui.release_ars.isChecked()
config.setting['track_ars'] = self.ui.track_ars.isChecked()
config.setting['disable_date_sanitize'] = self.ui.disable_date_sanitize.isChecked()
config.setting['formats_to_disable_date_sanitize'] = self.current_formats
config.setting['va_name'] = self.ui.va_name.text()
nat_name = self.ui.nat_name.text()
if nat_name != config.setting['nat_name']:
Expand Down Expand Up @@ -179,6 +189,8 @@ def set_enabled_states(self):
select_scripts_enabled = translate_checked and translate_exception_checked
self.ui.selected_scripts.setEnabled(select_scripts_enabled)
self.ui.select_scripts.setEnabled(select_scripts_enabled)
disable_date_sanitize_checked = self.ui.disable_date_sanitize.isChecked()
self.ui.selected_formats.setEnabled(disable_date_sanitize_checked)

def open_locale_selector(self):
dialog = MultiLocaleSelector(self)
Expand Down
Loading

0 comments on commit 51aaa38

Please sign in to comment.