Skip to content

Commit

Permalink
Move print function from __main__ to printing.py and give better names
Browse files Browse the repository at this point in the history
* Move __main__.list_od() to printing.format_node()
* Rename GerPrintEntryHeader() to format_od_header()
* Rename GetPrintEntry() to format_od_object()
  • Loading branch information
sveinse committed Dec 3, 2024
1 parent a693811 commit 13ac437
Show file tree
Hide file tree
Showing 7 changed files with 313 additions and 229 deletions.
58 changes: 3 additions & 55 deletions src/objdictgen/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

import objdictgen
from objdictgen import jsonod
from objdictgen.printing import GetPrintEntry
from objdictgen.printing import format_node
from objdictgen.typing import TDiffEntries, TDiffNodes, TPath

T = TypeVar('T')
Expand Down Expand Up @@ -122,59 +122,6 @@ def _printlines(entries: TDiffEntries):
_printlines(diffs[index])


def list_od(
od: "Node",
name: str,
opts: argparse.Namespace) -> Generator[str, None, None]:
"""Generator for producing the output for odg list"""

# Get the indexes to print and determine the order
keys = od.GetAllIndices(sort=opts.sort)
if opts.index:
indexp = [jsonod.str_to_int(i) for i in opts.index]
keys = [k for k in keys if k in indexp]
missing = ", ".join((str(k) for k in indexp if k not in keys))
if missing:
raise ValueError(f"Unknown index {missing}")

profiles = []
if od.DS302:
loaded, equal = jsonod.compare_profile("DS-302", od.DS302)
if equal:
extra = "DS-302 (equal)"
elif loaded:
extra = "DS-302 (not equal)"
else:
extra = "DS-302 (not loaded)"
profiles.append(extra)

pname = od.ProfileName
if pname and pname != 'None':
loaded, equal = jsonod.compare_profile(pname, od.Profile, od.SpecificMenu)
if equal:
extra = f"{pname} (equal)"
elif loaded:
extra = f"{pname} (not equal)"
else:
extra = f"{pname} (not loaded)"
profiles.append(extra)

if not opts.compact:
yield f"{Fore.CYAN}File:{Style.RESET_ALL} {name}"
yield f"{Fore.CYAN}Name:{Style.RESET_ALL} {od.Name} [{od.Type.upper()}] {od.Description}"
tp = ", ".join(profiles) or None
yield f"{Fore.CYAN}Profiles:{Style.RESET_ALL} {tp}"
if od.ID:
yield f"{Fore.CYAN}ID:{Style.RESET_ALL} {od.ID}"
yield ""

# Print the parameters
yield from GetPrintEntry(
od, keys=keys, short=opts.short, compact=opts.compact, unused=opts.unused,
verbose=opts.all, raw=opts.raw
)


@debug_wrapper()
def main(debugopts: DebugOpts, args: Sequence[str]|None = None):
""" Main command dispatcher """
Expand Down Expand Up @@ -265,6 +212,7 @@ def main(debugopts: DebugOpts, args: Sequence[str]|None = None):
subp.add_argument('--raw', action="store_true", help="Show raw parameter values")
subp.add_argument('--short', action="store_true", help="Do not list sub-index")
subp.add_argument('--unused', action="store_true", help="Include unused profile parameters")
subp.add_argument('--internal', action="store_true", help="Show internal data")
subp.add_argument('-D', '--debug', **opt_debug) # type: ignore[arg-type]
subp.add_argument('--no-color', action='store_true', help="Disable colored output")

Expand Down Expand Up @@ -400,7 +348,7 @@ def main(debugopts: DebugOpts, args: Sequence[str]|None = None):
print(Fore.LIGHTBLUE_EX + name + '\n' + "=" * len(name) + Style.RESET_ALL)

od = open_od(name)
for line in list_od(od, name, opts):
for line in format_node(od, name, index=opts.index, opts=opts):
print(line)


Expand Down
8 changes: 4 additions & 4 deletions src/objdictgen/nodelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@

import errno
import os
from pathlib import Path
import shutil
from dataclasses import dataclass
from pathlib import Path

from objdictgen import eds_utils
from objdictgen.node import Node
from objdictgen.nodemanager import NodeManager
from objdictgen.printing import GetPrintEntry
from objdictgen.printing import format_node
from objdictgen.typing import TODObj, TODSubObj, TPath

# ------------------------------------------------------------------------------
Expand Down Expand Up @@ -270,11 +270,11 @@ def main(projectdir):
print("MasterNode :")
node = manager.CurrentNode
if node:
for line in GetPrintEntry(node, raw=True):
for line in format_node(node, "MasterNode", raw=True):
print(line)
print()
for nodeid, nodeinfo in nodelist.SlaveNodes.items():
print(f"SlaveNode name={nodeinfo.Name} id=0x{nodeid:02X} :")
for line in GetPrintEntry(nodeinfo.Node):
for line in format_node(nodeinfo.Node, nodeinfo.Name):
print(line)
print()
5 changes: 0 additions & 5 deletions src/objdictgen/nodemanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
from pathlib import Path
from typing import Container, Generic, TypeVar, cast

import colorama

from objdictgen import maps
from objdictgen.maps import OD, ODMapping
from objdictgen.node import Node
Expand All @@ -35,9 +33,6 @@

log = logging.getLogger('objdictgen')

Fore = colorama.Fore
Style = colorama.Style

UNDO_BUFFER_LENGTH = 20

type_model = re.compile(r'([\_A-Z]*)([0-9]*)')
Expand Down
Loading

0 comments on commit 13ac437

Please sign in to comment.