def t(*dummy): image_path = os.path.join(os.path.dirname(__file__), 'data', 'nonexistent.png') meta = xmp.metadata(backend=backend) meta.import_(image_path) with temporary.directory() as tmpdir: os.chmod(tmpdir, 0o000) try: image_path = os.path.join(tmpdir, 'example.png') meta = xmp.metadata(backend=backend) with assert_raises(IOError): meta.import_(image_path) finally: os.chmod(tmpdir, 0o700)
def test_incl(self): path = os.path.join(datadir, 'onebit.bmp') in_image = pil.open(path) [width, height] = in_image.size sjbz_path = os.path.join(datadir, 'onebit.djvu') incl_path = os.path.join(datadir, 'shared_anno.iff') multichunk = djvu.Multichunk(width, height, 100, sjbz=sjbz_path, incl=incl_path) djvu_file = multichunk.save() with temporary.directory() as tmpdir: tmp_djvu_path = os.path.join(tmpdir, 'index.djvu') tmp_incl_path = os.path.join(tmpdir, 'shared_anno.iff') os.link(djvu_file.name, tmp_djvu_path) shutil.copyfile(incl_path, tmp_incl_path) out_image = ddjvu(tmp_djvu_path, fmt='pbm') assert_images_equal(in_image, out_image)
def _test_ocr(engine, layers): if not distutils.spawn.find_executable(engine): raise SkipTest('{cmd} not found'.format(cmd=engine)) remove_logging_handlers('ocrodjvu.') here = os.path.dirname(__file__) here = os.path.abspath(here) path = os.path.join(here, '..', 'data', 'alice.djvu') stdout = io.BytesIO() stderr = io.BytesIO() with temporary.directory() as tmpdir: tmp_path = os.path.join(tmpdir, 'tmp.djvu') with interim(sys, stdout=stdout, stderr=stderr): rc = try_run(ocrodjvu.main, ['', '--engine', engine, '--render', layers, '--save-bundled', tmp_path, path]) assert_multi_line_equal(stderr.getvalue(), '') assert_equal(rc, 0) assert_multi_line_equal(stdout.getvalue(), '')
def test_nonascii_path(): require_locale_encoding('UTF-8') # djvused breaks otherwise remove_logging_handlers('ocrodjvu.') here = os.path.dirname(__file__) here = os.path.abspath(here) path = os.path.join(here, '..', 'data', 'empty.djvu') stdout = io.BytesIO() stderr = io.BytesIO() with temporary.directory() as tmpdir: tmp_path = os.path.join(tmpdir, 'тмп.djvu') os.symlink(path, tmp_path) with interim(sys, stdout=stdout, stderr=stderr): rc = try_run(djvu2hocr.main, ['', tmp_path]) assert_equal(stderr.getvalue(), '') assert_equal(rc, 0) assert_not_equal(stdout.getvalue(), '')
def test_bad_page_id(): remove_logging_handlers('ocrodjvu.') here = os.path.dirname(__file__) here = os.path.abspath(here) path = os.path.join(here, '..', 'data', 'bad-page-id.djvu') stdout = io.BytesIO() stderr = io.BytesIO() with temporary.directory() as tmpdir: out_path = os.path.join(tmpdir, 'tmp.djvu') with interim(sys, stdout=stdout, stderr=stderr): with interim(ocrodjvu, system_encoding='ASCII'): rc = try_run(ocrodjvu.main, [ '', '--engine', '_dummy', '--save-bundled', out_path, path ]) assert_equal(stderr.getvalue(), '') assert_equal(rc, 0) assert_equal(stdout.getvalue(), '')
def test_path(self): path = os.getenv('PATH').split(':') with temporary.directory() as tmpdir: command_name = temporary.name(dir=tmpdir) command_path = os.path.join(tmpdir, command_name) with open(command_path, 'wt') as file: print('#!/bin/sh', file=file) print('printf 42', file=file) os.chmod(command_path, stat.S_IRWXU) path[:0] = [tmpdir] path = ':'.join(path) with interim_environ(PATH=path): child = ipc.Subprocess([command_name], stdout=ipc.PIPE, stderr=ipc.PIPE, ) stdout, stderr = child.communicate() assert_equal(stdout, '42') assert_equal(stderr, '')
def _test_ocr(engine, layers): if not distutils.spawn.find_executable(engine): raise SkipTest('{cmd} not found'.format(cmd=engine)) remove_logging_handlers('ocrodjvu.') here = os.path.dirname(__file__) here = os.path.abspath(here) path = os.path.join(here, '..', 'data', 'alice.djvu') stdout = io.BytesIO() stderr = io.BytesIO() with temporary.directory() as tmpdir: tmp_path = os.path.join(tmpdir, 'tmp.djvu') with interim(sys, stdout=stdout, stderr=stderr): rc = try_run(ocrodjvu.main, [ '', '--engine', engine, '--render', layers, '--save-bundled', tmp_path, path ]) assert_multi_line_equal(stderr.getvalue(), '') assert_equal(rc, 0) assert_multi_line_equal(stdout.getvalue(), '')
def _test_from_file(base_filename, index): base_filename = os.path.join(here, base_filename) test_filename = '{base}.test{i}'.format(base=base_filename, i=index) djvused_filename = base_filename + '.djvused' with open(test_filename, 'rb') as file: commandline = file.readline() expected_output = file.read() args = shlex.split(commandline) assert_equal(args[0], '#') with temporary.directory() as tmpdir: djvu_filename = os.path.join(tmpdir, 'empty.djvu') args += [djvu_filename] shutil.copy( os.path.join(os.path.dirname(__file__), '..', 'data', 'empty.djvu'), djvu_filename) ipc.Subprocess( ['djvused', '-f', djvused_filename, '-s', djvu_filename]).wait() xml_filename = os.path.join(tmpdir, 'output.html') with open(xml_filename, 'w+b') as xml_file: xmllint = ipc.Subprocess(['xmllint', '--format', '-'], stdin=ipc.PIPE, stdout=xml_file) try: with open(os.devnull, 'w') as null: with interim(sys, stdout=xmllint.stdin, stderr=null): with interim(djvu2hocr.logger, handlers=[]): rc = try_run(djvu2hocr.main, args) finally: xmllint.stdin.close() try: xmllint.wait() except ipc.CalledProcessError: # Raising the exception here is likely to hide the real # reason of the failure. pass assert_equal(rc, 0) xml_file.seek(0) output = xml_file.read() assert_multi_line_equal(expected_output, output)
def _test_from_file(base_filename, index): base_filename = os.path.join(here, base_filename) test_filename = '{base}.test{i}'.format(base=base_filename, i=index) djvused_filename = base_filename + '.djvused' with open(test_filename, 'rb') as file: commandline = file.readline() expected_output = file.read() args = shlex.split(commandline) assert_equal(args[0], '#') with temporary.directory() as tmpdir: djvu_filename = os.path.join(tmpdir, 'empty.djvu') args += [djvu_filename] shutil.copy( os.path.join(os.path.dirname(__file__), '..', 'data', 'empty.djvu'), djvu_filename) ipc.Subprocess(['djvused', '-f', djvused_filename, '-s', djvu_filename]).wait() xml_filename = os.path.join(tmpdir, 'output.html') with open(xml_filename, 'w+b') as xml_file: xmllint = ipc.Subprocess(['xmllint', '--format', '-'], stdin=ipc.PIPE, stdout=xml_file) try: with open(os.devnull, 'w') as null: with interim(sys, stdout=xmllint.stdin, stderr=null): with interim(djvu2hocr.logger, handlers=[]): rc = try_run(djvu2hocr.main, args) finally: xmllint.stdin.close() try: xmllint.wait() except ipc.CalledProcessError: # Raising the exception here is likely to hide the real # reason of the failure. pass assert_equal(rc, 0) xml_file.seek(0) output = xml_file.read() assert_multi_line_equal(expected_output, output)