fprime-gds 4.0.0a7__py3-none-any.whl → 4.0.0a9__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,229 +0,0 @@
1
- from __future__ import annotations
2
- from dataclasses import astuple
3
- import inspect
4
- import json
5
- from pathlib import Path
6
- from argparse import ArgumentParser
7
- import struct
8
- import zlib
9
- from fprime_gds.common.fpy.types import (
10
- StatementTemplate,
11
- StatementData,
12
- Header,
13
- Footer,
14
- HEADER_FORMAT,
15
- FOOTER_FORMAT,
16
- StatementType,
17
- FPY_DIRECTIVES,
18
- BytecodeParseContext,
19
- get_type_obj_for,
20
- )
21
- from fprime_gds.common.loaders.ch_json_loader import ChJsonLoader
22
- from fprime_gds.common.loaders.cmd_json_loader import CmdJsonLoader
23
- from fprime.common.models.serialize.numerical_types import (
24
- U8Type,
25
- )
26
-
27
- from fprime_gds.common.loaders.prm_json_loader import PrmJsonLoader
28
-
29
-
30
- def serialize_statement(stmt: StatementData) -> bytes:
31
- """converts a StatementData object into bytes that the FpySequencer can read"""
32
- # see https://github.com/nasa/fprime/issues/3023#issuecomment-2693051677
33
- # TODO replace this with actual documentation
34
-
35
- # type: U8 (0 if directive, 1 if cmd)
36
- # opcode: FwOpcodeType (default U32)
37
- # argBufSize: FwSizeStoreType (default U16)
38
- # argBuf: X bytes
39
-
40
- output = bytes()
41
- output += U8Type(stmt.template.statement_type.value).serialize()
42
- output += get_type_obj_for("FwOpcodeType")(stmt.template.opcode).serialize()
43
-
44
- arg_bytes = bytes()
45
- for arg in stmt.arg_values:
46
- arg_bytes += arg.serialize()
47
-
48
- output += get_type_obj_for("FwSizeStoreType")(len(arg_bytes)).serialize()
49
- output += arg_bytes
50
-
51
- return output
52
-
53
-
54
- def parse_str_as_statement(
55
- stmt: str, templates: list[StatementTemplate], context: BytecodeParseContext
56
- ) -> StatementData:
57
- """Converts a human-readable line of bytecode into a StatementData instance, given a list of
58
- possible statement templates"""
59
- name = stmt.split()[0]
60
- args = stmt[len(name) :]
61
-
62
- args = json.loads("[" + args + "]")
63
-
64
- matching_template = [t for t in templates if t.name == name]
65
- if len(matching_template) != 1:
66
- # no unique match
67
- if len(matching_template) == 0:
68
- raise RuntimeError("Could not find command or directive " + str(name))
69
- raise RuntimeError(
70
- "Found multiple commands or directives with name " + str(name)
71
- )
72
- matching_template = matching_template[0]
73
-
74
- arg_values = []
75
- if len(args) < len(matching_template.args):
76
- raise RuntimeError(
77
- "Missing arguments for statement "
78
- + str(matching_template.name)
79
- + ": "
80
- + str(matching_template.args[len(args) :])
81
- )
82
- if len(args) > len(matching_template.args):
83
- raise RuntimeError(
84
- "Extra arguments for"
85
- + str(matching_template.name)
86
- + ": "
87
- + str(args[len(matching_template.args) :])
88
- )
89
- for index, arg_json in enumerate(args):
90
- arg_type = matching_template.args[index]
91
- if inspect.isclass(arg_type):
92
- # it's a type. instantiate it with the json
93
- arg_value = arg_type(arg_json)
94
- else:
95
- # it's a function. give it the json and the ctx
96
- arg_value = arg_type(arg_json, context)
97
- arg_values.append(arg_value)
98
-
99
- return StatementData(matching_template, arg_values)
100
-
101
-
102
- def main():
103
- arg_parser = ArgumentParser()
104
- arg_parser.add_argument(
105
- "input", type=Path, help="The path to the input .fpybc file"
106
- )
107
-
108
- arg_parser.add_argument(
109
- "-d",
110
- "--dictionary",
111
- type=Path,
112
- help="The JSON topology dictionary to compile against",
113
- required=True,
114
- )
115
-
116
- arg_parser.add_argument(
117
- "-o",
118
- "--output",
119
- type=Path,
120
- help="The output .bin file path. Defaults to the input file path with a .bin extension",
121
- default=None,
122
- )
123
-
124
- args = arg_parser.parse_args()
125
-
126
- if not args.input.exists():
127
- print("Input file", args.input, "does not exist")
128
- exit(1)
129
-
130
- if not args.dictionary.exists():
131
- print("Dictionary file", args.dictionary, "does not exist")
132
- exit(1)
133
-
134
- serialize_bytecode(args.input, args.dictionary, args.output)
135
-
136
-
137
- def serialize_bytecode(input: Path, dictionary: Path, output: Path = None):
138
- """Given an input .fpybc file, and a dictionary .json file, converts the
139
- bytecode file into binary and writes it to the output file. If the output file
140
- is None, writes it to the input file with a .bin extension"""
141
- cmd_json_dict_loader = CmdJsonLoader(str(dictionary))
142
- (_, cmd_name_dict, _) = cmd_json_dict_loader.construct_dicts(
143
- str(dictionary)
144
- )
145
-
146
- stmt_templates = []
147
- stmt_templates.extend(FPY_DIRECTIVES)
148
- for cmd_template in cmd_name_dict.values():
149
- stmt_template = StatementTemplate(
150
- StatementType.CMD,
151
- cmd_template.opcode,
152
- cmd_template.get_full_name(),
153
- [arg[2] for arg in cmd_template.arguments],
154
- )
155
- stmt_templates.append(stmt_template)
156
-
157
- tlm_json_loader = ChJsonLoader(str(dictionary))
158
- (_, tlm_name_dict, _) = tlm_json_loader.construct_dicts(
159
- str(dictionary)
160
- )
161
-
162
- prm_json_loader = PrmJsonLoader(str(dictionary))
163
- (_, prm_name_dict, _) = prm_json_loader.construct_dicts(
164
- str(dictionary)
165
- )
166
-
167
- context = BytecodeParseContext()
168
- context.types = cmd_json_dict_loader.parsed_types
169
- context.channels = tlm_name_dict
170
- context.params = prm_name_dict
171
-
172
- input_lines = input.read_text().splitlines()
173
- input_lines = [line.strip() for line in input_lines]
174
- # remove comments and empty lines
175
- input_lines = [
176
- line for line in input_lines if not line.startswith(";") and len(line) > 0
177
- ]
178
-
179
- goto_tags = {}
180
- stmt_idx = 0
181
- statement_strs: list[str] = []
182
- for stmt in input_lines:
183
- if stmt.endswith(":"):
184
- # it's a goto tag
185
- goto_tags[stmt[:-1]] = stmt_idx
186
- else:
187
- statement_strs.append(stmt)
188
- stmt_idx += 1
189
-
190
- context.goto_tags = goto_tags
191
-
192
- statements: list[StatementData] = []
193
- for stmt_idx, stmt in enumerate(statement_strs):
194
- try:
195
- stmt_data = parse_str_as_statement(stmt, stmt_templates, context)
196
- statements.append(stmt_data)
197
- except BaseException as e:
198
- raise RuntimeError(
199
- "Exception while parsing statement index " + str(stmt_idx) + ": " + stmt
200
- ) from e
201
-
202
- # perform some checks for things we know will fail
203
- for stmt in statements:
204
- if stmt.template.name == "GOTO":
205
- if stmt.arg_values[0].val > len(statements):
206
- raise RuntimeError(
207
- f"GOTO index is outside the valid range for this sequence (was {stmt.arg_values[0].val}, should be <{len(statements)})"
208
- )
209
-
210
- output_bytes = bytes()
211
-
212
- for stmt in statements:
213
- output_bytes += serialize_statement(stmt)
214
-
215
- header = Header(0, 0, 0, 1, 0, len(statements), len(output_bytes))
216
- output_bytes = struct.pack(HEADER_FORMAT, *astuple(header)) + output_bytes
217
-
218
- crc = zlib.crc32(output_bytes) % (1 << 32)
219
- footer = Footer(crc)
220
- output_bytes += struct.pack(FOOTER_FORMAT, *astuple(footer))
221
-
222
- if output is None:
223
- output = input.with_suffix(".bin")
224
-
225
- output.write_bytes(output_bytes)
226
-
227
-
228
- if __name__ == "__main__":
229
- main()
@@ -1,203 +0,0 @@
1
- from __future__ import annotations
2
- from dataclasses import dataclass, field
3
- from enum import Enum
4
- import struct
5
- from typing import Any, Callable
6
- from fprime.common.models.serialize.type_base import BaseType, ValueType
7
- from fprime.common.models.serialize.time_type import TimeType
8
- from fprime.common.models.serialize.numerical_types import U32Type, U16Type, U8Type
9
- from fprime.common.models.serialize.string_type import StringType
10
-
11
- from fprime_gds.common.loaders.json_loader import PRIMITIVE_TYPE_MAP
12
- from fprime_gds.common.templates.ch_template import ChTemplate
13
- from fprime_gds.common.templates.prm_template import PrmTemplate
14
-
15
-
16
- def get_type_obj_for(type: str) -> type[ValueType]:
17
- if type == "FwOpcodeType":
18
- return U32Type
19
- elif type == "FwSizeStoreType":
20
- return U16Type
21
- elif type == "FwChanIdType":
22
- return U32Type
23
- elif type == "FwPrmIdType":
24
- return U32Type
25
-
26
- raise RuntimeError("Unknown FPrime type alias " + str(type))
27
-
28
-
29
- class StatementType(Enum):
30
- DIRECTIVE = 0
31
- CMD = 1
32
-
33
-
34
- @dataclass
35
- class StatementTemplate:
36
- """a statement with unspecified argument values"""
37
-
38
- statement_type: StatementType
39
- opcode: int
40
- name: str
41
- """fully qualified statement name"""
42
- args: list[type[BaseType] | Callable[[Any, BytecodeParseContext], BaseType]]
43
- """list of argument types of this statement, or functions that return an arg type"""
44
-
45
-
46
- @dataclass
47
- class StatementData:
48
- template: StatementTemplate
49
- arg_values: list[BaseType]
50
-
51
-
52
- HEADER_FORMAT = "!BBBBBHI"
53
- HEADER_SIZE = struct.calcsize(HEADER_FORMAT)
54
-
55
-
56
- @dataclass
57
- class Header:
58
- majorVersion: int
59
- minorVersion: int
60
- patchVersion: int
61
- schemaVersion: int
62
- argumentCount: int
63
- statementCount: int
64
- bodySize: int
65
-
66
-
67
- FOOTER_FORMAT = "!I"
68
- FOOTER_SIZE = struct.calcsize(FOOTER_FORMAT)
69
-
70
-
71
- @dataclass
72
- class Footer:
73
- crc: int
74
-
75
-
76
- class DirectiveOpcode(Enum):
77
- INVALID = 0x00000000
78
- WAIT_REL = 0x00000001
79
- WAIT_ABS = 0x00000002
80
- SET_LVAR = 0x00000003
81
- GOTO = 0x00000004
82
- IF = 0x00000005
83
- NO_OP = 0x00000006
84
- GET_TLM = 0x00000007
85
- GET_PRM = 0x00000008
86
-
87
-
88
- @dataclass
89
- class BytecodeParseContext:
90
- goto_tags: map[str, int] = field(default_factory=dict)
91
- """a map of tag name with tag statement index"""
92
- types: map[str, type[BaseType]] = field(default_factory=dict)
93
- """a map of name to all parsed types available in the dictionary"""
94
- channels: map[str, ChTemplate] = field(default_factory=dict)
95
- """a map of name to ChTemplate object for all tlm channels"""
96
- params: map[str, PrmTemplate] = field(default_factory=dict)
97
- """a map of name to PrmTemplate object for all prms"""
98
-
99
-
100
- def time_type_from_json(js, ctx: BytecodeParseContext):
101
- return TimeType(js["time_base"], js["time_context"], js["seconds"], js["useconds"])
102
-
103
-
104
- def arbitrary_type_from_json(js, ctx: BytecodeParseContext):
105
- type_name = js["type"]
106
-
107
- if type_name == "string":
108
- # by default no max size restrictions in the bytecode
109
- return StringType.construct_type(f"String", None)(js["value"])
110
-
111
- # try first checking parsed_types, then check primitive types
112
- type_class = ctx.types.get(type_name, PRIMITIVE_TYPE_MAP.get(type_name, None))
113
- if type_class is None:
114
- raise RuntimeError("Unknown type " + str(type_name))
115
-
116
- return type_class(js["value"])
117
-
118
-
119
- def goto_tag_or_idx_from_json(js, ctx: BytecodeParseContext):
120
- if isinstance(js, str):
121
- # it's a tag
122
- if js not in ctx.goto_tags:
123
- raise RuntimeError("Unknown goto tag " + str(js))
124
- return U32Type(ctx.goto_tags[js])
125
-
126
- # otherwise it is a statement index
127
- return U32Type(js)
128
-
129
-
130
- def tlm_chan_id_from_json(js, ctx: BytecodeParseContext):
131
- if isinstance(js, str):
132
- if js not in ctx.channels:
133
- raise RuntimeError("Unknown telemetry channel " + str(js))
134
- return get_type_obj_for("FwChanIdType")(ctx.channels[js].id)
135
- elif isinstance(js, int):
136
- matching = [tmp for tmp in ctx.channels.keys() if tmp.id == js]
137
- if len(matching) != 1:
138
- if len(matching) == 0:
139
- raise RuntimeError("Unknown telemetry channel id " + str(js))
140
- raise RuntimeError("Multiple matches for telemetry channel id " + str(js))
141
- matching = matching[0]
142
- return get_type_obj_for("FwChanIdType")(matching.id)
143
-
144
-
145
- def prm_id_from_json(js, ctx: BytecodeParseContext):
146
- if isinstance(js, str):
147
- if js not in ctx.params:
148
- raise RuntimeError("Unknown parameter " + str(js))
149
- return get_type_obj_for("FwPrmIdType")(ctx.params[js].prm_id)
150
- elif isinstance(js, int):
151
- matching = [tmp for tmp in ctx.params.keys() if tmp.prm_id == js]
152
- if len(matching) != 1:
153
- if len(matching) == 0:
154
- raise RuntimeError("Unknown param id " + str(js))
155
- raise RuntimeError("Multiple matches for param id " + str(js))
156
- matching = matching[0]
157
- return get_type_obj_for("FwPrmIdType")(matching.prm_id)
158
-
159
-
160
- FPY_DIRECTIVES: list[StatementTemplate] = [
161
- StatementTemplate(
162
- StatementType.DIRECTIVE,
163
- DirectiveOpcode.WAIT_REL.value,
164
- "WAIT_REL",
165
- [U32Type, U32Type],
166
- ),
167
- StatementTemplate(
168
- StatementType.DIRECTIVE,
169
- DirectiveOpcode.WAIT_ABS.value,
170
- "WAIT_ABS",
171
- [time_type_from_json],
172
- ),
173
- StatementTemplate(
174
- StatementType.DIRECTIVE,
175
- DirectiveOpcode.SET_LVAR.value,
176
- "SET_LVAR",
177
- [U8Type, arbitrary_type_from_json],
178
- ),
179
- StatementTemplate(
180
- StatementType.DIRECTIVE,
181
- DirectiveOpcode.GOTO.value,
182
- "GOTO",
183
- [goto_tag_or_idx_from_json],
184
- ),
185
- StatementTemplate(
186
- StatementType.DIRECTIVE,
187
- DirectiveOpcode.IF.value,
188
- "IF",
189
- [U8Type, goto_tag_or_idx_from_json],
190
- ),
191
- StatementTemplate(
192
- StatementType.DIRECTIVE,
193
- DirectiveOpcode.GET_TLM.value,
194
- "GET_TLM",
195
- [U8Type, U8Type, tlm_chan_id_from_json],
196
- ),
197
- StatementTemplate(
198
- StatementType.DIRECTIVE,
199
- DirectiveOpcode.GET_PRM.value,
200
- "GET_PRM",
201
- [U8Type, prm_id_from_json],
202
- ),
203
- ]