diff --git a/README.md b/README.md index 4801a5b..6473d28 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,7 @@ Available commands are: parameters. Downloaded files will be stored in a temporary directory, and their file URI will be passed back in the output JSON. +* `GET /clean` to clean temporary downloaded files. * `exit` to quit the script and end the conversation. JSON responses are the same one as from the HTTP server script. It is diff --git a/cozyweboob/WeboobProxy.py b/cozyweboob/WeboobProxy.py index 6fe077d..2b59a2a 100755 --- a/cozyweboob/WeboobProxy.py +++ b/cozyweboob/WeboobProxy.py @@ -6,6 +6,7 @@ Konnectors easily. Part of this code comes from [Kresus](https://github.com/bnjbvr/kresus/) written by bnjbvr and released under MIT. """ +from __future__ import absolute_import from __future__ import print_function import logging diff --git a/cozyweboob/__init__.py b/cozyweboob/__init__.py index 0c01c82..fb729aa 100644 --- a/cozyweboob/__init__.py +++ b/cozyweboob/__init__.py @@ -1,7 +1,9 @@ """ CozyWeboob main module """ -from cozyweboob.WeboobProxy import WeboobProxy -from cozyweboob.__main__ import main_fetch, main +from __future__ import absolute_import -__all__ = ["WeboobProxy", "main_fetch", "main"] +from cozyweboob.WeboobProxy import WeboobProxy +from cozyweboob.__main__ import clean, main_fetch, main + +__all__ = ["WeboobProxy", "clean", "main_fetch", "main"] diff --git a/cozyweboob/__main__.py b/cozyweboob/__main__.py index 485a3b7..eb74910 100644 --- a/cozyweboob/__main__.py +++ b/cozyweboob/__main__.py @@ -1,13 +1,17 @@ """ Main script for this module """ +from __future__ import absolute_import from __future__ import print_function import collections import importlib import json import logging +import os +import shutil import sys +import tempfile from getpass import getpass @@ -27,6 +31,28 @@ CAPABILITIES_CONVERSION_MODULES = importlib.import_module(".capabilities", package="cozyweboob") +def clean(): + """ + Delete all the temporary downloaded files. These are the + "cozyweboob-*-tmp" folders in your system tmp dir. + """ + sys_tmp_dir = tempfile.gettempdir() + tmp_dirs = [ + x + for x in os.listdir(sys_tmp_dir) + if os.path.isdir(os.path.join(sys_tmp_dir, x)) + ] + removed_dirs = [] + for tmp_dir in tmp_dirs: + if tmp_dir.startswith("cozyweboob-") and tmp_dir.endswith("-tmp"): + tmp_dir = os.path.join(sys_tmp_dir, tmp_dir) + removed_dirs.append(tmp_dir) + shutil.rmtree(tmp_dir) + return { + "removed_dirs": removed_dirs + } + + def main_fetch(used_modules): """ Main fetching code diff --git a/server.py b/server.py index e50ac1f..9b4cb13 100755 --- a/server.py +++ b/server.py @@ -4,12 +4,12 @@ HTTP server wrapper around weboob """ import logging import os -import shutil import tempfile from bottle import post, request, route, run, static_file from cozyweboob import main as cozyweboob +from cozyweboob import clean from cozyweboob import WeboobProxy from cozyweboob.tools.env import is_in_debug_mode @@ -47,21 +47,7 @@ def clean_view(): Delete all the temporary downloaded files. These are the "cozyweboob-*-tmp" folders in your system tmp dir. """ - sys_tmp_dir = tempfile.gettempdir() - tmp_dirs = [ - x - for x in os.listdir(sys_tmp_dir) - if os.path.isdir(os.path.join(sys_tmp_dir, x)) - ] - removed_dirs = [] - for tmp_dir in tmp_dirs: - if tmp_dir.startswith("cozyweboob-") and tmp_dir.endswith("-tmp"): - tmp_dir = os.path.join(sys_tmp_dir, tmp_dir) - removed_dirs.append(tmp_dir) - shutil.rmtree(tmp_dir) - return { - "removed_dirs": removed_dirs - } + return clean() @route("/list") @@ -83,6 +69,11 @@ def init(): format='%(levelname)s: %(message)s', level=logging.INFO ) + else: + logging.basicConfig( + format='%(levelname)s: %(message)s', + level=logging.ERROR + ) # Ensure all modules are installed and up to date before starting the # server logger.info("Ensuring all modules are installed and up to date.") diff --git a/stdin_conversation.py b/stdin_conversation.py index 3e10b5e..cb1f6e2 100755 --- a/stdin_conversation.py +++ b/stdin_conversation.py @@ -9,6 +9,7 @@ import logging import sys from cozyweboob import main as cozyweboob +from cozyweboob import clean from cozyweboob import WeboobProxy from cozyweboob.tools.env import is_in_debug_mode from cozyweboob.tools.jsonwriter import pretty_json @@ -48,9 +49,13 @@ def process_query(query): # List modules view logger.info("Calling /list view.") return list_view() + elif query == "GET /clean": + # Clean view + logger.info("Calling /clean view") + return clean() elif query.startswith("POST /fetch"): # Fetch modules view - logger.info("Calling /list view.") + logger.info("Calling /fetch view.") params = query.split()[2] return fetch_view(params) elif query == "exit": @@ -73,6 +78,11 @@ def main(): format='%(levelname)s: %(message)s', level=logging.INFO ) + else: + logging.basicConfig( + format='%(levelname)s: %(message)s', + level=logging.ERROR + ) # Ensure all modules are installed and up to date before starting the # server logger.info("Ensuring all modules are installed and up to date.")