Skip to content

Commit

Permalink
Fix sourcegen compatibility with Python 3.8
Browse files Browse the repository at this point in the history
  • Loading branch information
speth authored and ischoegl committed Nov 4, 2022
1 parent f1d86d1 commit bae69c0
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 18 deletions.
3 changes: 2 additions & 1 deletion interfaces/sourcegen/sourcegen/_HeaderFileParser.py
Expand Up @@ -3,6 +3,7 @@

from pathlib import Path
import re
from typing import List

from ._dataclasses import HeaderFile, Func, Param

Expand All @@ -28,7 +29,7 @@ def _parse_func(cls, c_func: str) -> Func:
name = front[-1]
return Func(ret_type, name, params)

def __init__(self, path: Path, ignore_funcs: list[str] = None):
def __init__(self, path: Path, ignore_funcs: List[str] = None):
self._path = path
self._ignore_funcs = ignore_funcs

Expand Down
3 changes: 2 additions & 1 deletion interfaces/sourcegen/sourcegen/_SourceGenerator.py
Expand Up @@ -3,6 +3,7 @@

from abc import ABCMeta, abstractmethod
from pathlib import Path
from typing import List

from ._dataclasses import HeaderFile

Expand All @@ -15,5 +16,5 @@ def __init__(self, out_dir: Path, config: dict):
pass

@abstractmethod
def generate_source(self, headers_files: list[HeaderFile]):
def generate_source(self, headers_files: List[HeaderFile]):
pass
5 changes: 3 additions & 2 deletions interfaces/sourcegen/sourcegen/_dataclasses.py
Expand Up @@ -3,6 +3,7 @@

from dataclasses import dataclass
from pathlib import Path
from typing import List
import re
from ._helpers import with_unpack_iter

Expand All @@ -23,7 +24,7 @@ class Func:

ret_type: str
name: str
params: list[Param]
params: List[Param]


@dataclass(frozen=True)
Expand All @@ -32,4 +33,4 @@ class HeaderFile:
"""Represents information about a parsed C header file"""

path: Path
funcs: list[Func]
funcs: List[Func]
5 changes: 3 additions & 2 deletions interfaces/sourcegen/sourcegen/_orchestrate.py
Expand Up @@ -4,6 +4,7 @@
import importlib
import inspect
from pathlib import Path
from typing import List, Dict
import ruamel.yaml

from ._HeaderFileParser import HeaderFileParser
Expand All @@ -24,8 +25,8 @@ def generate_source(lang: str, out_dir: str):
with config_path.open() as config_file:
config = ruamel.yaml.safe_load(config_file)

ignore_files: list[str] = config.get("ignore_files", [])
ignore_funcs: dict[str, list[str]] = config.get("ignore_funcs", {})
ignore_files: List[str] = config.get("ignore_files", [])
ignore_funcs: Dict[str, List[str]] = config.get("ignore_funcs", {})

files = (HeaderFileParser(f, ignore_funcs.get(f.name, [])).parse()
for f in _clib_path.glob("*.h")
Expand Down
17 changes: 9 additions & 8 deletions interfaces/sourcegen/sourcegen/csharp/_CSharpSourceGenerator.py
Expand Up @@ -3,6 +3,7 @@

from itertools import starmap
from pathlib import Path
from typing import List, Dict
import re

from ._dataclasses import CsFunc
Expand All @@ -16,7 +17,7 @@ class CSharpSourceGenerator(SourceGenerator):
"""The SourceGenerator for scaffolding C# files for the .NET interface"""

@staticmethod
def _join_params(params: list[Param]) -> str:
def _join_params(params: List[Param]) -> str:
return ", ".join(p.p_type + " " + p.name for p in params)

def _get_interop_func_text(self, func: CsFunc) -> str:
Expand Down Expand Up @@ -49,7 +50,7 @@ def _get_derived_handle_text(derived_class_name: str, base_class_name: str) -> s


def _get_property_text(self, clib_area: str, c_name: str, cs_name: str,
known_funcs: dict[str, CsFunc]) -> str:
known_funcs: Dict[str, CsFunc]) -> str:
getter = known_funcs.get(clib_area + "_" + c_name)

if getter:
Expand Down Expand Up @@ -193,7 +194,7 @@ def _write_file(self, filename: str, contents: str):

self._out_dir.joinpath(filename).write_text(contents)

def _scaffold_interop(self, header_file_path: Path, cs_funcs: list[CsFunc]):
def _scaffold_interop(self, header_file_path: Path, cs_funcs: List[CsFunc]):
functions_text = "\n\n".join(map(self._get_interop_func_text, cs_funcs))

interop_text = normalize_indent(f"""
Expand All @@ -212,7 +213,7 @@ def _scaffold_interop(self, header_file_path: Path, cs_funcs: list[CsFunc]):
self._write_file("Interop.LibCantera." + header_file_path.name + ".g.cs",
interop_text)

def _scaffold_handles(self, header_file_path: Path, handles: dict[str, str]):
def _scaffold_handles(self, header_file_path: Path, handles: Dict[str, str]):
handles_text = "\n\n".join(starmap(self._get_base_handle_text, handles.items()))

handles_text = normalize_indent(f"""
Expand Down Expand Up @@ -240,8 +241,8 @@ def _scaffold_derived_handles(self):

self._write_file("Interop.Handles.g.cs", derived_handles_text)

def _scaffold_wrapper_class(self, clib_area: str, props: dict[str, str],
known_funcs: dict[str, CsFunc]):
def _scaffold_wrapper_class(self, clib_area: str, props: Dict[str, str],
known_funcs: Dict[str, CsFunc]):
wrapper_class_name = self._get_wrapper_class_name(clib_area)
handle_class_name = self._get_handle_class_name(clib_area)

Expand Down Expand Up @@ -277,10 +278,10 @@ def _scaffold_wrapper_class(self, clib_area: str, props: dict[str, str],

self._write_file(wrapper_class_name + ".g.cs", wrapper_class_text)

def generate_source(self, headers_files: list[HeaderFile]):
def generate_source(self, headers_files: List[HeaderFile]):
self._out_dir.mkdir(parents=True, exist_ok=True)

known_funcs: dict[str, list[CsFunc]] = {}
known_funcs: Dict[str, List[CsFunc]] = {}

for header_file in headers_files:
cs_funcs = list(map(self._convert_func, header_file.funcs))
Expand Down
7 changes: 4 additions & 3 deletions interfaces/sourcegen/sourcegen/csharp/_Config.py
Expand Up @@ -2,6 +2,7 @@
# at https://cantera.org/license.txt for license and copyright information.

from dataclasses import dataclass
from typing import Dict

from .._helpers import get_preamble, normalize_indent

Expand Down Expand Up @@ -31,11 +32,11 @@ class Config:
preamble = "/*\n" + get_preamble() + "*/"

# These we load from the parsed YAML config file
class_crosswalk: dict[str, str]
class_crosswalk: Dict[str, str]

derived_handles: dict[str, str]
derived_handles: Dict[str, str]

wrapper_classes: dict[str, dict[str, str]]
wrapper_classes: Dict[str, Dict[str, str]]

@staticmethod
def from_parsed(parsed_config_file: dict):
Expand Down
3 changes: 2 additions & 1 deletion interfaces/sourcegen/sourcegen/csharp/_dataclasses.py
Expand Up @@ -2,6 +2,7 @@
# at https://cantera.org/license.txt for license and copyright information.

from dataclasses import dataclass
from typing import Union

from .._helpers import with_unpack_iter
from .._dataclasses import Func
Expand All @@ -13,4 +14,4 @@ class CsFunc(Func):
"""Represents a C# interop method"""

is_handle_release_func: bool
handle_class_name: str | None
handle_class_name: Union[str, None]

0 comments on commit bae69c0

Please sign in to comment.