Skip to content

Commit

Permalink
ruff check --fix --unsafe-fixes and manual fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
salkinium committed Nov 16, 2024
1 parent 72c81e5 commit 1949d5b
Show file tree
Hide file tree
Showing 18 changed files with 65 additions and 62 deletions.
2 changes: 1 addition & 1 deletion src/modm_data/cubehal/dmamux_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def _read_requests(hal_dma_file):
# For G0, WB and WL
def _read_requests_from_ll_dmamux(hal_dma_file, ll_dmamux_file):
dmamux_map = _read_map(ll_dmamux_file, _DMAMUX_PATTERN)
request_pattern = re.compile("^\s*#define\s+(?P<name>(DMA_REQUEST_\w+))\s+(?P<id>(LL_DMAMUX?_REQ_\w+))\s*")
request_pattern = re.compile(r"^\s*#define\s+(?P<name>(DMA_REQUEST_\w+))\s+(?P<id>(LL_DMAMUX?_REQ_\w+))\s*")
requests_map = _read_map(hal_dma_file, request_pattern)
out_map = {}
for r in requests_map.keys():
Expand Down
1 change: 0 additions & 1 deletion src/modm_data/cubemx/device_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@ def clean_up_version(version):

p["modules"] = modules
LOGGER.debug("Available Modules are:\n" + _modulesToString(modules))
instances = [m[1] for m in modules]
# print("\n".join(str(m) for m in modules))

# p["stm_header"] = stm_header
Expand Down
5 changes: 3 additions & 2 deletions src/modm_data/cubemx/stm32_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ def getFlashLatencyForDevice(did):
if lts is None:
return {} # family not known
# Convert MHz to Hz and filter out string keys
lconv = lambda l: {k: [int(f * 1e6) for f in v] for k, v in l.items() if isinstance(k, int)}
def lconv(lt):
return {k: [int(f * 1000000.0) for f in v] for k, v in lt.items() if isinstance(k, int)}
if isinstance(lts, dict):
return lconv(lts) # whole family uses same table
for lt in lts:
Expand Down Expand Up @@ -696,7 +697,7 @@ def getMemoryModel(device_id):
if all(device_id[k] in v for k, v in model.items() if k not in ["start", "memories"]):
mem_model = model
break
if mem_model == None:
if mem_model is None:
LOGGER.error(f"Memory model not found for device '{device_id.string}'")
exit(1)
start = dict(mem_fam["start"])
Expand Down
1 change: 0 additions & 1 deletion src/modm_data/header2svd/stmicro/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ def _normalize_adc_common(memtree):


def _normalize_i2sext(memtree):
ext = findall(memtree, lambda n: "ext" not in n.name, maxlevel=2)
for common in findall(memtree, lambda n: "ext" in n.name, maxlevel=2):
LOGGER.info(f"Removing aliased peripheral '{common}'!")
common.parent = None
Expand Down
6 changes: 2 additions & 4 deletions src/modm_data/html/stmicro/datasheet_stm32.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,7 @@ def _tables_alternate_functions(self):
def packages(self):
domains = self._table_pinout.domains_x(r"num(<br>)?ber|pins?:|pin/ball +name|:WLCSP.*?", br="<br>")
if not domains:
raise ValueError(
f"Unable to find package domains in table! {self._chapter_pins._relpath} {tables[0].caption()}"
)
raise ValueError(f"Unable to find package domains in table! {self._chapter_pins._relpath}")
packages = []
for domain in domains:
if domain == "Pin (UFQFPN48):Number":
Expand Down Expand Up @@ -208,7 +206,7 @@ def packages_pins(self):
r"COMP": ",COMP",
r"OPAMP,1": "OPAMP1",
}
afs_replace = {r"[- ]|\(.*?\)": "", "LCD_SEG": ",LCD_SEG"}
afs_replace = {r"[- ]|\(.*?\)": "", "LCD_SEG": ",LCD_SEG"} # noqa: F841
pos_replace = {r'[“”\-"]|NC|\(.*?\)': ""}
sig_replace = {
r"[- ]|\(.*?\)": "",
Expand Down
2 changes: 1 addition & 1 deletion src/modm_data/html/stmicro/reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def vector_tables(self):
@cache
def peripheral_maps(self, chapter, assert_table=True):
off_replace = {r" +": "", "0x000x00": "0x00", "to": "-", "×": "*", r"\(\d+\)": ""}
dom_replace = {r"Register +size": "Bit position"}
dom_replace = {r"Register +size": "Bit position"} # noqa: F841
reg_replace = {
r" +|\.+": "",
r"\(COM(\d)\)": r"_COM\1",
Expand Down
32 changes: 20 additions & 12 deletions src/modm_data/html2svd/stmicro/datasheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from anytree import RenderTree

from ...html.stmicro.helper import split_device_filter
from ...svd import *
from ...svd import Register, BitField, PeripheralType
from ...header2svd.stmicro.tree import _normalize_order
from ...html import replace as html_replace

Expand Down Expand Up @@ -112,7 +112,7 @@ def _peripheral_map_to_tree(chapter, peripheral_maps):
filters["devices"] = {r"STM32F10[123]"}
try:
offset = int(offset, 16)
except:
except ValueError:
pass
register_type = Register(rname, offset, filters=filters, parent=peripheral_type)
fields = [BitField(field, bit) for bit, field in bitfields.items()]
Expand All @@ -138,7 +138,8 @@ def _expand_register_offsets(peripheral_trees):
for offsets, registers in unexpanded.items():
print(offsets, registers)

conv = lambda i: int(i, 16)
def conv(i):
return int(i, 16)
# if match := re.search(r"x=([\d,]+)", registers[0].name):
# offsets = [offsets] * len(match.group(1).split(","))
if any(pat in offsets for pat in ["x=", "channelnumber"]):
Expand Down Expand Up @@ -175,24 +176,32 @@ def _expand_register_offsets(peripheral_trees):

fields = registers[0].children
if all(re.match(r"BKP\d+R", r.name) for r in registers):
name_template = lambda i: f"BKP{i}R"
def name_template(i):
return f"BKP{i}R"
elif "SAI" in peripheral.name:
name_template = lambda i: f"{registers[0].name[1:]}{chr(i+ord('A'))}"
def name_template(i):
return f"{registers[0].name[1:]}{chr(i + ord('A'))}"
elif "HRTIM" in peripheral.name:
name_template = lambda i: registers[0].name.replace("x", chr(i + ord("A")))
def name_template(i):
return registers[0].name.replace("x", chr(i + ord("A")))
elif "CAN" in peripheral.name:
name_template = lambda i: f"F{(i+3)//2}R{(i+1)%2+1}"
def name_template(i):
return f"F{(i + 3) // 2}R{(i + 1) % 2 + 1}"
elif "GFXMMU" in peripheral.name:
name_template = lambda i: f"LUT{i}{registers[0].name[-1]}"
def name_template(i):
return f"LUT{i}{registers[0].name[-1]}"
elif "HSEM" in peripheral.name:
name_template = lambda i: f"{registers[0].name[:-1]}{i+1}"
def name_template(i):
return f"{registers[0].name[:-1]}{i + 1}"
elif len(registers) == 1:
# if "x=" in registers[0].name:
# name_template = lambda i: f"{registers[0].name.split('x=')[0]}.{i}"
if "x" in registers[0].name:
name_template = lambda i: registers[0].name.replace("x", str(i))
def name_template(i):
return registers[0].name.replace("x", str(i))
else:
name_template = lambda i: f"{registers[0].name}.{i}"
def name_template(i):
return f"{registers[0].name}.{i}"
else:
print(f"Unknown expansion pattern for {registers}!")
return False
Expand Down Expand Up @@ -342,7 +351,6 @@ def memory_map_from_datasheet(ds):
register = ds.chapter(r"chapter +\d+ +register +mapping")
table = register.tables("register")[0]
print(table)
registers = {}
for row in table.cell_rows():
cname = row.match_value("name")[0].text()
ctype = row.match_value("type")[0].text()
Expand Down
27 changes: 18 additions & 9 deletions src/modm_data/html2svd/stmicro/reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ def _expand_register_offsets(peripheral_trees):
for offsets, registers in unexpanded.items():
print(offsets, registers)

conv = lambda i: int(i, 16)
def conv(i):
return int(i, 16)
# if match := re.search(r"x=([\d,]+)", registers[0].name):
# offsets = [offsets] * len(match.group(1).split(","))
if any(pat in offsets for pat in ["x=", "channelnumber"]):
Expand Down Expand Up @@ -175,24 +176,32 @@ def _expand_register_offsets(peripheral_trees):

fields = registers[0].children
if all(re.match(r"BKP\d+R", r.name) for r in registers):
name_template = lambda i: f"BKP{i}R"
def name_template(i):
return f"BKP{i}R"
elif "SAI" in peripheral.name:
name_template = lambda i: f"{registers[0].name[1:]}{chr(i+ord('A'))}"
def name_template(i):
return f"{registers[0].name[1:]}{chr(i + ord('A'))}"
elif "HRTIM" in peripheral.name:
name_template = lambda i: registers[0].name.replace("x", chr(i + ord("A")))
def name_template(i):
return registers[0].name.replace("x", chr(i + ord("A")))
elif "CAN" in peripheral.name:
name_template = lambda i: f"F{(i+3)//2}R{(i+1)%2+1}"
def name_template(i):
return f"F{(i + 3) // 2}R{(i + 1) % 2 + 1}"
elif "GFXMMU" in peripheral.name:
name_template = lambda i: f"LUT{i}{registers[0].name[-1]}"
def name_template(i):
return f"LUT{i}{registers[0].name[-1]}"
elif "HSEM" in peripheral.name:
name_template = lambda i: f"{registers[0].name[:-1]}{i+1}"
def name_template(i):
return f"{registers[0].name[:-1]}{i + 1}"
elif len(registers) == 1:
# if "x=" in registers[0].name:
# name_template = lambda i: f"{registers[0].name.split('x=')[0]}.{i}"
if "x" in registers[0].name:
name_template = lambda i: registers[0].name.replace("x", str(i))
def name_template(i):
return registers[0].name.replace("x", str(i))
else:
name_template = lambda i: f"{registers[0].name}.{i}"
def name_template(i):
return f"{registers[0].name}.{i}"
else:
print(f"Unknown expansion pattern for {registers}!")
return False
Expand Down
8 changes: 4 additions & 4 deletions src/modm_data/pdf/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ def _hline(pageobj, rotation, y, x0, x1, **kw):

def _line(pageobj, rotation, line, **kw):
if rotation:
obj = pp.raw.FPDFPageObj_CreateNewPath(height - line.p0.y, line.p0.x)
assert pp.raw.FPDFPath_LineTo(obj, height - line.p1.y, line.p1.x)
obj = pp.raw.FPDFPageObj_CreateNewPath(pageobj.height - line.p0.y, line.p0.x)
assert pp.raw.FPDFPath_LineTo(obj, pageobj.height - line.p1.y, line.p1.x)
else:
obj = pp.raw.FPDFPageObj_CreateNewPath(line.p0.x, line.p0.y)
assert pp.raw.FPDFPath_LineTo(obj, line.p1.x, line.p1.y)
Expand All @@ -32,7 +32,7 @@ def _line(pageobj, rotation, line, **kw):

def _rect(pageobj, rotation, rect, **kw):
if rotation:
obj = pp.raw.FPDFPageObj_CreateNewRect(height - rect.bottom - rect.height, rect.left, rect.height, rect.width)
obj = pp.raw.FPDFPageObj_CreateNewRect(pageobj.height - rect.bottom - rect.height, rect.left, rect.height, rect.width)
else:
obj = pp.raw.FPDFPageObj_CreateNewRect(rect.left, rect.bottom, rect.width, rect.height)
if fill := kw.get("fill"):
Expand All @@ -46,7 +46,7 @@ def _rect(pageobj, rotation, rect, **kw):


def render_page_pdf(doc, page, new_doc=None, index=0):
width, height = page.width, page.height
_, height = page.width, page.height

if new_doc is None:
new_doc = pp.raw.FPDF_CreateNewDocument()
Expand Down
3 changes: 2 additions & 1 deletion src/modm_data/pdf2html/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,8 @@ def objects_in_area(self, area: Rectangle, with_graphics: bool = True) -> list[C
objects += self.charlines_in_area(narea)
else:
oarea = obj.bbox.joined(obj.cbbox) if obj.cbbox else obj.bbox
predicate = lambda c: not obj.bbox.contains(c.origin)
def predicate(c):
return not obj.bbox.contains(c.origin)
lines = self.charlines_in_area(oarea, predicate)
# print(obj, oarea, lines, [line.content for line in lines])
objects += list(sorted(lines + [obj], key=lambda o: (-o.bbox.y, o.bbox.x)))
Expand Down
8 changes: 4 additions & 4 deletions src/modm_data/pdf2html/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ def render_page_pdf(doc, page, new_doc=None, index=0):

if False:
for ii in range(20):
_vline(new_page, rotation, page.width * ii / 20, 0, page.height, width=1, stroke="black")
_hline(new_page, rotation, page.height * ii / 20, 0, page.width, width=1, stroke="black")
_vline(new_page, rotation, width * ii / 20, 0, height, width=1, stroke="black")
_hline(new_page, rotation, height * ii / 20, 0, width, width=1, stroke="black")

# for name, distance in page._spacing.items():
# if name.startswith("x_"):
# _vline(new_page, rotation, distance, 0, page.height, width=0.5, stroke=0xFFA500)
# _vline(new_page, rotation, distance, 0, height, width=0.5, stroke=0xFFA500)
# else:
# _hline(new_page, rotation, distance, 0, page.width, width=0.5, stroke=0xFFA500)
# _hline(new_page, rotation, distance, 0, width, width=0.5, stroke=0xFFA500)

for name, area in page._areas.items():
if isinstance(area, list):
Expand Down
1 change: 0 additions & 1 deletion src/modm_data/pdf2html/stmicro/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ def main():
output_path = Path(args.output)
output_path.parent.mkdir(parents=True, exist_ok=True)

document = None
if args.parallel:
log = Path(f"log/stmicro/html/{doc.name}.txt")
log.parent.mkdir(exist_ok=True, parents=True)
Expand Down
5 changes: 2 additions & 3 deletions src/modm_data/pdf2html/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ def _cluster(lines, key):
ygrid[self.bbox.top].append(HLine(self.bbox.top, self.bbox.left, self.bbox.right))

# Fix the position keys properly
self._xgrid = {int(round(statistics.fmean(m.p0.x for m in l))): l for l in xgrid.values()}
self._ygrid = {int(round(statistics.fmean(m.p0.y for m in l))): l for l in ygrid.values()}
self._xgrid = {int(round(statistics.fmean(m.p0.x for m in line))): line for line in xgrid.values()}
self._ygrid = {int(round(statistics.fmean(m.p0.y for m in line))): line for line in ygrid.values()}
# Map the positions to integers
self._xpos = list(sorted(self._xgrid))
self._ypos = list(sorted(self._ygrid))
Expand Down Expand Up @@ -134,7 +134,6 @@ def _cell_borders(self, x: int, y: int, bbox: Rectangle, mask: int = 0b1111) ->

def _fix_borders(self, cells, x: int, y: int):
# We are looking at the 9 neighbors around the cells
cell = cells[(x, y)]
c = cells[(x, y)].b
r = cells[(x + 1, y)].b if cells[(x + 1, y)] is not None else TableCell.Borders(0, 0, 1, 0)
t = cells[(x, y + 1)].b if cells[(x, y + 1)] is not None else TableCell.Borders(0, 1, 0, 0)
Expand Down
2 changes: 1 addition & 1 deletion src/modm_data/svd/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def _compare_trees(left, right):
return False
if not left.children:
return True
return all(_compare_trees(l, r) for l, r in zip(left.children, right.children))
return all(_compare_trees(left, right) for left, right in zip(left.children, right.children))


def compare_device_trees(left, right):
Expand Down
10 changes: 0 additions & 10 deletions src/modm_data/svd/write.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from lxml import etree
from .model import Device, Peripheral, Register, BitField
from cmsis_svd.parser import SVDParser


def _add_element(node, tag, text=None):
Expand Down Expand Up @@ -92,12 +91,3 @@ def format_svd(register_tree):
def write_svd(svd, path, pretty=True):
with open(path, "wb") as file:
svd.write(file, pretty_print=pretty, doctype='<?xml version="1.0" encoding="utf-8" standalone="no"?>')


def read_svd(path) -> Device:
parser = SVDParser.for_xml_file(path)
for peripheral in parser.get_device().peripherals:
print("%s @ 0x%08x" % (peripheral.name, peripheral.base_address))

with open(path, "wb") as file:
svd.write(file, pretty_print=pretty, doctype='<?xml version="1.0" encoding="utf-8" standalone="no"?>')
3 changes: 2 additions & 1 deletion tools/marimo/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ def __(mo):

@app.cell
def __(__file__, filter_chapter, filter_document, filter_table, mo):
import lxml.html, lxml.etree
import lxml.html
import lxml.etree
import re
from pathlib import Path
import pandas as pd
Expand Down
2 changes: 1 addition & 1 deletion tools/scripts/search_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def main():
chanode = anytree.Node("chapter", parent=docnode, obj=chapter)
for table in tables:
print(table, table.caption())
tabnode = anytree.Node("table", parent=chanode, obj=table)
anytree.Node("table", parent=chanode, obj=table)

html = format_document(rootnode)
with open(Path(args.html), "wb") as f:
Expand Down
9 changes: 4 additions & 5 deletions tools/scripts/synchronize_docs.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
# Copyright 2022, Niklas Hauser
# SPDX-License-Identifier: MPL-2.0

import re, os, sys, subprocess, shutil
import re
import sys
import subprocess
from pathlib import Path
from jinja2 import Environment
from os import listdir
from os.path import isfile, join, abspath
from collections import defaultdict

TABLE_TEMPLATE = \
r"""
Expand Down Expand Up @@ -55,7 +54,7 @@ def extract(text, key):

def format_table(items, width, align=None):
subs = {"items": items, "width": width}
if align: subs["align"] = align;
if align: subs["align"] = align
return Environment().from_string(TABLE_TEMPLATE).render(subs)

def template(path_in, path_out, substitutions):
Expand Down

0 comments on commit 1949d5b

Please sign in to comment.