Skip to content

Commit

Permalink
Refactor parser pydoc strings
Browse files Browse the repository at this point in the history
  • Loading branch information
Qonfused committed Jun 21, 2023
1 parent 4157146 commit dc9bb4a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
22 changes: 18 additions & 4 deletions src/parsers/plist.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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:
Expand Down
29 changes: 27 additions & 2 deletions src/parsers/yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit dc9bb4a

Please sign in to comment.