2
2
import codecs
3
3
import csv as csv_parser
4
4
5
- from dateutil .parser import parse as timestamp_parser
6
5
import ciso8601
6
+ from urllib3 import HTTPResponse
7
7
8
8
from influxdb_client .client .flux_table import FluxTable , FluxColumn , FluxRecord
9
9
@@ -20,21 +20,32 @@ class FluxCsvParserException(Exception):
20
20
21
21
class FluxCsvParser (object ):
22
22
23
- def __init__ (self ) -> None :
23
+ def __init__ (self , response : HTTPResponse , stream : bool ) -> None :
24
+ self ._response = response
25
+ self .tables = []
26
+ self ._stream = stream
24
27
pass
25
28
26
- def parse_flux_response (self , response , cancellable , consumer ):
29
+ def __enter__ (self ):
30
+ self ._reader = csv_parser .reader (codecs .iterdecode (self ._response , 'utf-8' ))
31
+ return self
32
+
33
+ def __exit__ (self , exc_type , exc_val , exc_tb ):
34
+ self ._response .close ()
35
+
36
+ def generator (self ):
37
+ with self as parser :
38
+ yield from parser ._parse_flux_response ()
39
+
40
+ def _parse_flux_response (self ):
27
41
table_index = 0
28
42
start_new_table = False
29
43
table = None
30
44
parsing_state_error = False
31
- reader = csv_parser .reader (codecs .iterdecode (response , 'utf-8' ))
32
45
33
- for csv in reader :
46
+ for csv in self . _reader :
34
47
# debug
35
48
# print("parsing: ", csv)
36
- if (cancellable is not None ) and cancellable .canceled :
37
- return
38
49
39
50
# Response has HTTP status ok, but response is error.
40
51
if len (csv ) < 1 :
@@ -55,7 +66,7 @@ def parse_flux_response(self, response, cancellable, consumer):
55
66
if "#datatype" == token :
56
67
start_new_table = True
57
68
table = FluxTable ()
58
- consumer . accept_table ( index = table_index , cancellable = cancellable , flux_table = table )
69
+ self . _insert_table ( table , table_index )
59
70
table_index = table_index + 1
60
71
elif table is None :
61
72
raise FluxCsvParserException ("Unable to parse CSV response. FluxTable definition was not found." )
@@ -85,11 +96,16 @@ def parse_flux_response(self, response, cancellable, consumer):
85
96
flux_columns = table .columns
86
97
table = FluxTable ()
87
98
table .columns .extend (flux_columns )
88
- consumer . accept_table ( table_index , cancellable , table )
99
+ self . _insert_table ( table , table_index )
89
100
table_index = table_index + 1
90
101
91
102
flux_record = self .parse_record (table_index - 1 , table , csv )
92
- consumer .accept_record (table_index - 1 , cancellable , flux_record )
103
+
104
+ if not self ._stream :
105
+ self .tables [table_index - 1 ].records .append (flux_record )
106
+
107
+ yield flux_record
108
+
93
109
# debug
94
110
# print(flux_record)
95
111
@@ -163,14 +179,6 @@ def add_column_names_and_tags(table, csv):
163
179
column .label = csv [i ]
164
180
i += 1
165
181
166
-
167
- class FluxResponseConsumerTable :
168
-
169
- def __init__ (self ) -> None :
170
- self .tables = []
171
-
172
- def accept_table (self , index , cancellable , flux_table ):
173
- self .tables .insert (index , flux_table )
174
-
175
- def accept_record (self , index , cancellable , flux_record ):
176
- self .tables [index ].records .append (flux_record )
182
+ def _insert_table (self , table , table_index ):
183
+ if not self ._stream :
184
+ self .tables .insert (table_index , table )
0 commit comments