PyLint check

This commit is contained in:
Lucas Verney 2016-10-12 14:27:33 -04:00
parent ee677ab6d2
commit b242fc37b1
8 changed files with 90 additions and 40 deletions

View File

@ -13,8 +13,9 @@ import logging
from weboob.core import Weboob
from weboob.exceptions import ModuleInstallError
from tools.progress import DummyProgress
import tools.weboob_tools as weboob_tools
import cozyweboob.tools.weboob_tools as weboob_tools
from cozyweboob.tools.progress import DummyProgress
# Module specific logger
@ -68,13 +69,13 @@ class WeboobProxy(object):
# Install modules if required
for infos in modules.values():
if infos is not None and (
not infos.is_installed() or
not infos.is_local()
not infos.is_installed() or
not infos.is_local()
):
try:
repositories.install(infos, progress=DummyProgress())
except ModuleInstallError as e:
logger.info(str(e))
except ModuleInstallError as exception:
logger.info(str(exception))
return {
module_name: dict(infos.dump())
for module_name, infos in modules.items()

View File

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

View File

@ -13,9 +13,9 @@ from getpass import getpass
from requests.utils import dict_from_cookiejar
from WeboobProxy import WeboobProxy
from tools.env import is_in_debug_mode
from tools.jsonwriter import pretty_json
from cozyweboob.WeboobProxy import WeboobProxy
from cozyweboob.tools.env import is_in_debug_mode
from cozyweboob.tools.jsonwriter import pretty_json
# Module specific logger
@ -67,7 +67,10 @@ def main_fetch(used_modules):
fetching_function(
backend,
# If no actions specified, fetch but don't download
module.get("actions", {"fetch": True, "download": False})
module.get("actions", {
"fetch": True,
"download": False
})
)
)
except AttributeError:
@ -83,9 +86,9 @@ def main_fetch(used_modules):
except AttributeError:
# Avoid an AttributeError if no session is used for this module
fetched_data[module["id"]]["cookies"] = None
except Exception as e:
except Exception as exception:
# Store any error happening in a dedicated field
fetched_data[module["id"]]["error"] = e
fetched_data[module["id"]]["error"] = exception
if is_in_debug_mode():
# Reraise if in debug
raise

View File

@ -2,7 +2,7 @@
This module contains all the conversion functions associated to the Document
capability.
"""
from base import clean_object
from cozyweboob.capabilities.base import clean_object
from weboob.capabilities.bill import Bill
@ -34,6 +34,8 @@ def fetch_documents(document, subscriptions):
subscriptions: A list of subscriptions for the CapDocument object.
Returns: A tuple of cleaned list of documents and bills.
"""
# Get the BASEURL to generate absolute URLs
base_url = document.browser.BASEURL
try:
assert subscriptions
raw_documents = {
@ -41,7 +43,9 @@ def fetch_documents(document, subscriptions):
for subscription in subscriptions
}
raw_bills = {
subscription: [bill for bill in documents if isinstance(bill, Bill)]
subscription: [
bill for bill in documents if isinstance(bill, Bill)
]
for subscription, documents in raw_documents.items()
}
bills = {
@ -54,7 +58,8 @@ def fetch_documents(document, subscriptions):
documents = {
subscription: [
clean_object(bill, base_url=base_url)
for bill in documents_list if bill not in raw_bills[subscription]
for bill in documents_list
if bill not in raw_bills[subscription]
]
for subscription, documents_list in raw_documents.items()
}
@ -77,6 +82,8 @@ def fetch_details(document, subscriptions):
subscriptions: A list of subscriptions for the CapDocument object.
Returns: A cleaned list of detailed bills.
"""
# Get the BASEURL to generate absolute URLs
base_url = document.browser.BASEURL
try:
assert subscriptions
detailed_bills = {
@ -103,6 +110,8 @@ def fetch_history(document, subscriptions):
subscriptions: A list of subscriptions for the CapDocument object.
Returns: A cleaned list of history bills.
"""
# Get the BASEURL to generate absolute URLs
base_url = document.browser.BASEURL
try:
assert subscriptions
history_bills = {
@ -118,8 +127,38 @@ def fetch_history(document, subscriptions):
return history_bills
def fetch(document, fetch_actions):
"""
Fetch all required items from a CapDocument object.
def to_cozy(document, actions={"fetch": True, "download": False}):
Args:
document: The CapDocument object to fetch from.
fetch_actions: A dict describing what should be fetched (see README.md)
Returns:
A tuple of fetched subscriptions, documents, bills, detailed bills and
history bills.
"""
subscriptions = fetch_subscriptions(document)
if fetch_actions is True or "documents" in fetch_actions:
documents, bills = fetch_documents(document, subscriptions)
else:
documents, bills = None, None
if fetch_actions is True or "detailed_bills" in fetch_actions:
detailed_bills = fetch_details(document, subscriptions)
else:
detailed_bills = None
if fetch_actions is True or "history_bills" in fetch_actions:
history_bills = fetch_history(document, subscriptions)
else:
history_bills = None
return (subscriptions, documents, bills, detailed_bills, history_bills)
def to_cozy(document, actions=None):
"""
Export a CapDocument object to a JSON-serializable dict, to pass it to Cozy
instance.
@ -129,26 +168,21 @@ def to_cozy(document, actions={"fetch": True, "download": False}):
actions: A dict describing what should be fetched (see README.md).
Returns: A JSON-serializable dict for the input object.
"""
# Handle default parameters
if actions is None:
actions = {"fetch": True, "download": False}
# Get the BASEURL to generate absolute URLs
base_url = document.browser.BASEURL
# Handle fetch actions
if actions["fetch"] is True or "CapDocument" in actions["fetch"]:
subscriptions = fetch_subscriptions(document)
if actions["fetch"] is True or "documents" in actions["fetch"]["CapDocument"]:
documents, bills = fetch_documents(document, subscriptions)
if actions["fetch"] is True:
fetch_actions = actions["fetch"]
else:
documents, bills = None, None
if actions["fetch"] is True or "detailed_bills" in actions["fetch"]["CapDocument"]:
detailed_bills = fetch_details(document, subscriptions)
else:
detailed_bills = None
if actions["fetch"] is True or "history_bills" in actions["fetch"]["CapDocument"]:
history_bills = fetch_history(document, subscriptions)
else:
history_bills = None
fetch_actions = actions["fetch"]["CapDocument"]
subscriptions, documents, bills, detailed_bills, history_bills = fetch(
document, fetch_actions)
# Return a formatted dict with all the infos
return {

View File

@ -1,4 +1,7 @@
from . import CapDocument
"""
Capabilities submodule
"""
from cozyweboob.capabilities import CapDocument
__all__ = [
"CapDocument"

View File

@ -18,12 +18,12 @@ def clean_object(obj, base_url=None):
# Convert object to a dict of its fields
obj = obj.to_dict()
# Clean the various fields to be JSON-serializable
for k, v in obj.items():
if empty(v):
for key, value in obj.items():
if empty(value):
# Replace empty values by None, avoid "NotLoaded is not
# serializable" error
obj[k] = None
elif k == "url" and base_url:
obj[key] = None
elif key == "url" and base_url:
# Render full absolute URLs
obj[k] = base_url + v
obj[key] = base_url + value
return obj

View File

@ -11,4 +11,7 @@ def is_in_debug_mode():
Returns:
true / false
"""
return "COZYWEBOOB_ENV" in os.environ and os.environ["COZYWEBOOB_ENV"] == "debug"
return (
"COZYWEBOOB_ENV" in os.environ and
os.environ["COZYWEBOOB_ENV"] == "debug"
)

View File

@ -20,4 +20,7 @@ class DummyProgress:
pass
def prompt(self, message):
"""
Prompt function. Not implemented.
"""
raise NotImplementedError()