Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support for IPP Collections #513

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/print_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@


async def main() -> None:
"""Show example of executing operation against your IPP print server."""

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."""
async with IPP("ipp://192.168.1.92:631/ipp/print") as ipp:
response = await ipp.execute(
IppOperation.PRINT_JOB,
Expand All @@ -22,7 +22,7 @@ async def main() -> None:
"job-name": "My Test Job",
"document-format": "application/pdf",
},
'data': content,
"data": content,
},
)

Expand Down
41 changes: 41 additions & 0 deletions examples/print_from_tray2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# pylint: disable=W0621
"""Asynchronous Python client for IPP."""
import asyncio

import aiofiles

from pyipp import IPP
from pyipp.enums import IppOperation


async def main() -> None:
"""Print a PDF document with media from tray-2."""

pdf_file = "/path/to/pdf.pdf"
async with aiofiles.open(pdf_file, mode="rb") as file:
content = await file.read()

async with IPP("ipp://192.168.1.92:631/ipp/print") as ipp:
response = await ipp.execute(
IppOperation.PRINT_JOB,
{
"operation-attributes-tag": {
"requesting-user-name": "Me",
"job-name": "My Test Job",
"document-format": "application/pdf",
},
"job-attributes-tag": {
"media-col": {
"media-source": "tray-2",
},
},
"data": content,
},
)

print(response)


if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
51 changes: 50 additions & 1 deletion src/pyipp/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,51 @@ def construct_attribute(name: str, value: Any, tag: IppTag | None = None) -> byt
return byte_str


def encode_collection(name: str, collection: dict[str, Any]) -> bytes:
"""Encode a dict representing an IPP collection as a byte string.

Args:
----
name (str): The name of the collection
collection (dict[str, Any]): The collection contents

Returns:
-------
bytes: A binary string representing the collection
"""
byte_str = b""

byte_str += struct.pack(">b", IppTag.BEGIN_COLLECTION.value)
byte_str += struct.pack(">h", len(name))
byte_str += name.encode("utf-8")
byte_str += struct.pack(">h", 0)

for member_name, value in collection.items():
if isinstance(value, dict):
byte_str += encode_collection(name=member_name, collection=value)
else:
byte_str += struct.pack(">b", IppTag.MEMBER_NAME.value)
byte_str += struct.pack(">h", 0)
byte_str += struct.pack(">h", len(member_name))
byte_str += member_name.encode("utf-8")
if isinstance(value, int):
byte_str += struct.pack(">b", IppTag.INTEGER.value)
byte_str += struct.pack(">h", 0)
byte_str += struct.pack(">h", 4)
byte_str += struct.pack(">h", value)
else:
byte_str += struct.pack(">b", IppTag.KEYWORD.value)
byte_str += struct.pack(">h", 0)
byte_str += struct.pack(">h", len(value))
byte_str += value.encode("utf-8")

byte_str += struct.pack(">b", IppTag.END_COLLECTION.value)
byte_str += struct.pack(">h", 0)
byte_str += struct.pack(">h", 0)

return byte_str


def encode_dict(data: dict[str, Any]) -> bytes:
"""Serialize a dictionary of data into IPP format."""
version = data["version"] or DEFAULT_PROTO_VERSION
Expand All @@ -83,7 +128,11 @@ def encode_dict(data: dict[str, Any]) -> bytes:
encoded += struct.pack(">b", IppTag.JOB.value)

for attr, value in data["job-attributes-tag"].items():
encoded += construct_attribute(attr, value)
encoded += (
encode_collection(attr, value)
if isinstance(value, dict)
else construct_attribute(attr, value)
)

if isinstance(data.get("printer-attributes-tag"), dict):
encoded += struct.pack(">b", IppTag.PRINTER.value)
Expand Down
8 changes: 8 additions & 0 deletions src/pyipp/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,12 @@
"time-at-creation": IppTag.INTEGER,
"time-at-processing": IppTag.INTEGER,
"time-at-completed": IppTag.INTEGER,
"x-dimension": IppTag.INTEGER,
"y-dimension": IppTag.INTEGER,
"media-top-margin": IppTag.INTEGER,
"media-bottom-margin": IppTag.INTEGER,
"media-right-margin": IppTag.INTEGER,
"media-left-margin": IppTag.INTEGER,
"media-source": IppTag.KEYWORD,
"media-type": IppTag.KEYWORD,
}