Example #1
0
def show(directory, iterable, quiet, extract, limit, format, interactive):
    '''
    показывает информацию о книге (iterable - список книг, отдаваемый searchCatalog)
    directory - папка с архивами
    '''
    if not iterable:
        print ('{prog}: по данному запросу ничего не найдено'.format(prog=sys.argv[0]), file=sys.stderr)
        
    if not format:
        format = '{firstname} {lastname}  - {title}'
    info = \
    '''
    {title}
    
    Серия: {series}
    Подзаголовок: {subtitle}
    Автор: {lastname}, {firstname} {middlename}
    Язык: {language}
    Год выпуска: {year}
    ID книги: {id}'''
    
    i = 0
    if extract:
        if not os.path.exists(extract):
            os.mkdir(extract)
        
    for dct in iterable:
        if limit >= 0  and i >= limit:
            return 

        archive, file = catalog.findFile(dct['id'], directory)
        if not quiet:
            print(info.format(archive=archive, file=file, **dct))
            if archive and file:
                print ('    Файл: {file} в архиве {archive}'.format(file=file, archive=archive))
            else:
                print('    Файл: не найден')
                
                                
            print()
            print()
        if extract and isinstance(archive, str) and os.path.exists(archive):
            def prompt():
                s = input('Распаковать эту книгу (y/n; д/н) (по умолчанию НЕТ) ->')
                return bool(s in ('y', 'yes', 'д', 'да'))

            if (interactive and prompt()) or not interactive:
                myzip = zipfile.ZipFile(archive)
                myzip.extract(file, extract)
                os.rename(os.path.join(extract, file),
                    os.path.join(extract, ''.join((format.format(**dct), os.path.splitext(file)[1]))))
            
        i += 1
Example #2
0
 def test_findFile(self):
     '''check if findFile works'''
     id = '249564'
     directory = ('/home/pasha/media/books/Флибуста')
     t = catalog.findFile(id, directory)
     self.assertTrue(os.path.exists(t[0]))