From dc9bb4a53045c327568e2f70c1be6b6ec906e349 Mon Sep 17 00:00:00 2001 From: Cory Bennett <32466081+Qonfused@users.noreply.github.com> Date: Tue, 20 Jun 2023 22:59:13 -0500 Subject: [PATCH] Refactor parser pydoc strings --- src/parsers/plist.py | 22 ++++++++++++++++++---- src/parsers/yaml.py | 29 +++++++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/src/parsers/plist.py b/src/parsers/plist.py index ed3a48ca1..b6bf29c36 100644 --- a/src/parsers/plist.py +++ b/src/parsers/plist.py @@ -5,9 +5,11 @@ # SPDX-License-Identifier: BSD-3-Clause ## -import re + from base64 import b64encode, b64decode from dateutil.parser import parse +from typing import List, Literal, Tuple +import re from parsers._lib import _updateCursor from parsers.dict import flattenDict, nestedGet, nestedSet @@ -24,11 +26,12 @@ ] } -def parseSerializedTypes(stype: str, value: str): +def parseSerializedTypes(stype: str, + value: str) -> Tuple[str, any] | None: """Parse property list types to Python types. Args: - stype: Property list type. + stype: Property list type (literal). value: Property list value. Returns: @@ -49,7 +52,18 @@ def parseSerializedTypes(stype: str, value: str): except: pass # De-op return entry -def writeSerializedTypes(value, defaults=('dict', None)): +def writeSerializedTypes(value: Tuple[str, any] | any, + defaults: Tuple[str, any] | any=('dict', None)) -> List[str]: + """Parse Python types to property list entries. + + Args: + value: Tuple of type (literal) and value. + defaults: Fallback tuple of type (literal) and value. + + Returns: + A list of property list entries. + """ + # Extract native type and value stype, svalue = defaults if svalue is not None: diff --git a/src/parsers/yaml.py b/src/parsers/yaml.py index 0b706ca83..549cc15c4 100644 --- a/src/parsers/yaml.py +++ b/src/parsers/yaml.py @@ -13,8 +13,30 @@ from parsers.dict import flattenDict, nestedGet, nestedSet +def parseSerializedTypes(stype: str, + value: str) -> Tuple[str, any] | None: + """Parse YAML types to Python types. + + Args: + stype: YAML type (literal). + value: YAML value. + + Returns: + Tuple of parsed type (literal) and value. + """ + raise NotImplementedError() #TODO + def writeSerializedTypes(value: Tuple[str, any] | any, - schema=Literal['annotated', 'yaml']) -> str: + schema=Literal['annotated', 'yaml']) -> Tuple[str, any]: + """Parse Python types to YAML types. + + Args: + value: Tuple of type (literal) and value. + schema: Flag to control output schema. + + Returns: + Tuple of parsed type (literal) and value. + """ # Unpack native types stype, svalue = type(value).__name__, value @@ -108,6 +130,9 @@ def parseYAML(lines: list[str], case 'plist': entry = (tokens[1], ' '.join(tokens[2:])) case 'yaml': entry = ' '.join(tokens[1:]) + # TODO: Parse YAML types to Python types + # entry = parseSerializedTypes(...) + # Extract and validate parent tree level tree = cursor['keys'] while len(tree) >= level / max(1, cursor['indent']): tree.pop(-1) @@ -198,9 +223,9 @@ def writeYAML(lines: list[str]=[], padding = f'{padding[:-2]}- ' # Append value to entry stype, svalue = writeSerializedTypes(value, schema) - indent = max_tree_len - (cursor['indent']*j + len(f"{key}:")) match schema: case 'annotated': + indent = max_tree_len - (cursor['indent']*j + len(f"{key}:")) entry = f'{padding}{key}:{" ".rjust(indent + 1)}{stype} | {svalue}' case 'yaml': entry = f'{padding}{key}: {svalue}'.rstrip()