dynalite-eeprom 0.1.0__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.
@@ -0,0 +1,98 @@
1
+ """Decode memory dumps of Signify Dynalite DyNet devices, and write edits back.
2
+
3
+ Read a raw dump (``dynet dump -o``) with `Image.from_file`, then `decode` it.
4
+ `encode` writes edited fields back into the image, and `changes` lists the
5
+ bytes that differ. Only the memory layout is involved: nothing here talks to
6
+ a bus.
7
+ """
8
+
9
+ from .container import Header, Section
10
+ from .devices import (
11
+ Button,
12
+ Channel,
13
+ DeviceMemory,
14
+ Event,
15
+ Gateway,
16
+ Indicator,
17
+ LightBand,
18
+ LightDetector,
19
+ Listener,
20
+ Output,
21
+ Panel,
22
+ RelayController,
23
+ Sensor,
24
+ SensorPreset,
25
+ Task,
26
+ Unsupported,
27
+ decode,
28
+ )
29
+ from .encode import Change, EncodeError, changes, encode
30
+ from .image import DecodeError, Image
31
+ from .program import (
32
+ Branch,
33
+ Copy,
34
+ Delay,
35
+ End,
36
+ Instruction,
37
+ Jump,
38
+ Op,
39
+ Operand,
40
+ PresetToArea,
41
+ RampToLevel,
42
+ RecallPreset,
43
+ RunLocalTask,
44
+ SendPacket,
45
+ StopFade,
46
+ TaskControl,
47
+ Unknown,
48
+ disassemble,
49
+ )
50
+ from .report import describe, render, to_data
51
+
52
+ __all__ = [
53
+ "Branch",
54
+ "Button",
55
+ "Change",
56
+ "Channel",
57
+ "Copy",
58
+ "DecodeError",
59
+ "Delay",
60
+ "DeviceMemory",
61
+ "EncodeError",
62
+ "End",
63
+ "Event",
64
+ "Gateway",
65
+ "Header",
66
+ "Image",
67
+ "Indicator",
68
+ "Instruction",
69
+ "Jump",
70
+ "LightBand",
71
+ "LightDetector",
72
+ "Listener",
73
+ "Op",
74
+ "Operand",
75
+ "Output",
76
+ "Panel",
77
+ "PresetToArea",
78
+ "RampToLevel",
79
+ "RecallPreset",
80
+ "RelayController",
81
+ "RunLocalTask",
82
+ "Section",
83
+ "SendPacket",
84
+ "Sensor",
85
+ "SensorPreset",
86
+ "StopFade",
87
+ "Task",
88
+ "TaskControl",
89
+ "Unknown",
90
+ "Unsupported",
91
+ "changes",
92
+ "decode",
93
+ "describe",
94
+ "disassemble",
95
+ "encode",
96
+ "render",
97
+ "to_data",
98
+ ]
@@ -0,0 +1,5 @@
1
+ """Run the command line with ``python -m dynalite_eeprom``."""
2
+
3
+ from .cli import main
4
+
5
+ main()
dynalite_eeprom/cli.py ADDED
@@ -0,0 +1,129 @@
1
+ """``dynalite-eeprom``: describe dumps, export them as YAML, compile YAML back.
2
+
3
+ - ``dynalite-eeprom [show] FILE… [--json]`` describes dumps.
4
+ - ``dynalite-eeprom export FILE [-o OUT]`` writes a device's configuration
5
+ as YAML.
6
+ - ``dynalite-eeprom compile CONFIG --base FILE -o OUT`` applies an edited
7
+ configuration to the dump it came from and writes the new image, listing
8
+ the bytes that changed.
9
+
10
+ It reads and writes files only, and never touches the bus.
11
+ """
12
+
13
+ from __future__ import annotations
14
+
15
+ import argparse
16
+ import json
17
+ import sys
18
+ from datetime import UTC, datetime
19
+ from pathlib import Path
20
+ from typing import TYPE_CHECKING, TextIO
21
+
22
+ from .config import ConfigError, compile_config, export, load
23
+ from .devices import decode
24
+ from .encode import changes
25
+ from .image import DecodeError, Image
26
+ from .report import render, to_data
27
+
28
+ if TYPE_CHECKING:
29
+ from collections.abc import Sequence
30
+
31
+ EXIT_OK = 0
32
+ EXIT_ERROR = 1
33
+ COMMANDS = frozenset({"show", "export", "compile"})
34
+
35
+
36
+ def _parser() -> argparse.ArgumentParser:
37
+ parser = argparse.ArgumentParser(
38
+ prog="dynalite-eeprom",
39
+ description="Decode memory dumps of Signify Dynalite DyNet devices "
40
+ "(raw files from `dynet dump -o`), and turn them into YAML and back. "
41
+ "Reads and writes files only; never touches the bus.",
42
+ )
43
+ commands = parser.add_subparsers(dest="command", required=True)
44
+ show = commands.add_parser("show", help="describe dumps (the default)")
45
+ show.add_argument("files", nargs="+", metavar="FILE")
46
+ show.add_argument("--json", action="store_true", help="print JSON instead")
47
+ exporter = commands.add_parser("export", help="write a dump's config as YAML")
48
+ exporter.add_argument("file", metavar="FILE")
49
+ exporter.add_argument("-o", "--output", metavar="OUT", help="default: stdout")
50
+ compiler = commands.add_parser(
51
+ "compile", help="apply a YAML config to its dump, writing a new image"
52
+ )
53
+ compiler.add_argument("config", metavar="CONFIG")
54
+ compiler.add_argument(
55
+ "--base", required=True, metavar="FILE", help="the dump it was exported from"
56
+ )
57
+ compiler.add_argument("-o", "--output", required=True, metavar="OUT")
58
+ compiler.add_argument(
59
+ "--keep-stamp",
60
+ action="store_true",
61
+ help="don't set the programming stamp to now when something changes",
62
+ )
63
+ return parser
64
+
65
+
66
+ def _show(files: Sequence[str], *, as_json: bool, out: TextIO, err: TextIO) -> int:
67
+ status = EXIT_OK
68
+ results: list[object] = []
69
+ for path in files:
70
+ try:
71
+ memory = decode(Image.from_file(path))
72
+ except (OSError, DecodeError) as error:
73
+ print(f"dynalite-eeprom: {path}: {error}", file=err)
74
+ status = EXIT_ERROR
75
+ continue
76
+ if as_json:
77
+ results.append({"file": path, "memory": to_data(memory)})
78
+ else:
79
+ if len(files) > 1:
80
+ print(f"== {path}", file=out)
81
+ print(render(memory), file=out)
82
+ if as_json:
83
+ print(json.dumps(results, indent=2), file=out)
84
+ return status
85
+
86
+
87
+ def _export(path: str, output: str | None, out: TextIO) -> None:
88
+ text = export(decode(Image.from_file(path)))
89
+ if output is None:
90
+ out.write(text)
91
+ else:
92
+ Path(output).write_text(text, encoding="utf-8")
93
+
94
+
95
+ def _compile(args: argparse.Namespace, out: TextIO) -> None:
96
+ base = Image.from_file(args.base)
97
+ data = load(Path(args.config).read_text(encoding="utf-8"))
98
+ stamp = None if args.keep_stamp else datetime.now(UTC).replace(microsecond=0)
99
+ result = compile_config(data, base, stamp=stamp)
100
+ runs = changes(base, result)
101
+ Path(args.output).write_bytes(result.data)
102
+ if not runs:
103
+ print("no changes", file=out)
104
+ for run in runs:
105
+ print(f"0x{run.address:04x}: {run.data.hex(' ')}", file=out)
106
+
107
+
108
+ def run(argv: Sequence[str], out: TextIO, err: TextIO) -> int:
109
+ """Run the command line with `argv`, writing to `out` and `err`."""
110
+ argv = list(argv)
111
+ if argv and argv[0] not in COMMANDS and argv[0] not in {"-h", "--help"}:
112
+ argv.insert(0, "show")
113
+ args = _parser().parse_args(argv)
114
+ if args.command == "show":
115
+ return _show(args.files, as_json=args.json, out=out, err=err)
116
+ try:
117
+ if args.command == "export":
118
+ _export(args.file, args.output, out)
119
+ else:
120
+ _compile(args, out)
121
+ except (OSError, DecodeError, ConfigError) as error:
122
+ print(f"dynalite-eeprom: {error}", file=err)
123
+ return EXIT_ERROR
124
+ return EXIT_OK
125
+
126
+
127
+ def main() -> None:
128
+ """Entry point for the ``dynalite-eeprom`` command."""
129
+ sys.exit(run(sys.argv[1:], sys.stdout, sys.stderr))