From b242fc37b1c7d16365aa3bd0f58182f5ee31ccd3 Mon Sep 17 00:00:00 2001 From: "Phyks (Lucas Verney)" Date: Wed, 12 Oct 2016 14:27:33 -0400 Subject: [PATCH] PyLint check --- cozyweboob/WeboobProxy.py | 13 ++--- cozyweboob/__init__.py | 7 ++- cozyweboob/__main__.py | 15 +++--- cozyweboob/capabilities/CapDocument.py | 72 +++++++++++++++++++------- cozyweboob/capabilities/__init__.py | 5 +- cozyweboob/capabilities/base.py | 10 ++-- cozyweboob/tools/env.py | 5 +- cozyweboob/tools/progress.py | 3 ++ 8 files changed, 90 insertions(+), 40 deletions(-) diff --git a/cozyweboob/WeboobProxy.py b/cozyweboob/WeboobProxy.py index fec40f5..6fe077d 100755 --- a/cozyweboob/WeboobProxy.py +++ b/cozyweboob/WeboobProxy.py @@ -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() diff --git a/cozyweboob/__init__.py b/cozyweboob/__init__.py index 20eaf57..0c01c82 100644 --- a/cozyweboob/__init__.py +++ b/cozyweboob/__init__.py @@ -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"] diff --git a/cozyweboob/__main__.py b/cozyweboob/__main__.py index 6590126..485a3b7 100644 --- a/cozyweboob/__main__.py +++ b/cozyweboob/__main__.py @@ -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 diff --git a/cozyweboob/capabilities/CapDocument.py b/cozyweboob/capabilities/CapDocument.py index 0d82a4d..626f3d7 100644 --- a/cozyweboob/capabilities/CapDocument.py +++ b/cozyweboob/capabilities/CapDocument.py @@ -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 { diff --git a/cozyweboob/capabilities/__init__.py b/cozyweboob/capabilities/__init__.py index 951aa8e..1fe2761 100644 --- a/cozyweboob/capabilities/__init__.py +++ b/cozyweboob/capabilities/__init__.py @@ -1,4 +1,7 @@ -from . import CapDocument +""" +Capabilities submodule +""" +from cozyweboob.capabilities import CapDocument __all__ = [ "CapDocument" diff --git a/cozyweboob/capabilities/base.py b/cozyweboob/capabilities/base.py index ea02e43..bda2923 100644 --- a/cozyweboob/capabilities/base.py +++ b/cozyweboob/capabilities/base.py @@ -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 diff --git a/cozyweboob/tools/env.py b/cozyweboob/tools/env.py index a2db752..d70a2b9 100644 --- a/cozyweboob/tools/env.py +++ b/cozyweboob/tools/env.py @@ -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" + ) diff --git a/cozyweboob/tools/progress.py b/cozyweboob/tools/progress.py index 82a2afc..e4434c8 100644 --- a/cozyweboob/tools/progress.py +++ b/cozyweboob/tools/progress.py @@ -20,4 +20,7 @@ class DummyProgress: pass def prompt(self, message): + """ + Prompt function. Not implemented. + """ raise NotImplementedError()