From 03b1084c045b15578b4540a3d8bfe207364b327f Mon Sep 17 00:00:00 2001 From: "Nureev.I" Date: Tue, 20 Aug 2024 13:35:27 +0300 Subject: [PATCH] File uploading support --- examples/add_modify_printer.py | 35 ++++++++++++++++++ examples/{print_example.py => print_job.py} | 11 +++--- src/pyipp/serializer.py | 4 +- ...d-printer-with-driver-file-request-000.bin | Bin 0 -> 229 bytes tests/test_serializer.py | 25 +++++++++++++ 5 files changed, 67 insertions(+), 8 deletions(-) create mode 100644 examples/add_modify_printer.py rename examples/{print_example.py => print_job.py} (75%) create mode 100644 tests/fixtures/serializer/add-printer-with-driver-file-request-000.bin diff --git a/examples/add_modify_printer.py b/examples/add_modify_printer.py new file mode 100644 index 00000000..709404e6 --- /dev/null +++ b/examples/add_modify_printer.py @@ -0,0 +1,35 @@ +# pylint: disable=W0621 +"""Asynchronous Python client for IPP.""" +import asyncio +from pathlib import Path + +from pyipp import IPP +from pyipp.enums import IppOperation + + +async def main() -> None: + """Show an example of add or modifying printer on your IP print server.""" + content = Path("/path/to/driver.ppd").read_bytes() + + async with IPP( + host="ipp://127.0.0.1:631/printers/My_New_Printer", + username="", + password="", + ) as ipp: + response = await ipp.execute( + IppOperation.CUPS_ADD_MODIFY_PRINTER, + { + "printer-attributes-tag": { + "device-uri": "socket://192.168.0.12:9100", + "printer-info": "My awesome printer", + "printer-location": "office", + }, + "file": content, + }, + ) + + print(response) + + +if __name__ == "__main__": + asyncio.run(main()) diff --git a/examples/print_example.py b/examples/print_job.py similarity index 75% rename from examples/print_example.py rename to examples/print_job.py index 2f005559..12541254 100644 --- a/examples/print_example.py +++ b/examples/print_job.py @@ -1,18 +1,17 @@ # pylint: disable=W0621 """Asynchronous Python client for IPP.""" import asyncio +from pathlib import Path from pyipp import IPP from pyipp.enums import IppOperation async def main() -> None: + """Show an example of printing on your IP print server.""" + content = Path("/path/to/pdf.pdf").read_bytes() - pdf_file = '/path/to/pdf.pfd' - with open(pdf_file, 'rb') as f: - content = f.read() - - """Show example of executing operation against your IPP print server.""" + # then the printer must be shared if CUPS is used async with IPP("ipp://192.168.1.92:631/ipp/print") as ipp: response = await ipp.execute( IppOperation.PRINT_JOB, @@ -22,7 +21,7 @@ async def main() -> None: "job-name": "My Test Job", "document-format": "application/pdf", }, - 'data': content, + "file": content, }, ) diff --git a/src/pyipp/serializer.py b/src/pyipp/serializer.py index 3a9df1b6..a180cd19 100644 --- a/src/pyipp/serializer.py +++ b/src/pyipp/serializer.py @@ -93,7 +93,7 @@ def encode_dict(data: dict[str, Any]) -> bytes: encoded += struct.pack(">b", IppTag.END.value) - if "data" in data: - encoded += data["data"] + if isinstance(data.get("file"), bytes): + encoded += data["file"] return encoded diff --git a/tests/fixtures/serializer/add-printer-with-driver-file-request-000.bin b/tests/fixtures/serializer/add-printer-with-driver-file-request-000.bin new file mode 100644 index 0000000000000000000000000000000000000000..3be68133b1c884d2b583981e98c655b921470436 GIT binary patch literal 229 zcmYj~OAi4t6os$X0y|;lA4u96gfJ_FU_qLQwbZyqW2Q6OTaSNFJQ6v(@8p~>52FGA zf}YB< z!1KO{<6S0Mj66(}px^EMxb;?#-5PH@@tIDbZt&vJlF|`#_I0LZf>0I@I;DCkL~#a{ i171~#Z%xCd;Th>=^7%W#u None: assert result == load_fixture_binary( "serializer/get-printer-attributes-request-000.bin", ) + + +def test_encode_dict_with_file() -> None: + """Test the encode_dict method with upload file.""" + result = serializer.encode_dict( + { + "version": DEFAULT_PROTO_VERSION, + "operation": IppOperation.CUPS_ADD_MODIFY_PRINTER, + "request-id": 1, + "operation-attributes-tag": { + "attributes-charset": DEFAULT_CHARSET, + "attributes-natural-language": DEFAULT_CHARSET_LANGUAGE, + "printer-uri": "ipp://printer.example.com:632/printers/My_New_Printer", + "requesting-user-name": "PythonIPP", + }, + "printer-attributes-tag": { + "device-uri": "socket://0.0.0.0:9100", + }, + "file": b"*PPD-Adobe:...", + }, + ) + + assert result == load_fixture_binary( + "serializer/add-printer-with-driver-file-request-000.bin", + )