Fix imports, add clean in conversation script and fixing logging

This commit is contained in:
Lucas Verney 2016-11-01 04:44:32 +01:00
parent b7e442b8ca
commit 89893abc4c
6 changed files with 51 additions and 20 deletions

View File

@ -86,6 +86,7 @@ Available commands are:
parameters. parameters.
Downloaded files will be stored in a temporary directory, and their file URI Downloaded files will be stored in a temporary directory, and their file URI
will be passed back in the output JSON. will be passed back in the output JSON.
* `GET /clean` to clean temporary downloaded files.
* `exit` to quit the script and end the conversation. * `exit` to quit the script and end the conversation.
JSON responses are the same one as from the HTTP server script. It is JSON responses are the same one as from the HTTP server script. It is

View File

@ -6,6 +6,7 @@ Konnectors easily.
Part of this code comes from [Kresus](https://github.com/bnjbvr/kresus/) Part of this code comes from [Kresus](https://github.com/bnjbvr/kresus/)
written by bnjbvr and released under MIT. written by bnjbvr and released under MIT.
""" """
from __future__ import absolute_import
from __future__ import print_function from __future__ import print_function
import logging import logging

View File

@ -1,7 +1,9 @@
""" """
CozyWeboob main module CozyWeboob main module
""" """
from cozyweboob.WeboobProxy import WeboobProxy from __future__ import absolute_import
from cozyweboob.__main__ import main_fetch, main
__all__ = ["WeboobProxy", "main_fetch", "main"] from cozyweboob.WeboobProxy import WeboobProxy
from cozyweboob.__main__ import clean, main_fetch, main
__all__ = ["WeboobProxy", "clean", "main_fetch", "main"]

View File

@ -1,13 +1,17 @@
""" """
Main script for this module Main script for this module
""" """
from __future__ import absolute_import
from __future__ import print_function from __future__ import print_function
import collections import collections
import importlib import importlib
import json import json
import logging import logging
import os
import shutil
import sys import sys
import tempfile
from getpass import getpass from getpass import getpass
@ -27,6 +31,28 @@ CAPABILITIES_CONVERSION_MODULES = importlib.import_module(".capabilities",
package="cozyweboob") 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): def main_fetch(used_modules):
""" """
Main fetching code Main fetching code

View File

@ -4,12 +4,12 @@ HTTP server wrapper around weboob
""" """
import logging import logging
import os import os
import shutil
import tempfile import tempfile
from bottle import post, request, route, run, static_file from bottle import post, request, route, run, static_file
from cozyweboob import main as cozyweboob from cozyweboob import main as cozyweboob
from cozyweboob import clean
from cozyweboob import WeboobProxy from cozyweboob import WeboobProxy
from cozyweboob.tools.env import is_in_debug_mode 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 Delete all the temporary downloaded files. These are the
"cozyweboob-*-tmp" folders in your system tmp dir. "cozyweboob-*-tmp" folders in your system tmp dir.
""" """
sys_tmp_dir = tempfile.gettempdir() return clean()
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
}
@route("/list") @route("/list")
@ -83,6 +69,11 @@ def init():
format='%(levelname)s: %(message)s', format='%(levelname)s: %(message)s',
level=logging.INFO 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 # Ensure all modules are installed and up to date before starting the
# server # server
logger.info("Ensuring all modules are installed and up to date.") logger.info("Ensuring all modules are installed and up to date.")

View File

@ -9,6 +9,7 @@ import logging
import sys import sys
from cozyweboob import main as cozyweboob from cozyweboob import main as cozyweboob
from cozyweboob import clean
from cozyweboob import WeboobProxy from cozyweboob import WeboobProxy
from cozyweboob.tools.env import is_in_debug_mode from cozyweboob.tools.env import is_in_debug_mode
from cozyweboob.tools.jsonwriter import pretty_json from cozyweboob.tools.jsonwriter import pretty_json
@ -48,9 +49,13 @@ def process_query(query):
# List modules view # List modules view
logger.info("Calling /list view.") logger.info("Calling /list view.")
return list_view() return list_view()
elif query == "GET /clean":
# Clean view
logger.info("Calling /clean view")
return clean()
elif query.startswith("POST /fetch"): elif query.startswith("POST /fetch"):
# Fetch modules view # Fetch modules view
logger.info("Calling /list view.") logger.info("Calling /fetch view.")
params = query.split()[2] params = query.split()[2]
return fetch_view(params) return fetch_view(params)
elif query == "exit": elif query == "exit":
@ -73,6 +78,11 @@ def main():
format='%(levelname)s: %(message)s', format='%(levelname)s: %(message)s',
level=logging.INFO 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 # Ensure all modules are installed and up to date before starting the
# server # server
logger.info("Ensuring all modules are installed and up to date.") logger.info("Ensuring all modules are installed and up to date.")