Skip to content

Commit 4d2d9c9

Browse files
committed
scripts: updated code generation
1 parent 87b21b6 commit 4d2d9c9

29 files changed

+341
-4269
lines changed

scripts/inp_code.py

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,20 @@ def TABS(t):
1717
return t * ' '
1818

1919

20-
def CAMEL(name: str) -> str:
21-
name = re.sub('/', '_', name)
22-
if name:
23-
return name[0].upper() + name[1:]
20+
def CAMEL(name: str, more: str = '') -> str:
21+
if more:
22+
if '_' in name:
23+
name = CAMEL(name)
24+
return name.split('_')[0] + more + '_' + name.split('_')[1]
25+
else:
26+
return CAMEL(name) + more
2427
else:
25-
return ''
28+
name = re.sub('/', '_', name)
29+
30+
if name:
31+
return name[0].upper() + name[1:]
32+
else:
33+
return ''
2634

2735

2836
def SNAKE(name: str) -> str:
@@ -52,14 +60,24 @@ def _REGEX(element):
5260
elif attribute.type == 'types.Boolean':
5361
o += f'( {attribute.restriction})'
5462
else:
55-
o += f'( {{{attribute.type}._REGEX.pattern}})'
63+
if attribute.can_paren:
64+
o += f'( {{{attribute.type}._REGEX.pattern}}| [(]{{{attribute.type}._REGEX.pattern}}[)])'
65+
else:
66+
o += f'( {{{attribute.type}._REGEX.pattern}})'
5667

5768
if attribute.optional:
5869
o += '?'
5970

6071
return o
6172

6273

74+
def GET_REGEX(element):
75+
if element.regex:
76+
return element.regex
77+
else:
78+
return rf"{element.mnemonic}{r"(\d+)" if has_suffix(element) else ""}{r":(\S+)" if has_designator(element) else ""}{ATTRS_REGEX(element)}"
79+
80+
6381
def ATTRS_DATACLASS(element, t):
6482
a = []
6583
b = []
@@ -251,13 +269,13 @@ def INIT(element):
251269
from ._option import {CAMEL(element.name)}Option
252270
{''.join(f"from . import {SNAKE(option.name)}\n" if option.options else "" for option in element.options)[:-1]}
253271
{''.join(f'from .{CAMEL(option.name)} import {CAMEL(option.name)}\n' for option in element.options)[:-1]}
254-
{''.join(f'from .{CAMEL(option.name)} import {CAMEL(option.name).split('_')[0]}Builder{f"_{CAMEL(option.name).split('_')[1]}" if len(CAMEL(option.name).split('_')) - 1 else ""}\n' for option in element.options)[:-1]}
272+
{''.join(f'from .{CAMEL(option.name)} import {CAMEL(option.name, "Builder")}\n' for option in element.options)[:-1]}
255273
256274
__all__ = [
257275
"{CAMEL(element.name)}Option",
258276
{''.join(f'\t"{SNAKE(option.name)}",\n' if option.options else "" for option in element.options).strip()}
259277
{''.join(f'\t"{CAMEL(option.name)}",\n' for option in element.options).strip()}
260-
{''.join(f'\t"{CAMEL(option.name).split('_')[0]}Builder{f"_{CAMEL(option.name).split('_')[1]}" if len(CAMEL(option.name).split('_')) - 1 else ""}",\n' for option in element.options)[:-1].strip()}
278+
{''.join(f'\t"{CAMEL(option.name, "Builder")}",\n' for option in element.options).strip()}
261279
]
262280
"""[1:-1]
263281

@@ -283,7 +301,7 @@ class {CAMEL(element.name)}Option(Option):
283301
_KEYWORD = ""
284302
_SUBCLASSES = {{}}
285303
_REGEX = re.compile(
286-
rf"{r'|'.join(f"{option.mnemonic}{r'(\d+)' if has_suffix(option) else ''}{r':(\S+)' if has_designator(option) else ''}{ATTRS_REGEX(option)}" for option in sorted(element.options, reverse=True, key=lambda scheme: len(scheme.mnemonic)))}"
304+
rf"{r'|'.join(GET_REGEX(option) for option in sorted(element.options, reverse=True, key=lambda scheme: len(scheme.mnemonic)))}"
287305
)
288306
289307
def __init_subclass__(cls, keyword: str):
@@ -326,7 +344,7 @@ class {CAMEL(element.name)}({f"{CAMEL(parent_name)}Option, keyword='{element.mne
326344
327345
_ATTRS = {{{ATTRS_DICT(element)}}}
328346
329-
_REGEX = re.compile(rf"\\A{element.mnemonic}{r"(\d+)" if has_suffix(element) else ""}{r":(\S+)" if has_designator(element) else ""}{ATTRS_REGEX(element)}\\Z")
347+
_REGEX = re.compile(rf"\\A{GET_REGEX(element)}\\Z")
330348
331349
def __init__(self, {ATTRS_PARAM(element)}):
332350
"""
@@ -346,7 +364,7 @@ def __init__(self, {ATTRS_PARAM(element)}):
346364
347365
{element.extra.strip()}
348366
@dataclasses.dataclass
349-
class {CAMEL(element.name).split('_')[0]}Builder{f"_{CAMEL(element.name).split('_')[1]}" if len(CAMEL(element.name).split('_')) - 1 else ""}:
367+
class {CAMEL(element.name, "Builder")}:
350368
"""
351369
Builds ``{CAMEL(element.name)}``.
352370
@@ -358,10 +376,10 @@ class {CAMEL(element.name).split('_')[0]}Builder{f"_{CAMEL(element.name).split('
358376
359377
def build(self):
360378
"""
361-
Builds ``{CAMEL(element.name).split('_')[0]}Builder{f"_{CAMEL(element.name).split('_')[1]}" if len(CAMEL(element.name).split('_')) - 1 else ""}`` into ``{CAMEL(element.name)}``.
379+
Builds ``{CAMEL(element.name, "Builder")}`` into ``{CAMEL(element.name)}``.
362380
363381
Returns:
364-
``{CAMEL(element.name)}`` for ``{CAMEL(element.name).split('_')[0]}Builder{f"_{CAMEL(element.name).split('_')[1]}" if len(CAMEL(element.name).split('_')) - 1 else ""}``.
382+
``{CAMEL(element.name)}`` for ``{CAMEL(element.name, "Builder")}``.
365383
"""
366384
367385
{ATTRS_BUILDER(element, 2)}

0 commit comments

Comments
 (0)