Skip to content

Commit 88c1f22

Browse files
committed
[src] Factor out common pdf2html code from STM32 specialization
1 parent 9dafb92 commit 88c1f22

File tree

28 files changed

+1506
-872
lines changed

28 files changed

+1506
-872
lines changed

src/modm_data/html/document.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright 2022, Niklas Hauser
22
# SPDX-License-Identifier: MPL-2.0
33

4-
import re
4+
import re, os
55
import logging
66
from pathlib import Path
77
from functools import cached_property
@@ -13,7 +13,7 @@
1313
class Document:
1414
def __init__(self, path: str):
1515
self.path = Path(path)
16-
self.relpath = self.path.relative_to(Path().cwd())
16+
self.relpath = os.path.relpath(self.path, Path().cwd())
1717
self.fullname = self.path.stem
1818
self.name = self.fullname.split("-")[0]
1919
self.version = self.fullname.split("-")[1]
@@ -41,6 +41,8 @@ def chapter(self, pattern: str) -> Chapter:
4141
LOGGER.error(f"Cannot find chapter with pattern '{pattern}'!")
4242
if len(chapters) > 1:
4343
LOGGER.error(f"Found multiple chapters with pattern '{pattern}'!")
44+
for chapter in chapters:
45+
LOGGER.error(f" - {chapter.name}")
4446
assert len(chapters) == 1
4547
return chapters[0]
4648

src/modm_data/html/stmicro/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Copyright 2022, Niklas Hauser
22
# SPDX-License-Identifier: MPL-2.0
33

4-
from .datasheet import DatasheetMicro, DatasheetSensor
4+
from .datasheet_sensor import DatasheetSensor
5+
from .datasheet_stm32 import DatasheetStm32
56
from .reference import ReferenceManual
67
from .document import load_documents, load_document_devices
78
from .document import datasheet_for_device, reference_manual_for_device
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Copyright 2022, Niklas Hauser
2+
# SPDX-License-Identifier: MPL-2.0
3+
4+
import re
5+
import itertools
6+
from pathlib import Path
7+
from functools import cached_property, cache
8+
from collections import defaultdict
9+
10+
from .helper import split_device_filter, split_package
11+
from ...html.text import ReDict
12+
13+
import modm_data.html as html
14+
15+
16+
class DatasheetSensor(html.Document):
17+
def __init__(self, path: str):
18+
super().__init__(path)
19+
20+
def __repr__(self) -> str:
21+
return f"DSsensor({self.fullname})"
22+
23+
@cache
24+
def register_map(self, assert_table=True):
25+
pass

src/modm_data/html/stmicro/datasheet.py renamed to src/modm_data/html/stmicro/datasheet_stm32.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414
import modm_data.html as html
1515

1616

17-
class DatasheetMicro(html.Document):
17+
class DatasheetStm32(html.Document):
1818
def __init__(self, path: str):
1919
super().__init__(path)
2020
self._id = {}
2121
self._devices = {}
2222

2323
def __repr__(self) -> str:
24-
return f"DSµC({self.fullname})"
24+
return f"DSstm32({self.fullname})"
2525

2626
@cached_property
2727
def device_family(self) -> str:
@@ -247,11 +247,3 @@ def packages_pins(self):
247247
data_pin["alternate"][af].extend(signals)
248248

249249
return data_packages, data_pins
250-
251-
252-
class DatasheetSensor(html.Document):
253-
def __init__(self, path: str):
254-
super().__init__(path)
255-
256-
def __repr__(self) -> str:
257-
return f"DSsens({self.fullname})"

src/modm_data/html/stmicro/document.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
from collections import defaultdict
66
from ...html import Document
77
from ...utils import cache_path, ext_path
8-
from .datasheet import DatasheetMicro, DatasheetSensor
8+
from .datasheet_stm32 import DatasheetStm32
9+
from .datasheet_sensor import DatasheetSensor
910
from .reference import ReferenceManual
1011
from ...owl import DeviceIdentifier
1112
from ...owl.stmicro import did_from_string
@@ -27,15 +28,15 @@ def load_documents() -> list:
2728
# FIXME: Better detection that DS13252 is a STM32WB55 module, not a chip!
2829
if any("STM32" in h.html for h in chap[0].headings()) and \
2930
"DS13252" not in doc.name and "DS14096" not in doc.name:
30-
documents[doc.name][doc.version] = DatasheetMicro(path)
31+
documents[doc.name][doc.version] = DatasheetStm32(path)
3132
else:
3233
documents[doc.name][doc.version] = DatasheetSensor(path)
3334
elif "RM" in doc.name:
3435
documents[doc.name][doc.version] = ReferenceManual(path)
3536
return documents
3637

3738

38-
def load_document_devices(use_cached=True) -> tuple[dict[DeviceIdentifier, DatasheetMicro],
39+
def load_document_devices(use_cached=True) -> tuple[dict[DeviceIdentifier, DatasheetStm32],
3940
dict[DeviceIdentifier, ReferenceManual]]:
4041
global DOCUMENT_CACHE
4142
if DOCUMENT_CACHE is not None:
@@ -48,7 +49,7 @@ def load_document_devices(use_cached=True) -> tuple[dict[DeviceIdentifier, Datas
4849

4950
docs = {}
5051
for path in set(json_data["ds"].values()):
51-
docs[path] = DatasheetMicro(path)
52+
docs[path] = DatasheetStm32(path)
5253
for path in set(json_data["rm"].values()):
5354
docs[path] = ReferenceManual(path)
5455
datasheets = {did_from_string(did): docs[path]
@@ -63,7 +64,7 @@ def load_document_devices(use_cached=True) -> tuple[dict[DeviceIdentifier, Datas
6364
doc = list(versions.values())[-1]
6465
# print(doc.path_pdf.relative_to(Path().cwd()), doc.path.relative_to(Path().cwd()))
6566
# print(doc.devices)
66-
if isinstance(doc, DatasheetMicro):
67+
if isinstance(doc, DatasheetStm32):
6768
if not doc.devices:
6869
raise ValueError(f"{doc} has no associated devices!")
6970
for dev in doc.devices:
@@ -120,7 +121,7 @@ def _document_for_device(did: DeviceIdentifier, documents):
120121
return None
121122

122123

123-
def datasheet_for_device(did: DeviceIdentifier) -> DatasheetMicro:
124+
def datasheet_for_device(did: DeviceIdentifier) -> DatasheetStm32:
124125
datasheets, _ = load_document_devices()
125126
return _document_for_device(did, datasheets)
126127

src/modm_data/html2owl/stmicro/__main__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from collections import defaultdict
1010
from multiprocessing.pool import ThreadPool
1111

12-
from modm_data.html.stmicro import DatasheetMicro, ReferenceManual, load_documents
12+
from modm_data.html.stmicro import DatasheetStm32, ReferenceManual, load_documents
1313
from modm_data.owl import Store
1414
from modm_data.py2owl.stmicro import owl_from_doc
1515

@@ -25,7 +25,7 @@ def main():
2525
for name, versions in load_documents().items():
2626
# always use latest version for now
2727
doc = list(versions.values())[-1]
28-
if isinstance(doc, DatasheetMicro):
28+
if isinstance(doc, DatasheetStm32):
2929
docs.append(doc)
3030
elif isinstance(doc, ReferenceManual):
3131
docs.append(doc)
@@ -40,7 +40,7 @@ def main():
4040

4141
path = Path(args.document).absolute()
4242
if path.stem.startswith("DS"):
43-
doc = DatasheetMicro(path)
43+
doc = DatasheetStm32(path)
4444
elif path.stem.startswith("RM"):
4545
doc = ReferenceManual(path)
4646

src/modm_data/html2svd/stmicro/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
# SPDX-License-Identifier: MPL-2.0
33

44
from .reference import memory_map_from_reference_manual
5+
from .datasheet import memory_map_from_datasheet

src/modm_data/html2svd/stmicro/__main__.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,17 @@
88
from pathlib import Path
99
from multiprocessing.pool import ThreadPool
1010

11-
from modm_data.html.stmicro import ReferenceManual, load_documents
12-
from modm_data.html2svd.stmicro import memory_map_from_reference_manual
11+
from modm_data.html.stmicro import ReferenceManual, DatasheetSensor, load_documents
12+
from modm_data.html2svd.stmicro import memory_map_from_reference_manual, memory_map_from_datasheet
1313
from modm_data.svd import format_svd, write_svd
1414
from modm_data.utils import ext_path
1515
from anytree import RenderTree
1616

1717

1818
def main():
1919
parser = argparse.ArgumentParser()
20-
parser.add_argument("--document", type=str, default="")
20+
parser.add_argument("--stm32", type=Path)
21+
parser.add_argument("--sensor", type=Path)
2122
parser.add_argument("--all", action="store_true", default=False)
2223
args = parser.parse_args()
2324

@@ -30,20 +31,25 @@ def main():
3031
docs.append(doc)
3132

3233
Path("log/stmicro/svd").mkdir(exist_ok=True, parents=True)
33-
calls = [f"python3 -m modm_data.html2svd.stmicro --document {doc.path} "
34+
calls = [f"python3 -m modm_data.html2svd.stmicro --stm32 {doc.path} "
3435
f"> log/stmicro/svd/html_{doc.name}.txt 2>&1" for doc in docs]
3536
with ThreadPool() as pool:
3637
retvals = list(tqdm.tqdm(pool.imap(lambda c: subprocess.run(c, shell=True), calls), total=len(calls)))
3738
for retval, call in zip(retvals, calls):
3839
if retval.returncode != 0: print(call)
3940
return all(r.returncode == 0 for r in retvals)
4041

41-
path = Path(args.document).absolute()
42-
doc = ReferenceManual(path)
42+
if args.stm32:
43+
doc = ReferenceManual(args.stm32.absolute())
44+
elif args.sensor:
45+
doc = DatasheetSensor(args.sensor.absolute())
4346
print(doc.path_pdf.relative_to(Path().cwd()),
4447
doc.path.relative_to(Path().cwd()))
4548

46-
mmaptrees = memory_map_from_reference_manual(doc)
49+
if args.stm32:
50+
mmaptrees = memory_map_from_reference_manual(doc)
51+
elif args.sensor:
52+
mmaptrees = memory_map_from_datasheet(doc)
4753
for mmaptree in mmaptrees:
4854
print(RenderTree(mmaptree, maxlevel=2))
4955
svd = format_svd(mmaptree)

0 commit comments

Comments
 (0)