Improve structure

This commit is contained in:
Richard Chien 2018-06-26 10:25:11 +08:00
parent c9291e1cba
commit 681b7a382a
12 changed files with 62 additions and 26 deletions

2
.gitignore vendored
View File

@ -1,6 +1,4 @@
.idea .idea
data
config.py
__pycache__ __pycache__
.venv .venv
venv venv

View File

@ -53,30 +53,29 @@ def create_bot(config_object: Any = None):
_plugins = set() _plugins = set()
def load_plugins(): def load_plugins(plugin_dir: str, module_prefix: str):
_plugins.clear() for name in os.listdir(plugin_dir):
none_dir = __path__[0] path = os.path.join(plugin_dir, name)
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)
if os.path.isfile(path) and \ if os.path.isfile(path) and \
(item.startswith('_') or not item.endswith('.py')): (name.startswith('_') or not name.endswith('.py')):
continue continue
if os.path.isdir(path) and \ 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'))): os.path.join(path, '__init__.py'))):
continue continue
m = re.match(r'([_A-Z0-9a-z]+)(.py)?', item) m = re.match(r'([_A-Z0-9a-z]+)(.py)?', name)
if not m: if not m:
continue continue
mod_name = 'none.plugins.' + m.group(1) mod_name = f'{module_prefix}.{m.group(1)}'
try: try:
_plugins.add(importlib.import_module(mod_name)) _plugins.add(importlib.import_module(mod_name))
logger.info('Succeeded to import "{}"'.format(mod_name)) logger.info('Succeeded to import "{}"'.format(mod_name))
except ImportError: except ImportError:
logger.warning('Failed to import "{}"'.format(mod_name)) 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')

0
none/plugins/__init__.py Normal file
View File

0
none_demo/__init__.py Normal file
View File

9
none_demo/config.py Normal file
View File

@ -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'#|::?')}

View File

16
none_demo/run.py Normal file
View File

@ -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)

View File

@ -1 +0,0 @@
aiocqhttp

10
run.py
View File

@ -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)

25
setup.py Normal file
View File

@ -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",
),
)