Skip to content

Commit d899b71

Browse files
authored
[cuegui] Split config code into a new module. (#1095)
1 parent 75c7639 commit d899b71

3 files changed

Lines changed: 158 additions & 25 deletions

File tree

cuegui/cuegui/Config.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Copyright Contributors to the OpenCue Project
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
"""Functions for loading application state and settings from disk."""
17+
18+
from __future__ import print_function
19+
from __future__ import division
20+
from __future__ import absolute_import
21+
22+
import os
23+
import shutil
24+
25+
from PySide2 import QtCore
26+
27+
import cuegui.Constants
28+
import cuegui.Logger
29+
30+
logger = cuegui.Logger.getLogger(__file__)
31+
32+
33+
def startup(app_name):
34+
"""
35+
Reads config from disk, restoring default config if necessary.
36+
37+
:param app_name: application window name
38+
:type app_name: str
39+
:return: settings object containing the loaded settings
40+
:rtype: QtCore.QSettings
41+
"""
42+
# read saved config from disk
43+
# copy default config
44+
config_path = "/.%s/config" % app_name.lower()
45+
settings = QtCore.QSettings(QtCore.QSettings.IniFormat, QtCore.QSettings.UserScope, config_path)
46+
logger.info('Reading config file from %s', settings.fileName())
47+
local = settings.fileName()
48+
49+
# If the user has chose to revert the layout. delete the file and copy the default back.
50+
if settings.value('RevertLayout'):
51+
logger.warning('Found RevertLayout flag, will restore default config')
52+
os.remove(local)
53+
54+
# If the config file does not exist, copy over the default
55+
if not os.path.exists(local):
56+
default = os.path.join(cuegui.Constants.DEFAULT_INI_PATH, "%s.ini" % app_name.lower())
57+
logger.warning('Local config file not found at %s', local)
58+
logger.warning('Copying %s to %s', default, local)
59+
os.makedirs(os.path.dirname(local), exist_ok=True)
60+
shutil.copy2(default, local)
61+
settings.sync()
62+
63+
return settings

cuegui/cuegui/Main.py

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,13 @@
2020
from __future__ import print_function
2121
from __future__ import division
2222

23-
import os
24-
import shutil
2523
import signal
2624

2725
from PySide2 import QtCore
2826
from PySide2 import QtGui
2927
from PySide2 import QtWidgets
3028

29+
import cuegui.Config
3130
import cuegui.Constants
3231
import cuegui.Logger
3332
import cuegui.MainWindow
@@ -92,33 +91,11 @@ def startup(app_name, app_version, argv):
9291
QtGui.qApp.threads = []
9392
# pylint: enable=attribute-defined-outside-init
9493

95-
config_path = "/.%s/config" % app_name.lower()
96-
settings = QtCore.QSettings(QtCore.QSettings.IniFormat, QtCore.QSettings.UserScope, config_path)
97-
local = settings.fileName()
98-
# If the user has chose to revert the layout. delete the file and copy the default back.
99-
if settings.value('RevertLayout'):
100-
os.remove(local)
101-
94+
settings = cuegui.Config.startup(app_name)
10295
QtGui.qApp.settings = settings # pylint: disable=attribute-defined-outside-init
10396

10497
cuegui.Style.init()
10598

106-
# If the config file does not exist, copy over the default
107-
# pylint: disable=broad-except
108-
if not os.path.exists(local):
109-
default = os.path.join(cuegui.Constants.DEFAULT_INI_PATH, "%s.ini" % app_name.lower())
110-
logger.warning('Not found: %s\nCopying: %s', local, default)
111-
try:
112-
os.mkdir(os.path.dirname(local))
113-
except Exception as e:
114-
logger.debug(e)
115-
try:
116-
shutil.copy2(default, local)
117-
except Exception as e:
118-
logger.debug(e)
119-
settings.sync()
120-
# pylint: enable=broad-except
121-
12299
mainWindow = cuegui.MainWindow.MainWindow(app_name, app_version, None)
123100
mainWindow.displayStartupNotice()
124101
mainWindow.show()

cuegui/tests/Config_tests.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Copyright Contributors to the OpenCue Project
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
"""Tests for cuegui.Config"""
17+
18+
19+
from __future__ import print_function
20+
from __future__ import division
21+
from __future__ import absolute_import
22+
23+
import os
24+
import shutil
25+
import tempfile
26+
import unittest
27+
28+
from PySide2 import QtCore
29+
30+
import cuegui.Config
31+
32+
33+
CONFIG_INI = '''
34+
[General]
35+
Version=0.14
36+
37+
[CueCommander]
38+
Open=true
39+
Title=CustomWindowTitle
40+
OtherAttr=arbitrary-value
41+
'''
42+
43+
CONFIG_WITH_RESTORE_FLAG = '''
44+
[General]
45+
Version=0.14
46+
RevertLayout=true
47+
48+
[CueCommander]
49+
OtherAttr=arbitrary-value
50+
'''
51+
52+
53+
class ConfigTests(unittest.TestCase):
54+
def setUp(self):
55+
self.config_dir = tempfile.mkdtemp()
56+
QtCore.QSettings.setPath(
57+
QtCore.QSettings.IniFormat, QtCore.QSettings.UserScope, self.config_dir)
58+
59+
def tearDown(self):
60+
shutil.rmtree(self.config_dir)
61+
62+
def test__should_load_user_config(self):
63+
app_name = 'arbitraryapp'
64+
config_file_path = os.path.join(self.config_dir, '.%s' % app_name, 'config.ini')
65+
os.mkdir(os.path.dirname(config_file_path))
66+
with open(config_file_path, 'w') as fp:
67+
fp.write(CONFIG_INI)
68+
69+
settings = cuegui.Config.startup(app_name)
70+
71+
self.assertEqual('0.14', settings.value('Version'))
72+
self.assertEqual('true', settings.value('CueCommander/Open'))
73+
self.assertEqual('CustomWindowTitle', settings.value('CueCommander/Title'))
74+
self.assertEqual('arbitrary-value', settings.value('CueCommander/OtherAttr'))
75+
76+
def test__should_load_default_config(self):
77+
settings = cuegui.Config.startup('CueCommander')
78+
79+
self.assertEqual('false', settings.value('CueCommander/Open'))
80+
self.assertEqual('CueCommander', settings.value('CueCommander/Title'))
81+
self.assertFalse(settings.value('CueCommander/OtherAttr', False))
82+
83+
def test__should_restore_default_config(self):
84+
config_file_path = os.path.join(self.config_dir, '.cuecommander', 'config.ini')
85+
os.mkdir(os.path.dirname(config_file_path))
86+
with open(config_file_path, 'w') as fp:
87+
fp.write(CONFIG_WITH_RESTORE_FLAG)
88+
89+
settings = cuegui.Config.startup('CueCommander')
90+
91+
self.assertEqual('false', settings.value('CueCommander/Open'))
92+
self.assertEqual('CueCommander', settings.value('CueCommander/Title'))
93+
self.assertFalse(settings.value('CueCommander/OtherAttr', False))

0 commit comments

Comments
 (0)