Skip to content

Commit

Permalink
backup
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsNotSoftware committed Aug 29, 2024
1 parent c7f2ba1 commit 925c042
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/fcp/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def unwrap(self) -> tuple[None, None]:
def err(self) -> list[str]:
return self.error

def compound(self, result: Result) -> Union[Ok, Error]:
def compound(self, result: Result) -> Union[Ok, Any]:
if isinstance(result, Error):
v1 = self.error
v2 = result.error
Expand Down
2 changes: 1 addition & 1 deletion src/fcp/specs/signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Signal:
name: str
start: Optional[int]
unit: Optional[str]
comment: Optional[Comment]
comment: Optional[Comment] | str
length: Optional[int]
min_value: Optional[float]
max_value: Optional[float]
Expand Down
2 changes: 1 addition & 1 deletion src/fcp/specs/struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Struct:
name: str
signals: list[Signal]
meta: Optional[MetaData]
comment: Comment
comment: Optional[Comment]

def get_name(self) -> str:
return self.name
Expand Down
1 change: 0 additions & 1 deletion src/fcp/specs/v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ class FcpV2:
logs: list[log.Log]
version: str = "1.0"


def add_device(self, device: device.Device) -> None:
self.devices.append(device)

Expand Down
38 changes: 26 additions & 12 deletions src/fcp/v2_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from lark.lexer import Token
from lark.tree import Branch
from serde import from_dict

from .specs import device
from .specs import broadcast
Expand Down Expand Up @@ -145,9 +146,7 @@ def convert_params(params: dict[str, Callable]) -> dict[str, Any]:

values: dict[str, Callable] = {}
for name, value in params.items():

values.update(convertion_table[name](value)) # type: ignore

values.update(convertion_table[name](value)) # type: ignore

return values

Expand Down Expand Up @@ -206,9 +205,14 @@ def field(self, tree: ParseTree) -> Union[Ok, Error]:
meta = get_meta(tree, self) # type: ignore
return Ok(
signal.Signal(
name=name.value, # type: ignore
name=name.value, # type: §ignore
start=None, # type: ignore
unit=None, # type: ignore
length=None, # type: ignore
min_value=None, # type: ignore
max_value=None, # type: ignore
type=type,
field_id=field_id.value, # type: ignore
field_id=field_id, # type: ignore
meta=meta,
comment=comment.value, # type: ignore
**params,
Expand Down Expand Up @@ -578,13 +582,24 @@ def merge(fcp: dict[str, Any], fpi: dict[str, Any]) -> Ok:


def convert(module: dict[str, Any]) -> Ok:
to_list = lambda t, v: [from_dict(t, x) for x in v]

a = v2.FcpV2(
broadcasts=to_list(broadcast.Broadcast, module["broadcast"].values()),
devices=to_list(device.Device, module["device"].values()),
structs=to_list(struct.Struct, module["struct"].values()),
enums=to_list(enum.Enum, module["enum"].values()),
logs=to_list(log.Log, module["log"].values()),
version="3.0",
)

return Ok(
v2.FcpV2(
broadcasts=module["broadcast"].values(),
devices=module["device"].values(),
structs=module["struct"].values(),
enums=module["enum"].values(),
logs=module["log"].values(),
broadcasts=to_list(broadcast.Broadcast, module["broadcast"].values()),
devices=to_list(device.Device, module["device"].values()),
structs=to_list(struct.Struct, module["struct"].values()),
enums=to_list(enum.Enum, module["enum"].values()),
logs=to_list(log.Log, module["log"].values()),
version="3.0",
)
)
Expand Down Expand Up @@ -633,5 +648,4 @@ def get_fcp(fcp: str, fpi: str) -> Union[Ok, Error]:
fpi = deduplicate(resolve_imports(fpi).Q()).Q() # type: ignore

fcp_sources.update(fpi_sources)
return Ok((convert(merge(fcp, fpi).Q()), fcp_sources)) # type: ignore

return Ok((convert(merge(fcp, fpi).Q()), fcp_sources)) # type: ignore
11 changes: 9 additions & 2 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from fcp.specs.v2 import FcpV2
import pytest
import os
import json


from serde import from_dict
from fcp.v2_parser import get_fcp

THIS_DIR = os.path.dirname(os.path.abspath(__file__))
Expand All @@ -10,14 +13,18 @@
@pytest.mark.parametrize("test_name", ["001", "002"])
def test_parser(test_name):
config_dir = os.path.join(THIS_DIR, "configs")
print("config dir", config_dir)
fpi_config = os.path.join(config_dir, "test.fpi")
fcp_config = os.path.join(config_dir, test_name + ".fcp")
result = os.path.join(config_dir, test_name + ".json")

print(">>>", fcp_config)
fcp_v2, sources = get_fcp(fcp_config, fpi_config).unwrap()

fcp_json = fcp_v2.unwrap().to_dict()
print(fcp_v2.unwrap())
fcp_json = from_dict(FcpV2, fcp_v2.unwrap())
result = json.loads(open(result).read())

# print(fcp_json)
# print()
# print(result)
assert fcp_json == result
3 changes: 1 addition & 2 deletions tests/test_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ def test_fcp_v2_init(fcp_v2):
def test_fcp_v2_to_json(fcp_v2):
assert (
to_json(fcp_v2)
== '{"structs":[],"enums":[],"devices":[],"broadcasts":[],"logs":[],"version":"1.0"}'

== '{"structs":[],"enums":[],"devices":[],"broadcasts":[],"logs":[],"version":"3.0"}'
)


Expand Down

0 comments on commit 925c042

Please sign in to comment.