Skip to content

Commit

Permalink
Allow PyRFLX object creation directly from model
Browse files Browse the repository at this point in the history
ref #467
  • Loading branch information
jklmnn committed Oct 19, 2020
1 parent 5aca61b commit 21ff6aa
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ import sys

from rflx.pyrflx import MessageValue, PyRFLX

PYRFLX = PyRFLX(["tests/data/specs/tlv.rflx"])
PYRFLX = PyRFLX.parse(["tests/data/specs/tlv.rflx"])
TLV = PYRFLX["TLV"]


Expand Down
2 changes: 1 addition & 1 deletion examples/apps/icmp_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class ICMPSocket:
def __init__(self) -> None:
pyrflx = PyRFLX(["examples/specs/icmp.rflx"])
pyrflx = PyRFLX.parse(["examples/specs/icmp.rflx"])
self.package_icmp = pyrflx["ICMP"]
self.icmp_data = (
b"\x4a\xfc\x0d\x00\x00\x00\x00\x00\x10\x11\x12\x13\x14\x15\x16\x17"
Expand Down
2 changes: 1 addition & 1 deletion examples/apps/ping/ping.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from rflx.pyrflx import MessageValue, PyRFLX

PYRFLX = PyRFLX(["specs/ipv4.rflx"], True)
PYRFLX = PyRFLX.parse(["specs/ipv4.rflx"], True)
ICMP = PYRFLX["ICMP"]
IP = PYRFLX["IPv4"]

Expand Down
26 changes: 17 additions & 9 deletions rflx/pyrflx/pyrflx.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from pathlib import Path
from typing import Dict, Iterator, List

from rflx.model import Model
from rflx.pyrflx.typevalue import MessageValue
from rflx.specification import Parser

Expand All @@ -13,18 +14,10 @@
class PyRFLX:
def __init__(
self,
files: List[str],
skip_model_verification: bool = False,
model: Model,
skip_message_verification: bool = False,
) -> None:
parser = Parser(skip_model_verification)
self.__packages: Dict[str, Package] = {}

for f in files:
if not Path(f).is_file():
raise FileNotFoundError(f'file not found: "{f}"')
parser.parse(Path(f))
model = parser.create_model()
packages = set(str(m.package) for m in model.messages)
for p in packages:
self.__packages[p] = Package(p)
Expand All @@ -33,6 +26,21 @@ def __init__(
m, model.refinements, skip_message_verification
)

@classmethod
def parse(
cls,
files: List[str],
skip_model_verification: bool = False,
skip_message_verification: bool = False,
) -> "PyRFLX":
parser = Parser(skip_model_verification)
for f in files:
if not Path(f).is_file():
raise FileNotFoundError(f'file not found: "{f}"')
parser.parse(Path(f))
model = parser.create_model()
return cls(model, skip_message_verification)

def __getitem__(self, key: str) -> Package:
return self.__packages[key]

Expand Down
2 changes: 1 addition & 1 deletion tests/data/fixtures/pyrflx.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

@pytest.fixture(name="pyrflx_", scope="session")
def fixture_pyrflx() -> pyrflx.PyRFLX:
return pyrflx.PyRFLX(
return pyrflx.PyRFLX.parse(
[
f"{EX_SPEC_DIR}/ethernet.rflx",
f"{EX_SPEC_DIR}/icmp.rflx",
Expand Down
4 changes: 3 additions & 1 deletion tests/integration/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ class Benchmark:
def __init__(self, specdir: Path) -> None:
print("Loading...")
start = perf_counter()
self.__pyrflx = PyRFLX([str(specdir / "ipv4.rflx"), str(specdir / "icmp.rflx")], True, True)
self.__pyrflx = PyRFLX.parse(
[str(specdir / "ipv4.rflx"), str(specdir / "icmp.rflx")], True, True
)
self.__ipv4 = self.__pyrflx["IPv4"]
self.__icmp = self.__pyrflx["ICMP"]
print(f"Loaded in {perf_counter() - start} seconds")
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/pyrflx_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def test_imported_literals(tmp_path: Path) -> None:
"""
)

pyrflx_ = PyRFLX([str(tmp_path / "test.rflx")])
pyrflx_ = PyRFLX.parse([str(tmp_path / "test.rflx")])
m = pyrflx_["Test"]["Message"]

m.set("A", "E1")
Expand Down Expand Up @@ -246,7 +246,7 @@ def test_no_verification_ethernet(ethernet_frame_value: MessageValue) -> None:
ethernet_frame_value.set("Type_Length", int("0800", 16))
ethernet_frame_value.set("Payload", payload)
assert ethernet_frame_value.valid_message
pyrflx_ = PyRFLX(
pyrflx_ = PyRFLX.parse(
[f"{EX_SPEC_DIR}/ethernet.rflx"],
skip_model_verification=True,
skip_message_verification=True,
Expand All @@ -273,7 +273,7 @@ def test_no_verification_array_nested_messages(
array_message_value.set("Bar", foos)
assert array_message_value.valid_message

pyrflx_ = PyRFLX(
pyrflx_ = PyRFLX.parse(
[f"{SPEC_DIR}/array_message.rflx"],
skip_model_verification=True,
skip_message_verification=True,
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/pyrflx_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def assert_bytestring_error(msg: MessageValue) -> None:

def test_file_not_found(tmp_path: Path) -> None:
with pytest.raises(FileNotFoundError):
PyRFLX([f"{tmp_path}/test.rflx"])
PyRFLX.parse([f"{tmp_path}/test.rflx"])


def test_package_name() -> None:
Expand Down Expand Up @@ -72,14 +72,14 @@ def test_pyrflx_iterator(pyrflx_: PyRFLX) -> None:


def test_attributes(pyrflx_: PyRFLX) -> None:
pyrflx_ = PyRFLX([f"{SPEC_DIR}/tlv.rflx"])
pyrflx_ = PyRFLX.parse([f"{SPEC_DIR}/tlv.rflx"])
assert isinstance(pyrflx_["TLV"], Package)
tlv_package = pyrflx_["TLV"]
assert isinstance(tlv_package["Message"], MessageValue)


def test_no_verification(icmp_message_value: MessageValue) -> None:
pyrflx_ = PyRFLX(
pyrflx_ = PyRFLX.parse(
[f"{EX_SPEC_DIR}/icmp.rflx"], skip_model_verification=True, skip_message_verification=True
)
icmp_message_value_unv = pyrflx_["ICMP"]["Message"]
Expand Down

0 comments on commit 21ff6aa

Please sign in to comment.