Browse Source

Continue refactor

Phyks (Lucas Verney) 3 years ago
parent
commit
56c2156e33

+ 1
- 1
.gitignore View File

@@ -1,4 +1,4 @@
1 1
 *.pyc
2 2
 *.swp
3
-konnectors.*.json*
4 3
 *.json
4
+config.py

+ 4
- 0
config.py.example View File

@@ -0,0 +1,4 @@
1
+debug = False
2
+server = "cherrypy"
3
+host = "localhost"
4
+port = 7777

+ 31
- 1
infotuyoob/WeboobProxy.py View File

@@ -15,6 +15,7 @@ from weboob.exceptions import ModuleInstallError
15 15
 
16 16
 from infotuyoob.utils import weboob_utils
17 17
 from infotuyoob.utils.progress import DummyProgress
18
+from infotuyoob.utils.exceptions import CapNotFound
18 19
 
19 20
 
20 21
 # Module specific logger
@@ -107,7 +108,7 @@ class WeboobProxy(object):
107 108
             }
108 109
         }
109 110
 
110
-    def init_backend(self, modulename, parameters):
111
+    def _init_backend(self, modulename, parameters):
111 112
         """
112 113
         Backend initialization.
113 114
 
@@ -119,3 +120,32 @@ class WeboobProxy(object):
119 120
         # Build backend
120 121
         self.backend = self.weboob.build_backend(modulename, parameters)
121 122
         return self.backend
123
+
124
+
125
+    def fetch(self, modulename, parameters, capability=None, item=None):
126
+        """
127
+        Fetch data from a specific module.
128
+
129
+        Params:
130
+            - modulename: Module to fetch from.
131
+            - parameters:  TODO
132
+            - capability: Capability to restrict to.
133
+            - item: Item to restrict to, capability should be provided as well.
134
+
135
+        Returns:
136
+            TODO
137
+        """
138
+        # Instantiate a module backend
139
+        backend = self._init_backend(modulename, parameters)
140
+
141
+        # Default value for capabilities to iterate on  is all supported
142
+        # capabilities
143
+        capabilities = list(backend.iter_caps())
144
+        if capability:
145
+            if capability in capabilities:
146
+                capabilities = [capability]
147
+            else:
148
+                raise CapNotFound
149
+
150
+        for capability in capabilities:
151
+            pass  # TODO

+ 1
- 1
infotuyoob/__init__.py View File

@@ -1,6 +1,6 @@
1 1
 #!/usr/bin/env python
2 2
 # coding: utf-8
3 3
 
4
-from __future__ import absolute_import, unicode_literals
4
+from __future__ import absolute_import, print_function, unicode_literal
5 5
 
6 6
 __version__ = "0.1"

+ 7
- 47
infotuyoob/__main__.py View File

@@ -1,25 +1,20 @@
1 1
 """
2 2
 Main script for this module
3 3
 """
4
-from __future__ import absolute_import
5
-from __future__ import print_function
4
+# TODO
5
+from __future__ import absolute_import, print_function, unicode_literals
6 6
 
7 7
 import collections
8 8
 import importlib
9 9
 import json
10 10
 import logging
11
-import os
12
-import shutil
13 11
 import sys
14
-import tempfile
15 12
 
16 13
 from getpass import getpass
17 14
 
18 15
 from requests.utils import dict_from_cookiejar
19 16
 
20
-from cozyweboob.WeboobProxy import WeboobProxy
21
-from cozyweboob.tools.env import is_in_debug_mode
22
-from cozyweboob.tools.jsonwriter import pretty_json
17
+from infotuyoob.WeboobProxy import WeboobProxy
23 18
 
24 19
 
25 20
 # Module specific logger
@@ -31,29 +26,7 @@ CAPABILITIES_CONVERSION_MODULES = importlib.import_module(".capabilities",
31 26
                                                           package="cozyweboob")
32 27
 
33 28
 
34
-def clean():
35
-    """
36
-    Delete all the temporary downloaded files. These are the
37
-    "cozyweboob-*-tmp" folders in your system tmp dir.
38
-    """
39
-    sys_tmp_dir = tempfile.gettempdir()
40
-    tmp_dirs = [
41
-        x
42
-        for x in os.listdir(sys_tmp_dir)
43
-        if os.path.isdir(os.path.join(sys_tmp_dir, x))
44
-    ]
45
-    removed_dirs = []
46
-    for tmp_dir in tmp_dirs:
47
-        if tmp_dir.startswith("cozyweboob-") and tmp_dir.endswith("-tmp"):
48
-            tmp_dir = os.path.join(sys_tmp_dir, tmp_dir)
49
-            removed_dirs.append(tmp_dir)
50
-            shutil.rmtree(tmp_dir)
51
-    return {
52
-        "removed_dirs": removed_dirs
53
-    }
54
-
55
-
56
-def main_fetch(used_modules):
29
+def main_fetch(used_modules, debug=False):
57 30
     """
58 31
     Main fetching code
59 32
 
@@ -115,7 +88,7 @@ def main_fetch(used_modules):
115 88
         except Exception as exception:
116 89
             # Store any error happening in a dedicated field
117 90
             fetched_data[module["id"]]["error"] = exception
118
-            if is_in_debug_mode():
91
+            if debug:
119 92
                 # Reraise if in debug
120 93
                 raise
121 94
             else:
@@ -125,7 +98,7 @@ def main_fetch(used_modules):
125 98
     return fetched_data
126 99
 
127 100
 
128
-def main(json_params):
101
+def main(json_params, debug=False):
129 102
     """
130 103
     Main code
131 104
 
@@ -137,7 +110,7 @@ def main(json_params):
137 110
         # Fetch konnectors JSON description from stdin
138 111
         konnectors = json.loads(json_params)
139 112
         # Debug only: Handle missing passwords using getpass
140
-        if is_in_debug_mode():
113
+        if debug:
141 114
             for module in konnectors:
142 115
                 for param in module["parameters"]:
143 116
                     if not module["parameters"][param]:
@@ -152,16 +125,3 @@ def main(json_params):
152 125
 
153 126
     # Return the dict results
154 127
     return main_fetch(konnectors)
155
-
156
-
157
-if __name__ == '__main__':
158
-    try:
159
-        # Debug only: Set logging level and format
160
-        if is_in_debug_mode():
161
-            logging.basicConfig(
162
-                format='%(levelname)s: %(message)s',
163
-                level=logging.INFO
164
-            )
165
-        print(main(sys.stdin.read()))
166
-    except KeyboardInterrupt:
167
-        pass

+ 1
- 0
infotuyoob/capabilities/CapDocument.py View File

@@ -1,3 +1,4 @@
1
+# TODO
1 2
 """
2 3
 This module contains all the conversion functions associated to the Document
3 4
 capability.

+ 0
- 8
infotuyoob/capabilities/__init__.py View File

@@ -1,8 +0,0 @@
1
-"""
2
-Capabilities submodule
3
-"""
4
-from cozyweboob.capabilities import CapDocument
5
-
6
-__all__ = [
7
-    "CapDocument"
8
-]

+ 2
- 5
infotuyoob/capabilities/base.py View File

@@ -1,3 +1,4 @@
1
+# TODO
1 2
 """
2 3
 Common conversion functions for all the available capabilities.
3 4
 """
@@ -19,11 +20,7 @@ def clean_object(obj, base_url=None):
19 20
     obj = obj.to_dict()
20 21
     # Clean the various fields to be JSON-serializable
21 22
     for key, value in obj.items():
22
-        if empty(value):
23
-            # Replace empty values by None, avoid "NotLoaded is not
24
-            # serializable" error
25
-            obj[key] = None
26
-        elif key == "url" and base_url:
23
+        if key == "url" and base_url:
27 24
             # Render full absolute URLs
28 25
             obj[key] = base_url + value
29 26
     return obj

+ 40
- 3
infotuyoob/routes.py View File

@@ -4,6 +4,7 @@ from __future__ import unicode_literals, absolute_import
4 4
 
5 5
 import bottle
6 6
 
7
+from infotuyoob.utils.exceptions import CapNotFound
7 8
 from infotuyoob.utils.jsonwriter import pretty_json
8 9
 from infotuyoob.WeboobProxy import WeboobProxy
9 10
 
@@ -65,13 +66,49 @@ def api_v1_module_infos(module_name):
65 66
     )
66 67
 
67 68
 
69
+def _api_v1_module_fetch_helper(module_name, capability=None, item=None):
70
+    """
71
+    Helper for the fetching routes. See documentation under the routes below
72
+    for further info.
73
+    """
74
+    parameters = bottle.request.json
75
+    try:
76
+        return pretty_json(
77
+            WeboobProxy().fetch(module_name, parameters, capability, item)
78
+        )
79
+    except CapNotFound:
80
+        bottle.abort(400, "Unknown capability for module %s." % module_name)
81
+
82
+
68 83
 @app.post("/api/v1/modules/:module_name/fetch")
69
-def api_v1_module_fetch():
84
+def api_v1_module_fetch(module_name):
70 85
     """
71 86
     Fetch data from the specified module.
87
+
88
+    POST payload is the dict of parameters to use for the module, JSON encoded.
89
+    """
90
+    return _api_v1_module_fetch_helper(module_name)
91
+
92
+
93
+@app.post("/api/v1/modules/:module_name/fetch/:capability")
94
+def api_v1_module_fetch_capability(module_name, capability):
95
+    """
96
+    Fetch data from the specified module using the specified capability.
97
+
98
+    POST payload is the dict of parameters to use for the module, JSON encoded.
99
+    """
100
+    return _api_v1_module_fetch_helper(module_name, capability)
101
+
102
+
103
+@app.post("/api/v1/modules/:module_name/fetch/:capability/:item")
104
+def api_v1_module_fetch_capability_item(module_name, capability, item):
105
+    """
106
+    Fetch data of the specified type from the specified module using the
107
+    specified capability.
108
+
109
+    POST payload is the dict of parameters to use for the module, JSON encoded.
72 110
     """
73
-    # TODO
74
-    pass
111
+    return _api_v1_module_fetch_helper(module_name, capability, item)
75 112
 
76 113
 
77 114
 def get_app():

+ 1
- 0
infotuyoob/utils/__init__.py View File

@@ -0,0 +1 @@
1
+# coding: utf-8

+ 6
- 0
infotuyoob/utils/exceptions.py View File

@@ -0,0 +1,6 @@
1
+# coding: utf-8
2
+from __future__ import absolute_import, print_function, unicode_literal
3
+
4
+
5
+class CapNotFound(Exception):
6
+    pass

+ 6
- 11
infotuyoob/utils/jsonwriter.py View File

@@ -1,3 +1,4 @@
1
+# coding: utf-8
1 2
 """
2 3
 This module implements a custom JSON writer to be able to serialize data
3 4
 returned by Weboob and pretty print the output JSON.
@@ -5,27 +6,21 @@ returned by Weboob and pretty print the output JSON.
5 6
 Based upon
6 7
 http://stackoverflow.com/questions/11875770/how-to-overcome-datetime-datetime-not-json-serializable-in-python.
7 8
 """
9
+from __future__ import absolute_import, print_function, unicode_literal
8 10
 import json
9 11
 
10
-from datetime import date, datetime
11
-from decimal import Decimal
12
+from weboob.tools.json import WeboobEncoder
12 13
 
13 14
 
14
-class CustomJSONEncoder(json.JSONEncoder):
15
+class CustomJSONEncoder(WeboobEncoder):
15 16
     """
16 17
     Custom JSONEncoder to support more types.
17 18
     """
18 19
     def default(self, o):
19
-        if isinstance(o, datetime) or isinstance(o, date):
20
-            # Serialize datetime objects to ISO dates
21
-            return o.isoformat()
22
-        elif isinstance(o, Decimal):
23
-            # Serialize Decimal objects to string
24
-            return str(o)
25
-        elif isinstance(o, Exception):
20
+        if isinstance(o, Exception):
26 21
             # Serialize Exception objects to string representation
27 22
             return repr(o)
28
-        return json.JSONEncoder.default(self, o)
23
+        return super(CustomJSONEncoder, self).default(o)
29 24
 
30 25
 
31 26
 def pretty_json(obj):

+ 2
- 0
infotuyoob/utils/progress.py View File

@@ -1,6 +1,8 @@
1
+# coding: utf-8
1 2
 """
2 3
 Miscellaneous progress functions.
3 4
 """
5
+from __future__ import absolute_import, print_function, unicode_literal
4 6
 
5 7
 
6 8
 class DummyProgress:

+ 1
- 1
infotuyoob/utils/settings.py View File

@@ -1,6 +1,6 @@
1 1
 # coding: utf-8
2 2
 
3
-from __future__ import unicode_literals
3
+from __future__ import absolute_import, print_function, unicode_literal
4 4
 
5 5
 import json
6 6
 import os

+ 2
- 0
infotuyoob/utils/weboob_utils.py View File

@@ -1,6 +1,8 @@
1
+# coding: utf-8
1 2
 """
2 3
 Helper functions related to Weboob-specific code.
3 4
 """
5
+from __future__ import absolute_import, print_function, unicode_literal
4 6
 from weboob.tools.value import (ValueBackendPassword, ValueInt, ValueFloat,
5 7
                                 ValueBool)
6 8
 

+ 0
- 19
modules.json.sample View File

@@ -1,19 +0,0 @@
1
-[
2
-    {
3
-        "id": "amazon.fr",
4
-        "name": "amazon",
5
-        "parameters": {
6
-            "website": "www.amazon.fr",
7
-            "email": "someone@example.com",
8
-            "password": "MY_AWESOME_PASSWORD"
9
-        }
10
-    },
11
-    {
12
-        "id": "freemobile",
13
-        "name": "freemobile",
14
-        "parameters": {
15
-            "login": "12345678",
16
-            "password": ""
17
-        }
18
-    }
19
-]