Skip to content

Commit

Permalink
Code cleanup and formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-bc committed May 1, 2024
1 parent d52fa53 commit d8296fc
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 56 deletions.
36 changes: 14 additions & 22 deletions src/fprime_gds/common/loaders/ch_json_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,30 @@

from fprime_gds.common.templates.ch_template import ChTemplate
from fprime_gds.common.loaders.json_loader import JsonLoader
from enum import Enum

Check notice

Code scanning / CodeQL

Unused import Note

Import of 'Enum' is not used.


class ChJsonLoader(JsonLoader):
"""Class to load python based telemetry channel dictionaries"""

# Field names in the python module files (used to construct dictionaries)
ID_FIELD = "id"
NAME_FIELD = "name"
KIND_FIELD = "kind"
DESC_FIELD = "annotation"
TYPE_FIELD = "type"
FMT_STR_FIELD = "format"
ID = "id"
NAME = "name"
DESC = "annotation"
TYPE = "type"
FMT_STR = "format"
LIMIT_FIELD = "limit"
LIMIT_LOW = "low"
LIMIT_HIGH = "high"
LIMIT_RED = "red"
LIMIT_ORANGE = "orange"
LIMIT_YELLOW = "yellow"

def __init__(self, dict_path: str):
super().__init__(dict_path)

def construct_dicts(self, dict_path: str):
def construct_dicts(self, _):
"""
Constructs and returns python dictionaries keyed on id and name
Args:
path: Path to JSON dictionary file
_: Unused argument (inherited)
Returns:
A tuple with two channel dictionaries (python type dict):
(id_dict, name_dict). The keys should be the channels' id and
Expand All @@ -59,33 +55,29 @@ def construct_dicts(self, dict_path: str):
)

def construct_template_from_dict(self, channel_dict: dict) -> ChTemplate:
component_name = channel_dict[self.NAME_FIELD].split(".")[0]
channel_name = channel_dict[self.NAME_FIELD].split(".")[1]
channel_type = channel_dict["type"]
component_name = channel_dict[self.NAME].split(".")[0]
channel_name = channel_dict[self.NAME].split(".")[1]
channel_type = channel_dict[self.TYPE]
type_obj = self.parse_type(channel_type)
format_str = JsonLoader.preprocess_format_str(
channel_dict.get(self.FMT_STR_FIELD)
)
format_str = JsonLoader.preprocess_format_str(channel_dict.get(self.FMT_STR))

limit_field = channel_dict.get(self.LIMIT_FIELD)
limit_low = limit_field.get(self.LIMIT_LOW) if limit_field else None
limit_high = limit_field.get(self.LIMIT_HIGH) if limit_field else None

limit_low_yellow = limit_low.get(self.LIMIT_YELLOW) if limit_low else None
limit_low_red = limit_low.get(self.LIMIT_RED) if limit_low else None
limit_low_orange = limit_low.get(self.LIMIT_ORANGE) if limit_low else None

limit_high_yellow = limit_high.get(self.LIMIT_YELLOW) if limit_high else None
limit_high_red = limit_high.get(self.LIMIT_RED) if limit_high else None
limit_high_orange = limit_high.get(self.LIMIT_ORANGE) if limit_high else None

return ChTemplate(
channel_dict[self.ID_FIELD],
channel_dict[self.ID],
channel_name,
component_name,
type_obj,
ch_fmt_str=format_str,
ch_desc=channel_dict.get(self.DESC_FIELD),
ch_desc=channel_dict.get(self.DESC),
low_red=limit_low_red,
low_orange=limit_low_orange,
low_yellow=limit_low_yellow,
Expand Down
38 changes: 14 additions & 24 deletions src/fprime_gds/common/loaders/cmd_json_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,22 @@
class CmdJsonLoader(JsonLoader):
"""Class to load xml based command dictionaries"""

MNEMONIC_TAG = "name"
NAME_TAG = "name"
OPCODE_TAG = "opcode"
DESC_TAG = "annotation"
PARAMETERS_TAG = "formalParams"

def __init__(self, dict_path: str):
super().__init__(dict_path)

def construct_dicts(self, path):
def construct_dicts(self, _):
"""
Constructs and returns python dictionaries keyed on id and name
Args:
path: Path to JSON dictionary file
_: Unused argument (inherited)
Returns:
A tuple with two command dictionaries (python type dict):
(id_dict, name_dict). The keys are the events' id and name fields
respectively and the values are CmdTemplate objects
A tuple with two command dictionaries (python type dict):
(id_dict, name_dict). The keys are the events' id and name fields
respectively and the values are CmdTemplate objects
"""

id_dict = {}
name_dict = {}

Expand All @@ -49,26 +45,20 @@ def construct_dicts(self, path):
)

def construct_template_from_dict(self, cmd_dict: dict) -> CmdTemplate:
cmd_name = cmd_dict.get("name")

cmd_name = cmd_dict.get(self.NAME_TAG)
cmd_comp = cmd_name.split(".")[0]
cmd_mnemonic = cmd_name.split(".")[1]

cmd_opcode = cmd_dict.get("opcode")

cmd_desc = cmd_dict.get("annotation")

cmd_opcode = cmd_dict.get(self.OPCODE_TAG)
cmd_desc = cmd_dict.get(self.DESC_TAG)
# Parse Arguments
cmd_args = []
for arg in cmd_dict.get("formalParams", []):
for param in cmd_dict.get(self.PARAMETERS_TAG, []):
cmd_args.append(
(
arg.get("name"),
arg.get("annotation"),
self.parse_type(arg.get("type")),
param.get("name"),
param.get("annotation"),
self.parse_type(param.get("type")),
)
)

return CmdTemplate(
cmd_opcode, cmd_mnemonic, cmd_comp, cmd_args, cmd_desc
)
return CmdTemplate(cmd_opcode, cmd_mnemonic, cmd_comp, cmd_args, cmd_desc)
15 changes: 5 additions & 10 deletions src/fprime_gds/common/loaders/event_json_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,30 @@
@author thomas-bc
"""

from fprime_gds.common.data_types import exceptions
from fprime_gds.common.templates.event_template import EventTemplate
from fprime_gds.common.utils.event_severity import EventSeverity

# Custom Python Modules
from fprime_gds.common.loaders.json_loader import JsonLoader


class EventJsonLoader(JsonLoader):
"""Class to load xml based event dictionaries"""

EVENT_SECT = "events"

COMP_TAG = "component"
NAME_TAG = "name"
ID_TAG = "id"
SEVERITY_TAG = "severity"
FMT_STR_TAG = "format"
DESC_TAG = "annotation"
PARAMETERS_TAG = "formalParams"

def construct_dicts(self, path):
def construct_dicts(self, _):
"""
Constructs and returns python dictionaries keyed on id and name
This function should not be called directly, instead, use
get_id_dict(path) and get_name_dict(path)
Args:
path: Path to the xml dictionary file containing event information
_: Unused argument (inherited)
Returns:
A tuple with two event dictionaries (python type dict):
Expand All @@ -57,7 +52,7 @@ def construct_dicts(self, path):
)

def construct_template_from_dict(self, event_dict: dict):
event_mnemonic = event_dict.get("name")
event_mnemonic = event_dict.get(self.NAME_TAG)
event_comp = event_mnemonic.split(".")[0]
event_name = event_mnemonic.split(".")[1]

Expand All @@ -72,7 +67,7 @@ def construct_template_from_dict(self, event_dict: dict):

# Parse arguments
event_args = []
for arg in event_dict.get("formalParams", []):
for arg in event_dict.get(self.PARAMETERS_TAG, []):
event_args.append(
(
arg.get("name"),
Expand Down

0 comments on commit d8296fc

Please sign in to comment.