kafi 0.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- kafi-0.0.1/LICENSE +201 -0
- kafi-0.0.1/PKG-INFO +24 -0
- kafi-0.0.1/README.md +5 -0
- kafi-0.0.1/kafi/__init__.py +0 -0
- kafi-0.0.1/kafi/functional.py +236 -0
- kafi-0.0.1/kafi/helpers.py +218 -0
- kafi-0.0.1/kafi/kafi.py +5 -0
- kafi-0.0.1/kafi/shell.py +114 -0
- kafi-0.0.1/kafi/storage.py +154 -0
- kafi-0.0.1/kafi/storage_reader.py +46 -0
- kafi-0.0.1/kafi.egg-info/PKG-INFO +24 -0
- kafi-0.0.1/kafi.egg-info/SOURCES.txt +20 -0
- kafi-0.0.1/kafi.egg-info/dependency_links.txt +1 -0
- kafi-0.0.1/kafi.egg-info/requires.txt +8 -0
- kafi-0.0.1/kafi.egg-info/top_level.txt +1 -0
- kafi-0.0.1/setup.cfg +4 -0
- kafi-0.0.1/setup.py +34 -0
- kafi-0.0.1/test/test_fs_azureblob.py +363 -0
- kafi-0.0.1/test/test_fs_local.py +384 -0
- kafi-0.0.1/test/test_fs_s3.py +363 -0
- kafi-0.0.1/test/test_kafka_cluster.py +729 -0
- kafi-0.0.1/test/test_kafka_restproxy.py +599 -0
kafi-0.0.1/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
kafi-0.0.1/PKG-INFO
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: kafi
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Kafka and Files
|
|
5
|
+
Home-page: https://github.com/xdgrulez/kafi
|
|
6
|
+
Author: Ralph M. Debusmann
|
|
7
|
+
Author-email: matthias.debusmann@gmail.com
|
|
8
|
+
License: Apache License 2.0
|
|
9
|
+
Platform: UNKNOWN
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.6
|
|
14
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
License-File: LICENSE
|
|
17
|
+
|
|
18
|
+
# kafi
|
|
19
|
+
|
|
20
|
+
*kafi* brings the two worlds of Kafka and files together.
|
|
21
|
+
|
|
22
|
+
*kafi* is the successor of *kash.py* which is the successor of *streampunk*.
|
|
23
|
+
|
|
24
|
+
|
kafi-0.0.1/README.md
ADDED
|
File without changes
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import time
|
|
2
|
+
|
|
3
|
+
# Constants
|
|
4
|
+
|
|
5
|
+
ALL_MESSAGES = -1
|
|
6
|
+
|
|
7
|
+
#
|
|
8
|
+
|
|
9
|
+
class Functional:
|
|
10
|
+
def foldl(self, resource, foldl_function, initial_acc, n=ALL_MESSAGES, **kwargs):
|
|
11
|
+
verbose_int = self.verbose()
|
|
12
|
+
progress_num_messages_int = self.progress_num_messages()
|
|
13
|
+
#
|
|
14
|
+
reader = self.openr(resource, **kwargs)
|
|
15
|
+
#
|
|
16
|
+
def foldl_function1(acc_progress_message_counter_int_tuple, message_dict):
|
|
17
|
+
(acc, progress_message_counter_int) = acc_progress_message_counter_int_tuple
|
|
18
|
+
#
|
|
19
|
+
acc = foldl_function(acc, message_dict)
|
|
20
|
+
#
|
|
21
|
+
progress_message_counter_int += 1
|
|
22
|
+
if verbose_int > 0 and progress_message_counter_int % progress_num_messages_int == 0:
|
|
23
|
+
print(f"Read: {progress_message_counter_int}")
|
|
24
|
+
#
|
|
25
|
+
return (acc, progress_message_counter_int)
|
|
26
|
+
#
|
|
27
|
+
result_progress_message_counter_int_tuple = reader.foldl(foldl_function1, (initial_acc, 0), n, **kwargs)
|
|
28
|
+
#
|
|
29
|
+
reader.close()
|
|
30
|
+
#
|
|
31
|
+
return result_progress_message_counter_int_tuple
|
|
32
|
+
|
|
33
|
+
#
|
|
34
|
+
|
|
35
|
+
def flatmap(self, resource, flatmap_function, n=ALL_MESSAGES, **kwargs):
|
|
36
|
+
def foldl_function(list, message_dict):
|
|
37
|
+
list += flatmap_function(message_dict)
|
|
38
|
+
#
|
|
39
|
+
return list
|
|
40
|
+
#
|
|
41
|
+
return self.foldl(resource, foldl_function, [], n, **kwargs)
|
|
42
|
+
|
|
43
|
+
def map(self, resource, map_function, n=ALL_MESSAGES, **kwargs):
|
|
44
|
+
def flatmap_function(message_dict):
|
|
45
|
+
return [map_function(message_dict)]
|
|
46
|
+
#
|
|
47
|
+
return self.flatmap(resource, flatmap_function, n, **kwargs)
|
|
48
|
+
|
|
49
|
+
def filter(self, resource, filter_function, n=ALL_MESSAGES, **kwargs):
|
|
50
|
+
def flatmap_function(message_dict):
|
|
51
|
+
return [message_dict] if filter_function(message_dict) else []
|
|
52
|
+
#
|
|
53
|
+
return self.flatmap(resource, flatmap_function, n, **kwargs)
|
|
54
|
+
|
|
55
|
+
def foreach(self, resource, foreach_function, n=ALL_MESSAGES, **kwargs):
|
|
56
|
+
def foldl_function(_, message_dict):
|
|
57
|
+
foreach_function(message_dict)
|
|
58
|
+
#
|
|
59
|
+
self.foldl(resource, foldl_function, None, n, **kwargs)
|
|
60
|
+
|
|
61
|
+
#
|
|
62
|
+
|
|
63
|
+
def flatmap_to(self, resource, target_storage, target_resource, flatmap_function, n=ALL_MESSAGES, **kwargs):
|
|
64
|
+
progress_num_messages_int = self.progress_num_messages()
|
|
65
|
+
verbose_int = self.verbose()
|
|
66
|
+
#
|
|
67
|
+
def write_batch(batch_message_dict_list):
|
|
68
|
+
value_list = [message_dict["value"] for message_dict in batch_message_dict_list]
|
|
69
|
+
#
|
|
70
|
+
key_list = [message_dict["key"] for message_dict in batch_message_dict_list]
|
|
71
|
+
if "preserve_partition" in kwargs and kwargs["preserve_partition"] == True:
|
|
72
|
+
partition_list = [message_dict["partition"] for message_dict in batch_message_dict_list]
|
|
73
|
+
else:
|
|
74
|
+
partition_list = None
|
|
75
|
+
if "preserve_timestamp" in kwargs and kwargs["preserve_timestamp"] == True:
|
|
76
|
+
timestamp_list = [message_dict["timestamp"][1] for message_dict in batch_message_dict_list]
|
|
77
|
+
else:
|
|
78
|
+
timestamp_list = None
|
|
79
|
+
headers_list = [message_dict["headers"] for message_dict in batch_message_dict_list]
|
|
80
|
+
target_writer.write(value_list, key=key_list, partition=partition_list, timestamp=timestamp_list, headers=headers_list)
|
|
81
|
+
#
|
|
82
|
+
|
|
83
|
+
def foldl_function(write_batch_size_int_batch_message_dict_list_progress_message_counter_int_tuple, message_dict):
|
|
84
|
+
(write_batch_size_int, batch_message_dict_list, progress_message_counter_int) = write_batch_size_int_batch_message_dict_list_progress_message_counter_int_tuple
|
|
85
|
+
#
|
|
86
|
+
message_dict_list = flatmap_function(message_dict)
|
|
87
|
+
#
|
|
88
|
+
batch_message_dict_list += message_dict_list
|
|
89
|
+
#
|
|
90
|
+
if len(batch_message_dict_list) >= write_batch_size_int:
|
|
91
|
+
write_batch(batch_message_dict_list)
|
|
92
|
+
#
|
|
93
|
+
progress_message_counter_int += len(batch_message_dict_list)
|
|
94
|
+
if verbose_int > 0 and progress_message_counter_int % progress_num_messages_int == 0:
|
|
95
|
+
print(f"Written: {progress_message_counter_int}")
|
|
96
|
+
#
|
|
97
|
+
return (write_batch_size_int, [], progress_message_counter_int)
|
|
98
|
+
else:
|
|
99
|
+
return (write_batch_size_int, batch_message_dict_list, progress_message_counter_int)
|
|
100
|
+
|
|
101
|
+
source_kwargs = kwargs.copy()
|
|
102
|
+
if "source_key_type" in kwargs:
|
|
103
|
+
source_kwargs["key_type"] = kwargs["source_key_type"]
|
|
104
|
+
if "source_value_type" in kwargs:
|
|
105
|
+
source_kwargs["value_type"] = kwargs["source_value_type"]
|
|
106
|
+
if "source_type" in kwargs:
|
|
107
|
+
source_kwargs["type"] = kwargs["source_type"]
|
|
108
|
+
if "source_key_schema" in kwargs:
|
|
109
|
+
source_kwargs["key_schema"] = kwargs["source_key_schema"]
|
|
110
|
+
if "source_value_schema" in kwargs:
|
|
111
|
+
source_kwargs["value_schema"] = kwargs["source_value_schema"]
|
|
112
|
+
if "source_key_schema_id" in kwargs:
|
|
113
|
+
source_kwargs["key_schema_id"] = kwargs["source_key_schema_id"]
|
|
114
|
+
if "source_value_schema_id" in kwargs:
|
|
115
|
+
source_kwargs["value_schema_id"] = kwargs["source_value_schema_id"]
|
|
116
|
+
#
|
|
117
|
+
target_kwargs = kwargs.copy()
|
|
118
|
+
if "target_key_type" in kwargs:
|
|
119
|
+
target_kwargs["key_type"] = kwargs["target_key_type"]
|
|
120
|
+
if "target_value_type" in kwargs:
|
|
121
|
+
target_kwargs["value_type"] = kwargs["target_value_type"]
|
|
122
|
+
if "target_type" in kwargs:
|
|
123
|
+
target_kwargs["type"] = kwargs["target_type"]
|
|
124
|
+
if "target_key_schema" in kwargs:
|
|
125
|
+
target_kwargs["key_schema"] = kwargs["target_key_schema"]
|
|
126
|
+
if "target_value_schema" in kwargs:
|
|
127
|
+
target_kwargs["value_schema"] = kwargs["target_value_schema"]
|
|
128
|
+
if "target_key_schema_id" in kwargs:
|
|
129
|
+
target_kwargs["key_schema_id"] = kwargs["target_key_schema_id"]
|
|
130
|
+
if "target_value_schema_id" in kwargs:
|
|
131
|
+
target_kwargs["value_schema_id"] = kwargs["target_value_schema_id"]
|
|
132
|
+
#
|
|
133
|
+
write_batch_size_int = kwargs["write_batch_size"] if "write_batch_size" in kwargs else target_storage.write_batch_size()
|
|
134
|
+
#
|
|
135
|
+
target_writer = target_storage.openw(target_resource, **target_kwargs)
|
|
136
|
+
#
|
|
137
|
+
(write_batch_size_int_batch_message_dict_list_progress_message_counter_int_tuple, read_message_counter_int) = self.foldl(resource, foldl_function, (write_batch_size_int, [], 0), n, **source_kwargs)
|
|
138
|
+
(_, batch_message_dict_list, written_progress_message_counter_int) = write_batch_size_int_batch_message_dict_list_progress_message_counter_int_tuple
|
|
139
|
+
#
|
|
140
|
+
if len(batch_message_dict_list) > 0:
|
|
141
|
+
write_batch(batch_message_dict_list)
|
|
142
|
+
written_progress_message_counter_int += len(batch_message_dict_list)
|
|
143
|
+
#
|
|
144
|
+
target_writer.close()
|
|
145
|
+
#
|
|
146
|
+
return (read_message_counter_int, written_progress_message_counter_int)
|
|
147
|
+
|
|
148
|
+
def map_to(self, resource, target_storage, target_resource, map_function, n=ALL_MESSAGES, **kwargs):
|
|
149
|
+
def flatmap_function(message_dict):
|
|
150
|
+
return [map_function(message_dict)]
|
|
151
|
+
#
|
|
152
|
+
return self.flatmap_to(resource, target_storage, target_resource, flatmap_function, n, **kwargs)
|
|
153
|
+
|
|
154
|
+
def filter_to(self, resource, target_storage, target_resource, filter_function, n=ALL_MESSAGES, **kwargs):
|
|
155
|
+
def flatmap_function(message_dict):
|
|
156
|
+
return [message_dict] if filter_function(message_dict) else []
|
|
157
|
+
#
|
|
158
|
+
return self.flatmap_to(resource, target_storage, target_resource, flatmap_function, n, **kwargs)
|
|
159
|
+
|
|
160
|
+
#
|
|
161
|
+
|
|
162
|
+
def zip_foldl(self, resource1, storage2, resource2, zip_foldl_function, initial_acc, n=ALL_MESSAGES, **kwargs):
|
|
163
|
+
n_int = n
|
|
164
|
+
#
|
|
165
|
+
read_batch_size_int = kwargs["read_batch_size"] if "read_batch_size" in kwargs else self.read_batch_size()
|
|
166
|
+
if read_batch_size_int > n_int:
|
|
167
|
+
read_batch_size_int = n_int
|
|
168
|
+
#
|
|
169
|
+
break_function = kwargs["break_function"] if "break_function" in kwargs else lambda _, _1: False
|
|
170
|
+
#
|
|
171
|
+
kwargs1 = kwargs.copy()
|
|
172
|
+
kwargs1["group"] = kwargs1["group1"] if "group1" in kwargs1 else None
|
|
173
|
+
kwargs1["offsets"] = kwargs1["offsets1"] if "offsets1" in kwargs1 else None
|
|
174
|
+
kwargs1["key_type"] = kwargs1["key_type1"] if "key_type1" in kwargs1 else "bytes"
|
|
175
|
+
kwargs1["value_type"] = kwargs1["value_type1"] if "value_type1" in kwargs1 else "bytes"
|
|
176
|
+
kwargs1["type"] = kwargs1["type1"] if "type1" in kwargs1 else "bytes"
|
|
177
|
+
#
|
|
178
|
+
kwargs2 = kwargs.copy()
|
|
179
|
+
kwargs2["group"] = kwargs2["group2"] if "group2" in kwargs2 else None
|
|
180
|
+
kwargs2["offsets"] = kwargs2["offsets2"] if "offsets2" in kwargs2 else None
|
|
181
|
+
kwargs2["key_type"] = kwargs2["key_type2"] if "key_type2" in kwargs2 else "bytes"
|
|
182
|
+
kwargs2["value_type"] = kwargs2["value_type2"] if "value_type2" in kwargs2 else "bytes"
|
|
183
|
+
kwargs2["type"] = kwargs2["type2"] if "type2" in kwargs2 else "bytes"
|
|
184
|
+
#
|
|
185
|
+
reader1 = self.openr(resource1, **kwargs1)
|
|
186
|
+
reader2 = storage2.openr(resource2, **kwargs2)
|
|
187
|
+
#
|
|
188
|
+
message_counter_int1 = 0
|
|
189
|
+
message_counter_int2 = 0
|
|
190
|
+
acc = initial_acc
|
|
191
|
+
break_bool = False
|
|
192
|
+
while True:
|
|
193
|
+
message_dict_list1 = []
|
|
194
|
+
while True:
|
|
195
|
+
message_dict_list1 += reader1.read(n=read_batch_size_int)
|
|
196
|
+
if not message_dict_list1 or read_batch_size_int == ALL_MESSAGES or len(message_dict_list1) == read_batch_size_int:
|
|
197
|
+
break
|
|
198
|
+
if not message_dict_list1:
|
|
199
|
+
break
|
|
200
|
+
num_messages_int1 = len(message_dict_list1)
|
|
201
|
+
message_counter_int1 += num_messages_int1
|
|
202
|
+
if self.verbose() > 0 and message_counter_int1 % self.progress_num_messages() == 0:
|
|
203
|
+
print(f"Read (storage 1): {message_counter_int1}")
|
|
204
|
+
#
|
|
205
|
+
read_batch_size_int2 = num_messages_int1 if num_messages_int1 < read_batch_size_int else read_batch_size_int
|
|
206
|
+
message_dict_list2 = []
|
|
207
|
+
while True:
|
|
208
|
+
message_dict_list2 += reader2.read(n=read_batch_size_int2)
|
|
209
|
+
if not message_dict_list2 or read_batch_size_int2 == ALL_MESSAGES or len(message_dict_list2) == read_batch_size_int2:
|
|
210
|
+
break
|
|
211
|
+
if not message_dict_list2:
|
|
212
|
+
break
|
|
213
|
+
num_messages_int2 = len(message_dict_list2)
|
|
214
|
+
message_counter_int2 += num_messages_int2
|
|
215
|
+
if self.verbose() > 0 and message_counter_int2 % self.progress_num_messages() == 0:
|
|
216
|
+
print(f"Read (storage 2): {message_counter_int2}")
|
|
217
|
+
#
|
|
218
|
+
if num_messages_int1 != num_messages_int2:
|
|
219
|
+
break
|
|
220
|
+
#
|
|
221
|
+
for message_dict1, message_dict2 in zip(message_dict_list1, message_dict_list2):
|
|
222
|
+
if break_function(message_dict1, message_dict2):
|
|
223
|
+
break_bool = True
|
|
224
|
+
break
|
|
225
|
+
acc = zip_foldl_function(acc, message_dict1, message_dict2)
|
|
226
|
+
#
|
|
227
|
+
if break_bool:
|
|
228
|
+
break
|
|
229
|
+
#
|
|
230
|
+
if n_int != ALL_MESSAGES:
|
|
231
|
+
if message_counter_int1 >= n_int:
|
|
232
|
+
break
|
|
233
|
+
#
|
|
234
|
+
reader1.close()
|
|
235
|
+
reader2.close()
|
|
236
|
+
return acc, message_counter_int1, message_counter_int2
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import base64
|
|
2
|
+
import binascii
|
|
3
|
+
import json
|
|
4
|
+
import requests
|
|
5
|
+
from requests.adapters import HTTPAdapter, Retry
|
|
6
|
+
import sys
|
|
7
|
+
import time
|
|
8
|
+
|
|
9
|
+
def get_millis():
|
|
10
|
+
return int(time.time()*1000)
|
|
11
|
+
|
|
12
|
+
def is_interactive():
|
|
13
|
+
return hasattr(sys, 'ps1')
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def is_file(str):
|
|
17
|
+
return "/" in str
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def pretty(dict):
|
|
21
|
+
return json.dumps(dict, indent=2)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def ppretty(dict):
|
|
25
|
+
print(pretty(dict))
|
|
26
|
+
|
|
27
|
+
#
|
|
28
|
+
|
|
29
|
+
def create_session(retries_int):
|
|
30
|
+
#logging.basicConfig(level=logging.DEBUG)
|
|
31
|
+
session = requests.Session()
|
|
32
|
+
retry = Retry(total=retries_int, backoff_factor=1, status_forcelist=[500, 502, 503, 504, 404], allowed_methods=None)
|
|
33
|
+
adapter = HTTPAdapter(max_retries=retry)
|
|
34
|
+
session.mount('http://', adapter)
|
|
35
|
+
session.mount('https://', adapter)
|
|
36
|
+
#
|
|
37
|
+
return session
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def get(url_str, headers_dict, payload_dict=None, auth_str_tuple=None, retries=0):
|
|
41
|
+
session = create_session(retries)
|
|
42
|
+
if payload_dict is None:
|
|
43
|
+
response = session.get(url_str, headers=headers_dict, auth=auth_str_tuple)
|
|
44
|
+
else:
|
|
45
|
+
response = session.get(url_str, headers=headers_dict, json=payload_dict, auth=auth_str_tuple)
|
|
46
|
+
#
|
|
47
|
+
if is_json(response.text):
|
|
48
|
+
response_dict = response.json()
|
|
49
|
+
else:
|
|
50
|
+
response_dict = {"response": response.text}
|
|
51
|
+
#
|
|
52
|
+
if "error_code" in response_dict and response_dict["error_code"] > 400:
|
|
53
|
+
raise Exception(response_dict["message"])
|
|
54
|
+
#
|
|
55
|
+
if response.ok:
|
|
56
|
+
return response_dict
|
|
57
|
+
else:
|
|
58
|
+
raise Exception(response_dict)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def delete(url_str, headers_dict, auth_str_tuple=None, retries=10):
|
|
62
|
+
session = create_session(retries)
|
|
63
|
+
response = session.delete(url_str, headers=headers_dict, auth=auth_str_tuple)
|
|
64
|
+
if is_json(response.text):
|
|
65
|
+
response_dict = response.json()
|
|
66
|
+
else:
|
|
67
|
+
response_dict = {"response": response.text}
|
|
68
|
+
#
|
|
69
|
+
if "error_code" in response_dict and response_dict["error_code"] > 400:
|
|
70
|
+
raise Exception(response_dict["message"])
|
|
71
|
+
#
|
|
72
|
+
if response.ok:
|
|
73
|
+
return response_dict
|
|
74
|
+
else:
|
|
75
|
+
raise Exception(response_dict)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def post(url_str, headers_dict, payload_dict_or_generator, auth_str_tuple=None, retries=10):
|
|
79
|
+
session = create_session(retries)
|
|
80
|
+
if isinstance(payload_dict_or_generator, dict):
|
|
81
|
+
response = session.post(url_str, headers=headers_dict, json=payload_dict_or_generator, auth=auth_str_tuple)
|
|
82
|
+
else:
|
|
83
|
+
response = session.post(url_str, headers=headers_dict, data=payload_dict_or_generator, auth=auth_str_tuple)
|
|
84
|
+
#
|
|
85
|
+
if is_json(response.text):
|
|
86
|
+
response_dict = response.json()
|
|
87
|
+
#
|
|
88
|
+
if "error_code" in response_dict and response_dict["error_code"] > 400:
|
|
89
|
+
raise Exception(response_dict["message"])
|
|
90
|
+
#
|
|
91
|
+
if response.ok:
|
|
92
|
+
return response_dict
|
|
93
|
+
else:
|
|
94
|
+
raise Exception(response_dict)
|
|
95
|
+
else:
|
|
96
|
+
response_text_list = "[" + response.text[:-2].replace("\r\n", ",") + "]"
|
|
97
|
+
response_dict_list = json.loads(response_text_list)
|
|
98
|
+
#
|
|
99
|
+
for response_dict in response_dict_list:
|
|
100
|
+
if "error_code" in response_dict and response_dict["error_code"] > 400:
|
|
101
|
+
raise Exception(response_dict["message"])
|
|
102
|
+
#
|
|
103
|
+
if response.ok:
|
|
104
|
+
return response_dict_list
|
|
105
|
+
else:
|
|
106
|
+
raise Exception(response_dict_list)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def get_auth_str_tuple(basic_auth_user_info):
|
|
110
|
+
if basic_auth_user_info is None:
|
|
111
|
+
auth_str_tuple = None
|
|
112
|
+
else:
|
|
113
|
+
auth_str_tuple = tuple(basic_auth_user_info.split(":"))
|
|
114
|
+
#
|
|
115
|
+
return auth_str_tuple
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def is_json(str):
|
|
119
|
+
try:
|
|
120
|
+
json.loads(str)
|
|
121
|
+
except ValueError as e:
|
|
122
|
+
return False
|
|
123
|
+
return True
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def is_pattern(str):
|
|
127
|
+
return "*" in str or "?" in str or ("[" in str and "]" in str) or ("[!" in str and "]" in str)
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def is_base64_encoded(str_or_bytes_or_dict):
|
|
131
|
+
try:
|
|
132
|
+
if isinstance(str_or_bytes_or_dict, bytes):
|
|
133
|
+
decoded_bytes = base64.b64decode(str_or_bytes_or_dict)
|
|
134
|
+
elif isinstance(str_or_bytes_or_dict, str):
|
|
135
|
+
decoded_bytes = base64.b64decode(bytes(str_or_bytes_or_dict, encoding="utf-8"))
|
|
136
|
+
elif isinstance(str_or_bytes_or_dict, dict):
|
|
137
|
+
decoded_bytes = base64.b64decode(bytes(json.dumps(str_or_bytes_or_dict), encoding="utf-8"))
|
|
138
|
+
else:
|
|
139
|
+
return False
|
|
140
|
+
encoded_bytes = base64.b64encode(decoded_bytes)
|
|
141
|
+
return str_or_bytes_or_dict == encoded_bytes
|
|
142
|
+
except (binascii.Error, UnicodeDecodeError):
|
|
143
|
+
return False
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def base64_encode(str_or_bytes_or_dict):
|
|
147
|
+
if isinstance(str_or_bytes_or_dict, bytes):
|
|
148
|
+
encoded_bytes = base64.b64encode(str_or_bytes_or_dict)
|
|
149
|
+
elif isinstance(str_or_bytes_or_dict, str):
|
|
150
|
+
encoded_bytes = base64.b64encode(bytes(str_or_bytes_or_dict, encoding="utf-8"))
|
|
151
|
+
elif isinstance(str_or_bytes_or_dict, dict):
|
|
152
|
+
encoded_bytes = base64.b64encode(bytes(json.dumps(str_or_bytes_or_dict), encoding="utf-8"))
|
|
153
|
+
return encoded_bytes
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def base64_decode(base64_str):
|
|
157
|
+
return base64.b64decode(bytes(base64_str, encoding="utf-8"))
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
def bytes_or_str_to_bytes(bytes_or_str):
|
|
161
|
+
if isinstance(bytes_or_str, bytes):
|
|
162
|
+
return_bytes = bytes_or_str
|
|
163
|
+
elif isinstance(bytes_or_str, str):
|
|
164
|
+
return_bytes = bytes(bytes_or_str, encoding="utf-8")
|
|
165
|
+
#
|
|
166
|
+
return return_bytes
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def bytes_to_str(bytes):
|
|
170
|
+
if bytes is None:
|
|
171
|
+
str = None
|
|
172
|
+
else:
|
|
173
|
+
str = bytes.decode("utf-8")
|
|
174
|
+
#
|
|
175
|
+
return str
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
def bytes_to_dict(bytes):
|
|
179
|
+
if bytes is None:
|
|
180
|
+
dict = None
|
|
181
|
+
else:
|
|
182
|
+
dict = json.loads(bytes.decode("utf-8"))
|
|
183
|
+
#
|
|
184
|
+
return dict
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
def str_to_bytes(str):
|
|
188
|
+
if str is None:
|
|
189
|
+
bytes = None
|
|
190
|
+
else:
|
|
191
|
+
bytes = str.encode("utf-8")
|
|
192
|
+
#
|
|
193
|
+
return bytes
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
def dict_to_bytes(dict):
|
|
197
|
+
if dict is None:
|
|
198
|
+
bytes = None
|
|
199
|
+
else:
|
|
200
|
+
bytes = str(dict).encode("utf-8")
|
|
201
|
+
#
|
|
202
|
+
return bytes
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
# def find_file_offset_of_message(path_file_str, message_int, message_separator="\n"):
|
|
206
|
+
# file_offset_int = 0
|
|
207
|
+
# #
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
# for _ in range(line_int):
|
|
212
|
+
# line_str = bufferedReader.readline()
|
|
213
|
+
# if not line_str:
|
|
214
|
+
# break
|
|
215
|
+
# file_offset_int += len(line_str)
|
|
216
|
+
# #
|
|
217
|
+
# return file_offset_int
|
|
218
|
+
|