gvsig-scripting / org.gvsig.scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.app / org.gvsig.scripting.app.mainplugin / src / main / resources-plugin / scripting / lib / cartodb / imports.py @ 564
History | View | Annotate | Download (6.05 KB)
1 |
from cartodb import CartoDBException |
---|---|
2 |
|
3 |
|
4 |
class ImportJob(object): |
5 |
"""
|
6 |
Equivalent to a job process on CartoDB.
|
7 |
New jobs have no id until they're "run". Then, they can be updated so that their attributes
|
8 |
reflect the current status of the job.
|
9 |
"""
|
10 |
id = None
|
11 |
|
12 |
def __init__(self, client, type_guessing=True, quoted_fields_guessing=True, content_guessing=False, create_vis=False, **kwargs): |
13 |
"""
|
14 |
:param client: Client to make authorized requests (currently only CartoDBAPIKey is supported)
|
15 |
:param type_guessing: If set to False disables field type guessing (for Excel and CSVs)
|
16 |
:param quoted_fields_guessing: If set to False disables type guessing of CSV fields that come inside double quotes
|
17 |
:param content_guessing: Set it to True to enable content guessing and automatic geocoding based on results. Currently it only implemenents geocoding of countries.
|
18 |
:param create_vis: Set it to true to flag the import so when it finishes, it creates automatically Map after importing the Dataset
|
19 |
:param kwargs: Dictionary with the data attributes that come from the Import API response. They will be mapped to real object attributes.
|
20 |
:return:
|
21 |
"""
|
22 |
self.client = client
|
23 |
self.run_params = {"type_guessing": type_guessing, |
24 |
"quoted_fields_guessing": quoted_fields_guessing,
|
25 |
"content_guessing": content_guessing,
|
26 |
"create_vis": create_vis}
|
27 |
|
28 |
self.update_from_dict(kwargs)
|
29 |
|
30 |
def update_from_dict(self, data_dict): |
31 |
"""
|
32 |
:param data_dict: Dictionary to be mapped into object attributes
|
33 |
:return:
|
34 |
"""
|
35 |
for k, v in data_dict.items(): # More efficient if use 'future.utils.iteritems' or 'six.iteritems' |
36 |
setattr(self, k, v) |
37 |
if "item_queue_id" in data_dict: |
38 |
self.id = data_dict["item_queue_id"] |
39 |
|
40 |
def req(self, url, api_params=None, client_params=None): |
41 |
"""
|
42 |
Make the actual request to the Import API
|
43 |
:param url: Endpoint URL
|
44 |
:param api_params: Params to be appended to the URL
|
45 |
:param client_params: Params to be sent to the client (and, in turn, to requests)
|
46 |
:return:
|
47 |
"""
|
48 |
resp = self.client.req(url, params=api_params, **client_params or {}) |
49 |
response_data = self.client.get_response_data(resp, True) |
50 |
self.update_from_dict(response_data)
|
51 |
|
52 |
def run(self): |
53 |
"""
|
54 |
Creates the job import on the CartoDB server, must be implemented by children classes
|
55 |
"""
|
56 |
raise NotImplementedError |
57 |
|
58 |
def update(self): |
59 |
"""
|
60 |
Updates the information of the import job against the CartoDB server
|
61 |
:return:
|
62 |
"""
|
63 |
if self.id is None: |
64 |
raise CartoDBException("Import job needs to be run first!") |
65 |
|
66 |
self.req("%s/%s" % (self.client.imports_url, self.id)) |
67 |
|
68 |
|
69 |
class FileImport(ImportJob): |
70 |
"""
|
71 |
This class provides support for uploading and importing local files into CartoDB
|
72 |
"""
|
73 |
def __init__(self, file_name, *args, **kwargs): |
74 |
"""
|
75 |
:param file_name: File name (paths are supported)
|
76 |
:param args: Sent to parent class
|
77 |
:param kwargs: Sent to parent class
|
78 |
:return:
|
79 |
"""
|
80 |
super(FileImport, self).__init__(*args, **kwargs) |
81 |
self.files = {'file': open(file_name, 'rb')} |
82 |
|
83 |
def run(self): |
84 |
"""
|
85 |
Actually creates the job import on the CartoDB server
|
86 |
:return:
|
87 |
"""
|
88 |
self.req(self.client.imports_url, api_params=self.run_params, client_params={"files": self.files, "http_method": "POST"}) |
89 |
|
90 |
|
91 |
class URLImport(ImportJob): |
92 |
"""
|
93 |
This class provides support for uploading and importing remote files into CartoDB
|
94 |
No sync support yet
|
95 |
"""
|
96 |
def __init__(self, url, *args, **kwargs): |
97 |
"""
|
98 |
:param url: Remote URL for the file
|
99 |
:param args: Sent to parent class
|
100 |
:param kwargs: Sent to parent class
|
101 |
:return:
|
102 |
"""
|
103 |
super(URLImport, self).__init__(*args, **kwargs) |
104 |
self.url = url
|
105 |
|
106 |
def run(self): |
107 |
"""
|
108 |
Actually creates the job import on the CartoDB server
|
109 |
:return:
|
110 |
"""
|
111 |
api_params = self.run_params
|
112 |
api_params["url"] = self.url |
113 |
|
114 |
self.req(self.client.imports_url, api_params=api_params, client_params={"http_method": "POST"}) |
115 |
|
116 |
|
117 |
class ImportManager(object): |
118 |
item_queue_id = None
|
119 |
|
120 |
def __init__(self, client): |
121 |
"""
|
122 |
:param client: Client to make authorized requests (currently only CartoDBAPIKey is supported)
|
123 |
:return:
|
124 |
"""
|
125 |
self.client = client
|
126 |
|
127 |
def get(self, id=None, ids_only=False): |
128 |
"""
|
129 |
Get one import job or a list with all the current (pending) import jobs
|
130 |
:param id: If set, only this job will be retrieved. This works no matter the state of the job
|
131 |
:param ids_only: If True, a list of IDs is returned; if False, a list of ImportJob objects is returned
|
132 |
:return: An import job, a list of import job IDs or a list of import jobs
|
133 |
"""
|
134 |
if id is not None: |
135 |
resp = self.client.req("%s/%s" % (self.client.imports_url, id)) |
136 |
response_data = self.client.get_response_data(resp, True) |
137 |
return ImportJob(self.client, **response_data) |
138 |
else:
|
139 |
imports = [] |
140 |
|
141 |
resp = self.client.req(self.client.imports_url) |
142 |
response_data = self.client.get_response_data(resp, True) |
143 |
if response_data["success"] is True: |
144 |
for import_job_id in response_data["imports"]: |
145 |
if ids_only is True: |
146 |
imports.append(import_job_id) |
147 |
else:
|
148 |
imports.append(self.get(import_job_id))
|
149 |
|
150 |
return imports
|
151 |
|
152 |
def all(self, ids_only=False): |
153 |
"""
|
154 |
Get all the current (pending) import jobs
|
155 |
:param ids_only: If True, a list of IDs is returned; if False, a list of ImportJob objects is returned
|
156 |
:return: A list of import job IDs or a list of import jobs
|
157 |
"""
|
158 |
return self.get(ids_only=ids_only) |