From 3278cb0de301ee542b1fe8245a6c359193373b6b Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Thu, 10 Mar 2022 08:14:46 -0800 Subject: [PATCH] add type hints --- docs/parsers/arp.md | 2 +- jc/parsers/arp.py | 23 ++++++++++++++--------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/docs/parsers/arp.md b/docs/parsers/arp.md index c036899f..a6c9b966 100644 --- a/docs/parsers/arp.md +++ b/docs/parsers/arp.md @@ -127,7 +127,7 @@ Examples: ### parse ```python -def parse(data, raw=False, quiet=False) +def parse(data: str, raw: bool = False, quiet: bool = False) -> List[Dict] ``` Main text parsing function diff --git a/jc/parsers/arp.py b/jc/parsers/arp.py index 7524f265..26203490 100644 --- a/jc/parsers/arp.py +++ b/jc/parsers/arp.py @@ -117,6 +117,7 @@ Examples: } ] """ +from typing import List, Dict, Any import jc.utils import jc.parsers.universal @@ -134,7 +135,7 @@ class info(): __version__ = info.version -def _process(proc_data): +def _process(proc_data: List[Dict]) -> List[Dict]: """ Final processing to conform to the schema. @@ -160,7 +161,11 @@ def _process(proc_data): return proc_data -def parse(data, raw=False, quiet=False): +def parse( + data: str, + raw: bool = False, + quiet: bool = False +) -> List[Dict]: """ Main text parsing function @@ -190,7 +195,7 @@ def parse(data, raw=False, quiet=False): if cleandata[0][-1] == ']': for line in cleandata: splitline = line.split() - output_line = { + output_line: Dict[str, Any] = { 'name': splitline[0], 'address': splitline[1].lstrip('(').rstrip(')'), 'hwtype': splitline[-1].lstrip('[').rstrip(']'), @@ -220,13 +225,13 @@ def parse(data, raw=False, quiet=False): # otherwise, try bsd style else: for line in cleandata: - line = line.split() + splitline = line.split() output_line = { - 'name': line[0], - 'address': line[1].lstrip('(').rstrip(')'), - 'hwtype': line[4].lstrip('[').rstrip(']'), - 'hwaddress': line[3], - 'iface': line[6], + 'name': splitline[0], + 'address': splitline[1].lstrip('(').rstrip(')'), + 'hwtype': splitline[4].lstrip('[').rstrip(']'), + 'hwaddress': splitline[3], + 'iface': splitline[6], } raw_output.append(output_line)