removed code; moved clean_filename method to utils

This commit is contained in:
Andreas Preikschat 2011-08-12 16:54:16 +02:00
parent 3f3ab71eeb
commit 11f3a9e13a
4 changed files with 16 additions and 35 deletions

View File

@ -386,6 +386,17 @@ def split_filename(path):
else:
return os.path.split(path)
def clean_filename(filename):
"""
Removes invalid characters from the given ``filename``.
``filename``
The "dirty" file name to clean.
"""
if not isinstance(filename, unicode):
filename = unicode(filename, u'utf-8')
return re.sub(r'[/\\?*|<>\[\]":<>+%]+', u'_', filename).strip(u'_')
def delete_file(file_path_name):
"""
Deletes a file from the system.
@ -492,4 +503,4 @@ from actions import ActionList
__all__ = [u'AppLocation', u'get_application_version', u'check_latest_version',
u'add_actions', u'get_filesystem_encoding', u'LanguageManager',
u'ActionList', u'get_web_page', u'file_is_unicode', u'get_uno_command',
u'get_uno_instance', u'delete_file']
u'get_uno_instance', u'delete_file', u'clean_filename']

View File

@ -39,7 +39,7 @@ from openlp.core.lib.ui import UiStrings, critical_error_message_box
from openlp.core.ui.wizard import OpenLPWizard, WizardStrings
from openlp.core.utils import AppLocation
from openlp.plugins.bibles.lib.db import BibleDB, BibleMeta, OldBibleDB, \
BiblesResourcesDB, clean_filename
BiblesResourcesDB
from openlp.plugins.bibles.lib.http import BSExtract, BGExtract, CWExtract
log = logging.getLogger(__name__)
@ -432,24 +432,6 @@ class BibleUpgradeForm(OpenLPWizard):
continue
# Move bibles to temp dir.
shutil.move(os.path.join(self.path, filename[0]), temp_dir)
version_name = unicode(self.versionNameEdit[number].text())
if not version_name:
critical_error_message_box(UiStrings().EmptyField,
translate('BiblesPlugin.UpgradeWizardForm',
'You need to specify a version name for your Bible.'))
self.versionNameEdit[number].setFocus()
return False
elif os.path.exists(os.path.join(self.path,
clean_filename(version_name))):
critical_error_message_box(
translate('BiblesPlugin.UpgradeWizardForm',
'Bible Exists'),
translate('BiblesPlugin.UpgradeWizardForm',
'This Bible already exists. Please upgrade '
'a different Bible, delete the existing one or '
'uncheck.'))
self.versionNameEdit[number].setFocus()
return False
return True
if self.currentPage() == self.progressPage:
return True

View File

@ -39,7 +39,7 @@ from sqlalchemy.orm.exc import UnmappedClassError
from openlp.core.lib import Receiver, translate
from openlp.core.lib.db import BaseModel, init_db, Manager
from openlp.core.lib.ui import critical_error_message_box
from openlp.core.utils import AppLocation
from openlp.core.utils import AppLocation, clean_filename
log = logging.getLogger(__name__)
@ -63,19 +63,6 @@ class Verse(BaseModel):
"""
pass
def clean_filename(filename):
"""
Clean up the version name of the Bible and convert it into a valid
file name.
``filename``
The "dirty" file name or version name.
"""
if not isinstance(filename, unicode):
filename = unicode(filename, u'utf-8')
filename = re.sub(r'[^\w]+', u'_', filename).strip(u'_')
return filename + u'.sqlite'
def init_schema(url):
"""
Setup a bible database connection and initialise the database schema.
@ -158,7 +145,7 @@ class BibleDB(QtCore.QObject, Manager):
self.name = kwargs[u'name']
if not isinstance(self.name, unicode):
self.name = unicode(self.name, u'utf-8')
self.file = clean_filename(self.name)
self.file = clean_filename(self.name) + u'.sqlite'
if u'file' in kwargs:
self.file = kwargs[u'file']
Manager.__init__(self, u'bibles', init_schema, self.file)

View File

@ -35,6 +35,7 @@ import re
from lxml import etree
from openlp.core.lib import check_directory_exists, Receiver, translate
from openlp.core.utils import clean_filename
from openlp.plugins.songs.lib import OpenLyrics
log = logging.getLogger(__name__)