Skip to content

Commit

Permalink
Merge pull request #541 from zapta/develop
Browse files Browse the repository at this point in the history
Resource files cleanup in progress.
  • Loading branch information
cavearr authored Jan 13, 2025
2 parents 884eabd + 2cd73f2 commit b2bad1b
Show file tree
Hide file tree
Showing 47 changed files with 474 additions and 197 deletions.
9 changes: 6 additions & 3 deletions apio/apio_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
from typing import Optional, Dict
import click
from click import secho
from apio import util, env_options
from apio.profile import Profile
from apio.utils import jsonc, util, env_options
from apio.managers.project import (
Project,
ProjectResolver,
Expand Down Expand Up @@ -321,8 +321,8 @@ def _load_resource_file(filepath: Path) -> dict:
try:
with filepath.open(encoding="utf8") as file:

# -- Read the json file
data_json = file.read()
# -- Read the json with comments file
data_jsonc = file.read()

# -- json file NOT FOUND! This is an apio system error
# -- It should never ocurr unless there is a bug in the
Expand All @@ -344,6 +344,9 @@ def _load_resource_file(filepath: Path) -> dict:
# -- Abort!
sys.exit(1)

# -- Convert the jsonc to json by removing '//' comments.
data_json = jsonc.to_json(data_jsonc)

# -- Parse the json format!
try:
resource = json.loads(data_json)
Expand Down
2 changes: 1 addition & 1 deletion apio/commands/apio.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


import click
from apio.cmd_util import ApioSubgroup, ApioGroup
from apio.utils.cmd_util import ApioSubgroup, ApioGroup

# -- Import sub commands.
from apio.commands import (
Expand Down
20 changes: 16 additions & 4 deletions apio/commands/apio_boards.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
import sys
from pathlib import Path
from dataclasses import dataclass
from typing import List
from typing import List, Dict
import click
from click import secho, style
from apio.apio_context import ApioContext, ApioContextScope
from apio import util
from apio.utils import util
from apio.commands import options
from apio.managers.examples import Examples


# R0801: Similar lines in 2 files
Expand All @@ -27,6 +28,7 @@ class Entry:

board: str
board_description: str
examples_count: str
fpga: str
programmer: str
fpga_arch: str
Expand All @@ -52,13 +54,19 @@ def sort_key(self):

# R0914: Too many local variables (17/15)
# pylint: disable=R0914
# pylint: disable=too-many-statements
def list_boards(apio_ctx: ApioContext, verbose: bool):
"""Prints all the available board definitions."""

# -- Get examples counts by board. This is a sparse dictionary.
examples = Examples(apio_ctx)
examples_counts: Dict[str, int] = examples.count_examples_by_board()

# -- Collect the boards info into a list of entires, one per board.
entries: List[Entry] = []
for board, board_info in apio_ctx.boards.items():
board_description = board_info.get("description", "")
examples_count = " " + str(examples_counts.get(board, ""))
programmer = board_info.get("programmer", {}).get("type", "")
fpga = board_info.get("fpga", "")
fpga_info = apio_ctx.fpgas.get(fpga, {})
Expand All @@ -71,6 +79,7 @@ def list_boards(apio_ctx: ApioContext, verbose: bool):
Entry(
board,
board_description,
examples_count,
fpga,
programmer,
fpga_arch,
Expand All @@ -85,11 +94,12 @@ def list_boards(apio_ctx: ApioContext, verbose: bool):
entries.sort(key=lambda x: x.sort_key())

# -- Compute the columns widths.
margin = 4
board_len = max(len(x.board) for x in entries) + margin
margin = 2 if verbose else 4
board_len = max(len(x.board) for x in entries) + margin - 2
board_description_len = (
max(len(x.board_description) for x in entries) + margin
)
examples_count_len = 7 + margin
fpga_len = max(len(x.fpga) for x in entries) + margin
programmer_len = max(len(x.programmer) for x in entries) + margin
fpga_arch_len = max(len(x.fpga_arch) for x in entries) + margin
Expand All @@ -101,6 +111,7 @@ def list_boards(apio_ctx: ApioContext, verbose: bool):
# -- Construct the title fields.
parts = []
parts.append(f"{'BOARD':<{board_len}}")
parts.append(f"{'EXAMPLES':<{examples_count_len}}")
if verbose:
parts.append(f"{'DESCRIPTION':<{board_description_len}}")
parts.append(f"{'ARCH':<{fpga_arch_len}}")
Expand All @@ -123,6 +134,7 @@ def list_boards(apio_ctx: ApioContext, verbose: bool):
# -- Construct the line fields.
parts = []
parts.append(style(f"{x.board:<{board_len}}", fg="cyan"))
parts.append(f"{x.examples_count:<{examples_count_len}}")
if verbose:
parts.append(f"{x.board_description:<{board_description_len}}")
parts.append(f"{x.fpga_arch:<{fpga_arch_len}}")
Expand Down
3 changes: 1 addition & 2 deletions apio/commands/apio_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
DEFAULT_TOP_MODULE,
create_project_file,
)
from apio import util
from apio import cmd_util
from apio.utils import util, cmd_util
from apio.commands import options
from apio.apio_context import ApioContext, ApioContextScope

Expand Down
2 changes: 1 addition & 1 deletion apio/commands/apio_drivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import sys
import click
from apio.cmd_util import ApioGroup, ApioSubgroup
from apio.utils.cmd_util import ApioGroup, ApioSubgroup
from apio.commands import apio_drivers_ftdi, apio_drivers_serial
from apio.apio_context import ApioContext, ApioContextScope
from apio.managers.system import System
Expand Down
2 changes: 1 addition & 1 deletion apio/commands/apio_drivers_ftdi.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import click
from apio.managers.drivers import Drivers
from apio.apio_context import ApioContext, ApioContextScope
from apio.cmd_util import ApioGroup, ApioSubgroup
from apio.utils.cmd_util import ApioGroup, ApioSubgroup
from apio.managers.system import System


Expand Down
2 changes: 1 addition & 1 deletion apio/commands/apio_drivers_serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from apio.managers.drivers import Drivers
from apio.managers.system import System
from apio.apio_context import ApioContext, ApioContextScope
from apio.cmd_util import ApioGroup, ApioSubgroup
from apio.utils.cmd_util import ApioGroup, ApioSubgroup


# -- apio driver serial install
Expand Down
4 changes: 2 additions & 2 deletions apio/commands/apio_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
from apio.managers.examples import Examples
from apio.commands import options
from apio.apio_context import ApioContext, ApioContextScope
from apio import util
from apio.cmd_util import ApioGroup, ApioSubgroup
from apio.utils import util
from apio.utils.cmd_util import ApioGroup, ApioSubgroup


# ---- apio examples list
Expand Down
2 changes: 1 addition & 1 deletion apio/commands/apio_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
import click
from click import secho
from apio.apio_context import ApioContext, ApioContextScope
from apio import pkg_util, util
from apio.commands import options
from apio.managers import installer
from apio.utils import util, pkg_util


# ---------------------------
Expand Down
21 changes: 18 additions & 3 deletions apio/commands/apio_fpgas.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
import sys
from pathlib import Path
from dataclasses import dataclass
from typing import List
from typing import List, Dict
import click
from click import secho, style, echo
from apio.apio_context import ApioContext, ApioContextScope
from apio import util
from apio.utils import util
from apio.commands import options


Expand All @@ -25,6 +25,7 @@ class Entry:
"""A class to hold the field of a single line of the report."""

fpga: str
board_count: int
fpga_arch: str
fpga_part_num: str
fpga_size: str
Expand All @@ -50,10 +51,19 @@ def sort_key(self):
def list_fpgas(apio_ctx: ApioContext, verbose: bool):
"""Prints all the available FPGA definitions."""

# -- Collect a sparse dict with fpga ids to board count.
boards_counts: Dict[str, int] = {}
for board_info in apio_ctx.boards.values():
fpga = board_info.get("fpga", None)
if fpga:
old_count = boards_counts.get(fpga, 0)
boards_counts[fpga] = old_count + 1

# -- Collect all entries.
entries: List[Entry] = []
for fpga, fpga_info in apio_ctx.fpgas.items():
# -- Construct the Entry for this fpga.
board_count = boards_counts.get(fpga, 0)
fpga_arch = fpga_info.get("arch", "")
fpga_part_num = fpga_info.get("part_num", "")
fpga_size = fpga_info.get("size", "")
Expand All @@ -63,6 +73,7 @@ def list_fpgas(apio_ctx: ApioContext, verbose: bool):
entries.append(
Entry(
fpga,
board_count,
fpga_arch,
fpga_part_num,
fpga_size,
Expand All @@ -75,8 +86,9 @@ def list_fpgas(apio_ctx: ApioContext, verbose: bool):
entries.sort(key=lambda x: x.sort_key())

# -- Compute field lengths
margin = 4
margin = 3
fpga_len = max(len(x.fpga) for x in entries) + margin
board_count_len = 6 + margin
fpga_arch_len = max(len(x.fpga_arch) for x in entries) + margin
fpga_part_num_len = max(len(x.fpga_part_num) for x in entries) + margin
fpga_size_len = max(len(x.fpga_size) for x in entries) + margin
Expand All @@ -86,6 +98,7 @@ def list_fpgas(apio_ctx: ApioContext, verbose: bool):
# -- Construct the title fields.
parts = []
parts.append(f"{'FPGA ID':<{fpga_len}}")
parts.append(f"{'BOARDS':<{board_count_len}}")
parts.append(f"{'AECH':<{fpga_arch_len}}")
parts.append(f"{'PART NUMBER':<{fpga_part_num_len}}")
parts.append(f"{'SIZE':<{fpga_size_len}}")
Expand All @@ -102,6 +115,8 @@ def list_fpgas(apio_ctx: ApioContext, verbose: bool):
# -- Construct the fpga fields.
parts = []
parts.append(style(f"{x.fpga:<{fpga_len}}", fg="cyan"))
board_count = f"{x.board_count:>3}" if x.board_count else ""
parts.append(f"{board_count:<{board_count_len}}")
parts.append(f"{x.fpga_arch:<{fpga_arch_len}}")
parts.append(f"{x.fpga_part_num:<{fpga_part_num_len}}")
parts.append(f"{x.fpga_size:<{fpga_size_len}}")
Expand Down
4 changes: 2 additions & 2 deletions apio/commands/apio_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
from pathlib import Path
import click
from apio.managers.scons import SCons
from apio import cmd_util
from apio.utils import cmd_util
from apio.commands import options
from apio.apio_context import ApioContext, ApioContextScope
from apio.util import nameof
from apio.utils.util import nameof


# ---------------------------
Expand Down
2 changes: 1 addition & 1 deletion apio/commands/apio_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from pathlib import Path
import click
from apio.managers.scons import SCons
from apio import cmd_util
from apio.utils import cmd_util
from apio.commands import options
from apio.apio_context import ApioContext, ApioContextScope

Expand Down
4 changes: 2 additions & 2 deletions apio/commands/apio_packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
from click import secho
from apio.managers import installer
from apio.apio_context import ApioContext, ApioContextScope
from apio import pkg_util
from apio.utils import pkg_util
from apio.commands import options
from apio.cmd_util import ApioGroup, ApioSubgroup
from apio.utils.cmd_util import ApioGroup, ApioSubgroup


# ------ apio packages install
Expand Down
4 changes: 2 additions & 2 deletions apio/commands/apio_preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

import click
from click import secho, echo, style
from apio import cmd_util
from apio.utils import cmd_util
from apio.apio_context import ApioContext, ApioContextScope
from apio.cmd_util import ApioGroup, ApioSubgroup
from apio.utils.cmd_util import ApioGroup, ApioSubgroup

# ---- apio preferences list

Expand Down
4 changes: 2 additions & 2 deletions apio/commands/apio_raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
from typing import Tuple, List
import click
from click import secho
from apio import pkg_util, cmd_util
from apio.apio_context import ApioContext, ApioContextScope
from apio.commands import options
from apio.util import nameof
from apio.utils import cmd_util, pkg_util
from apio.utils.util import nameof
from apio.managers import installer


Expand Down
4 changes: 2 additions & 2 deletions apio/commands/apio_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

import click
from click import secho
from apio import util
from apio.utils import util
from apio.apio_context import ApioContext, ApioContextScope
from apio.cmd_util import ApioGroup, ApioSubgroup
from apio.utils.cmd_util import ApioGroup, ApioSubgroup


# ------ apio system info
Expand Down
2 changes: 1 addition & 1 deletion apio/commands/apio_upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import click
from click import secho
from packaging import version
from apio import util
from apio.utils import util


# ---------------------------
Expand Down
2 changes: 1 addition & 1 deletion apio/commands/apio_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import click
from apio.managers.scons import SCons
from apio.managers.drivers import Drivers
from apio import cmd_util
from apio.utils import cmd_util
from apio.commands import options
from apio.apio_context import ApioContext, ApioContextScope

Expand Down
2 changes: 1 addition & 1 deletion apio/commands/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from pathlib import Path
import click
from apio import cmd_util
from apio.utils import cmd_util


# The design is based on the idea here https://stackoverflow.com/a/77732441.
Expand Down
2 changes: 1 addition & 1 deletion apio/managers/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import requests
import click
from click import secho
from apio import util
from apio.utils import util

# -- Timeout for geting a reponse from the server when downloading
# -- a file (in seconds)
Expand Down
2 changes: 1 addition & 1 deletion apio/managers/drivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import subprocess
from pathlib import Path
from click import secho
from apio import util
from apio.utils import util
from apio.apio_context import ApioContext
from apio.managers import installer

Expand Down
Loading

0 comments on commit b2bad1b

Please sign in to comment.