From 01ddaecb82a38db8ecaf9b4c9db19d91fe10b7b8 Mon Sep 17 00:00:00 2001 From: Benjamin Golder Date: Sat, 12 Mar 2016 13:28:41 -0800 Subject: [PATCH] get_data_fields with integration test --- src/pdftk_wrapper.py | 18 +++++++++++------- tests/integration/test_pdftk.py | 10 +++++++++- tests/unit/test_pdftk.py | 10 ---------- 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/src/pdftk_wrapper.py b/src/pdftk_wrapper.py index db03484..37db095 100644 --- a/src/pdftk_wrapper.py +++ b/src/pdftk_wrapper.py @@ -50,14 +50,14 @@ def _clean_up_tmp_files(self): path = self._tmp_files.pop() os.remove(path) - def _get_file_contents(self, path, decode=False): + def _get_file_contents(self, path, decode=False, encoding=None): """given a file path, return the contents of the file if decode is True, the contents will be decoded using the default encoding """ bytestring = open(path, 'rb').read() if decode: - return bytestring.decode(self.encoding) + return bytestring.decode(encoding or self.encoding) return bytestring def get_fdf(self, fp): @@ -68,14 +68,18 @@ def get_fdf(self, fp): tmp_outfile = self._write_tmp_file() self.run_command([fp, 'generate_fdf', 'output', tmp_outfile]) - return self._get_file_contents( + contents = self._get_file_contents( tmp_outfile, decode=True) - - def get_xfdf(self, fp): - return None + return contents def get_data_fields(self, fp): - return None + fp = self._coerce_to_file_path(fp) + tmp_outfile = self._write_tmp_file() + self.run_command([fp, 'dump_data_fields_utf8', + 'output', tmp_outfile]) + contents = self._get_file_contents( + tmp_outfile, decode=True, encoding='utf-8') + return contents def parse_fdf_fields(self, fdf_str): yield None diff --git a/tests/integration/test_pdftk.py b/tests/integration/test_pdftk.py index 0cf45e0..48e7653 100644 --- a/tests/integration/test_pdftk.py +++ b/tests/integration/test_pdftk.py @@ -17,6 +17,7 @@ def setUp(self): self.tmp_dir = 'data' self.sample_form_path = 'data/sample_pdfs/sample_form.pdf' self.fdf_str_sample = '%FDF-1.2\n%âãÏÓ\n1 0 obj \n<<\n/FDF \n<<\n/Fields [\n<<\n/V /Yes\n/T (Language 2 Check Box)\n>> \n<<\n/V ()\n/T (Address 2 Text Box)\n>> \n<<\n/V /Off\n/T (Language 3 Check Box)\n>> \n<<\n/V ()\n/T (City Text Box)\n>> \n<<\n/V /Off\n/T (Language 1 Check Box)\n>> \n<<\n/V /Off\n/T (Driving License Check Box)\n>> \n<<\n/V ()\n/T (Given Name Text Box)\n>> \n<<\n/V /Off\n/T (Language 5 Check Box)\n>> \n<<\n/V ()\n/T (House nr Text Box)\n>> \n<<\n/V (150)\n/T (Height Formatted Field)\n>> \n<<\n/V ()\n/T (Family Name Text Box)\n>> \n<<\n/V ()\n/T (Address 1 Text Box)\n>> \n<<\n/V /Off\n/T (Language 4 Check Box)\n>> \n<<\n/V ()\n/T (Postcode Text Box)\n>>]\n>>\n>>\nendobj \ntrailer\n\n<<\n/Root 1 0 R\n>>\n%%EOF\n' + self.data_fields_str_sample = '---\nFieldType: Text\nFieldName: Given Name Text Box\nFieldNameAlt: First name\nFieldFlags: 0\nFieldValue: \nFieldJustification: Left\nFieldMaxLength: 40\n---\nFieldType: Text\nFieldName: Family Name Text Box\nFieldNameAlt: Last name\nFieldFlags: 0\nFieldValue: \nFieldJustification: Left\nFieldMaxLength: 40\n---\nFieldType: Text\nFieldName: Address 1 Text Box\nFieldFlags: 0\nFieldValue: \nFieldJustification: Left\nFieldMaxLength: 40\n---\nFieldType: Text\nFieldName: House nr Text Box\nFieldNameAlt: House and floor\nFieldFlags: 0\nFieldValue: \nFieldJustification: Left\nFieldMaxLength: 20\n---\nFieldType: Text\nFieldName: Address 2 Text Box\nFieldFlags: 0\nFieldValue: \nFieldJustification: Left\nFieldMaxLength: 40\n---\nFieldType: Text\nFieldName: Postcode Text Box\nFieldFlags: 0\nFieldValue: \nFieldJustification: Left\nFieldMaxLength: 20\n---\nFieldType: Text\nFieldName: City Text Box\nFieldFlags: 0\nFieldValue: \nFieldJustification: Left\nFieldMaxLength: 40\n---\nFieldType: Text\nFieldName: Height Formatted Field\nFieldNameAlt: Value from 40 to 250 cm\nFieldFlags: 0\nFieldValue: 150\nFieldValueDefault: 150\nFieldJustification: Left\nFieldMaxLength: 20\n---\nFieldType: Button\nFieldName: Driving License Check Box\nFieldNameAlt: Car driving license\nFieldFlags: 0\nFieldValue: Off\nFieldJustification: Left\nFieldStateOption: Off\nFieldStateOption: Yes\n---\nFieldType: Button\nFieldName: Language 1 Check Box\nFieldFlags: 0\nFieldValue: Off\nFieldJustification: Left\nFieldStateOption: Off\nFieldStateOption: Yes\n---\nFieldType: Button\nFieldName: Language 2 Check Box\nFieldFlags: 0\nFieldValue: Yes\nFieldJustification: Left\nFieldStateOption: Off\nFieldStateOption: Yes\n---\nFieldType: Button\nFieldName: Language 3 Check Box\nFieldFlags: 0\nFieldValue: Off\nFieldJustification: Left\nFieldStateOption: Off\nFieldStateOption: Yes\n---\nFieldType: Button\nFieldName: Language 4 Check Box\nFieldFlags: 0\nFieldValue: Off\nFieldJustification: Left\nFieldStateOption: Off\nFieldStateOption: Yes\n---\nFieldType: Button\nFieldName: Language 5 Check Box\nFieldFlags: 0\nFieldValue: Off\nFieldJustification: Left\nFieldStateOption: Off\nFieldStateOption: Yes\n' def test_pdftk_errors(self): pdftk = PDFTKWrapper() @@ -32,11 +33,18 @@ def test_pdftk_errors(self): with self.assertRaises(PdftkError): pdftk.run_command(args) - def test_generate_fdf(self): + def test_get_fdf(self): pdftk = PDFTKWrapper() results = pdftk.get_fdf(self.sample_form_path) self.assertEqual(results, self.fdf_str_sample) + def test_get_data_fields(self): + pdftk = PDFTKWrapper() + results = pdftk.get_data_fields(self.sample_form_path) + self.assertEqual(results, self.data_fields_str_sample) + + + diff --git a/tests/unit/test_pdftk.py b/tests/unit/test_pdftk.py index cdb54e2..228ccaf 100644 --- a/tests/unit/test_pdftk.py +++ b/tests/unit/test_pdftk.py @@ -33,16 +33,6 @@ def test_get_fdf(self): contents_getter.assert_called_once_with( 'tmp_path.fdf', decode=True) - def test_get_xfdf(self): - pdftk = PDFTKWrapper() - fp = 'something.pdf' - results = pdftk.get_xfdf(fp) - - def test_get_data_fields(self): - pdftk = PDFTKWrapper() - fp = 'something.pdf' - results = pdftk.get_data_fields(fp) - def test_parse_fdf_fields(self): pdftk = PDFTKWrapper() fdf_sample = """