issue #2959: rename pdfParser.py to PdfParser.py

This commit is contained in:
Dvořák Václav 2018-01-31 00:35:55 +01:00
parent 9be8d669f9
commit c15a0b2fce
4 changed files with 31 additions and 31 deletions

View File

@ -1,5 +1,5 @@
from helper import unittest, PillowTestCase, hopper from helper import unittest, PillowTestCase, hopper
from PIL import Image, pdfParser from PIL import Image, PdfParser
import io import io
import os import os
import os.path import os.path
@ -19,7 +19,7 @@ class TestFilePdf(PillowTestCase):
# Assert # Assert
self.assertTrue(os.path.isfile(outfile)) self.assertTrue(os.path.isfile(outfile))
self.assertGreater(os.path.getsize(outfile), 0) self.assertGreater(os.path.getsize(outfile), 0)
with pdfParser.PdfParser(outfile) as pdf: with PdfParser.PdfParser(outfile) as pdf:
if kwargs.get("append_images", False) or kwargs.get("append", False): if kwargs.get("append_images", False) or kwargs.get("append", False):
self.assertGreater(len(pdf.pages), 1) self.assertGreater(len(pdf.pages), 1)
else: else:
@ -106,10 +106,10 @@ class TestFilePdf(PillowTestCase):
def test_pdf_open(self): def test_pdf_open(self):
# fail on a buffer full of null bytes # fail on a buffer full of null bytes
self.assertRaises(pdfParser.PdfFormatError, pdfParser.PdfParser, buf=bytearray(65536)) self.assertRaises(PdfParser.PdfFormatError, PdfParser.PdfParser, buf=bytearray(65536))
# make an empty PDF object # make an empty PDF object
with pdfParser.PdfParser() as empty_pdf: with PdfParser.PdfParser() as empty_pdf:
self.assertEqual(len(empty_pdf.pages), 0) self.assertEqual(len(empty_pdf.pages), 0)
self.assertEqual(len(empty_pdf.info), 0) self.assertEqual(len(empty_pdf.info), 0)
self.assertFalse(empty_pdf.should_close_buf) self.assertFalse(empty_pdf.should_close_buf)
@ -119,7 +119,7 @@ class TestFilePdf(PillowTestCase):
pdf_filename = self.helper_save_as_pdf("RGB") pdf_filename = self.helper_save_as_pdf("RGB")
# open the PDF file # open the PDF file
with pdfParser.PdfParser(filename=pdf_filename) as hopper_pdf: with PdfParser.PdfParser(filename=pdf_filename) as hopper_pdf:
self.assertEqual(len(hopper_pdf.pages), 1) self.assertEqual(len(hopper_pdf.pages), 1)
self.assertTrue(hopper_pdf.should_close_buf) self.assertTrue(hopper_pdf.should_close_buf)
self.assertTrue(hopper_pdf.should_close_file) self.assertTrue(hopper_pdf.should_close_file)
@ -127,14 +127,14 @@ class TestFilePdf(PillowTestCase):
# read a PDF file from a buffer with a non-zero offset # read a PDF file from a buffer with a non-zero offset
with open(pdf_filename, "rb") as f: with open(pdf_filename, "rb") as f:
content = b"xyzzy" + f.read() content = b"xyzzy" + f.read()
with pdfParser.PdfParser(buf=content, start_offset=5) as hopper_pdf: with PdfParser.PdfParser(buf=content, start_offset=5) as hopper_pdf:
self.assertEqual(len(hopper_pdf.pages), 1) self.assertEqual(len(hopper_pdf.pages), 1)
self.assertFalse(hopper_pdf.should_close_buf) self.assertFalse(hopper_pdf.should_close_buf)
self.assertFalse(hopper_pdf.should_close_file) self.assertFalse(hopper_pdf.should_close_file)
# read a PDF file from an already open file # read a PDF file from an already open file
with open(pdf_filename, "rb") as f: with open(pdf_filename, "rb") as f:
with pdfParser.PdfParser(f=f) as hopper_pdf: with PdfParser.PdfParser(f=f) as hopper_pdf:
self.assertEqual(len(hopper_pdf.pages), 1) self.assertEqual(len(hopper_pdf.pages), 1)
self.assertTrue(hopper_pdf.should_close_buf) self.assertTrue(hopper_pdf.should_close_buf)
self.assertFalse(hopper_pdf.should_close_file) self.assertFalse(hopper_pdf.should_close_file)
@ -149,13 +149,13 @@ class TestFilePdf(PillowTestCase):
def test_pdf_append(self): def test_pdf_append(self):
# make a PDF file # make a PDF file
pdf_filename = self.helper_save_as_pdf("RGB", producer="pdfParser") pdf_filename = self.helper_save_as_pdf("RGB", producer="PdfParser")
# open it, check pages and info # open it, check pages and info
with pdfParser.PdfParser(pdf_filename, mode="r+b") as pdf: with PdfParser.PdfParser(pdf_filename, mode="r+b") as pdf:
self.assertEqual(len(pdf.pages), 1) self.assertEqual(len(pdf.pages), 1)
self.assertEqual(len(pdf.info), 1) self.assertEqual(len(pdf.info), 1)
self.assertEqual(pdf.info.Producer, "pdfParser") self.assertEqual(pdf.info.Producer, "PdfParser")
# append some info # append some info
pdf.info.Title = "abc" pdf.info.Title = "abc"
@ -167,7 +167,7 @@ class TestFilePdf(PillowTestCase):
pdf.write_xref_and_trailer() pdf.write_xref_and_trailer()
# open it again, check pages and info again # open it again, check pages and info again
with pdfParser.PdfParser(pdf_filename) as pdf: with PdfParser.PdfParser(pdf_filename) as pdf:
self.assertEqual(len(pdf.pages), 1) self.assertEqual(len(pdf.pages), 1)
self.assertEqual(len(pdf.info), 6) self.assertEqual(len(pdf.info), 6)
self.assertEqual(pdf.info.Title, "abc") self.assertEqual(pdf.info.Title, "abc")
@ -178,12 +178,12 @@ class TestFilePdf(PillowTestCase):
mode_CMYK.save(pdf_filename, append=True, save_all=True, append_images=[mode_P]) mode_CMYK.save(pdf_filename, append=True, save_all=True, append_images=[mode_P])
# open the PDF again, check pages and info again # open the PDF again, check pages and info again
with pdfParser.PdfParser(pdf_filename) as pdf: with PdfParser.PdfParser(pdf_filename) as pdf:
self.assertEqual(len(pdf.pages), 3) self.assertEqual(len(pdf.pages), 3)
self.assertEqual(len(pdf.info), 6) self.assertEqual(len(pdf.info), 6)
self.assertEqual(pdfParser.decode_text(pdf.info[b"Title"]), "abc") self.assertEqual(PdfParser.decode_text(pdf.info[b"Title"]), "abc")
self.assertEqual(pdf.info.Title, "abc") self.assertEqual(pdf.info.Title, "abc")
self.assertEqual(pdf.info.Producer, "pdfParser") self.assertEqual(pdf.info.Producer, "PdfParser")
self.assertEqual(pdf.info.Keywords, "qw)e\\r(ty") self.assertEqual(pdf.info.Keywords, "qw)e\\r(ty")
self.assertEqual(pdf.info.Subject, u"ghi\uABCD") self.assertEqual(pdf.info.Subject, u"ghi\uABCD")
@ -192,7 +192,7 @@ class TestFilePdf(PillowTestCase):
pdf_filename = self.helper_save_as_pdf("RGB", title="title", author="author", subject="subject", keywords="keywords", creator="creator", producer="producer") pdf_filename = self.helper_save_as_pdf("RGB", title="title", author="author", subject="subject", keywords="keywords", creator="creator", producer="producer")
# open it, check pages and info # open it, check pages and info
with pdfParser.PdfParser(pdf_filename) as pdf: with PdfParser.PdfParser(pdf_filename) as pdf:
self.assertEqual(len(pdf.info), 6) self.assertEqual(len(pdf.info), 6)
self.assertEqual(pdf.info.Title, "title") self.assertEqual(pdf.info.Title, "title")
self.assertEqual(pdf.info.Author, "author") self.assertEqual(pdf.info.Author, "author")

View File

@ -1,6 +1,6 @@
from helper import unittest, PillowTestCase from helper import unittest, PillowTestCase
from PIL.pdfParser import IndirectObjectDef, IndirectReference, PdfBinary, PdfDict, PdfFormatError, PdfName, PdfParser, PdfStream, decode_text, encode_text, pdf_repr from PIL.PdfParser import IndirectObjectDef, IndirectReference, PdfBinary, PdfDict, PdfFormatError, PdfName, PdfParser, PdfStream, decode_text, encode_text, pdf_repr
class TestPdfParser(PillowTestCase): class TestPdfParser(PillowTestCase):

View File

@ -20,7 +20,7 @@
# Image plugin for PDF images (output only). # Image plugin for PDF images (output only).
## ##
from . import Image, ImageFile, ImageSequence, pdfParser from . import Image, ImageFile, ImageSequence, PdfParser
import io import io
__version__ = "0.5" __version__ = "0.5"
@ -55,9 +55,9 @@ def _save(im, fp, filename, save_all=False):
producer = im.encoderinfo.get("producer", None) producer = im.encoderinfo.get("producer", None)
if is_appending: if is_appending:
existing_pdf = pdfParser.PdfParser(f=fp, filename=filename, mode="r+b") existing_pdf = PdfParser.PdfParser(f=fp, filename=filename, mode="r+b")
else: else:
existing_pdf = pdfParser.PdfParser(f=fp, filename=filename, mode="w+b") existing_pdf = PdfParser.PdfParser(f=fp, filename=filename, mode="w+b")
if title: if title:
existing_pdf.info.Title = title existing_pdf.info.Title = title
@ -123,26 +123,26 @@ def _save(im, fp, filename, save_all=False):
if im.mode == "1": if im.mode == "1":
filter = "ASCIIHexDecode" filter = "ASCIIHexDecode"
colorspace = pdfParser.PdfName("DeviceGray") colorspace = PdfParser.PdfName("DeviceGray")
procset = "ImageB" # grayscale procset = "ImageB" # grayscale
bits = 1 bits = 1
elif im.mode == "L": elif im.mode == "L":
filter = "DCTDecode" filter = "DCTDecode"
# params = "<< /Predictor 15 /Columns %d >>" % (width-2) # params = "<< /Predictor 15 /Columns %d >>" % (width-2)
colorspace = pdfParser.PdfName("DeviceGray") colorspace = PdfParser.PdfName("DeviceGray")
procset = "ImageB" # grayscale procset = "ImageB" # grayscale
elif im.mode == "P": elif im.mode == "P":
filter = "ASCIIHexDecode" filter = "ASCIIHexDecode"
palette = im.im.getpalette("RGB") palette = im.im.getpalette("RGB")
colorspace = [pdfParser.PdfName("Indexed"), pdfParser.PdfName("DeviceRGB"), 255, pdfParser.PdfBinary(palette)] colorspace = [PdfParser.PdfName("Indexed"), PdfParser.PdfName("DeviceRGB"), 255, PdfParser.PdfBinary(palette)]
procset = "ImageI" # indexed color procset = "ImageI" # indexed color
elif im.mode == "RGB": elif im.mode == "RGB":
filter = "DCTDecode" filter = "DCTDecode"
colorspace = pdfParser.PdfName("DeviceRGB") colorspace = PdfParser.PdfName("DeviceRGB")
procset = "ImageC" # color images procset = "ImageC" # color images
elif im.mode == "CMYK": elif im.mode == "CMYK":
filter = "DCTDecode" filter = "DCTDecode"
colorspace = pdfParser.PdfName("DeviceCMYK") colorspace = PdfParser.PdfName("DeviceCMYK")
procset = "ImageC" # color images procset = "ImageC" # color images
else: else:
raise ValueError("cannot save mode %s" % im.mode) raise ValueError("cannot save mode %s" % im.mode)
@ -175,11 +175,11 @@ def _save(im, fp, filename, save_all=False):
width, height = im.size width, height = im.size
existing_pdf.write_obj(image_refs[pageNumber], stream=op.getvalue(), existing_pdf.write_obj(image_refs[pageNumber], stream=op.getvalue(),
Type=pdfParser.PdfName("XObject"), Type=PdfParser.PdfName("XObject"),
Subtype=pdfParser.PdfName("Image"), Subtype=PdfParser.PdfName("Image"),
Width=width, # * 72.0 / resolution, Width=width, # * 72.0 / resolution,
Height=height, # * 72.0 / resolution, Height=height, # * 72.0 / resolution,
Filter=pdfParser.PdfName(filter), Filter=PdfParser.PdfName(filter),
BitsPerComponent=bits, BitsPerComponent=bits,
DecodeParams=params, DecodeParams=params,
ColorSpace=colorspace) ColorSpace=colorspace)
@ -188,9 +188,9 @@ def _save(im, fp, filename, save_all=False):
# page # page
existing_pdf.write_page(page_refs[pageNumber], existing_pdf.write_page(page_refs[pageNumber],
Resources=pdfParser.PdfDict( Resources=PdfParser.PdfDict(
ProcSet=[pdfParser.PdfName("PDF"), pdfParser.PdfName(procset)], ProcSet=[PdfParser.PdfName("PDF"), PdfParser.PdfName(procset)],
XObject=pdfParser.PdfDict(image=image_refs[pageNumber])), XObject=PdfParser.PdfDict(image=image_refs[pageNumber])),
MediaBox=[0, 0, int(width * 72.0 / resolution), int(height * 72.0 / resolution)], MediaBox=[0, 0, int(width * 72.0 / resolution), int(height * 72.0 / resolution)],
Contents=contents_refs[pageNumber] Contents=contents_refs[pageNumber]
) )
@ -198,7 +198,7 @@ def _save(im, fp, filename, save_all=False):
# #
# page contents # page contents
page_contents = pdfParser.make_bytes( page_contents = PdfParser.make_bytes(
"q %d 0 0 %d 0 0 cm /image Do Q\n" % ( "q %d 0 0 %d 0 0 cm /image Do Q\n" % (
int(width * 72.0 / resolution), int(width * 72.0 / resolution),
int(height * 72.0 / resolution))) int(height * 72.0 / resolution)))