Skip to content
Merged
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
17 changes: 9 additions & 8 deletions src/pyipp/serializer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Data Serializer for IPP."""
from __future__ import annotations

import logging
import random
import struct
from typing import Any
Expand All @@ -9,8 +10,10 @@
from .enums import IppTag
from .tags import ATTRIBUTE_TAG_MAP

_LOGGER = logging.getLogger(__name__)

def construct_attibute_values(tag: IppTag, value: Any) -> bytes:

def construct_attribute_values(tag: IppTag, value: Any) -> bytes:
"""Serialize the attribute values into IPP format."""
byte_str = b""

Expand All @@ -32,13 +35,11 @@ def construct_attribute(name: str, value: Any, tag: IppTag | None = None) -> byt
"""Serialize the attribute into IPP format."""
byte_str = b""

if not tag:
tag = ATTRIBUTE_TAG_MAP.get(name, None)

if not tag:
if not tag and not (tag := ATTRIBUTE_TAG_MAP.get(name, None)):
_LOGGER.debug("Unknown IppTag for %s", name)
return byte_str

if isinstance(value, (list, tuple, set)):
if isinstance(value, (list, tuple, set)): # noqa: UP038
for index, list_value in enumerate(value):
byte_str += struct.pack(">b", tag.value)

Expand All @@ -48,14 +49,14 @@ def construct_attribute(name: str, value: Any, tag: IppTag | None = None) -> byt
else:
byte_str += struct.pack(">h", 0)

byte_str += construct_attibute_values(tag, list_value)
byte_str += construct_attribute_values(tag, list_value)
else:
byte_str = struct.pack(">b", tag.value)

byte_str += struct.pack(">h", len(name))
byte_str += name.encode("utf-8")

byte_str += construct_attibute_values(tag, value)
byte_str += construct_attribute_values(tag, value)

return byte_str

Expand Down
15 changes: 15 additions & 0 deletions src/pyipp/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,33 @@
"document-format": IppTag.MIME_TYPE,
"last-document": IppTag.BOOLEAN,
"copies": IppTag.INTEGER,
"job-cancel-after": IppTag.INTEGER,
"job-hold-until": IppTag.KEYWORD,
"job-k-octets": IppTag.INTEGER,
"job-impressions-completed": IppTag.INTEGER,
"job-media-sheets-completed": IppTag.INTEGER,
"job-originating-host-name": IppTag.NAME,
"job-originating-user-name": IppTag.NAME,
"job-printer-state-message": IppTag.TEXT,
"job-printer-state-reasons": IppTag.KEYWORD,
"job-priority": IppTag.INTEGER,
"number-up": IppTag.INTEGER,
"job-sheets": IppTag.NAME,
"job-uri": IppTag.URI,
"job-state": IppTag.ENUM,
"job-state-reasons": IppTag.KEYWORD,
"job-uuid": IppTag.URI,
"requested-attributes": IppTag.KEYWORD,
"member-uris": IppTag.URI,
"operations-supported": IppTag.ENUM,
"ppd-name": IppTag.NAME,
"printer-state-reason": IppTag.KEYWORD,
"printer-is-shared": IppTag.BOOLEAN,
"printer-error-policy": IppTag.NAME,
"printer-geo-location": IppTag.URI,
"printer-info": IppTag.TEXT,
"printer-organization": IppTag.TEXT,
"printer-organizational-unit": IppTag.TEXT,
"which-jobs": IppTag.KEYWORD,
"my-jobs": IppTag.BOOLEAN,
"purge-jobs": IppTag.BOOLEAN,
Expand All @@ -44,4 +56,7 @@
"finishings": IppTag.ENUM,
"orientation-requested": IppTag.ENUM,
"print-quality": IppTag.ENUM,
"time-at-creation": IppTag.INTEGER,
"time-at-processing": IppTag.INTEGER,
"time-at-completed": IppTag.INTEGER,
}
14 changes: 12 additions & 2 deletions tests/test_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

def test_construct_attibute_values() -> None:
"""Test the __construct_attibute_values method."""
result = serializer.construct_attibute_values(
result = serializer.construct_attribute_values(
IppTag.INTEGER, IppOperation.GET_PRINTER_ATTRIBUTES
)
assert result == b"\x00\x04\x00\x00\x00\x0b"

result = serializer.construct_attibute_values(
result = serializer.construct_attribute_values(
IppTag.ENUM, IppOperation.GET_PRINTER_ATTRIBUTES
)
assert result == b"\x00\x04\x00\x00\x00\x0b"
Expand All @@ -31,6 +31,16 @@ def test_construct_attribute() -> None:
assert result == b"#\x00\x14operations-supported\x00\x04\x00\x00\x00\x0b"


def test_construct_attribute_no_tag_unmapped() -> None:
"""Test the construct_attribute method with no tag and unmapped attribute name."""
result = serializer.construct_attribute(
"no-tag-unmapped",
None,
)

assert result == b""


def test_encode_dict() -> None:
"""Test the encode_dict method."""
result = serializer.encode_dict(
Expand Down