Merge branch 'plugins-deps' into 'master'
Plugins deps See merge request poezio/poezio!81
This commit is contained in:
commit
e11d42f653
4 changed files with 38 additions and 19 deletions
|
@ -28,14 +28,13 @@ class Plugin(BasePlugin):
|
|||
help='Embed an image url into the contact\'s client',
|
||||
usage='<image_url>')
|
||||
|
||||
def embed_image_url(self, args):
|
||||
def embed_image_url(self, url):
|
||||
tab = self.api.current_tab()
|
||||
message = self.core.xmpp.make_message(tab.jid)
|
||||
message['body'] = args
|
||||
message['oob']['url'] = args
|
||||
if isinstance(tab, tabs.MucTab):
|
||||
message['type'] = 'groupchat'
|
||||
else:
|
||||
message['body'] = url
|
||||
message['oob']['url'] = url
|
||||
message['type'] = 'groupchat'
|
||||
if not isinstance(tab, tabs.MucTab):
|
||||
message['type'] = 'chat'
|
||||
tab.add_message(
|
||||
message['body'],
|
||||
|
@ -46,3 +45,6 @@ class Plugin(BasePlugin):
|
|||
typ=1,
|
||||
)
|
||||
message.send()
|
||||
# TODO: Fix refreshing. The following doesn't work.
|
||||
tab.refresh()
|
||||
self.core.tab_win.refresh()
|
||||
|
|
|
@ -16,6 +16,9 @@ This plugin adds a command to the chat tabs.
|
|||
|
||||
|
||||
"""
|
||||
|
||||
from typing import Optional
|
||||
|
||||
import asyncio
|
||||
import traceback
|
||||
from os.path import expanduser
|
||||
|
@ -30,7 +33,11 @@ from poezio import tabs
|
|||
|
||||
|
||||
class Plugin(BasePlugin):
|
||||
dependencies = {'embed'}
|
||||
|
||||
def init(self):
|
||||
self.embed = self.refs['embed']
|
||||
|
||||
if not self.core.xmpp['xep_0363']:
|
||||
raise Exception('slixmpp XEP-0363 plugin failed to load')
|
||||
for _class in (tabs.PrivateTab, tabs.StaticConversationTab, tabs.DynamicConversationTab, tabs.MucTab):
|
||||
|
@ -43,18 +50,23 @@ class Plugin(BasePlugin):
|
|||
short='Upload a file',
|
||||
completion=self.completion_filename)
|
||||
|
||||
async def async_upload(self, filename):
|
||||
async def upload(self, filename) -> Optional[str]:
|
||||
try:
|
||||
url = await self.core.xmpp['xep_0363'].upload_file(filename)
|
||||
except UploadServiceNotFound:
|
||||
self.api.information('HTTP Upload service not found.', 'Error')
|
||||
return
|
||||
return None
|
||||
except Exception:
|
||||
exception = traceback.format_exc()
|
||||
self.api.information('Failed to upload file: %s' % exception,
|
||||
'Error')
|
||||
return
|
||||
self.core.insert_input_text(url)
|
||||
return None
|
||||
return url
|
||||
|
||||
async def send_upload(self, filename):
|
||||
url = await self.upload(filename)
|
||||
if url is not None:
|
||||
self.embed.embed_image_url(url)
|
||||
|
||||
@command_args_parser.quoted(1)
|
||||
def command_upload(self, args):
|
||||
|
@ -63,7 +75,7 @@ class Plugin(BasePlugin):
|
|||
return
|
||||
filename, = args
|
||||
filename = expanduser(filename)
|
||||
asyncio.ensure_future(self.async_upload(filename))
|
||||
asyncio.ensure_future(self.send_upload(filename))
|
||||
|
||||
@staticmethod
|
||||
def completion_filename(the_input):
|
||||
|
|
|
@ -4,7 +4,7 @@ These are used in the plugin system added in poezio 0.7.5
|
|||
(see plugin_manager.py)
|
||||
"""
|
||||
|
||||
from typing import Set
|
||||
from typing import Any, Dict, Set
|
||||
from asyncio import iscoroutinefunction
|
||||
from functools import partial
|
||||
from configparser import RawConfigParser
|
||||
|
@ -406,6 +406,8 @@ class BasePlugin(object, metaclass=SafetyMetaclass):
|
|||
|
||||
default_config = None
|
||||
dependencies: Set[str] = set()
|
||||
# This dict will get populated when the plugin is initialized
|
||||
refs: Dict[str, Any] = {}
|
||||
|
||||
def __init__(self, name, plugin_api, core, plugins_conf_dir):
|
||||
self.__name = name
|
||||
|
|
|
@ -136,6 +136,8 @@ class PluginManager:
|
|||
name, dep
|
||||
)
|
||||
return None
|
||||
# Add reference of the dep to the plugin's usage
|
||||
module.Plugin.refs[dep] = self.plugins[dep]
|
||||
|
||||
self.plugins[name] = module.Plugin(name, self.plugin_api, self.core,
|
||||
self.plugins_conf_dir)
|
||||
|
@ -158,13 +160,14 @@ class PluginManager:
|
|||
|
||||
if name in self.plugins:
|
||||
try:
|
||||
self.plugins[name]._unloading = True # Prevents loops
|
||||
for rdep in self.rdeps[name].copy():
|
||||
if rdep in self.plugins and not self.plugins[rdep]._unloading:
|
||||
self.unload(rdep)
|
||||
if rdep in self.plugins:
|
||||
log.debug('Failed to unload reverse dependency %s first.', rdep)
|
||||
return None
|
||||
if self.plugins[name] is not None:
|
||||
self.plugins[name]._unloading = True # Prevents loops
|
||||
for rdep in self.rdeps[name].copy():
|
||||
if rdep in self.plugins and not self.plugins[rdep]._unloading:
|
||||
self.unload(rdep)
|
||||
if rdep in self.plugins:
|
||||
log.debug('Failed to unload reverse dependency %s first.', rdep)
|
||||
return None
|
||||
|
||||
for command in self.commands[name].keys():
|
||||
del self.core.commands[command]
|
||||
|
|
Loading…
Reference in a new issue