Codestyle homogeneization

This commit is contained in:
Lucas Verney 2016-10-03 03:24:01 +02:00
parent 6df5e99868
commit 96634a322e
2 changed files with 54 additions and 50 deletions

View File

@ -9,12 +9,13 @@ written by bnjbvr and released under MIT.
from __future__ import print_function from __future__ import print_function
import collections import collections
import getpass
import importlib import importlib
import json import json
import logging import logging
import sys import sys
from getpass import getpass
from requests.utils import dict_from_cookiejar from requests.utils import dict_from_cookiejar
from weboob.core import Weboob from weboob.core import Weboob
@ -56,54 +57,48 @@ class WeboobProxy(object):
""" """
Weboob().update(progress=DummyProgress()) Weboob().update(progress=DummyProgress())
@staticmethod def __init__(self):
def list_modules(capability=None): """
Create a Weboob handle.
"""
# Get a weboob instance
self.weboob = Weboob()
self.backend = None
def install_modules(self, capability=None, name=None):
""" """
List all available modules and their configuration options. List all available modules and their configuration options.
Args: Args:
capability: Restrict the modules list to a given capability. capability: Restrict the modules to install to a given capability.
Returns: A dict mapping module names to supported capabilities and name: Only install the specified module.
available configuration options. Returns: The list of installed module infos.
""" """
available_modules = {}
moduleInfos = Weboob().repositories.get_all_modules_info(capability)
for module in moduleInfos:
available_modules[module] = {
"infos": dict(moduleInfos[module].dump()),
"config": None # TODO: Get config options from module
}
return available_modules
def __init__(self, modulename, parameters):
"""
Create a Weboob handle and try to load the modules.
Args:
modulename: the name of the weboob module to use.
parameters: A dict of parameters to pass the weboob module.
"""
# Get a weboob instance
self.weboob = Weboob()
# Install the module if necessary and hide the progress.
repositories = self.weboob.repositories repositories = self.weboob.repositories
minfo = repositories.get_module_info(modulename) if name:
if minfo is not None and not minfo.is_installed(): modules = [repositories.get_module_info(name)]
repositories.install(minfo, progress=DummyProgress()) else:
# Build a backend for this module modules = repositories.get_all_modules_info(capability)
self.backend = self.weboob.build_backend(modulename, parameters) for module in modules:
if module is not None and not module.is_installed():
repositories.install(module, progress=DummyProgress())
return modules
def get_backend(self): def init_backend(self, modulename, parameters):
""" """
Backend getter. Backend initialization.
Returns: Returns:
the built backend. the built backend.
""" """
# Ensure module is installed
self.install_modules(name=modulename)
# Build backend
self.backend = self.weboob.build_backend(modulename, parameters)
return self.backend return self.backend
def mainFetch(used_modules): def main_fetch(used_modules):
""" """
Main fetching code Main fetching code
@ -121,12 +116,13 @@ def mainFetch(used_modules):
logger.info("Start fetching from konnectors.") logger.info("Start fetching from konnectors.")
for module in used_modules: for module in used_modules:
try: try:
weboob_proxy = WeboobProxy()
logger.info("Fetching data from module %s.", module["id"]) logger.info("Fetching data from module %s.", module["id"])
# Get associated backend for this module # Get associated backend for this module
backend = WeboobProxy( backend = weboob_proxy.init_backend(
module["name"], module["name"],
module["parameters"] module["parameters"]
).get_backend() )
for capability in backend.iter_caps(): # Supported capabilities for capability in backend.iter_caps(): # Supported capabilities
# Get capability class name for dynamic import of converter # Get capability class name for dynamic import of converter
capability = capability.__name__ capability = capability.__name__
@ -143,10 +139,13 @@ def mainFetch(used_modules):
logger.info("Fetching capability %s.", capability) logger.info("Fetching capability %s.", capability)
# Fetch data and merge them with the ones from other # Fetch data and merge them with the ones from other
# capabilities # capabilities
fetched_data[module["id"]].update(fetching_function(backend)) fetched_data[module["id"]].update(
fetching_function(backend)
)
except AttributeError: except AttributeError:
# In case the converter does not exist on our side # In case the converter does not exist on our side
logger.error("%s capability is not implemented.", capability) logger.error("%s capability is not implemented.",
capability)
continue continue
# Store session cookie of this module, to fetch files afterwards # Store session cookie of this module, to fetch files afterwards
try: try:
@ -182,11 +181,13 @@ def main(json_params):
konnectors = json.loads(json_params) konnectors = json.loads(json_params)
# Debug only: Handle missing passwords using getpass # Debug only: Handle missing passwords using getpass
if is_in_debug_mode(): if is_in_debug_mode():
for module in range(len(konnectors)): for module in konnectors:
for param in konnectors[module]["parameters"]: for param in module["parameters"]:
if not konnectors[module]["parameters"][param]: if not module["parameters"][param]:
konnectors[module]["parameters"][param] = getpass.getpass( module["parameters"][param] = getpass(
"Password for module %s? " % konnectors[module]["id"] "Password for module %s? " % (
module["id"],
)
) )
except ValueError: except ValueError:
logger.error("Invalid JSON input.") logger.error("Invalid JSON input.")
@ -194,7 +195,7 @@ def main(json_params):
# Output the JSON formatted results on stdout # Output the JSON formatted results on stdout
return pretty_json( return pretty_json(
mainFetch(konnectors) main_fetch(konnectors)
) )

View File

@ -3,9 +3,12 @@ CapDocument
This capability is used for modules that have billing support. This capability is used for modules that have billing support.
| Key | Value | | Key | Value | Type |
|-----------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| |-----------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------|
| subscriptions | List of subscriptions (contracts) | | subscriptions | List of subscriptions (contracts) | Subscription |
| bills | Map of bills for each subscription. Bills are final document produced by the third party. | | bills | Map of bills for each subscription. Bills are final document produced by the third party. | Bill |
| history_bills | Map of history bills for each subscription. History bills are detailed counts for any event resulting in a transaction (typically any communication for a phone service provider) | | history_bills | Map of history bills for each subscription. History bills are detailed counts for any event resulting in a transaction (typically any communication for a phone service provider) | Detail |
| detailed_bills | Map of detailed bills for each subscription. Detailed bills are aggregated counts by facturation type (typically voice and texts for a phone service provider) | | detailed_bills | Map of detailed bills for each subscription. Detailed bills are aggregated counts by facturation type (typically voice and texts for a phone service provider) | Detail |
The fields available for any type are listed [in the Weboob
doc](http://dev.weboob.org/api/capabilities/bill).