Skip to content

Commit

Permalink
Parse more enums to python enums (#573)
Browse files Browse the repository at this point in the history
  • Loading branch information
ctalkington committed May 18, 2024
1 parent 406dd32 commit e626e4e
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 58 deletions.
22 changes: 15 additions & 7 deletions src/pyipp/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,11 +312,19 @@ class IppOrientationRequested(IntEnum):


ATTRIBUTE_ENUM_MAP = {
"document-state": IppDocumentState,
"finishings": IppFinishing,
"job-state": IppJobState,
"orientation-requested": IppOrientationRequested,
"printer-state": IppPrinterState,
"print-quality": IppPrintQuality,
"status-code": IppStatus,
"document-state": IppDocumentState, # PWG5100.5
"finishings": IppFinishing, # RFC8011
"finishings-default": IppFinishing, # RFC8011
"finishings-supported": IppFinishing, # RFC8011
"job-state": IppJobState, # RFC8011
"media-source-feed-orientation": IppOrientationRequested, # PWG5100.7
"operations-supported": IppOperation, # RFC8011
"orientation-requested": IppOrientationRequested, # RFC8011
"orientation-requested-default": IppOrientationRequested, # RFC8011
"orientation-requested-supported": IppOrientationRequested, # RFC8011
"printer-state": IppPrinterState, # RFC8011
"print-quality": IppPrintQuality, # RFC8011
"print-quality-default": IppPrintQuality, # RFC8011
"print-quality-supported": IppPrintQuality, # RFC8011
"status-code": IppStatus, # RFC8011
}
29 changes: 17 additions & 12 deletions src/pyipp/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,32 +39,33 @@ def parse_ieee1284_device_id(device_id: str) -> dict[str, str]:
def parse_collection(data: bytes, offset: int) -> tuple[dict[str, Any], int]:
"""Parse member attributes from IPP collection."""
collection_data: dict[str, Any] = {}
member_name: str = "unknown"
member_name: str = ""

_LOGGER.debug("#BeginCollectionOffset: %s", offset)

while struct.unpack_from("b", data, offset)[0] != IppTag.END_COLLECTION.value:
attribute, new_offset = parse_attribute(data, offset)
attribute, next_attr_offset = parse_attribute(data, offset, member_name)

if attribute["tag"] == IppTag.MEMBER_NAME.value:
member_name = attribute["value"]
else:
elif member_name:
collection_data[member_name] = attribute["value"]

offset = new_offset

next_attribute, next_attr_offset = parse_attribute(data, offset)
if next_attribute["tag"] == IppTag.END_COLLECTION.value:
offset = next_attr_offset

_LOGGER.debug("#EndCollectionOffset: %s", new_offset)
_LOGGER.debug("#EndCollectionOffset: %s", offset)

return collection_data, offset
# skip over end of collection marker
_, next_attr_offset = parse_attribute(data, offset)

return collection_data, next_attr_offset


# pylint: disable=R0912,R0915
def parse_attribute( # noqa: PLR0912, PLR0915
data: bytes,
offset: int,
prev_attr_name: str = "",
) -> tuple[dict[str, Any], int]:
"""Parse attribute from IPP data.
Expand All @@ -86,6 +87,8 @@ def parse_attribute( # noqa: PLR0912, PLR0915
attribute["name"] = data[offset:offset_length].decode("utf-8")
offset += attribute["name-length"]

resolved_attr_name = attribute["name"] or prev_attr_name

if attribute["name"]:
_LOGGER.debug(
"Attribute Name: %s (%s)", attribute["name"], hex(attribute["tag"]),
Expand All @@ -104,9 +107,9 @@ def parse_attribute( # noqa: PLR0912, PLR0915

if (
attribute["tag"] == IppTag.ENUM.value
and attribute["name"] in ATTRIBUTE_ENUM_MAP
and resolved_attr_name in ATTRIBUTE_ENUM_MAP
):
enum_class = ATTRIBUTE_ENUM_MAP[attribute["name"]]
enum_class = ATTRIBUTE_ENUM_MAP[resolved_attr_name]
attribute["value"] = enum_class(attribute["value"])

offset += 4
Expand Down Expand Up @@ -287,7 +290,9 @@ def parse( # noqa: PLR0912, PLR0915
attribute_key = "unsupported-attributes"
offset += 1

attribute, new_offset = parse_attribute(raw_data, offset)
attribute, new_offset = parse_attribute(
raw_data, offset, previous_attribute_name,
)

# if attribute has a name -> add it
# if attribute doesn't have a name -> it is part of an array
Expand Down
78 changes: 39 additions & 39 deletions tests/__snapshots__/test_parser.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
'image/urf',
'image/jpeg',
]),
'finishings-default': 3,
'finishings-supported': 3,
'finishings-default': <IppFinishing.NONE: 3>,
'finishings-supported': <IppFinishing.NONE: 3>,
'generated-natural-language-supported': 'de',
'identify-actions-default': list([
'flash',
Expand Down Expand Up @@ -135,7 +135,7 @@
'media-source': 'main',
'media-source-properties': dict({
'media-source-feed-direction': 'long-edge-first',
'media-source-feed-orientation': 5,
'media-source-feed-orientation': <IppOrientationRequested.REVERSE_LANDSCAPE: 5>,
}),
'media-top-margin': 300,
'media-type': 'stationery',
Expand All @@ -152,7 +152,7 @@
'media-source': 'main',
'media-source-properties': dict({
'media-source-feed-direction': 'long-edge-first',
'media-source-feed-orientation': 5,
'media-source-feed-orientation': <IppOrientationRequested.REVERSE_LANDSCAPE: 5>,
}),
'media-top-margin': 300,
'media-type': 'stationery',
Expand All @@ -168,7 +168,7 @@
'media-source': 'main',
'media-source-properties': dict({
'media-source-feed-direction': 'long-edge-first',
'media-source-feed-orientation': 5,
'media-source-feed-orientation': <IppOrientationRequested.REVERSE_LANDSCAPE: 5>,
}),
'media-top-margin': 0,
'media-type': 'stationery',
Expand Down Expand Up @@ -320,20 +320,20 @@
'multiple-operation-time-out': 150,
'natural-language-configured': 'de',
'operations-supported': list([
2,
4,
5,
6,
8,
9,
10,
11,
60,
]),
'orientation-requested-default': 3,
<IppOperation.PRINT_JOB: 2>,
<IppOperation.VALIDATE_JOB: 4>,
<IppOperation.CREATE_JOB: 5>,
<IppOperation.SEND_DOCUMENT: 6>,
<IppOperation.CANCEL_JOB: 8>,
<IppOperation.GET_JOB_ATTRIBUTES: 9>,
<IppOperation.GET_JOBS: 10>,
<IppOperation.GET_PRINTER_ATTRIBUTES: 11>,
<IppOperation.IDENTIFY_PRINTER: 60>,
]),
'orientation-requested-default': <IppOrientationRequested.PORTRAIT: 3>,
'orientation-requested-supported': list([
3,
4,
<IppOrientationRequested.PORTRAIT: 3>,
<IppOrientationRequested.LANDSCAPE: 4>,
]),
'output-bin-default': 'face-up',
'output-bin-supported': 'face-up',
Expand All @@ -355,10 +355,10 @@
]),
'print-content-optimize-default': 'auto',
'print-content-optimize-supported': 'auto',
'print-quality-default': 4,
'print-quality-default': <IppPrintQuality.NORMAL: 4>,
'print-quality-supported': list([
4,
5,
<IppPrintQuality.NORMAL: 4>,
<IppPrintQuality.HIGH: 5>,
]),
'print-scaling-default': 'auto',
'print-scaling-supported': list([
Expand Down Expand Up @@ -481,8 +481,8 @@
'copies',
'sides',
]),
'finishings-default': 3,
'finishings-supported': 3,
'finishings-default': <IppFinishing.NONE: 3>,
'finishings-supported': <IppFinishing.NONE: 3>,
'generated-natural-language-supported': 'en',
'identify-actions-default': 'flash',
'identify-actions-supported': 'flash',
Expand Down Expand Up @@ -766,19 +766,19 @@
'multiple-operation-time-out-action': 'abort-job',
'natural-language-configured': 'en',
'operations-supported': list([
2,
4,
5,
6,
8,
9,
10,
11,
59,
60,
]),
'orientation-requested-default': 3,
'orientation-requested-supported': 3,
<IppOperation.PRINT_JOB: 2>,
<IppOperation.VALIDATE_JOB: 4>,
<IppOperation.CREATE_JOB: 5>,
<IppOperation.SEND_DOCUMENT: 6>,
<IppOperation.CANCEL_JOB: 8>,
<IppOperation.GET_JOB_ATTRIBUTES: 9>,
<IppOperation.GET_JOBS: 10>,
<IppOperation.GET_PRINTER_ATTRIBUTES: 11>,
<IppOperation.CLOSE_JOB: 59>,
<IppOperation.IDENTIFY_PRINTER: 60>,
]),
'orientation-requested-default': <IppOrientationRequested.PORTRAIT: 3>,
'orientation-requested-supported': <IppOrientationRequested.PORTRAIT: 3>,
'output-bin-default': 'face-up',
'output-bin-supported': 'face-up',
'pages-per-minute': 9,
Expand All @@ -795,10 +795,10 @@
]),
'print-content-optimize-default': 'auto',
'print-content-optimize-supported': 'auto',
'print-quality-default': 4,
'print-quality-default': <IppPrintQuality.NORMAL: 4>,
'print-quality-supported': list([
4,
5,
<IppPrintQuality.NORMAL: 4>,
<IppPrintQuality.HIGH: 5>,
]),
'print-scaling-default': 'auto',
'print-scaling-supported': list([
Expand Down

0 comments on commit e626e4e

Please sign in to comment.