From db7e74bcfe75d7139c0e214539deec8fd10ba53f Mon Sep 17 00:00:00 2001 From: Stephan Richter Date: Fri, 28 Feb 2014 23:51:53 -0500 Subject: [PATCH] Added tests for the package. --- .gitignore | 3 +++ src/img2pdf.py | 46 +++++++++++++++++++++----------------- src/tests/__init__.py | 7 ++++++ src/tests/test.jpg | Bin 0 -> 2348 bytes src/tests/test.pdf | Bin 0 -> 3289 bytes src/tests/test.png | Bin 0 -> 1130 bytes src/tests/test_img2pdf.py | 20 +++++++++++++++++ 7 files changed, 55 insertions(+), 21 deletions(-) create mode 100644 .gitignore create mode 100644 src/tests/__init__.py create mode 100644 src/tests/test.jpg create mode 100644 src/tests/test.pdf create mode 100644 src/tests/test.png create mode 100644 src/tests/test_img2pdf.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1d2abaa --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.pyc +build +src/*.egg-info diff --git a/src/img2pdf.py b/src/img2pdf.py index fff4b08..c242b79 100755 --- a/src/img2pdf.py +++ b/src/img2pdf.py @@ -17,10 +17,21 @@ import sys import zlib import argparse import struct -from Pillow import Image +from PIL import Image from datetime import datetime from jp2 import parsejp2 +# XXX: Switch to use logging module. +def debug_out(message, verbose=True): + if verbose: + sys.stderr.write("D: "+message+"\n") + +def error_out(message): + sys.stderr.write("E: "+message+"\n") + +def warning_out(message): + sys.stderr.write("W: "+message+"\n") + def parse(cont, indent=1): if type(cont) is dict: return "<<\n"+"\n".join( @@ -42,8 +53,9 @@ class obj(object): def tostring(self): if self.stream: - return "%d 0 obj " % ( - self.identifier+parse(self.content) + + return ( + "%d 0 obj " % self.identifier + + parse(self.content) + "\nstream\n" + self.stream + "\nendstream\nendobj\n") else: return "%d 0 obj "%self.identifier+parse(self.content)+" endobj\n" @@ -55,7 +67,7 @@ class pdfdoc(object): keywords=None): self.version = version # default pdf version 1.3 now = datetime.now() - objects = [] + self.objects = [] info = {} if title: @@ -188,14 +200,6 @@ def convert(images, dpi, title=None, author=None, creator=None, producer=None, creationdate=None, moddate=None, subject=None, keywords=None, colorspace=None, verbose=False): - def debug_out(message): - if verbose: - sys.stderr.write("D: "+message+"\n") - def error_out(message): - sys.stderr.write("E: "+message+"\n") - def warning_out(message): - sys.stderr.write("W: "+message+"\n") - pdf = pdfdoc(3, title, author, creator, producer, creationdate, moddate, subject, keywords) @@ -216,37 +220,37 @@ def convert(images, dpi, title=None, author=None, creator=None, producer=None, if dpi: ndpi = dpi, dpi - debug_out("input dpi (forced) = %d x %d"%ndpi) + debug_out("input dpi (forced) = %d x %d"%ndpi, verbose) else: ndpi = (96, 96) # TODO: read real dpi - debug_out("input dpi = %d x %d"%ndpi) + debug_out("input dpi = %d x %d"%ndpi, verbose) if colorspace: color = colorspace debug_out("input colorspace (forced) = %s"%(ics)) else: color = ics - debug_out("input colorspace = %s"%(ics)) + debug_out("input colorspace = %s"%(ics), verbose) else: width, height = imgdata.size imgformat = imgdata.format if dpi: ndpi = dpi, dpi - debug_out("input dpi (forced) = %d x %d"%ndpi) + debug_out("input dpi (forced) = %d x %d"%ndpi, verbose) else: ndpi = imgdata.info.get("dpi", (96, 96)) - debug_out("input dpi = %d x %d"%ndpi) + debug_out("input dpi = %d x %d"%ndpi, verbose) if colorspace: color = colorspace - debug_out("input colorspace (forced) = %s"%(color)) + debug_out("input colorspace (forced) = %s"%(color), verbose) else: color = imgdata.mode - debug_out("input colorspace = %s"%(color)) + debug_out("input colorspace = %s"%(color), verbose) - debug_out("width x height = %d x %d"%(width,height)) - debug_out("imgformat = %s"%imgformat) + debug_out("width x height = %d x %d"%(width,height), verbose) + debug_out("imgformat = %s"%imgformat, verbose) # depending on the input format, determine whether to pass the raw # image or the zlib compressed color information diff --git a/src/tests/__init__.py b/src/tests/__init__.py new file mode 100644 index 0000000..8fd6866 --- /dev/null +++ b/src/tests/__init__.py @@ -0,0 +1,7 @@ +import unittest +import test_img2pdf + +def test_suite(): + return unittest.TestSuite(( + unittest.makeSuite(test_img2pdf.TestImg2Pdf), + )) diff --git a/src/tests/test.jpg b/src/tests/test.jpg new file mode 100644 index 0000000000000000000000000000000000000000..2c036e93991fe97cf6e9a6c1a60232f96d0d8d90 GIT binary patch literal 2348 zcmb7Fc{o)28$ajF48si0Y=)@DG%`eX7v*MWFpQJ@5DZoagfj`UN8ZY-7o= z1V9i3tc4dKSO*AZK|bsdA8$$+C*%;tios+Fh5$1F0YMusG^Ef`XcP*GM2U-vilDLL zSS&^ygOQNL!xEA>Nel*-hH(TUiA2Imk!7TbGI%11xKRW|2y>7qF%(LSD1nh6{@*5O z18`_S0|-WdH~_+d2plN54afojV&eq=Wg?;|G*bAA`1i*f2*DtLMSuVVBVYgk-=%%c zf_`J#6|L?y@?9FVj;bIy(<4;@m5k!`Ajclx=M~|ijoqxq5uO301_JFd!I;@ z2tpLlgJ5YgNYW7Yg%)-TqM-E!Ush7v#D{2-+Kn=}V*h@4U}0$Vy#NTVTZ+q0m#BK% zk?Dd9YiwqO2Wl==YsK1=txlpV{io=0rd^%!&#MiuWxpGyw*b>0a%dy=eOmXfciel8 zU9)K_TE6^la9?%Aml#-FU%-v5F4xb|-2K)vE;{k@d_&vGyq)E}2h4}$o{|dOY!;3l z+lXAEI}jFj4v_xfx!;qPv0~YT zT%|1Cd5rW2?sYkN&QZn2PsuMZ2CfWsDm~4`;7Sdkz2zGA^)Je~&E}Wd3Bkdg$DCUf zAf}J%kjAE$D(SqDPEN06BB`q-=4ncSrCE|tSJTP21zYpXB!?9nF< z?(utTjEPOW?3}VID9QB|$Y+{mty4jVRyIK=B)g5ZNM?;sU&6leatmPh_WQS;$Sg?l z=O?(ao5Wg6en=YAbIQe!iuc?InK^TMWg*f<_KzDE$p5aXM~(IwF?P|PoRuS#tP)G{hFE=lrn1m~%w2Y0TG zwAa)P#V$x6sXuDrDSl9m!;+834!S3h%{y-6pKtG`^tI{}kI$ab&Q=bbwI^M4Hktl< z;_b8Ne(bz;ud6 zQO|d=*rTwvE|XGn_CFExhbc6hb_OzEze9&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2!fese{00YuVL_t(|+U=UpOEO&)$G?_P zm|2jd5lKSOB1UTwlA^?rl?xXw+7z^C*{T+8`|*!Nn-E+C7DNeg2W_9OcIkI zF$ofrATfztC}{}*K@i~edJzZ&5DJA5kH?WnBv4aRgQlh?w6(RNr>6(Ky}cM38iLtu zhC-o`o+B&^0Dt8v_q^Xh5Ck?oJI$;0*X!Bb+#EYUKW9=|KgJl7t(b?0hfJr_ zN#2h_{F7yvJv%#t%jLq$%ga}vTCK*!#01=KH#m-iR;z_lsl?OM6K-y9;P?BnwY7!Q z(^Kg>#(qFCq3v(j%E!mY2je(SG?&BSU=I%u-x?nb2HDiq6sxVRm7LGA6Vv1Ii00|; z?q;b}s$e_eaF~sZjF6a>6mxuhTr|(x+8V1ArS@5N=geR*C~EUO|IhfQ_{pCJ*=Mbv z{IpO?Tlr{cXh15J`f6t~87P&N$pcI6v+Tai>;-#%ex`C>wW8DMMD3lO9TKxz85kH4 zwHFo^@c8&hVpb!Qlar$M?d>i4`uec4vV!;bcS?ZDkpPu)U(AN*dF<`&36Lzs6a)c0&m$ZT|It=wXD2Kc3mgsyTrL-kMx$iKlx)$U+&3m~ zZ*Od7W`-%1%5U3MC=|?Yx3kU7O-U9FWLE*o=KA`Y&Ck!X_V)IIUKpp-$>Q<2qdgen0J*QV6q!u(7d`GaPSkZ&RVY+Hkww2m}Jq>-D03kH`DEiF)~RCs-T#ogTbIy#E_ w`uehj{ECKILR!!{lf)!QOoGHDNKD!BADSeKE=>}7g8%>k07*qoM6N<$f=j9dhX4Qo literal 0 HcmV?d00001 diff --git a/src/tests/test_img2pdf.py b/src/tests/test_img2pdf.py new file mode 100644 index 0000000..82bc316 --- /dev/null +++ b/src/tests/test_img2pdf.py @@ -0,0 +1,20 @@ +import datetime +import os +import unittest +import img2pdf + +HERE = os.path.dirname(__file__) +moddate = datetime.datetime(2014, 1, 1) + +class TestImg2Pdf(unittest.TestCase): + def test_jpg2pdf(self): + with open(os.path.join(HERE, 'test.jpg'), 'r') as img_fp: + with open(os.path.join(HERE, 'test.pdf'), 'r') as pdf_fp: + self.assertEqual( + img2pdf.convert([img_fp], 150, + creationdate=moddate, moddate=moddate), + pdf_fp.read()) + + def test_png2pdf(self): + with open(os.path.join(HERE, 'test.png'), 'r') as img_fp: + self.assertRaises(SystemExit, img2pdf.convert, [img_fp], 150)