mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-07 10:14:46 -04:00
Implement #3246 (additional file naming options for "Saving books" tab)
This commit is contained in:
parent
fd2d6bdd3d
commit
779fcd4fe2
@ -758,7 +758,8 @@ class CheckIntegrity(QProgressDialog):
|
||||
|
||||
def __init__(self, db, parent=None):
|
||||
QProgressDialog.__init__(self, parent)
|
||||
self.setCancelButtonText('')
|
||||
self.db = db
|
||||
self.setCancelButton(None)
|
||||
self.setMinimum(0)
|
||||
self.setMaximum(100)
|
||||
self.setWindowTitle(_('Checking database integrity'))
|
||||
|
@ -26,7 +26,8 @@ class AddSave(QTabWidget, Ui_TabWidget):
|
||||
self.removeTab(2)
|
||||
c = config()
|
||||
opts = c.parse()
|
||||
for x in ('asciiize', 'update_metadata', 'save_cover', 'write_opf'):
|
||||
for x in ('asciiize', 'update_metadata', 'save_cover', 'write_opf',
|
||||
'replace_whitespace', 'to_lowercase'):
|
||||
g = getattr(self, 'opt_'+x)
|
||||
g.setChecked(getattr(opts, x))
|
||||
help = '\n'.join(textwrap.wrap(c.get_option(x).help, 75))
|
||||
@ -74,7 +75,8 @@ class AddSave(QTabWidget, Ui_TabWidget):
|
||||
if not self.validate():
|
||||
return False
|
||||
c = config()
|
||||
for x in ('asciiize', 'update_metadata', 'save_cover', 'write_opf'):
|
||||
for x in ('asciiize', 'update_metadata', 'save_cover', 'write_opf',
|
||||
'replace_whitespace', 'to_lowercase'):
|
||||
c.set(x, getattr(self, 'opt_'+x).isChecked())
|
||||
for x in ('formats', 'template', 'timefmt'):
|
||||
c.set(x, unicode(getattr(self, 'opt_'+x).text()).strip())
|
||||
|
@ -77,14 +77,14 @@
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="0" colspan="2">
|
||||
<item row="1" column="0">
|
||||
<widget class="QCheckBox" name="opt_save_cover">
|
||||
<property name="text">
|
||||
<string>Save &cover separately</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="2" column="0" colspan="2">
|
||||
<item row="2" column="0">
|
||||
<widget class="QCheckBox" name="opt_update_metadata">
|
||||
<property name="text">
|
||||
<string>Update &metadata in saved copies</string>
|
||||
@ -163,6 +163,20 @@
|
||||
</layout>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="1">
|
||||
<widget class="QCheckBox" name="opt_replace_whitespace">
|
||||
<property name="text">
|
||||
<string>Replace space with &underscores</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="2" column="1">
|
||||
<widget class="QCheckBox" name="opt_to_lowercase">
|
||||
<property name="text">
|
||||
<string>Change paths to &lowercase</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
</widget>
|
||||
|
@ -530,6 +530,12 @@ an opf file). You can get id numbers from the list command.
|
||||
parser.add_option(switch, default=opt.default,
|
||||
help=opt.help, dest=pref)
|
||||
|
||||
for pref in ('replace_whitespace', 'to_lowercase'):
|
||||
opt = c.get_option(pref)
|
||||
switch = '--'+pref.replace('_', '-')
|
||||
parser.add_option(switch, default=False, action='store_true',
|
||||
help=opt.help)
|
||||
|
||||
opts, args = parser.parse_args(sys.argv[1:]+args)
|
||||
if (len(args) < 2 and not opts.all):
|
||||
parser.print_help()
|
||||
|
@ -6,7 +6,7 @@ __license__ = 'GPL v3'
|
||||
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
|
||||
__docformat__ = 'restructuredtext en'
|
||||
|
||||
import os, traceback, cStringIO
|
||||
import os, traceback, cStringIO, re
|
||||
|
||||
from calibre.utils.config import Config, StringConfig
|
||||
from calibre.utils.filenames import shorten_components_to, supports_long_names, \
|
||||
@ -71,6 +71,10 @@ def config(defaults=None):
|
||||
x('timefmt', default='%b, %Y',
|
||||
help=_('The format in which to display dates. %d - day, %b - month, '
|
||||
'%Y - year. Default is: %b, %Y'))
|
||||
x('to_lowercase', default=False,
|
||||
help=_('Convert paths to lowercase.'))
|
||||
x('replace_whitespace', default=False,
|
||||
help=_('Replace whitespace with underscores.'))
|
||||
return c
|
||||
|
||||
def preprocess_template(template):
|
||||
@ -81,7 +85,9 @@ def preprocess_template(template):
|
||||
template = template.decode(preferred_encoding, 'replace')
|
||||
return template
|
||||
|
||||
def get_components(template, mi, id, timefmt='%b %Y', length=250, sanitize_func=ascii_filename):
|
||||
def get_components(template, mi, id, timefmt='%b %Y', length=250,
|
||||
sanitize_func=ascii_filename, replace_whitespace=False,
|
||||
to_lowercase=False):
|
||||
format_args = dict(**FORMAT_ARGS)
|
||||
if mi.title:
|
||||
format_args['title'] = mi.title
|
||||
@ -113,6 +119,11 @@ def get_components(template, mi, id, timefmt='%b %Y', length=250, sanitize_func=
|
||||
components = [str(id)]
|
||||
components = [x.encode(filesystem_encoding, 'replace') if isinstance(x,
|
||||
unicode) else x for x in components]
|
||||
if to_lowercase:
|
||||
components = [x.lower() for x in components]
|
||||
if replace_whitespace:
|
||||
components = [re.sub(r'\s', '_', x) for x in components]
|
||||
|
||||
return shorten_components_to(length, components)
|
||||
|
||||
|
||||
@ -134,7 +145,9 @@ def save_book_to_disk(id, db, root, opts, length):
|
||||
return True, id, mi.title
|
||||
|
||||
components = get_components(opts.template, mi, id, opts.timefmt, length,
|
||||
ascii_filename if opts.asciiize else sanitize_file_name)
|
||||
ascii_filename if opts.asciiize else sanitize_file_name,
|
||||
to_lowercase=opts.to_lowercase,
|
||||
replace_whitespace=opts.replace_whitespace)
|
||||
base_path = os.path.join(root, *components)
|
||||
base_name = os.path.basename(base_path)
|
||||
dirpath = os.path.dirname(base_path)
|
||||
|
Loading…
x
Reference in New Issue
Block a user