-
-
-
-
-'''
-
def send_message(msg=''):
prints('Notifying calibre of the change')
from calibre.utils.ipc import RC
@@ -130,81 +49,67 @@ def get_db(dbpath, options):
return LibraryDatabase2(dbpath)
def do_list(db, fields, afields, sort_by, ascending, search_text, line_width, separator,
- prefix, output_format, subtitle='Books in the calibre database'):
+ prefix, subtitle='Books in the calibre database'):
if sort_by:
db.sort(sort_by, ascending)
if search_text:
db.search(search_text)
- authors_to_string = output_format in ['stanza', 'text']
- data = db.get_data_as_dict(prefix, authors_as_string=authors_to_string)
+ data = db.get_data_as_dict(prefix, authors_as_string=True)
fields = ['id'] + fields
title_fields = fields
fields = [db.custom_column_label_map[x[1:]]['num'] if x[0]=='*'
else x for x in fields]
- if output_format == 'text':
- for f in data:
- fmts = [x for x in f['formats'] if x is not None]
- f['formats'] = u'[%s]'%u','.join(fmts)
- widths = list(map(lambda x : 0, fields))
- for record in data:
- for f in record.keys():
- if hasattr(record[f], 'isoformat'):
- record[f] = isoformat(record[f], as_utc=False)
- else:
- record[f] = unicode(record[f])
- record[f] = record[f].replace('\n', ' ')
- for i in data:
- for j, field in enumerate(fields):
- widths[j] = max(widths[j], len(unicode(i[field])))
- screen_width = terminal_controller.COLS if line_width < 0 else line_width
- if not screen_width:
- screen_width = 80
- field_width = screen_width//len(fields)
- base_widths = map(lambda x: min(x+1, field_width), widths)
+ for f in data:
+ fmts = [x for x in f['formats'] if x is not None]
+ f['formats'] = u'[%s]'%u','.join(fmts)
+ widths = list(map(lambda x : 0, fields))
+ for record in data:
+ for f in record.keys():
+ if hasattr(record[f], 'isoformat'):
+ record[f] = isoformat(record[f], as_utc=False)
+ else:
+ record[f] = unicode(record[f])
+ record[f] = record[f].replace('\n', ' ')
+ for i in data:
+ for j, field in enumerate(fields):
+ widths[j] = max(widths[j], len(unicode(i[field])))
- while sum(base_widths) < screen_width:
- adjusted = False
- for i in range(len(widths)):
- if base_widths[i] < widths[i]:
- base_widths[i] += min(screen_width-sum(base_widths), widths[i]-base_widths[i])
- adjusted = True
- break
- if not adjusted:
+ screen_width = terminal_controller.COLS if line_width < 0 else line_width
+ if not screen_width:
+ screen_width = 80
+ field_width = screen_width//len(fields)
+ base_widths = map(lambda x: min(x+1, field_width), widths)
+
+ while sum(base_widths) < screen_width:
+ adjusted = False
+ for i in range(len(widths)):
+ if base_widths[i] < widths[i]:
+ base_widths[i] += min(screen_width-sum(base_widths), widths[i]-base_widths[i])
+ adjusted = True
break
+ if not adjusted:
+ break
- widths = list(base_widths)
- titles = map(lambda x, y: '%-*s%s'%(x-len(separator), y, separator),
- widths, title_fields)
- print terminal_controller.GREEN + ''.join(titles)+terminal_controller.NORMAL
+ widths = list(base_widths)
+ titles = map(lambda x, y: '%-*s%s'%(x-len(separator), y, separator),
+ widths, title_fields)
+ print terminal_controller.GREEN + ''.join(titles)+terminal_controller.NORMAL
- wrappers = map(lambda x: TextWrapper(x-1), widths)
- o = cStringIO.StringIO()
+ wrappers = map(lambda x: TextWrapper(x-1), widths)
+ o = cStringIO.StringIO()
- for record in data:
- text = [wrappers[i].wrap(unicode(record[field]).encode('utf-8')) for i, field in enumerate(fields)]
- lines = max(map(len, text))
- for l in range(lines):
- for i, field in enumerate(text):
- ft = text[i][l] if l < len(text[i]) else ''
- filler = '%*s'%(widths[i]-len(ft)-1, '')
- o.write(ft)
- o.write(filler+separator)
- print >>o
- return o.getvalue()
- elif output_format == 'xml':
- template = MarkupTemplate(XML_TEMPLATE)
- return template.generate(data=data, os=os).render('xml')
- elif output_format == 'stanza':
- data = [i for i in data if i.has_key('fmt_epub')]
- for x in data:
- if isinstance(x['fmt_epub'], unicode):
- x['fmt_epub'] = x['fmt_epub'].encode('utf-8')
- if isinstance(x['cover'], unicode):
- x['cover'] = x['cover'].encode('utf-8')
- template = MarkupTemplate(STANZA_TEMPLATE)
- return template.generate(id="urn:calibre:main", data=data, subtitle=subtitle,
- sep=os.sep, quote=quote, updated=db.last_modified()).render('xml')
+ for record in data:
+ text = [wrappers[i].wrap(unicode(record[field]).encode('utf-8')) for i, field in enumerate(fields)]
+ lines = max(map(len, text))
+ for l in range(lines):
+ for i, field in enumerate(text):
+ ft = text[i][l] if l < len(text[i]) else ''
+ filler = '%*s'%(widths[i]-len(ft)-1, '')
+ o.write(ft)
+ o.write(filler+separator)
+ print >>o
+ return o.getvalue()
def list_option_parser(db=None):
fields = set(FIELDS)
@@ -236,9 +141,6 @@ List the books available in the calibre database.
help=_('The maximum width of a single line in the output. Defaults to detecting screen size.'))
parser.add_option('--separator', default=' ', help=_('The string used to separate fields. Default is a space.'))
parser.add_option('--prefix', default=None, help=_('The prefix for all file paths. Default is the absolute path to the library folder.'))
- of = ['text', 'xml', 'stanza']
- parser.add_option('--output-format', choices=of, default='text',
- help=_('The format in which to output the data. Available choices: %s. Defaults is text.')%of)
return parser
@@ -272,7 +174,7 @@ def command_list(args, dbpath):
return 1
print do_list(db, fields, afields, opts.sort_by, opts.ascending, opts.search, opts.line_width, opts.separator,
- opts.prefix, opts.output_format)
+ opts.prefix)
return 0