2
2
3
3
In this module are grouped all the dataclasses that are hydrated when the API is responding something.
4
4
"""
5
+
5
6
from __future__ import annotations
6
7
7
8
import datetime as dt
@@ -47,7 +48,13 @@ class TomtomResponseStatus(JSONWizard, LoadMixin):
47
48
response_status : str
48
49
urls : Optional [List [str ]]
49
50
50
- def __init__ (self , job_id : int , job_state : TomtomJobState , response_status : str , urls : Optional [List [str ]] = None ):
51
+ def __init__ (
52
+ self ,
53
+ job_id : int ,
54
+ job_state : TomtomJobState ,
55
+ response_status : str ,
56
+ urls : Optional [List [str ]] = None ,
57
+ ):
51
58
self .job_id = job_id
52
59
self .job_state = job_state
53
60
self .response_status = response_status
@@ -57,7 +64,7 @@ def load_to_enum(o, base_type: Type[Enum]) -> Enum:
57
64
return base_type .from_str (o )
58
65
59
66
def display_info (self ) -> str :
60
- return f' { self .job_id } ({ self .job_state .name } )'
67
+ return f" { self .job_id } ({ self .job_state .name } )"
61
68
62
69
def download (self , file_type : TomtomDownloadFileType ) -> bytes :
63
70
"""Download the job report, in the given format.
@@ -82,24 +89,38 @@ def download(self, file_type: TomtomDownloadFileType) -> bytes:
82
89
This exception can also be triggered if there is an error while downloading the file.
83
90
"""
84
91
if self .job_state != TomtomJobState .DONE :
85
- raise DownloadException (f'The "DONE" state is required for downloading the report ({ self .job_state } ).' )
92
+ raise DownloadException (
93
+ f'The "DONE" state is required for downloading the report ({ self .job_state } ).'
94
+ )
86
95
87
96
if self .urls is None :
88
- raise DownloadException ('There is no download url for this job.' )
97
+ raise DownloadException ("There is no download url for this job." )
98
+
99
+ filtered_links = [link for link in self .urls if f"{ file_type .value } ?" in link ]
100
+
101
+ # if nothing is found, remove the compression extension
102
+ if len (filtered_links ) == 0 :
103
+ filtered_links = [
104
+ link
105
+ for link in self .urls
106
+ if f"{ file_type .value .replace ('.zip' , '' )} ?" in link
107
+ ]
89
108
90
- filtered_links = [ link for link in self . urls if f' { file_type . value } ?' in link ]
109
+ # then perform a sanity check
91
110
if len (filtered_links ) != 1 :
92
- log .debug (f'Available links: { self .urls } .' )
93
- log .debug (f'Provided file type: { file_type } .' )
94
- raise DownloadException (f'Impossible to find the desired file in the following links: { filtered_links } .' )
111
+ log .debug (f"Available links: { self .urls } ." )
112
+ log .debug (f"Provided file type: { file_type } ." )
113
+ raise DownloadException (
114
+ f"Impossible to find the desired file in the following links: { filtered_links } ."
115
+ )
95
116
96
117
link = filtered_links [0 ]
97
- log .debug (f' Performing request to { link } ' )
118
+ log .debug (f" Performing request to { link } " )
98
119
response = requests .get (link )
99
120
100
121
if response .status_code != 200 :
101
- log .debug (f' Response: { response } ' )
102
- raise DownloadException (f' Error while downloading { link } ' )
122
+ log .debug (f" Response: { response } " )
123
+ raise DownloadException (f" Error while downloading { link } " )
103
124
104
125
return response .content
105
126
@@ -124,10 +145,10 @@ def write(self, file: Path, file_type: TomtomDownloadFileType) -> None:
124
145
"""
125
146
content = self .download (file_type )
126
147
if file .exists ():
127
- log .warning (f' The following file will be overwritten: { file } ' )
148
+ log .warning (f" The following file will be overwritten: { file } " )
128
149
129
- log .debug (f' Writing file { file } ' )
130
- with open (file , 'wb' ) as f :
150
+ log .debug (f" Writing file { file } " )
151
+ with open (file , "wb" ) as f :
131
152
f .write (content )
132
153
133
154
@@ -145,25 +166,29 @@ class TomtomJobInfo(JSONWizard, LoadMixin):
145
166
name : str
146
167
created_at : dt .datetime
147
168
state : TomtomJobState
148
- job_id : int = json_field ('id' , all = True )
149
- job_type : str = json_field (' type' , all = True )
150
- completed_at : Optional [dt .datetime ] = json_field (' completed_at' , default = None )
169
+ job_id : int = json_field ("id" , all = True )
170
+ job_type : str = json_field (" type" , all = True )
171
+ completed_at : Optional [dt .datetime ] = json_field (" completed_at" , default = None )
151
172
152
173
def load_to_enum (o , base_type : Type [Enum ]) -> Enum :
153
174
return base_type .from_str (o )
154
175
155
176
def display_info (self ) -> str :
156
- datetime_fmt = ' %Y-%m-%d %H:%M:%S'
177
+ datetime_fmt = " %Y-%m-%d %H:%M:%S"
157
178
start = self .created_at .strftime (datetime_fmt )
158
- end = 'None' if self .completed_at is None else self .completed_at .strftime (datetime_fmt )
159
- return f'┌─ [{ self .job_id } ] { self .name } // { self .state .name } \n └─ { self .job_type } <{ start } ⟶ { end } >'
179
+ end = (
180
+ "None"
181
+ if self .completed_at is None
182
+ else self .completed_at .strftime (datetime_fmt )
183
+ )
184
+ return f"┌─ [{ self .job_id } ] { self .name } // { self .state .name } \n └─ { self .job_type } <{ start } ⟶ { end } >"
160
185
161
186
162
187
@dataclass
163
188
class Sort (JSONWizard ):
164
- is_sorted : bool = json_field (' sorted' , all = True )
165
- is_unsorted : bool = json_field (' unsorted' , all = True )
166
- is_empty : bool = json_field (' empty' , all = True )
189
+ is_sorted : bool = json_field (" sorted" , all = True )
190
+ is_unsorted : bool = json_field (" unsorted" , all = True )
191
+ is_empty : bool = json_field (" empty" , all = True )
167
192
168
193
169
194
@dataclass
@@ -194,5 +219,5 @@ class TomtomResponseSearchJobs(JSONWizard):
194
219
@dataclass
195
220
class JsonIpResponse (JSONWizard ):
196
221
ip : str
197
- geo_ip : str = json_field (' geo-ip' , all = True )
198
- api_help : str = json_field (' API Help' , all = True )
222
+ geo_ip : str = json_field (" geo-ip" , all = True )
223
+ api_help : str = json_field (" API Help" , all = True )
0 commit comments