Skip to content

Commit

Permalink
✨ split events into single files
Browse files Browse the repository at this point in the history
  • Loading branch information
yanyongyu authored Feb 4, 2024
1 parent 0baeb3c commit fec42f4
Show file tree
Hide file tree
Showing 269 changed files with 2,435 additions and 2,150 deletions.
19 changes: 12 additions & 7 deletions codegen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ class Data:
payload_types: list[str]


def module_name(event: str, action: Data) -> str:
return f"{event}_{action.action}" if action.action else event


def pascal_case(*names: str) -> str:
words = chain.from_iterable(name.split("_") for name in names)
return "".join(word if word.isupper() else word.capitalize() for word in words)
Expand Down Expand Up @@ -82,6 +86,7 @@ def build_event():
output_dir.mkdir(parents=True, exist_ok=True)

env = Environment(loader=PackageLoader("codegen"))
env.globals["filename"] = module_name

# generate base model
template = env.get_template("_base.py.jinja")
Expand All @@ -96,13 +101,13 @@ def build_event():
# generate event models
template = env.get_template("event.py.jinja")
for event, actions in event_types.items():
event_text = template.render(
event=event,
actions=actions,
message_events=MESSAGE_EVENTS,
has_message_events=HAS_MESSAGE_EVENTS,
)
(output_dir / f"{event}.py").write_text(event_text)
for action in actions:
event_text = template.render(
action=action,
message_events=MESSAGE_EVENTS,
has_message_events=HAS_MESSAGE_EVENTS,
)
(output_dir / f"{module_name(event, action)}.py").write_text(event_text)

# generate init file
template = env.get_template("__init__.py.jinja")
Expand Down
8 changes: 4 additions & 4 deletions codegen/templates/__init__.py.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ if TYPE_CHECKING:
from ._types import events as events
{% for event, actions in types.items() %}
{% for action in actions %}
from .{{ event }} import {{ action.class_name }} as {{ action.class_name }}
from .{{ filename(event, action) }} import {{ action.class_name }} as {{ action.class_name }}
{% endfor %}
{% endfor %}
else:
__lazy_vars__ = {
{% for event, actions in types.items() %}
".{{ event }}": (
{% for action in actions %}
{% for action in actions %}
".{{ filename(event, action) }}": (
"{{ action.class_name }}",
{% endfor %}
),
{% endfor %}
{% endfor %}
"._base": (
"Event",
),
Expand Down
5 changes: 0 additions & 5 deletions codegen/templates/event.py.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@ from typing import Union
from functools import cached_property
from typing_extensions import override

{% for action in actions %}
{% for model in action.payload_types %}
from githubkit.versions.latest.models import {{ model }}
{% endfor %}
{% endfor %}

from ..message import Message

from ._base import Event

{% for action in actions %}

class {{ action.class_name }}(Event):
{% if action.payload_types | length > 1 %}
payload: Union[{{ action.payload_types | join(", ") }}]
Expand All @@ -40,4 +36,3 @@ class {{ action.class_name }}(Event):
keep_untouched = (cached_property,)

{% endif %}
{% endfor %}
1,114 changes: 560 additions & 554 deletions nonebot/adapters/github/event/__init__.py

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from githubkit.versions.latest.models import (
WebhookBranchProtectionConfigurationEnabled,
WebhookBranchProtectionConfigurationDisabled,
)

Expand All @@ -9,8 +8,3 @@
class BranchProtectionConfigurationDisabled(Event):

payload: WebhookBranchProtectionConfigurationDisabled


class BranchProtectionConfigurationEnabled(Event):

payload: WebhookBranchProtectionConfigurationEnabled
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from githubkit.versions.latest.models import WebhookBranchProtectionConfigurationEnabled

from ._base import Event


class BranchProtectionConfigurationEnabled(Event):

payload: WebhookBranchProtectionConfigurationEnabled
22 changes: 0 additions & 22 deletions nonebot/adapters/github/event/branch_protection_rule.py

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from githubkit.versions.latest.models import WebhookBranchProtectionRuleCreated

from ._base import Event


class BranchProtectionRuleCreated(Event):

payload: WebhookBranchProtectionRuleCreated
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from githubkit.versions.latest.models import WebhookBranchProtectionRuleDeleted

from ._base import Event


class BranchProtectionRuleDeleted(Event):

payload: WebhookBranchProtectionRuleDeleted
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from githubkit.versions.latest.models import WebhookBranchProtectionRuleEdited

from ._base import Event


class BranchProtectionRuleEdited(Event):

payload: WebhookBranchProtectionRuleEdited
28 changes: 0 additions & 28 deletions nonebot/adapters/github/event/check_run.py

This file was deleted.

8 changes: 8 additions & 0 deletions nonebot/adapters/github/event/check_run_completed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from githubkit.versions.latest.models import WebhookCheckRunCompleted

from ._base import Event


class CheckRunCompleted(Event):

payload: WebhookCheckRunCompleted
8 changes: 8 additions & 0 deletions nonebot/adapters/github/event/check_run_created.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from githubkit.versions.latest.models import WebhookCheckRunCreated

from ._base import Event


class CheckRunCreated(Event):

payload: WebhookCheckRunCreated
8 changes: 8 additions & 0 deletions nonebot/adapters/github/event/check_run_requested_action.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from githubkit.versions.latest.models import WebhookCheckRunRequestedAction

from ._base import Event


class CheckRunRequestedAction(Event):

payload: WebhookCheckRunRequestedAction
8 changes: 8 additions & 0 deletions nonebot/adapters/github/event/check_run_rerequested.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from githubkit.versions.latest.models import WebhookCheckRunRerequested

from ._base import Event


class CheckRunRerequested(Event):

payload: WebhookCheckRunRerequested
22 changes: 0 additions & 22 deletions nonebot/adapters/github/event/check_suite.py

This file was deleted.

8 changes: 8 additions & 0 deletions nonebot/adapters/github/event/check_suite_completed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from githubkit.versions.latest.models import WebhookCheckSuiteCompleted

from ._base import Event


class CheckSuiteCompleted(Event):

payload: WebhookCheckSuiteCompleted
8 changes: 8 additions & 0 deletions nonebot/adapters/github/event/check_suite_requested.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from githubkit.versions.latest.models import WebhookCheckSuiteRequested

from ._base import Event


class CheckSuiteRequested(Event):

payload: WebhookCheckSuiteRequested
8 changes: 8 additions & 0 deletions nonebot/adapters/github/event/check_suite_rerequested.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from githubkit.versions.latest.models import WebhookCheckSuiteRerequested

from ._base import Event


class CheckSuiteRerequested(Event):

payload: WebhookCheckSuiteRerequested
40 changes: 0 additions & 40 deletions nonebot/adapters/github/event/code_scanning_alert.py

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from githubkit.versions.latest.models import WebhookCodeScanningAlertAppearedInBranch

from ._base import Event


class CodeScanningAlertAppearedInBranch(Event):

payload: WebhookCodeScanningAlertAppearedInBranch
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from githubkit.versions.latest.models import WebhookCodeScanningAlertClosedByUser

from ._base import Event


class CodeScanningAlertClosedByUser(Event):

payload: WebhookCodeScanningAlertClosedByUser
8 changes: 8 additions & 0 deletions nonebot/adapters/github/event/code_scanning_alert_created.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from githubkit.versions.latest.models import WebhookCodeScanningAlertCreated

from ._base import Event


class CodeScanningAlertCreated(Event):

payload: WebhookCodeScanningAlertCreated
8 changes: 8 additions & 0 deletions nonebot/adapters/github/event/code_scanning_alert_fixed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from githubkit.versions.latest.models import WebhookCodeScanningAlertFixed

from ._base import Event


class CodeScanningAlertFixed(Event):

payload: WebhookCodeScanningAlertFixed
8 changes: 8 additions & 0 deletions nonebot/adapters/github/event/code_scanning_alert_reopened.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from githubkit.versions.latest.models import WebhookCodeScanningAlertReopened

from ._base import Event


class CodeScanningAlertReopened(Event):

payload: WebhookCodeScanningAlertReopened
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from githubkit.versions.latest.models import WebhookCodeScanningAlertReopenedByUser

from ._base import Event


class CodeScanningAlertReopenedByUser(Event):

payload: WebhookCodeScanningAlertReopenedByUser
22 changes: 0 additions & 22 deletions nonebot/adapters/github/event/custom_property.py

This file was deleted.

Loading

0 comments on commit fec42f4

Please sign in to comment.