Esempio n. 1
0
def test_discover_flirror_modules_wrong_values():
    module_1 = FlirrorModule("module_1", __name__)
    module_2 = FlirrorModule("module_2", __name__)

    class invalid_plugin:
        FLIRROR_MODULE = [module_1]
        FLIRROR_MODULES = [5, module_2, "abc"]

    flirror_modules = discover_flirror_modules(
        {"invalid_plugin": invalid_plugin})

    # FLIRROR_MODULE cannot be loaded as it does not provide a single element.
    # FLIRROR_MODULES contains some invalid module (not instance of
    # FlirrorModule). Thus, only module_2 is discovered in the end.
    assert flirror_modules == [module_2]
Esempio n. 2
0
def test_discover_flirror_modules():
    module_1 = FlirrorModule("module_1", __name__)
    module_2 = FlirrorModule("module_2", __name__)
    module_3 = FlirrorModule("module_3", __name__)

    # As we are mainly relying on the functionality of getattr(), we could mock
    # a plugin module with a simple class providing the necessary attributes.
    class valid_plugin:
        FLIRROR_MODULE = module_1
        FLIRROR_MODULES = [module_2, module_3]

    flirror_modules = discover_flirror_modules({"valid_plugin": valid_plugin})

    # We assume that all modules are valid modules because they are all
    # instances of FlirrorModule.
    assert len(flirror_modules) == 3
    assert flirror_modules == [module_1, module_2, module_3]
Esempio n. 3
0
import logging

from flask import current_app, jsonify, render_template, request, Response

from flirror.modules import FlirrorModule

LOGGER = logging.getLogger(__name__)

clock_module = FlirrorModule("clock", __name__, template_folder="templates")


@clock_module.view()
def get() -> Response:
    # The clock module only uses a subset of the flirror.basic_get() method as
    # it does not need to access the database.
    module_id = request.args.get("module_id")
    if not module_id:
        return current_app.json_abort(400, "Parameter 'module_id' is missing")
    module_configs = [
        m for m in current_app.config.get("MODULES", {}) if m.get("id") == module_id
    ]
    if module_configs:
        module_config = module_configs[0]
    else:
        return current_app.json_abort(
            400,
            f"Could not find any module config for ID '{module_id}'. "
            "Are you sure this one is specified in the config file?",
        )

    context = {
Esempio n. 4
0
import logging
import time

from flask import current_app

from flirror.modules import FlirrorModule

LOGGER = logging.getLogger(__name__)

# TODO (felix): Define some default values in FlirrorModule?
demo_module = FlirrorModule("flirror_demo",
                            __name__,
                            template_folder="templates")


@demo_module.view()
def get():
    return current_app.basic_get(template_name="demo/index.html")


@demo_module.crawler()
def crawl(module_id, app, message):
    demo_data = {"_timestamp": time.time(), "message": message}
    app.store_module_data(module_id, demo_data)
Esempio n. 5
0
import logging
import time
from typing import Any, Dict, List, Tuple

from alpha_vantage.timeseries import TimeSeries
from flask import current_app, Response
from requests.exceptions import ConnectionError

from flirror.exceptions import CrawlerDataError
from flirror.modules import FlirrorModule

LOGGER = logging.getLogger(__name__)

stocks_module = FlirrorModule("stocks", __name__, template_folder="templates")


@stocks_module.view()
def get() -> Response:
    return current_app.basic_get(template_name="stocks/index.html")


@stocks_module.app_template_filter()
def list_filter(list_of_dicts_to_filter: List[Dict], key: str):
    return [d[key] for d in list_of_dicts_to_filter]


@stocks_module.crawler()
def crawl(
    module_id: str,
    app,
    api_key: str,
Esempio n. 6
0
import time
from datetime import datetime
from typing import Any, Dict

import feedparser
from flask import current_app, Response

from flirror.exceptions import CrawlerDataError
from flirror.modules import FlirrorModule

LOGGER = logging.getLogger(__name__)

DEFAULT_MAX_ITEMS = 5

newsfeed_module = FlirrorModule("newsfeed",
                                __name__,
                                template_folder="templates")


@newsfeed_module.view()
def get() -> Response:
    return current_app.basic_get(template_name="newsfeed/index.html")


@newsfeed_module.crawler()
def crawl(module_id: str,
          app,
          url: str,
          name: str,
          max_items: int = DEFAULT_MAX_ITEMS) -> None:
    LOGGER.info("Requesting news feed '%s' from '%s'", name, url)
Esempio n. 7
0
from flirror.modules import FlirrorModule

LOGGER = logging.getLogger(__name__)

DEFAULT_MAX_ITEMS = 5
# TODO (felix): Maybe we could use this also as a fallback if no calendar from
# the list matched.
DEFAULT_CALENDAR = "primary"

API_SERVICE_NAME = "calendar"
API_VERSION = "v3"

SCOPES = ["https://www.googleapis.com/auth/calendar.readonly"]

calendar_module = FlirrorModule("calendar",
                                __name__,
                                template_folder="templates")


@calendar_module.view()
def get() -> Response:
    return current_app.basic_get("calendar/index.html")


@calendar_module.crawler()
def crawl(module_id: str,
          app,
          calendars: List[str],
          max_items: int = DEFAULT_MAX_ITEMS) -> None:

    # TODO (felix): Get rid of this, it's only needed to store the oauth token
Esempio n. 8
0
    "02n": "wi wi-night-alt-cloudy",
    "03n": "wi wi-cloud",
    "04n": "wi wi-cloudy",
    "09n": "wi wi-night-showers-rain",
    "10n": "wi wi-night-rain",
    "11n": "wi wi-night-thunderstorm",
    "13n": "wi wi-night-snow",
    "50n": "wi wi-dust",
}

DEFAULT_TEMP_UNIT = "celsius"
DEFAULT_LANGUAGE = "en"

# TODO (felix): Define some default values in FlirrorModule?
weather_module = FlirrorModule("weather",
                               __name__,
                               template_folder="templates")


# A template filter to find the correct weather icon by name
@weather_module.app_template_filter()
def weather_icon(icon_name: str) -> Optional[str]:
    return WEATHER_ICONS.get(icon_name)


@weather_module.view()
def get() -> Response:
    return current_app.basic_get(template_name="weather/index.html")


@weather_module.crawler()