Makefile: Include py-topdir.mk

Include py-topdir.mk, which entails loads of fallout from make check. Fix it.

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2026-06-10 13:28:35 +02:00
commit e9845b5a1f
Signed by: Jan Lindemann
GPG key ID: 3750640C9E25DD61
45 changed files with 1796 additions and 1191 deletions

View file

@ -1,14 +1,16 @@
# -*- coding: utf-8 -*-
from __future__ import annotations
from typing import Any, List, Optional, Union
import fnmatch
import re
import re, fnmatch
from collections import OrderedDict
from enum import Enum, auto
from typing import TYPE_CHECKING
from ..log import *
if TYPE_CHECKING:
from typing import List, Optional, Union, Any
from ..log import DEBUG, get_caller_pos, slog
def quote(s):
if is_quoted(s):
@ -26,7 +28,7 @@ def is_quoted(s: str) -> bool:
if len(s) < 2:
return False
d = s[0]
if d == s[-1] and d in [ '"', "'" ]:
if d == s[-1] and d in ['"', "'"]:
return True
return False
@ -38,16 +40,18 @@ def cleanup_string(s: str) -> str:
return s[1:-1].replace('\\' + s[0], s[0])
return s
class StringTree: # export
class StringTree: # export
def __init__(self, path: str, content: str, parent: StringTree|None=None) -> None:
def __init__(
self, path: str, content: str, parent: StringTree | None = None
) -> None:
slog(DEBUG, f'Constructing StringTree(path="{path}", content="{content}")')
self.__parent = parent
self.children: OrderedDict[str, StringTree] = OrderedDict()
self.content: Optional[str] = None
self.__set(path, content)
assert(hasattr(self, "content"))
assert (hasattr(self, "content"))
#assert self.content is not None
# root (content = [ symbols ])
@ -65,60 +69,89 @@ class StringTree: # export
#parent.dump(INFO, "These children are added")
self.content = parent.content
for name, c in parent.children.items():
if not name in self.children.keys():
if name not in self.children.keys():
slog(DEBUG, f'At {self.content}: Adding new child {c}')
self.children[name] = c
else:
self.children[name].__adopt_children(c)
def __set(self, path_, content, split=True):
slog(DEBUG, ('At "{}": '.format(str(self.content)) if hasattr(self, "content") else "") + f'Setting "{path_}" -> "{content}"')
#assert self.content != str(content) # Not sure what the idea behind this was. It often goes off, and all works fine without.
if content is not None and not type(content) in [str, StringTree]:
raise Exception("Tried to add content of unsupported type {}".format(type(content).__name__))
def __set(self, path_, content, split = True):
slog(
DEBUG,
('At "{}": '.format(str(self.content)) if hasattr(self, "content") else "")
+ f'Setting "{path_}" -> "{content}"'
)
# Not sure what the idea behind this was. It often goes off, and all
# works fine without.
#assert self.content != str(content)
if content is not None and type(content) not in [str, StringTree]:
raise Exception(
"Tried to add content of unsupported type {}".format(
type(content).__name__
)
)
if path_ is None:
if isinstance(content, str):
self.content = cleanup_string(content)
elif isinstance(content, StringTree):
self.__adopt_children(content)
else:
raise Exception("Tried to add content of unsupported type {}".format(type(content).__name__))
slog(DEBUG, " -- content = >" + str(content) + "<, self.content = >" + str(self.content) + "<")
raise Exception(
"Tried to add content of unsupported type {}".format(
type(content).__name__
)
)
slog(
DEBUG,
" -- content = >" + str(content) + "<, self.content = >" +
str(self.content) + "<"
)
return self
path = cleanup_string(path_)
components = path.split('.') if split else [ path ]
l = len(components)
if len(path) == 0 or l == 0:
#assert self.content is None or (isinstance(content, StringTree) and content.content == self.content)
components = path.split('.') if split else [path]
length = len(components)
if len(path) == 0 or length == 0:
#assert self.content is None or (
# isinstance(content, StringTree) and content.content == self.content
#)
if isinstance(content, StringTree):
#assert isinstance(content, StringTree), "Type: " + type(content).__name__
#assert isinstance(content, StringTree), (
# f'Type: {type(content).__name__ }'
#)
self.__adopt_children(content)
else:
if self.content != content:
#self.content = cleanup_string(content)
slog(DEBUG, f'Changing content: "{self.content}" ->"{content}"')
assert(content != '"[a-zA-Z0-9+_*/-]"')
assert (content != '"[a-zA-Z0-9+_*/-]"')
self.content = content
#assert(content != "'antlr_doesnt_understand_vertical_tab'")
#self.children[content] = StringTree(None, content)
return self
#assert self.content is not None, "tried to set empty content to {}".format(path_)
#assert self.content is not None, f'Tried to set empty content to "{path_}"'
nibble = components[0]
rest = '.'.join(components[1:])
if nibble not in self.children:
self.children[nibble] = StringTree('', content=nibble, parent=self)
if l > 1:
self.children[nibble] = StringTree('', content = nibble, parent = self)
if length > 1:
assert len(rest) > 0
return self.children[nibble].__set(rest, content=content)
return self.children[nibble].__set(rest, content = content)
# last component, a.k.a. leaf
if content is not None:
gc = content if isinstance(content, StringTree) else StringTree('', content=content, parent=self.children[nibble])
gc = content if isinstance(content, StringTree) else StringTree(
'', content = content, parent = self.children[nibble]
)
# Make sure no existing grand child is updated. It would reside too
# far up in the grand child OrderedDict, we need it last
if gc.content in self.children[nibble].children:
del self.children[nibble].children[gc.content]
assert gc.content is not None, 'Grand-child content is None'
self.children[nibble].children[gc.content] = gc
return self.children[nibble]
@ -129,17 +162,17 @@ class StringTree: # export
r = self.get(path)
if r is None:
raise KeyError(path)
return r.value() # type: ignore
return r.value() # type: ignore
def __setitem__(self, key, value):
return self.__set(key, value)
def __dump(self, prio, indent=0, **kwargs):
def __dump(self, prio, indent = 0, **kwargs):
caller = kwargs['caller'] if 'caller' in kwargs.keys() else get_caller_pos(1)
slog(prio, '|' + (' ' * indent) + str(self.content), caller=caller)
slog(prio, '|' + (' ' * indent) + str(self.content), caller = caller)
indent += 2
for name, child in self.children.items():
child.__dump(prio, indent=indent, caller=caller)
child.__dump(prio, indent = indent, caller = caller)
@property
def path(self):
@ -164,7 +197,12 @@ class StringTree: # export
raise Exception("Tried to set empty content")
self.content = content
def add(self, path: str, content: Optional[Union[str, StringTree]] = None, split: bool = True) -> StringTree:
def add(
self,
path: str,
content: Optional[Union[str, StringTree]] = None,
split: bool = True
) -> StringTree:
slog(DEBUG, f'-- At "{self.content}": Adding "{path}" -> "{content}"')
return self.__set(path, content, split)
@ -176,7 +214,7 @@ class StringTree: # export
slog(DEBUG, "returning myself")
return self
if is_quoted(path_):
if not path in self.children.keys():
if path not in self.children.keys():
return None
return self.children[path]
components = path.split('.')
@ -185,7 +223,7 @@ class StringTree: # export
name = cleanup_string(components[0])
if not hasattr(self, "children"):
return None
if not name in self.children.keys():
if name not in self.children.keys():
slog(DEBUG, "Name \"" + name + "\" is not in children of", self.content)
for child in self.children:
slog(DEBUG, "child = ", child)
@ -193,7 +231,7 @@ class StringTree: # export
relpath = '.'.join(components[1:])
return self.children[name].get(relpath)
def value(self, path = None, default=None) -> Optional[str]:
def value(self, path = None, default = None) -> Optional[str]:
if path:
child = self.get(path)
if child is None:
@ -204,7 +242,7 @@ class StringTree: # export
if len(self.children) == 0:
raise Exception('tried to get value from leaf "{}"'.format(self.content))
slog(DEBUG, f'Returning value from children {self.children}')
return self.children[next(reversed(self.children))].content # type: ignore
return self.children[next(reversed(self.children))].content # type: ignore
@property
def parent(self):
@ -216,9 +254,12 @@ class StringTree: # export
return self
return self.__parent.root
def child_list(self, depth_first: bool=True) -> List[StringTree]:
if depth_first == False:
raise Exception("tried to retrieve child list with breadth-first search, not yet implemented")
def child_list(self, depth_first: bool = True) -> List[StringTree]:
if not depth_first:
raise Exception(
'Tried to retrieve child list with breadth-first '
'search, not yet implemented'
)
r = []
for name, c in self.children.items():
r.append(c)
@ -230,32 +271,30 @@ class StringTree: # export
msg = ''
if args is not None:
msg = ' ' + ' '.join(args) + ' '
slog(prio, ",------------" + msg + "----------- >", caller=caller)
self.__dump(prio, indent=0, caller=caller)
slog(prio, "`------------" + msg + "----------- <", caller=caller)
slog(prio, ",------------" + msg + "----------- >", caller = caller)
self.__dump(prio, indent = 0, caller = caller)
slog(prio, "`------------" + msg + "----------- <", caller = caller)
class Match(Enum):
Equal = auto()
RegExArg = auto()
RegExConf = auto()
GlobArg = auto()
GlobConf = auto()
Equal = auto()
RegExArg = auto()
RegExConf = auto()
GlobArg = auto()
GlobConf = auto()
def __find(self, key: str|None, val: str|None, match: Match, depth_first: bool):
def __find(self, key: str | None, val: str | None, m: Match, depth_first: bool):
def __children():
for name, child in self.children.items():
ret.extend(child.__find(key, val, match, depth_first))
ret.extend(child.__find(key, val, m, depth_first))
def __self():
_val = self.value()
_content = self.content
try:
if (
(key == _content and matcher(val, _val))
or (key is None and matcher(val, _val))
or (key == _content and val is None)
):
if ((key == _content and matcher(val, _val))
or (key is None and matcher(val, _val))
or (key == _content and val is None)):
ret.append(self)
except Exception as e:
if isinstance(e, re.PatternError):
@ -263,29 +302,33 @@ class StringTree: # export
else:
raise
def __debug_matcher(matcher, log_level=DEBUG):
def __select_matcher(m: StringTree.Match) -> Any:
match m:
case self.Match.Equal:
return lambda x, y: x == y
case self.Match.RegExArg:
return lambda x, y: re.search(x, y) is not None
case self.Match.RegExConf:
return lambda x, y: re.search(y, x) is not None
case self.Match.GlobArg:
return lambda x, y: fnmatch.fnmatch(y, x)
case self.Match.GlobConf:
return lambda x, y: fnmatch.fnmatch(x, y)
case _:
raise NotImplementedError(f'Matcher {m} is not yet implemented')
def __debug_matcher(matcher, log_level = DEBUG):
def __matcher(x, y):
slog(log_level, f'Comparing "{x}" ~ "{y}"')
return matcher(x, y)
return __matcher
if not self.children:
return []
matcher = lambda x, y: x == y
match match:
case self.Match.Equal:
pass
case self.Match.RegExArg:
matcher = lambda x, y: re.search(x, y) is not None
case self.Match.RegExConf:
matcher = lambda x, y: re.search(y, x) is not None
case self.Match.GlobArg:
matcher = lambda x, y: fnmatch.fnmatch(y, x)
case self.Match.GlobConf:
matcher = lambda x, y: fnmatch.fnmatch(x, y)
case _:
raise NotImplementedError(f'Matcher {match} is not yet implemented')
matcher = __select_matcher(m)
ret: list[StringTree] = []
@ -298,5 +341,16 @@ class StringTree: # export
return ret
def find(self, key: str|None=None, val: str|None=None, match: Match=Match.Equal, depth_first: bool=False):
return [ node.parent.path for node in self.__find(key, val, match=match, depth_first=depth_first)]
def find(
self,
key: str | None = None,
val: str | None = None,
match: Match = Match.Equal,
depth_first: bool = False
):
ret: list[str] = []
for node in self.__find(key, val, m = match, depth_first = depth_first):
if node.parent is None:
break
ret.append(node.parent.path)
return ret

View file

@ -1,9 +1,9 @@
# -*- coding: utf-8 -*-
import glob
import os
import re
import os, glob
from .StringTree import *
from ..log import *
from ..log import DEBUG, ERR, INFO, slog, slog_m
from .StringTree import StringTree, cleanup_string
def _cleanup_line(line: str) -> str:
line = line.strip()
@ -15,18 +15,22 @@ def _cleanup_line(line: str) -> str:
if c == in_quote:
in_quote = None
else:
if c in [ '"', "'" ]:
if c in ['"', "'"]:
in_quote = c
elif in_quote is None and c == '#':
return r.strip()
r += c
if len(r) >= 2 and r[0] in [ '"', "'" ] and r[-1] == r[0]:
if len(r) >= 2 and r[0] in ['"', "'"] and r[-1] == r[0]:
return r[1:-1]
return r
def parse(s: str, allow_full_lines: bool=True, root_content: str='root') -> StringTree: # export
def parse( # export
s: str,
allow_full_lines: bool = True,
root_content: str = 'root'
) -> StringTree:
slog_m(DEBUG, "--->--- parsing --->---\n" + s + "\n---<--- parsing ---<---\n")
root = StringTree('', content=root_content)
root = StringTree('', content = root_content)
sec = ''
for line in s.splitlines():
slog(DEBUG, f'Parsing: "{line}"')
@ -47,7 +51,7 @@ def parse(s: str, allow_full_lines: bool=True, root_content: str='root') -> Stri
root.add(sec)
continue
elif line[0] == ']':
assert(len(sec) > 0)
assert (len(sec) > 0)
sec = '.'.join(sec.split('.')[0:-1])
continue
lhs = ''
@ -67,17 +71,19 @@ def parse(s: str, allow_full_lines: bool=True, root_content: str='root') -> Stri
raise Exception("failed to parse assignment", line)
rhs = 'empty'
split = False
root.add(sec + '.' + cleanup_string(lhs), cleanup_string(rhs), split=split)
root.add(sec + '.' + cleanup_string(lhs), cleanup_string(rhs), split = split)
return root
def _read_lines_from_one_path(path: str, throw=True, level=0, log_prio=INFO, paths_buf=None):
def _read_lines_from_one_path(
path: str, throw = True, level = 0, log_prio = INFO, paths_buf = None
):
try:
with open(path, 'r') as infile:
slog(log_prio, 'Reading {}"{}"'.format(' ' * level * 2, path))
if paths_buf is not None:
paths_buf.append(path)
ret = []
for line in infile: # lines are all trailed by \n
for line in infile: # lines are all trailed by \n
m = re.search(r'^\s*(-)*include\s+(\S+)', line)
if m:
optional = m.group(1) == '-'
@ -86,7 +92,12 @@ def _read_lines_from_one_path(path: str, throw=True, level=0, log_prio=INFO, pat
dir_name = os.path.dirname(path)
if len(dir_name):
include_path = dir_name + '/' + include_path
include_lines = _read_lines(include_path, throw=(not optional), level=level+1, paths_buf=paths_buf)
include_lines = _read_lines(
include_path,
throw = (not optional),
level = level + 1,
paths_buf = paths_buf
)
if include_lines is None:
slog(DEBUG, f'{path}: Failed to process "{line}"')
continue
@ -100,17 +111,26 @@ def _read_lines_from_one_path(path: str, throw=True, level=0, log_prio=INFO, pat
raise
return None
def _read_lines(path: str, throw=True, level=0, log_prio=INFO, paths_buf=None):
def _read_lines(path: str, throw = True, level = 0, log_prio = INFO, paths_buf = None):
paths = glob.glob(path)
ret = []
for p in paths:
rr = _read_lines_from_one_path(p, throw=throw, level=level, log_prio=log_prio, paths_buf=paths_buf)
rr = _read_lines_from_one_path(
p, throw = throw, level = level, log_prio = log_prio, paths_buf = paths_buf
)
if rr is None:
return None
ret.extend(rr)
return ret
def read(path: str, root_content: str='root', log_prio=INFO, paths_buf=None) -> StringTree: # export
lines = _read_lines_from_one_path(path, log_prio=log_prio, paths_buf=paths_buf)
def read( # export
path: str,
root_content: str = 'root',
log_prio = INFO,
paths_buf = None
) -> StringTree:
lines = _read_lines_from_one_path(path, log_prio = log_prio, paths_buf = paths_buf)
if lines is None:
raise Exception(f'Could not read ini file from "{path}"')
s = ''.join(lines)
return parse(s, root_content=root_content)
return parse(s, root_content = root_content)