예제 #1
0
 def test_clear_cache(self):
     """Test if djvu file contains text."""
     djvu = DjVuFile(self.file_djvu)
     self.assertTrue(djvu.has_text())
     djvu._has_text = False
     self.assertFalse(djvu.has_text())
     self.assertTrue(djvu.has_text(force=True))
예제 #2
0
 def test_unicode_method(self):
     """Test __unicode__() method."""
     djvu = DjVuFile(self.file_djvu)
     expected = "DjVuFile('%s')" % self.file_djvu
     if PY2:
         self.assertEqual(unicode(djvu), expected)
     else:
         self.assertEqual(djvu.__unicode__(), expected)
예제 #3
0
 def test_get_not_existing_page(self):
     """Test error is raised if djvu file has no text."""
     djvu = DjVuFile(self.file_djvu_wo_text)
     self.assertFalse(djvu.has_text())
     self.assertRaises(ValueError, djvu.get_page, 1)
예제 #4
0
 def test_get_not_existing_page_number(self):
     """Test if djvu file contains text."""
     djvu = DjVuFile(self.file_djvu)
     self.assertTrue(djvu.has_text())
     self.assertRaises(ValueError, djvu.get_page, 100)
예제 #5
0
 def test_get_existing_page_number(self):
     """Test if djvu file contains text."""
     djvu = DjVuFile(self.file_djvu)
     self.assertTrue(djvu.has_text())
     txt = djvu.get_page(1)
     self.assertEqual(txt, self.test_txt)
예제 #6
0
 def test_number_of_images(self):
     """Test page number generator."""
     djvu = DjVuFile(self.file_djvu)
     self.assertEqual(djvu.number_of_images(), 4)
예제 #7
0
 def test_get_existing_page_number(self):
     """Test text is returned for existing page number."""
     djvu = DjVuFile(self.file_djvu)
     self.assertTrue(djvu.has_text())
     txt = djvu.get_page(1)
     self.assertEqual(txt, self.test_txt)
예제 #8
0
 def test_get_most_common_info(self):
     """Test that most common (size, dpi) are returned."""
     djvu = DjVuFile(self.file_djvu)
     self.assertEqual(djvu.get_most_common_info(), ('1092x221', 600))
예제 #9
0
 def test_number_of_images(self):
     """Test page number generator."""
     djvu = DjVuFile(self.file_djvu)
     self.assertEqual(djvu.number_of_images(), 4)
예제 #10
0
 def test_get_most_common_info(self):
     """Test page number generator."""
     djvu = DjVuFile(self.file_djvu)
     self.assertEqual(djvu.get_most_common_info(), ('1092x221', 600))
예제 #11
0
 def test_page_info(self):
     """Test page info retrieval."""
     djvu = DjVuFile(self.file_djvu)
     self.assertEqual(djvu.page_info(1),
                      ('{myfile.djvu}', ('1092x221', 600)))
예제 #12
0
 def test_get_most_common_info(self):
     """Test that most common (size, dpi) are returned."""
     djvu = DjVuFile(self.file_djvu)
     self.assertEqual(djvu.get_most_common_info(), ('1092x221', 600))
예제 #13
0
 def test_get_not_existing_page_number(self):
     """Test error is raised if djvu page number is out of range."""
     djvu = DjVuFile(self.file_djvu)
     self.assertTrue(djvu.has_text())
     self.assertRaises(ValueError, djvu.get_page, 100)
예제 #14
0
 def test_get_existing_page_number(self):
     """Test text is returned for existing page number."""
     djvu = DjVuFile(self.file_djvu)
     self.assertTrue(djvu.has_text())
     txt = djvu.get_page(1)
     self.assertEqual(txt, self.test_txt)
예제 #15
0
 def test_delete_not_existing_page_number(self):
     """Test djvu page cannot be deleted if page number is out of range."""
     djvu = DjVuFile(self.file_djvu)
     self.assertRaises(ValueError, djvu.delete_page, 100)
예제 #16
0
def main(*args: str) -> None:
    """
    Process command line arguments and invoke bot.

    If args is an empty list, sys.argv is used.

    :param args: command line arguments
    """
    index = None
    djvu_path = '.'  # default djvu file directory
    pages = '1-'
    options = {}

    # Parse command line arguments.
    local_args = pywikibot.handle_args(args)
    for arg in local_args:
        opt, _, value = arg.partition(':')
        if opt == '-index':
            index = value
        elif opt == '-djvu':
            djvu_path = value
        elif opt == '-pages':
            pages = value
        elif opt == '-summary':
            options['summary'] = value
        elif opt in ('-force', '-always'):
            options[opt[1:]] = True
        else:
            pywikibot.output('Unknown argument ' + arg)

    # index is mandatory.
    if not index:
        pywikibot.bot.suggest_help(missing_parameters=['-index'])
        return

    # If djvu_path is not a file, build djvu_path from dir+index.
    djvu_path = os.path.expanduser(djvu_path)
    djvu_path = os.path.abspath(djvu_path)
    if not os.path.exists(djvu_path):
        pywikibot.error('No such file or directory: ' + djvu_path)
        return

    if os.path.isdir(djvu_path):
        djvu_path = os.path.join(djvu_path, index)

    # Check the djvu file exists and, if so, create the DjVuFile wrapper.
    djvu = DjVuFile(djvu_path)

    if not djvu.has_text():
        pywikibot.error('No text layer in djvu file {}'.format(djvu.file))
        return

    # Parse pages param.
    pages = pages.split(',')
    for i, page_interval in enumerate(pages):
        start, sep, end = page_interval.partition('-')
        start = 1 if not start else int(start)
        if not sep:
            end = start
        else:
            end = int(end) if end else djvu.number_of_images()
        pages[i] = (start, end)

    site = pywikibot.Site()
    if not site.has_extension('ProofreadPage'):
        pywikibot.error('Site {} must have ProofreadPage extension.'
                        .format(site))
        return

    index_page = pywikibot.Page(site, index, ns=site.proofread_index_ns)

    if not index_page.exists():
        raise NoPageError(index)

    pywikibot.output('uploading text from {} to {}'
                     .format(djvu.file, index_page.title(as_link=True)))

    bot = DjVuTextBot(djvu, index_page, pages=pages, site=site, **options)
    bot.run()
예제 #17
0
 def test_file_existence(self):
     """Test file existence checks."""
     djvu = DjVuFile(self.file_djvu)
     self.assertEqual(os.path.abspath(self.file_djvu), djvu.file)
     self.assertRaises(IOError, DjVuFile, self.file_djvu_not_existing)
예제 #18
0
 def test_whiten_not_existing_page_number(self):
     """Test djvu page cannot be whitend if page number is out of range."""
     djvu = DjVuFile(self.file_djvu)
     with self.assertRaises(ValueError):
         djvu.whiten_page(100)
예제 #19
0
 def test_page_info(self):
     """Test page info retrieval."""
     djvu = DjVuFile(self.file_djvu)
     self.assertEqual(djvu.page_info(1),
                      ('{myfile.djvu}', ('1092x221', 600)))
예제 #20
0
 def test_repr_method(self):
     """Test __repr__() method."""
     djvu = DjVuFile(self.file_djvu)
     expected = "pywikibot.tools.djvu.DjVuFile('{}')".format(self.file_djvu)
     self.assertEqual(repr(djvu), expected)
예제 #21
0
 def test_has_text(self):
     """Test if djvu file contains text."""
     djvu = DjVuFile(self.file_djvu)
     self.assertTrue(djvu.has_text())
     djvu = DjVuFile(self.file_djvu_wo_text)
     self.assertFalse(djvu.has_text())
예제 #22
0
 def test_str_method(self):
     """Test __str__() method."""
     djvu = DjVuFile(self.file_djvu)
     expected = "DjVuFile('{}')".format(self.file_djvu)
     self.assertEqual(str(djvu), expected)
예제 #23
0
def main(*args):
    """
    Process command line arguments and invoke bot.

    If args is an empty list, sys.argv is used.

    @param args: command line arguments
    @type args: list of unicode
    """
    index = None
    djvu_path = '.'  # default djvu file directory
    pages = '1-'
    options = {}

    # Parse command line arguments.
    local_args = pywikibot.handle_args(args)
    for arg in local_args:
        if arg.startswith('-index:'):
            index = arg[7:]
        elif arg.startswith('-djvu:'):
            djvu_path = arg[len('-djvu:'):]
        elif arg.startswith('-pages:'):
            pages = arg[7:]
        elif arg.startswith('-summary:'):
            options['summary'] = arg[len('-summary:'):]
        elif arg == '-force':
            options['force'] = True
        elif arg == '-always':
            options['always'] = True
        else:
            pywikibot.output('Unknown argument %s' % arg)

    # index is mandatory.
    if not index:
        pywikibot.bot.suggest_help(missing_parameters=['-index'])
        return False

    # If djvu_path is not a fle, build djvu_path from dir+index.
    djvu_path = os.path.expanduser(djvu_path)
    djvu_path = os.path.abspath(djvu_path)
    if not os.path.exists(djvu_path):
        pywikibot.error('No such file or directory: %s' % djvu_path)
        return False
    if os.path.isdir(djvu_path):
        djvu_path = os.path.join(djvu_path, index)

    # Check the djvu file exists and, if so, create the DjVuFile wrapper.
    djvu = DjVuFile(djvu_path)

    if not djvu.has_text():
        pywikibot.error('No text layer in djvu file %s' % djvu.file_djvu)
        return False

    # Parse pages param.
    pages = pages.split(',')
    for interval in range(len(pages)):
        start, sep, end = pages[interval].partition('-')
        start = 1 if not start else int(start)
        if not sep:
            end = start
        else:
            end = int(end) if end else djvu.number_of_images()
        pages[interval] = (start, end)

    site = pywikibot.Site()
    if not site.has_extension('ProofreadPage'):
        pywikibot.error('Site %s must have ProofreadPage extension.' % site)
        return False

    index_page = pywikibot.Page(site, index, ns=site.proofread_index_ns)

    if not index_page.exists():
        raise pywikibot.NoPage(index)

    pywikibot.output('uploading text from %s to %s'
                     % (djvu.file_djvu, index_page.title(asLink=True)))

    bot = DjVuTextBot(djvu, index_page, pages, **options)
    bot.run()
예제 #24
0
def main(*args):
    """
    Process command line arguments and invoke bot.

    If args is an empty list, sys.argv is used.

    @param args: command line arguments
    @type args: list of unicode
    """
    index = None
    djvu_path = '.'  # default djvu file directory
    pages = '1-'
    options = {}

    # Parse command line arguments.
    local_args = pywikibot.handle_args(args)
    for arg in local_args:
        if arg.startswith('-index:'):
            index = arg[7:]
        elif arg.startswith('-djvu:'):
            djvu_path = arg[len('-djvu:'):]
        elif arg.startswith('-pages:'):
            pages = arg[7:]
        elif arg.startswith('-summary:'):
            options['summary'] = arg[len('-summary:'):]
        elif arg == '-force':
            options['force'] = True
        elif arg == '-always':
            options['always'] = True
        else:
            pywikibot.output('Unknown argument %s' % arg)

    # index is mandatory.
    if not index:
        pywikibot.bot.suggest_help(missing_parameters=['-index'])
        return False

    # If djvu_path is not a fle, build djvu_path from dir+index.
    djvu_path = os.path.expanduser(djvu_path)
    djvu_path = os.path.abspath(djvu_path)
    if not os.path.exists(djvu_path):
        pywikibot.error('No such file or directory: %s' % djvu_path)
        return False
    if os.path.isdir(djvu_path):
        djvu_path = os.path.join(djvu_path, index)

    # Check the djvu file exists and, if so, create the DjVuFile wrapper.
    djvu = DjVuFile(djvu_path)

    if not djvu.has_text():
        pywikibot.error('No text layer in djvu file %s' % djvu.file_djvu)
        return False

    # Parse pages param.
    pages = pages.split(',')
    for interval in range(len(pages)):
        start, sep, end = pages[interval].partition('-')
        start = 1 if not start else int(start)
        if not sep:
            end = start
        else:
            end = int(end) if end else djvu.number_of_images()
        pages[interval] = (start, end)

    site = pywikibot.Site()
    if not site.has_extension('ProofreadPage'):
        pywikibot.error('Site %s must have ProofreadPage extension.' % site)
        return False

    index_page = pywikibot.Page(site, index, ns=site.proofread_index_ns)

    if not index_page.exists():
        raise pywikibot.NoPage(index)

    pywikibot.output('uploading text from %s to %s' %
                     (djvu.file_djvu, index_page.title(asLink=True)))

    bot = DjVuTextBot(djvu, index_page, pages, **options)
    bot.run()
예제 #25
0
 def test_has_text(self):
     """Test if djvu file contains text."""
     djvu = DjVuFile(self.file_djvu)
     self.assertTrue(djvu.has_text())
     djvu = DjVuFile(self.file_djvu_wo_text)
     self.assertFalse(djvu.has_text())
예제 #26
0
 def test_get_not_existing_page_number(self):
     """Test error is raised if djvu page number is out of range."""
     djvu = DjVuFile(self.file_djvu)
     self.assertTrue(djvu.has_text())
     self.assertRaises(ValueError, djvu.get_page, 100)
예제 #27
0
 def test_get_not_existing_page(self):
     """Test if djvu file contains text."""
     djvu = DjVuFile(self.file_djvu_wo_text)
     self.assertFalse(djvu.has_text())
     self.assertRaises(ValueError, djvu.get_page, 100)
예제 #28
0
 def test_get_existing_page_number(self):
     """Test if djvu file contains text."""
     djvu = DjVuFile(self.file_djvu)
     self.assertTrue(djvu.has_text())
     txt = djvu.get_page(1)
     self.assertEqual(txt, self.test_txt)