mirror of
https://github.com/fergalmoran/picard.git
synced 2026-01-06 16:44:06 +00:00
Drop class UserProfileGroups, use module level methods
This commit is contained in:
@@ -41,7 +41,7 @@ from picard import (
|
||||
PICARD_VERSION,
|
||||
log,
|
||||
)
|
||||
from picard.profile import UserProfileGroups
|
||||
from picard.profile import profile_groups_all_settings
|
||||
from picard.version import Version
|
||||
|
||||
|
||||
@@ -180,7 +180,7 @@ class SettingConfigSection(ConfigSection):
|
||||
|
||||
def __getitem__(self, name):
|
||||
# Don't process settings that are not profile-specific
|
||||
if name in UserProfileGroups.all_settings():
|
||||
if name in profile_groups_all_settings():
|
||||
for profile_id, settings in self._get_active_profile_settings():
|
||||
if name in settings and settings[name] is not None:
|
||||
return settings[name]
|
||||
@@ -191,7 +191,7 @@ class SettingConfigSection(ConfigSection):
|
||||
|
||||
def __setitem__(self, name, value):
|
||||
# Don't process settings that are not profile-specific
|
||||
if name in UserProfileGroups.all_settings():
|
||||
if name in profile_groups_all_settings():
|
||||
for profile_id, settings in self._get_active_profile_settings():
|
||||
if name in settings:
|
||||
self._save_profile_setting(profile_id, name, value)
|
||||
|
||||
@@ -31,68 +31,64 @@ from collections import (
|
||||
|
||||
SettingDesc = namedtuple('SettingDesc', ('name', 'highlights'))
|
||||
|
||||
_settings_groups = {}
|
||||
_groups_order = defaultdict(lambda: -1)
|
||||
_groups_count = 0
|
||||
|
||||
class UserProfileGroups():
|
||||
"""Provides information about the profile groups available for selecting in a user profile,
|
||||
and the title and settings that apply to each profile group.
|
||||
|
||||
def profile_groups_order(group):
|
||||
global _groups_count
|
||||
if _groups_order[group] == -1:
|
||||
_groups_order[group] = _groups_count
|
||||
_groups_count += 1
|
||||
|
||||
|
||||
def profile_groups_add_setting(group, option_name, highlights, title=None):
|
||||
if group not in _settings_groups:
|
||||
_settings_groups[group] = {'title': title or group}
|
||||
if 'settings' not in _settings_groups[group]:
|
||||
_settings_groups[group]['settings'] = []
|
||||
_settings_groups[group]['settings'].append(SettingDesc(option_name, highlights))
|
||||
|
||||
|
||||
def profile_groups_all_settings():
|
||||
for value in _settings_groups.values():
|
||||
if 'settings' in value:
|
||||
for s in value['settings']:
|
||||
yield s.name
|
||||
|
||||
|
||||
def profile_groups_settings(group):
|
||||
if group in _settings_groups:
|
||||
if 'settings' in _settings_groups[group]:
|
||||
yield from _settings_groups[group]['settings']
|
||||
|
||||
|
||||
def profile_groups_keys():
|
||||
"""Iterable of all setting groups keys.
|
||||
|
||||
Yields:
|
||||
str: Key
|
||||
"""
|
||||
_settings_groups = {}
|
||||
yield from _settings_groups.keys()
|
||||
|
||||
|
||||
def profile_groups_group_from_page(page):
|
||||
try:
|
||||
return _settings_groups[page.NAME]
|
||||
except (AttributeError, KeyError):
|
||||
return None
|
||||
|
||||
|
||||
def profile_groups_values():
|
||||
"""Returns values sorted by (groups_order, group name)"""
|
||||
for k in sorted(_settings_groups, key=lambda k: (_groups_order[k], k)):
|
||||
yield _settings_groups[k]
|
||||
|
||||
|
||||
def profile_groups_reset():
|
||||
"""Used when testing"""
|
||||
global _settings_groups, _groups_order, _groups_count
|
||||
_settings_groups = {}
|
||||
_groups_order = defaultdict(lambda: -1)
|
||||
_groups_count = 0
|
||||
|
||||
@classmethod
|
||||
def order(cls, group):
|
||||
if cls._groups_order[group] == -1:
|
||||
cls._groups_order[group] = cls._groups_count
|
||||
cls._groups_count += 1
|
||||
|
||||
@classmethod
|
||||
def append_to_group(cls, group, option, highlights, title=None):
|
||||
if group not in cls._settings_groups:
|
||||
cls._settings_groups[group] = {'title': title or group}
|
||||
if 'settings' not in cls._settings_groups[group]:
|
||||
cls._settings_groups[group]['settings'] = []
|
||||
cls._settings_groups[group]['settings'].append(SettingDesc(option, highlights))
|
||||
|
||||
@classmethod
|
||||
def all_settings(cls):
|
||||
for value in cls._settings_groups.values():
|
||||
if 'settings' in value:
|
||||
for s in value['settings']:
|
||||
yield s.name
|
||||
|
||||
@classmethod
|
||||
def settings(cls, group):
|
||||
if group in cls._settings_groups:
|
||||
if 'settings' in cls._settings_groups[group]:
|
||||
yield from cls._settings_groups[group]['settings']
|
||||
|
||||
@classmethod
|
||||
def keys(cls):
|
||||
"""Iterable of all setting groups keys.
|
||||
|
||||
Yields:
|
||||
str: Key
|
||||
"""
|
||||
yield from cls._settings_groups.keys()
|
||||
|
||||
@classmethod
|
||||
def group_from_page(cls, page):
|
||||
try:
|
||||
return cls._settings_groups[page.NAME]
|
||||
except (AttributeError, KeyError):
|
||||
return None
|
||||
|
||||
@classmethod
|
||||
def values(cls):
|
||||
"""Returns values sorted by (groups_order, group name)"""
|
||||
for k in sorted(cls._settings_groups, key=lambda k: (cls._groups_order[k], k)):
|
||||
yield cls._settings_groups[k]
|
||||
|
||||
@classmethod
|
||||
def reset(cls):
|
||||
"""Used when testing"""
|
||||
cls._settings_groups = {}
|
||||
cls._groups_order = defaultdict(lambda: -1)
|
||||
cls._groups_count = 0
|
||||
|
||||
@@ -36,7 +36,7 @@ from picard.config import (
|
||||
)
|
||||
from picard.i18n import gettext as _
|
||||
from picard.plugin import ExtensionPoint
|
||||
from picard.profile import UserProfileGroups
|
||||
from picard.profile import profile_groups_add_setting
|
||||
|
||||
|
||||
class OptionsCheckError(Exception):
|
||||
@@ -137,7 +137,7 @@ class OptionsPage(QtWidgets.QWidget):
|
||||
raise Exception(f"Cannot register setting for non-existing option {name}")
|
||||
self._registered_settings.append(option)
|
||||
if highlights is not None:
|
||||
UserProfileGroups.append_to_group(self.NAME, name, tuple(highlights), title=self.TITLE)
|
||||
profile_groups_add_setting(self.NAME, name, tuple(highlights), title=self.TITLE)
|
||||
|
||||
|
||||
_pages = ExtensionPoint(label='pages')
|
||||
|
||||
@@ -48,7 +48,10 @@ from picard.i18n import (
|
||||
N_,
|
||||
gettext as _,
|
||||
)
|
||||
from picard.profile import UserProfileGroups
|
||||
from picard.profile import (
|
||||
profile_groups_group_from_page,
|
||||
profile_groups_order,
|
||||
)
|
||||
from picard.util import restore_method
|
||||
|
||||
from picard.ui import (
|
||||
@@ -104,7 +107,7 @@ class OptionsDialog(PicardDialog, SingletonDialog):
|
||||
self.item_to_page[item] = page
|
||||
self.page_to_item[page.NAME] = item
|
||||
self.ui.pages_stack.addWidget(page)
|
||||
UserProfileGroups.order(page.NAME)
|
||||
profile_groups_order(page.NAME)
|
||||
else:
|
||||
item.setFlags(QtCore.Qt.ItemFlag.ItemIsEnabled)
|
||||
self.add_pages(page.NAME, default_page, item)
|
||||
@@ -206,7 +209,7 @@ class OptionsDialog(PicardDialog, SingletonDialog):
|
||||
if not items:
|
||||
return
|
||||
page = self.item_to_page[items[0]]
|
||||
option_group = UserProfileGroups.group_from_page(page)
|
||||
option_group = profile_groups_group_from_page(page)
|
||||
if not option_group:
|
||||
message_box = QtWidgets.QMessageBox(self)
|
||||
message_box.setIcon(QtWidgets.QMessageBox.Icon.Information)
|
||||
@@ -247,7 +250,7 @@ class OptionsDialog(PicardDialog, SingletonDialog):
|
||||
bg_color = colors.get_color('profile_hl_bg')
|
||||
|
||||
for page in self.pages:
|
||||
option_group = UserProfileGroups.group_from_page(page)
|
||||
option_group = profile_groups_group_from_page(page)
|
||||
if option_group:
|
||||
if load_settings:
|
||||
page.load()
|
||||
@@ -296,7 +299,7 @@ class OptionsDialog(PicardDialog, SingletonDialog):
|
||||
return self.item_to_page[self.page_to_item[name]]
|
||||
|
||||
def page_has_attached_profiles(self, page, enabled_profiles_only=False):
|
||||
option_group = UserProfileGroups.group_from_page(page)
|
||||
option_group = profile_groups_group_from_page(page)
|
||||
if not option_group:
|
||||
return False
|
||||
working_profiles, working_settings = self.get_working_profile_data()
|
||||
|
||||
@@ -42,7 +42,7 @@ from picard.i18n import (
|
||||
gettext as _,
|
||||
gettext_constants,
|
||||
)
|
||||
from picard.profile import UserProfileGroups
|
||||
from picard.profile import profile_groups_values
|
||||
from picard.script import get_file_naming_script_presets
|
||||
from picard.util import get_base_title
|
||||
|
||||
@@ -210,7 +210,7 @@ class ProfilesOptionsPage(OptionsPage):
|
||||
if settings is None:
|
||||
return
|
||||
self.building_tree = True
|
||||
for group in UserProfileGroups.values():
|
||||
for group in profile_groups_values():
|
||||
title = _(group['title'])
|
||||
group_settings = group['settings']
|
||||
widget_item = QtWidgets.QTreeWidgetItem([title])
|
||||
|
||||
@@ -35,7 +35,15 @@ from picard.config import (
|
||||
SettingConfigSection,
|
||||
TextOption,
|
||||
)
|
||||
from picard.profile import UserProfileGroups
|
||||
from picard.profile import (
|
||||
profile_groups_add_setting,
|
||||
profile_groups_all_settings,
|
||||
profile_groups_keys,
|
||||
profile_groups_order,
|
||||
profile_groups_reset,
|
||||
profile_groups_settings,
|
||||
profile_groups_values,
|
||||
)
|
||||
|
||||
|
||||
class TestPicardProfilesCommon(PicardTestCase):
|
||||
@@ -63,14 +71,14 @@ class TestPicardProfilesCommon(PicardTestCase):
|
||||
Option('profiles', self.SETTINGS_KEY, {})
|
||||
|
||||
# Get valid profile option settings for testing
|
||||
UserProfileGroups.reset()
|
||||
profile_groups_reset()
|
||||
for n in range(0, 4):
|
||||
group = 'group%d' % (n % 2)
|
||||
title = 'title_' + group
|
||||
name = 'opt%d' % n
|
||||
highlights = ('obj%d' % i for i in range(0, n))
|
||||
UserProfileGroups.append_to_group(group, name, highlights, title=title)
|
||||
option_settings = list(UserProfileGroups.all_settings())
|
||||
profile_groups_add_setting(group, name, highlights, title=title)
|
||||
option_settings = list(profile_groups_all_settings())
|
||||
self.test_setting_0 = option_settings[0]
|
||||
self.test_setting_1 = option_settings[1]
|
||||
self.test_setting_2 = option_settings[2]
|
||||
@@ -104,48 +112,48 @@ class TestPicardProfilesCommon(PicardTestCase):
|
||||
class TestUserProfileGroups(TestPicardProfilesCommon):
|
||||
|
||||
def test_has_groups(self):
|
||||
groups = list(UserProfileGroups.keys())
|
||||
groups = list(profile_groups_keys())
|
||||
self.assertEqual(groups, ['group0', 'group1'])
|
||||
|
||||
def test_groups_have_items(self):
|
||||
for group in UserProfileGroups.keys():
|
||||
settings = UserProfileGroups.settings(group)
|
||||
for group in profile_groups_keys():
|
||||
settings = profile_groups_settings(group)
|
||||
self.assertNotEqual(settings, {})
|
||||
|
||||
def test_no_duplicate_settings(self):
|
||||
count1 = 0
|
||||
for group in UserProfileGroups.keys():
|
||||
settings = UserProfileGroups.settings(group)
|
||||
for group in profile_groups_keys():
|
||||
settings = profile_groups_settings(group)
|
||||
count1 += len(list(settings))
|
||||
count2 = len(list(UserProfileGroups.all_settings()))
|
||||
count2 = len(list(profile_groups_all_settings()))
|
||||
self.assertEqual(count1, count2)
|
||||
|
||||
def test_settings_have_no_blank_keys(self):
|
||||
for group in UserProfileGroups.keys():
|
||||
settings = UserProfileGroups.settings(group)
|
||||
for group in profile_groups_keys():
|
||||
settings = profile_groups_settings(group)
|
||||
for name, highlights in settings:
|
||||
self.assertNotEqual(name.strip(), "")
|
||||
|
||||
def test_groups_have_title(self):
|
||||
for value in UserProfileGroups.values():
|
||||
for value in profile_groups_values():
|
||||
self.assertTrue(value['title'].startswith('title_'))
|
||||
|
||||
def test_groups_have_highlights(self):
|
||||
for group in UserProfileGroups.keys():
|
||||
for setting in UserProfileGroups.settings(group):
|
||||
for group in profile_groups_keys():
|
||||
for setting in profile_groups_settings(group):
|
||||
self.assertIsNotNone(setting.highlights)
|
||||
|
||||
def test_order(self):
|
||||
result_before = [value['title'] for value in UserProfileGroups.values()]
|
||||
result_before = [value['title'] for value in profile_groups_values()]
|
||||
self.assertEqual(
|
||||
result_before,
|
||||
['title_group0', 'title_group1']
|
||||
)
|
||||
|
||||
UserProfileGroups.order('group1')
|
||||
UserProfileGroups.order('group0')
|
||||
profile_groups_order('group1')
|
||||
profile_groups_order('group0')
|
||||
|
||||
result_after = [value['title'] for value in UserProfileGroups.values()]
|
||||
result_after = [value['title'] for value in profile_groups_values()]
|
||||
self.assertEqual(
|
||||
result_after,
|
||||
['title_group1', 'title_group0']
|
||||
|
||||
Reference in New Issue
Block a user