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

View File

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

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

View File

@ -1,22 +0,0 @@
from none.command import Session, CommandGroup
from . import expressions as expr
w = CommandGroup('weather')
@w.command('weather', aliases=('天气', '天气预报'))
async def weather(session: Session):
city = session.require_arg('city', prompt_expr=expr.WHICH_CITY)
await session.send_expr(expr.REPORT, city=city)
@weather.args_parser
async def _(session: Session):
if session.current_key:
session.args[session.current_key] = session.current_arg_text.strip()
@w.command('suggestion', aliases=('生活指数', '生活建议', '生活提示'))
async def suggestion(session: Session):
await session.send('suggestion')

View File

@ -1,12 +0,0 @@
WHICH_CITY = (
'你想知道哪个城市的天气呢?',
'你要查询的城市是哪个呢?',
'你要查询哪个城市呢?',
'哪个城市呢?',
'请告诉我你要查询的城市~',
)
REPORT = (
'你查询了{city}的天气',
'{city}的天气是……',
)