From 50afa23b9dc26f914522a55fe5cbb893d34ad27c Mon Sep 17 00:00:00 2001
From: romangraef <roman.graef@gmail.com>
Date: Sat, 17 Mar 2018 22:11:47 +0100
Subject: added todos as well as regex based command triggers.

---
 lib/__init__.py     | 64 ++++-------------------------------------------------
 lib/commands.py     | 48 ++++++++++++++++++++++++++++++++++++++++
 lib/common.py       | 16 ++++++++++++++
 lib/match_script.py | 42 +++++++++++++++++++++++++++++++++++
 4 files changed, 110 insertions(+), 60 deletions(-)
 create mode 100644 lib/commands.py
 create mode 100644 lib/common.py
 create mode 100644 lib/match_script.py

(limited to 'lib')

diff --git a/lib/__init__.py b/lib/__init__.py
index edb1b42..0cec7cf 100644
--- a/lib/__init__.py
+++ b/lib/__init__.py
@@ -1,61 +1,5 @@
-from typing import List
+from .commands import CommandContext, get_all_commands, get_command_name, get_command_description, description, name, \
+    is_command, register_command, get_command_by_name
 
-import pyrogram
-from pyrogram.api import types as tgtypes
-
-
-def property_decorator(key):
-    def decorator(value):
-        def wrapper(func):
-            setattr(func, key, value)
-            return func
-
-        return wrapper
-
-    return decorator
-
-
-name = property_decorator('name')
-description = property_decorator('description')
-
-
-def is_command(func):
-    return hasattr(func, 'name') and func.name is not None
-
-
-def get_command_name(func):
-    return func.name
-
-
-def get_command_description(func):
-    return func.description
-
-
-commands = {}
-
-
-def get_all_commands():
-    return commands.values()
-
-
-class CommandContext(object):
-    def __init__(self, client: pyrogram.Client, channel, args: List[str], message: tgtypes.Message):
-        import re
-        self.args = args
-        self.client = client
-        self.channel = channel
-        self.message = message
-        self.rest_content = re.sub('^.*? ', '', message.message)
-        self.author = message.from_id
-
-    def respond(self, text):
-        self.client.send_message(self.channel, text=text)
-
-    def edit(self, text):
-        self.client.edit_message_text(chat_id=self.channel, message_id=self.message.id, text=text)
-
-
-def register_command(func):
-    if not is_command(func):
-        return
-    commands[get_command_name(func)] = func
+from .match_script import get_match_script_matcher, is_match_script, match_text, register_match_script, \
+    get_match_scripts, MatchContext
diff --git a/lib/commands.py b/lib/commands.py
new file mode 100644
index 0000000..638ab9b
--- /dev/null
+++ b/lib/commands.py
@@ -0,0 +1,48 @@
+import typing
+
+import pyrogram
+from pyrogram.api import types as tgtypes
+
+from lib.common import CommonContext
+from util import property_decorator
+
+commands = {}
+
+
+def get_all_commands():
+    return commands.values()
+
+
+class CommandContext(CommonContext):
+    def __init__(self, client: pyrogram.Client, channel, args: typing.List[str], message: tgtypes.Message):
+        super().__init__(client, channel, message)
+        import re
+        self.args = args
+        self.rest_content = re.sub('^.*? ', '', message.message)
+
+
+
+def register_command(func):
+    if not is_command(func):
+        return
+    commands[get_command_name(func)] = func
+
+
+def is_command(func):
+    return hasattr(func, 'name') and func.name is not None
+
+
+def get_command_name(func):
+    return func.name
+
+
+def get_command_description(func):
+    return func.description
+
+
+def get_command_by_name(command_name: str):
+    return commands[command_name.lower()]
+
+
+name = property_decorator('name')
+description = property_decorator('description')
diff --git a/lib/common.py b/lib/common.py
new file mode 100644
index 0000000..ade2891
--- /dev/null
+++ b/lib/common.py
@@ -0,0 +1,16 @@
+import pyrogram
+from pyrogram.api import types as tgtypes
+
+
+class CommonContext:
+    def __init__(self, client: pyrogram.Client, channel, message: tgtypes.Message):
+        self.client = client
+        self.channel = channel
+        self.message = message
+        self.author = message.from_id
+
+    def respond(self, text):
+        self.client.send_message(self.channel, text=text)
+
+    def edit(self, text):
+        self.client.edit_message_text(chat_id=self.channel, message_id=self.message.id, text=text)
diff --git a/lib/match_script.py b/lib/match_script.py
new file mode 100644
index 0000000..124c23b
--- /dev/null
+++ b/lib/match_script.py
@@ -0,0 +1,42 @@
+import pyrogram
+from pyrogram.api import types as tgtypes
+
+from lib.common import CommonContext
+
+
+def match_text(regex):
+    import re
+    regex = re.compile(regex)
+
+    def wrapper(func):
+        func.regex = regex
+        return func
+
+    return wrapper
+
+
+match_scripts = {}
+
+
+def is_match_script(func):
+    return hasattr(func, 'regex') and func.regex is not None
+
+
+def get_match_script_matcher(func):
+    return func.regex
+
+
+def register_match_script(func):
+    match_scripts[func.regex] = func
+
+
+class MatchContext(CommonContext):
+    def __init__(self, client: pyrogram.Client, channel, message: tgtypes.Message, match, groups, named_groups):
+        super().__init__(client, channel, message)
+        self.match = match
+        self.groups = groups
+        self.named_groups = named_groups
+
+
+def get_match_scripts():
+    return match_scripts
-- 
cgit