Skip to content

Commit

Permalink
🐛 version 0.5.2
Browse files Browse the repository at this point in the history
fix codegen
  • Loading branch information
RF-Tar-Railt committed May 19, 2024
1 parent b3a1c27 commit 17536d8
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 14 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "tarina"
version = "0.5.1"
version = "0.5.2"
description = "A collection of common utils for Arclet"
authors = [
{name = "RF-Tar-Railt", email = "rf_tar_railt@qq.com"},
Expand Down
4 changes: 3 additions & 1 deletion src/tarina/i18n/model.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# This file is @generated by tarina.lang CLI tool
# It is not intended for manual editing.

from tarina.lang.model import LangModel, LangItem


Expand All @@ -11,4 +14,3 @@ class Lang_:

class Lang(LangModel):
lang = Lang_

14 changes: 7 additions & 7 deletions src/tarina/lang/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
from pathlib import Path
import json

CONFIG_TEMPLATE = """
CONFIG_TEMPLATE = """\
{
"default": "zh-CN",
"frozen": [],
"require": []
}
"""

CONFIG_INIT = """
CONFIG_INIT = """\
# This file is @generated by tarina.lang CLI tool
# It is not intended for manual editing.
Expand All @@ -25,7 +25,7 @@
"""


TEMPLATE_SCHEMA = """
TEMPLATE_SCHEMA = """\
{
"title": "Template",
"description": "Template for lang items to generate schema for lang files",
Expand Down Expand Up @@ -61,16 +61,16 @@
}
"""

TEMPLATE_TEMPLATE = """
TEMPLATE_TEMPLATE = """\
{
"$schema": "./.template.schema.json",
"$schema": ".template.schema.json",
"scopes": []
}
"""

LANG_TEMPLATE = """
LANG_TEMPLATE = """\
{
"$schema": "./.lang.schema.json"
"$schema": ".lang.schema.json"
}
"""

Expand Down
11 changes: 6 additions & 5 deletions src/tarina/lang/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from pathlib import Path
from tarina.lang import lang
from tarina.lang.schema import _TemplateDict, get_template
from tarina.tools import pascal_case
import keyword


Expand All @@ -27,15 +28,15 @@ class LangModel:
from tarina.lang.model import LangModel, LangItem
{scope_classes}
class Lang(LangModel):
{scopes}
{scope_classes}class Lang(LangModel):
{scopes}\
"""


SCOPE_TEMPLATE = """\
class {scope}:
{types}
"""

TYPE_TEMPLATE = """\
Expand All @@ -55,15 +56,15 @@ def generate_model(root: Path):
scope = re.sub(r"[\W\s]+", "_", s["scope"])
if scope in keyword.kwlist:
scope += "_"
scope_class = scope.capitalize()
scope_class = pascal_case(scope)
if scope_class == "Lang":
scope_class += "_"
types_str = ""
for t in s["types"]:
name = re.sub(r"[\W\s]+", "_", t)
if name in keyword.kwlist:
name += "_"
types_str += TYPE_TEMPLATE.format(name=t, scope=s["scope"], type=t)
types_str += TYPE_TEMPLATE.format(name=name, scope=s["scope"], type=t)
scopes_classes += SCOPE_TEMPLATE.format(scope=scope_class, types=types_str)
scopes_str += f" {scope} = {scope_class}\n"
return MODEL_TEMPLATE.format(scope_classes=scopes_classes, scopes=scopes_str)
Expand Down
24 changes: 24 additions & 0 deletions src/tarina/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,27 @@ def safe_eval(route: str, _locals: Dict[str, Any]):
else:
res = getattr(res, part)
return res


def uncapitalize(source: str) -> str:
return source[0].lower() + source[1:]


def camel_case(source: str) -> str:
return re.sub("[_-][a-z]", lambda mat: mat[0][1:].upper(), source)


def pascal_case(source: str) -> str:
return re.sub("[_-][a-z]", lambda mat: mat[0][1:].upper(), source.capitalize())


def param_case(source: str) -> str:
return re.sub(
".[A-Z]+", lambda mat: mat[0][0] + "-" + mat[0][1:].lower(), uncapitalize(source).replace("_", "-")
)


def snake_case(source: str) -> str:
return re.sub(
".[A-Z]", lambda mat: mat[0][0] + "_" + mat[0][1:].lower(), uncapitalize(source).replace("-", "_")
)

0 comments on commit 17536d8

Please sign in to comment.