Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Isolate components #22

Merged
merged 2 commits into from
Jun 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@
# Remove it from the list of argument, nobody should know
sys.argv.remove("--with_name")

target_dependencies = {
"facebook": ["flask", "requests_toolbelt"],
"stonks": ["yahoo-fin", "pandas"],
"arxiv": ["arxiv"],
"weather": ["pyowm"],
"wiki": ["wikipedia"],
"github": ["pygithub"],
"twitter": ["tweepy"],
"tests": ["numpy"]
}

# Create a special target for full
target_dependencies["full"] = set().union(*target_dependencies.values())

# Readup the readme
README = (pathlib.Path(__file__).parent / "readme.md").read_text()
setup(
Expand All @@ -29,20 +43,10 @@
package_dir={"": "src"},
packages=find_packages("src"),
install_requires=[
"numpy",
"requests",
"regex",
"arxiv",
"pyowm",
"psutil",
"wikipedia",
"pygithub",
"tweepy"
],
extras_require={
"facebook": ["flask", "requests_toolbelt"],
"stonks": ["yahoo-fin", "pandas"],
},
extras_require=target_dependencies,
entry_points={
"console_scripts": [
"{0} = pybliotecario.pybliotecario:main".format(pybliotecario_name),
Expand Down
4 changes: 3 additions & 1 deletion src/pybliotecario/backend/backend_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import pathlib
from datetime import datetime
import numpy as np

from pybliotecario.backend.telegram_util import TelegramMessage

Expand All @@ -20,6 +19,9 @@ class TestMessage(TelegramMessage):


def _create_fake_msg(text):
"""Create fake messages for testing"""
import numpy as np

ret = {
"update_id": np.random.randint(1000),
"message": {
Expand Down
5 changes: 2 additions & 3 deletions src/pybliotecario/components/arxiv_mod.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ def query_recent(category):
results = arxiv.Search(
query=category, max_results=75, sort_by=arxiv.SortCriterion.LastUpdatedDate
).results()
indx = -1
elements = []
for i, element in enumerate(results):
for _, element in enumerate(results):
time_s = element.updated
if not is_today(time_s):
break
Expand Down Expand Up @@ -149,7 +148,7 @@ class Arxiv(Component):

help_text = """ > Arxiv module
/arxiv arxiv_id: sends information about the given id
/arxiv_get arxiv_id: sends the PDF for the given id """
/arxiv_get arxiv_id: sends the PDF for the given id"""

def __init__(self, telegram_object, configuration=None, **kwargs):
super().__init__(telegram_object, configuration=configuration, **kwargs)
Expand Down
3 changes: 1 addition & 2 deletions src/pybliotecario/components/dnd.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ def roll_dice(text):

class DnD(Component):
help_text = """ > DnD module
/r, /roll dice [text]: roll a dice in the format NdM+Mod
"""
/r, /roll dice [text]: roll a dice in the format NdM+Mod"""

section_name = "DnD"
default_roll = "1d20"
Expand Down
3 changes: 1 addition & 2 deletions src/pybliotecario/components/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ def run_command(command):

class System(Component):
help_text = """ > System component
/system uptime: returns the uptime of the computer in which the bot lives
"""
/system uptime: returns the uptime of the computer in which the bot lives"""

def __init__(self, telegram_object, **kwargs):
super().__init__(telegram_object, **kwargs)
Expand Down
3 changes: 1 addition & 2 deletions src/pybliotecario/components/twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ class TwitterComponent(Component):
> Twitter module
/twitter_tl [@user] [N=20]: send the last N tweets from the TL [of user @user]
/twitter_mentions [N=5]: send the last N mentions
/twitter_tweet <text>: send a tweet
"""
/twitter_tweet <text>: send a tweet"""

def __init__(self, telegram_object, configuration=None, **kwargs):
super().__init__(telegram_object, configuration=configuration, **kwargs)
Expand Down
7 changes: 3 additions & 4 deletions src/pybliotecario/components/wiki.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

# TODO use the selected language

import regex
import re
import wikipedia
from pybliotecario.components.component_core import Component

# log = logging.getLogger(__name__)
GET_N = regex.compile(r"^\d+")
GET_N = re.compile(r"^\d+")
MAX_SIZE = 4000
WIKI_NAME = "WIKIPEDIA"
DEFAULT_LANGUAGE = "EN"
Expand All @@ -22,8 +22,7 @@ class WikiComponent(Component):

help_text = """ > Wikipedia module
/wiki term: search for term in wikipedia, return the summary
/wiki_full N term: read the full article and return N times the defined summary_size
"""
/wiki_full N term: read the full article and return N times the defined summary_size"""

def __init__(self, telegram_object, configuration=None, **kwargs):
super().__init__(telegram_object, configuration=configuration, **kwargs)
Expand Down
69 changes: 37 additions & 32 deletions src/pybliotecario/on_cmd_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
This is a design choice as this way it is not necessary to have all dependencies
if you want to run only some submodules of the pybliotecario.
"""

import subprocess as sp
import os
import logging
import importlib

Expand All @@ -33,12 +30,15 @@ def send_help(tele_api, chat_id):
("wiki", "WikiComponent"),
("system", "System"),
("stocks", "Stocks"),
("twitter", "TwitterComponent")
("twitter", "TwitterComponent"),
]
full_help = []
for module, cls in components:
Actor = import_component(module, cls)
full_help.append(Actor.help_msg())
try:
Actor = import_component(module, cls)
full_help.append(Actor.help_msg())
except ModuleNotFoundError:
pass
tele_api.send_message("\n".join(full_help), chat_id)
return None

Expand All @@ -50,32 +50,37 @@ def act_on_telegram_command(tele_api, message_obj, config):
tg_command = message_obj.command
chat_id = config["DEFAULT"]["chat_id"]

if tg_command == "ip":
from pybliotecario.components.ip_lookup import IpLookup as Actor
elif tg_command.lower() in ("is_pid_alive", "kill_pid"):
from pybliotecario.components.pid import ControllerPID as Actor
elif tg_command in ("arxiv_query", "arxiv", "arxivget", "arxiv_get"):
from pybliotecario.components.arxiv_mod import Arxiv as Actor
elif tg_command == "script":
from pybliotecario.components.scripts import Script as Actor
elif tg_command in ("r", "roll"):
from pybliotecario.components.dnd import DnD as Actor
elif tg_command in ("reaction_save", "reaction", "reaction_list"):
from pybliotecario.components.reactions import Reactions as Actor
elif tg_command[:4] == "wiki":
from pybliotecario.components.wiki import WikiComponent as Actor
elif tg_command == "system":
from pybliotecario.components.system import System as Actor
elif tg_command == "stock_price":
from pybliotecario.components.stocks import Stocks as Actor
elif tg_command.startswith("twitter_"):
from pybliotecario.components.twitter import TwitterComponent as Actor

elif tg_command == "help":
return send_help(tele_api, chat_id)
else:
log.info("No actor declared for this command: {0}".format(tg_command))
return None
try:
if tg_command == "ip":
from pybliotecario.components.ip_lookup import IpLookup as Actor
elif tg_command.lower() in ("is_pid_alive", "kill_pid"):
from pybliotecario.components.pid import ControllerPID as Actor
elif tg_command in ("arxiv_query", "arxiv", "arxivget", "arxiv_get"):
from pybliotecario.components.arxiv_mod import Arxiv as Actor
elif tg_command == "script":
from pybliotecario.components.scripts import Script as Actor
elif tg_command in ("r", "roll"):
from pybliotecario.components.dnd import DnD as Actor
elif tg_command in ("reaction_save", "reaction", "reaction_list"):
from pybliotecario.components.reactions import Reactions as Actor
elif tg_command[:4] == "wiki":
from pybliotecario.components.wiki import WikiComponent as Actor
elif tg_command == "system":
from pybliotecario.components.system import System as Actor
elif tg_command == "stock_price":
from pybliotecario.components.stocks import Stocks as Actor
elif tg_command.startswith("twitter_"):
from pybliotecario.components.twitter import TwitterComponent as Actor

elif tg_command == "help":
return send_help(tele_api, chat_id)
else:
log.info("No actor declared for this command: {0}".format(tg_command))
return None

except ModuleNotFoundError as e:
log.error(f"The component {tg_command} raised the following error: {e}")
return tele_api.send_message(f"Dependencies for {tg_command} are missing", chat_id)

actor_instance = Actor(
tele_api,
Expand Down
5 changes: 4 additions & 1 deletion src/pybliotecario/pybliotecario.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,10 @@ def main(cmdline_arg=None, tele_api=None, config=None):
if chat_id is not None:
config.set("DEFAULT", "chat_id", chat_id)

on_cmdline.run_command(args, tele_api, config)
try:
on_cmdline.run_command(args, tele_api, config)
except ModuleNotFoundError as e:
logger.error("In order to use this option you need to install the module '%s'", e.name)

if args.daemon:
logger.info("Activating main loop")
Expand Down