1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-08-08 22:36:48 +02:00

allow parser to deal with null input

This commit is contained in:
Kelly Brazil
2024-12-20 16:31:38 -08:00
parent 8995ac686a
commit 7fda79d841
2 changed files with 56 additions and 44 deletions

View File

@ -90,6 +90,7 @@ from typing import Dict
import jc.utils
from jc.utils import convert_to_int
from jc.exceptions import ParseError
class info():
"""Provides parser metadata (version, author, etc.)"""
@ -115,6 +116,9 @@ def _process(proc_data: Dict) -> Dict:
Returns:
(dict) processed structured data adhering to the schema
"""
if not proc_data:
return {}
# Initialize the processed dictionary
processed = {
"control_name": proc_data.get("control_name", ""),
@ -209,6 +213,8 @@ def parse(
# starts the parsing from here
mapping: Dict = {}
if jc.utils.has_data(data):
# split lines and than work on each line
lines = data.splitlines()
first_line = lines[0].strip()
@ -217,7 +223,7 @@ def parse(
if first_line.startswith("Simple mixer control"):
control_name = first_line.split("'")[1]
else:
raise ValueError("Invalid amixer output format: missing control name.")
raise ParseError("Invalid amixer output format: missing control name.")
# map the control name
mapping["control_name"] = control_name

View File

@ -22,6 +22,12 @@ class AmixerTests(unittest.TestCase):
self.test_files_json = [f'{file}.json' for file in self.TEST_FILES_NAME]
self.test_files_processed_json = [f'{file}-processed.json' for file in self.TEST_FILES_NAME]
def test_amixer_sget_nodata(self):
"""
Test 'amixer' with no data
"""
self.assertEqual(jc.parsers.amixer.parse('', quiet=True), {})
def test_amixer_sget(self):
for file_out, file_json, file_processed_json in zip(self.test_files_out, self.test_files_json,
self.test_files_processed_json):