Clean up directory creation

This commit is contained in:
Tim Bentley 2011-01-01 17:23:24 +00:00
parent 3190599a76
commit f6a8e6884c
5 changed files with 43 additions and 68 deletions

View File

@ -34,7 +34,7 @@ from subprocess import Popen, PIPE
from PyQt4 import QtCore, QtGui
from openlp.core.lib import Receiver
from openlp.core.lib import Receiver, checkDirectoryExists
from openlp.core.resources import qInitResources
from openlp.core.ui.mainwindow import MainWindow
from openlp.core.ui.exceptionform import ExceptionForm
@ -216,7 +216,7 @@ class OpenLP(QtGui.QApplication):
def setNormalCursor(self):
"""
Sets the Normal Cursor forthe Application
Sets the Normal Cursor for the Application
"""
self.restoreOverrideCursor()
@ -243,8 +243,7 @@ def main():
help='Set the Qt4 style (passed directly to Qt4).')
# Set up logging
log_path = AppLocation.get_directory(AppLocation.CacheDir)
if not os.path.exists(log_path):
os.makedirs(log_path)
checkDirectoryExists(log_path)
filename = os.path.join(log_path, u'openlp.log')
logfile = logging.FileHandler(filename, u'w')
logfile.setFormatter(logging.Formatter(

View File

@ -306,6 +306,17 @@ def expand_tags(text):
text = text.replace(tag[u'end tag'], tag[u'end html'])
return text
def checkDirectoryExists(dir):
"""
Check a theme directory exists and if not create it
``dir``
Theme directory to make sure exists
"""
log.debug(u'checkDirectoryExists')
if not os.path.exists(dir):
os.mkdir(dir)
from theme import ThemeLevel, ThemeXML, BackgroundGradientType, \
BackgroundType, HorizontalType, VerticalType
from displaytags import DisplayTags

View File

@ -387,7 +387,6 @@ class SlideController(QtGui.QWidget):
Settings dialog has changed the screen size of adjust output and
screen previews.
"""
log.debug(u'screenSizeChanged live = %s' % self.isLive)
# rebuild display as screen size changed
self.display = MainDisplay(self, self.screens, self.isLive)
self.display.imageManager = self.parent.renderManager.image_manager
@ -403,7 +402,6 @@ class SlideController(QtGui.QWidget):
Takes care of the SlidePreview's size. Is called when one of the the
splitters is moved or when the screen size is changed.
"""
log.debug(u'previewSizeChanged live = %s' % self.isLive)
if self.ratio < float(self.PreviewFrame.width()) / float(
self.PreviewFrame.height()):
# We have to take the height as limit.

View File

@ -36,7 +36,7 @@ from openlp.core.ui import FileRenameForm, ThemeForm
from openlp.core.theme import Theme
from openlp.core.lib import OpenLPToolbar, ThemeXML, get_text_file_string, \
build_icon, Receiver, SettingsManager, translate, check_item_selected, \
BackgroundType, BackgroundGradientType
BackgroundType, BackgroundGradientType, checkDirectoryExists
from openlp.core.utils import AppLocation, get_filesystem_encoding
log = logging.getLogger(__name__)
@ -125,9 +125,9 @@ class ThemeManager(QtGui.QWidget):
# Variables
self.themelist = []
self.path = AppLocation.get_section_data_path(self.settingsSection)
self.checkDirectoryExists(self.path)
checkDirectoryExists(self.path)
self.thumbPath = os.path.join(self.path, u'thumbnails')
self.checkDirectoryExists(self.thumbPath)
checkDirectoryExists(self.thumbPath)
self.themeForm.path = self.path
self.oldBackgroundImage = None
# Last little bits of setting up
@ -457,17 +457,6 @@ class ThemeManager(QtGui.QWidget):
else:
return self._createThemeFromXml(xml, self.path)
def checkDirectoryExists(self, dir):
"""
Check a theme directory exists and if not create it
``dir``
Theme directory to make sure exists
"""
log.debug(u'check themes')
if not os.path.exists(dir):
os.mkdir(dir)
def unzipTheme(self, filename, dir):
"""
Unzip the theme, remove the preview file if stored
@ -498,8 +487,7 @@ class ThemeManager(QtGui.QWidget):
theme_dir = None
if osfile.endswith(os.path.sep):
theme_dir = os.path.join(dir, osfile)
if not os.path.exists(theme_dir):
os.mkdir(os.path.join(dir, osfile))
checkDirectoryExists(theme_dir)
else:
fullpath = os.path.join(dir, osfile)
names = osfile.split(os.path.sep)
@ -509,8 +497,7 @@ class ThemeManager(QtGui.QWidget):
themename = names[0]
if theme_dir is None:
theme_dir = os.path.join(dir, names[0])
if not os.path.exists(theme_dir):
os.mkdir(os.path.join(dir, names[0]))
checkDirectoryExists(theme_dir)
if os.path.splitext(ucsfile)[1].lower() in [u'.xml']:
xml_data = zip.read(file)
try:
@ -529,16 +516,19 @@ class ThemeManager(QtGui.QWidget):
theme = self._createThemeFromXml(filexml, self.path)
self.generateAndSaveImage(dir, themename, theme)
else:
QtGui.QMessageBox.critical(self,
translate('OpenLP.ThemeManager', 'Error'),
translate('OpenLP.ThemeManager',
'File is not a valid theme.'))
Receiver.send_message(u'openlp_error_message', {
u'title': translate('OpenLP.ThemeManager', \
'Validation Error'),
u'message':translate('OpenLP.ThemeManager',
'File is not a valid theme.')})
log.exception(u'Theme file does not contain XML data %s' %
filename)
except (IOError, NameError):
QtGui.QMessageBox.critical(self,
translate('OpenLP.ThemeManager', 'Error'),
translate('OpenLP.ThemeManager', 'File is not a valid theme.'))
Receiver.send_message(u'openlp_error_message', {
u'title': translate('OpenLP.ThemeManager', \
'Validation Error'),
u'message':translate('OpenLP.ThemeManager',
'File is not a valid theme.')})
log.exception(u'Importing theme from zip failed %s' % filename)
finally:
if zip:
@ -571,11 +561,11 @@ class ThemeManager(QtGui.QWidget):
"""
theme_dir = os.path.join(self.path, themeName)
if os.path.exists(theme_dir):
QtGui.QMessageBox.critical(self,
translate('OpenLP.ThemeManager', 'Theme Exists'),
translate('OpenLP.ThemeManager',
'A theme with this name already exists.'),
(QtGui.QMessageBox.Ok), QtGui.QMessageBox.Ok)
Receiver.send_message(u'openlp_error_message', {
u'title': translate('OpenLP.ThemeManager', \
'Validation Error'),
u'message':translate('OpenLP.ThemeManager',
'A theme with this name already exists.')})
return False
return True
@ -588,8 +578,7 @@ class ThemeManager(QtGui.QWidget):
theme_pretty_xml = theme.extract_formatted_xml()
log.debug(u'saveTheme %s %s', name, theme_pretty_xml)
theme_dir = os.path.join(self.path, name)
if not os.path.exists(theme_dir):
os.mkdir(os.path.join(self.path, name))
checkDirectoryExists(theme_dir)
theme_file = os.path.join(theme_dir, name + u'.xml')
if imageTo and self.oldBackgroundImage and \
imageTo != self.oldBackgroundImage:
@ -699,34 +688,13 @@ class ThemeManager(QtGui.QWidget):
if testPlugin:
for plugin in self.parent.pluginManager.plugins:
if plugin.usesTheme(theme):
QtGui.QMessageBox.critical(self,
translate('OpenLP.ThemeManager', 'Error'),
unicode(translate('OpenLP.ThemeManager',
'Theme %s is used in the %s plugin.')) % \
(theme, plugin.name))
Receiver.send_message(u'openlp_error_message', {
u'title': translate('OpenLP.ThemeManager', \
'Validation Error'),
u'message': unicode(translate('OpenLP.ThemeManager',
'Theme %s is used in the %s plugin.')) % \
(theme, plugin.name)})
return False
if unicode(self.serviceComboBox.currentText()) == theme:
QtGui.QMessageBox.critical(self,
translate('OpenLP.ThemeManager', 'Error'),
translate('OpenLP.ThemeManager',
'You are unable to delete the default theme.'))
return False
else:
if testPlugin:
for plugin in self.parent.pluginManager.plugins:
if plugin.usesTheme(theme):
QtGui.QMessageBox.critical(self,
translate('OpenLP.ThemeManager', 'Error'),
unicode(translate('OpenLP.ThemeManager',
'Theme %s is used in the %s plugin.')) % \
(theme, plugin.name))
return False
if unicode(self.serviceComboBox.currentText()) == theme:
QtGui.QMessageBox.critical(self,
translate('OpenLP.ThemeManager', 'Error'),
unicode(translate('OpenLP.ThemeManager',
'Theme %s is used by the service manager.')) % theme)
return False
return True
def _migrateVersion122(self, xml_data):

View File

@ -31,7 +31,7 @@ from PyQt4 import QtCore, QtGui
from openlp.core.lib import MediaManagerItem, BaseListWithDnD, build_icon, \
context_menu_action, ItemCapabilities, SettingsManager, translate, \
check_item_selected, Receiver
check_item_selected, Receiver, checkDirectoryExists
from openlp.core.utils import AppLocation, get_images_filter
log = logging.getLogger(__name__)
@ -79,8 +79,7 @@ class ImageMediaItem(MediaManagerItem):
self.servicePath = os.path.join(
AppLocation.get_section_data_path(self.settingsSection),
u'thumbnails')
if not os.path.exists(self.servicePath):
os.mkdir(self.servicePath)
checkDirectoryExists(self.servicePath)
self.loadList(SettingsManager.load_list(
self.settingsSection, self.settingsSection))