From 681b7a382a8561cb303a4476cb605a43c44c1562 Mon Sep 17 00:00:00 2001 From: Richard Chien Date: Tue, 26 Jun 2018 10:25:11 +0800 Subject: [PATCH] Improve structure --- .gitignore | 2 -- none/__init__.py | 25 +++++++++---------- none/plugins/__init__.py | 0 none_demo/__init__.py | 0 none_demo/config.py | 9 +++++++ none_demo/plugins/__init__.py | 0 .../plugins/weather/__init__.py | 0 .../plugins/weather/expressions.py | 0 none_demo/run.py | 16 ++++++++++++ requirements.txt | 1 - run.py | 10 -------- setup.py | 25 +++++++++++++++++++ 12 files changed, 62 insertions(+), 26 deletions(-) create mode 100644 none/plugins/__init__.py create mode 100644 none_demo/__init__.py create mode 100644 none_demo/config.py create mode 100644 none_demo/plugins/__init__.py rename {none => none_demo}/plugins/weather/__init__.py (100%) rename {none => none_demo}/plugins/weather/expressions.py (100%) create mode 100644 none_demo/run.py delete mode 100644 requirements.txt delete mode 100644 run.py create mode 100644 setup.py diff --git a/.gitignore b/.gitignore index b10bd6a1..79f80263 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,4 @@ .idea -data -config.py __pycache__ .venv venv \ No newline at end of file diff --git a/none/__init__.py b/none/__init__.py index 9c194f8e..72bc1948 100644 --- a/none/__init__.py +++ b/none/__init__.py @@ -53,30 +53,29 @@ def create_bot(config_object: Any = None): _plugins = set() -def load_plugins(): - _plugins.clear() - none_dir = __path__[0] - plugins_dir = os.path.join(none_dir, 'plugins') - saved_cwd = os.getcwd() - os.chdir(none_dir) - for item in os.listdir(plugins_dir): - path = os.path.join(plugins_dir, item) +def load_plugins(plugin_dir: str, module_prefix: str): + for name in os.listdir(plugin_dir): + path = os.path.join(plugin_dir, name) if os.path.isfile(path) and \ - (item.startswith('_') or not item.endswith('.py')): + (name.startswith('_') or not name.endswith('.py')): continue if os.path.isdir(path) and \ - (path.startswith('_') or not os.path.exists( + (name.startswith('_') or not os.path.exists( os.path.join(path, '__init__.py'))): continue - m = re.match(r'([_A-Z0-9a-z]+)(.py)?', item) + m = re.match(r'([_A-Z0-9a-z]+)(.py)?', name) if not m: continue - mod_name = 'none.plugins.' + m.group(1) + mod_name = f'{module_prefix}.{m.group(1)}' try: _plugins.add(importlib.import_module(mod_name)) logger.info('Succeeded to import "{}"'.format(mod_name)) except ImportError: logger.warning('Failed to import "{}"'.format(mod_name)) - os.chdir(saved_cwd) + + +def load_builtin_plugins(): + plugin_dir = os.path.join(os.path.dirname(__file__), 'plugins') + load_plugins(plugin_dir, 'none.plugins') diff --git a/none/plugins/__init__.py b/none/plugins/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/none_demo/__init__.py b/none_demo/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/none_demo/config.py b/none_demo/config.py new file mode 100644 index 00000000..692e786d --- /dev/null +++ b/none_demo/config.py @@ -0,0 +1,9 @@ +import re + +from none.default_config import * + +SECRET = 'abc' + +SUPERUSERS = {1002647525} +COMMAND_START = {'', '/', '!', '/', '!', re.compile(r'^>+\s*')} +COMMAND_SEP = {'/', '.', re.compile(r'#|::?')} diff --git a/none_demo/plugins/__init__.py b/none_demo/plugins/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/none/plugins/weather/__init__.py b/none_demo/plugins/weather/__init__.py similarity index 100% rename from none/plugins/weather/__init__.py rename to none_demo/plugins/weather/__init__.py diff --git a/none/plugins/weather/expressions.py b/none_demo/plugins/weather/expressions.py similarity index 100% rename from none/plugins/weather/expressions.py rename to none_demo/plugins/weather/expressions.py diff --git a/none_demo/run.py b/none_demo/run.py new file mode 100644 index 00000000..17f088c5 --- /dev/null +++ b/none_demo/run.py @@ -0,0 +1,16 @@ +from os import path + +import none + +from none_demo import config + +bot = none.create_bot(config) + +none.load_builtin_plugins() +plugin_dir = path.join(path.dirname(__file__), 'plugins') +none.load_plugins(plugin_dir, 'none_demo.plugins') + +app = bot.asgi + +if __name__ == '__main__': + bot.run(host=config.HOST, port=config.PORT) diff --git a/requirements.txt b/requirements.txt deleted file mode 100644 index 8358b1da..00000000 --- a/requirements.txt +++ /dev/null @@ -1 +0,0 @@ -aiocqhttp diff --git a/run.py b/run.py deleted file mode 100644 index ff4eb441..00000000 --- a/run.py +++ /dev/null @@ -1,10 +0,0 @@ -import config -import none - -bot = none.create_bot(config) -none.load_plugins() - -app = bot.asgi - -if __name__ == '__main__': - bot.run(host=config.HOST, port=config.PORT) diff --git a/setup.py b/setup.py new file mode 100644 index 00000000..d69121e4 --- /dev/null +++ b/setup.py @@ -0,0 +1,25 @@ +from setuptools import setup, find_packages + +with open('README.md', 'r', encoding='utf-8') as f: + long_description = f.read() + +setup( + name='none-bot', + version='0.0.1', + packages=find_packages(include=('none', 'none.*')), + url='https://github.com/richardchien/none-bot', + license='AGPL-3.0', + author='Richard Chien', + author_email='richardchienthebest@gmail.com', + description='A QQ bot framework', + long_description=long_description, + long_description_content_type="text/markdown", + install_requires=['aiocqhttp>=0.3'], + python_requires='>=3.6', + platforms='any', + classifiers=( + "Programming Language :: Python :: 3", + "License :: OSI Approved :: MIT License", + "Operating System :: OS Independent", + ), +)