etlplus 0.9.1__py3-none-any.whl → 0.9.2__py3-none-any.whl
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.
- etlplus/README.md +37 -0
- etlplus/__init__.py +1 -26
- etlplus/api/README.md +51 -3
- etlplus/api/__init__.py +10 -0
- etlplus/api/config.py +39 -28
- etlplus/api/endpoint_client.py +3 -3
- etlplus/api/enums.py +51 -0
- etlplus/api/pagination/client.py +1 -1
- etlplus/api/rate_limiting/config.py +13 -1
- etlplus/api/rate_limiting/rate_limiter.py +8 -11
- etlplus/api/request_manager.py +11 -6
- etlplus/api/transport.py +14 -2
- etlplus/api/types.py +96 -6
- etlplus/{run_helpers.py → api/utils.py} +209 -153
- etlplus/cli/README.md +40 -0
- etlplus/cli/commands.py +76 -43
- etlplus/cli/constants.py +1 -1
- etlplus/cli/handlers.py +40 -12
- etlplus/cli/io.py +2 -2
- etlplus/cli/main.py +1 -1
- etlplus/cli/state.py +4 -7
- etlplus/database/README.md +48 -0
- etlplus/database/ddl.py +1 -1
- etlplus/database/engine.py +19 -3
- etlplus/database/orm.py +2 -0
- etlplus/database/schema.py +1 -1
- etlplus/enums.py +1 -157
- etlplus/file/README.md +105 -0
- etlplus/file/__init__.py +25 -0
- etlplus/file/_imports.py +141 -0
- etlplus/file/_io.py +160 -0
- etlplus/file/accdb.py +78 -0
- etlplus/file/arrow.py +78 -0
- etlplus/file/avro.py +176 -0
- etlplus/file/bson.py +77 -0
- etlplus/file/cbor.py +78 -0
- etlplus/file/cfg.py +79 -0
- etlplus/file/conf.py +80 -0
- etlplus/file/core.py +322 -0
- etlplus/file/csv.py +79 -0
- etlplus/file/dat.py +78 -0
- etlplus/file/dta.py +77 -0
- etlplus/file/duckdb.py +78 -0
- etlplus/file/enums.py +343 -0
- etlplus/file/feather.py +111 -0
- etlplus/file/fwf.py +77 -0
- etlplus/file/gz.py +123 -0
- etlplus/file/hbs.py +78 -0
- etlplus/file/hdf5.py +78 -0
- etlplus/file/ini.py +79 -0
- etlplus/file/ion.py +78 -0
- etlplus/file/jinja2.py +78 -0
- etlplus/file/json.py +98 -0
- etlplus/file/log.py +78 -0
- etlplus/file/mat.py +78 -0
- etlplus/file/mdb.py +78 -0
- etlplus/file/msgpack.py +78 -0
- etlplus/file/mustache.py +78 -0
- etlplus/file/nc.py +78 -0
- etlplus/file/ndjson.py +108 -0
- etlplus/file/numbers.py +75 -0
- etlplus/file/ods.py +79 -0
- etlplus/file/orc.py +111 -0
- etlplus/file/parquet.py +113 -0
- etlplus/file/pb.py +78 -0
- etlplus/file/pbf.py +77 -0
- etlplus/file/properties.py +78 -0
- etlplus/file/proto.py +77 -0
- etlplus/file/psv.py +79 -0
- etlplus/file/rda.py +78 -0
- etlplus/file/rds.py +78 -0
- etlplus/file/sas7bdat.py +78 -0
- etlplus/file/sav.py +77 -0
- etlplus/file/sqlite.py +78 -0
- etlplus/file/stub.py +84 -0
- etlplus/file/sylk.py +77 -0
- etlplus/file/tab.py +81 -0
- etlplus/file/toml.py +78 -0
- etlplus/file/tsv.py +80 -0
- etlplus/file/txt.py +102 -0
- etlplus/file/vm.py +78 -0
- etlplus/file/wks.py +77 -0
- etlplus/file/xls.py +88 -0
- etlplus/file/xlsm.py +79 -0
- etlplus/file/xlsx.py +99 -0
- etlplus/file/xml.py +185 -0
- etlplus/file/xpt.py +78 -0
- etlplus/file/yaml.py +95 -0
- etlplus/file/zip.py +175 -0
- etlplus/file/zsav.py +77 -0
- etlplus/ops/README.md +50 -0
- etlplus/ops/__init__.py +61 -0
- etlplus/{extract.py → ops/extract.py} +81 -99
- etlplus/{load.py → ops/load.py} +78 -101
- etlplus/{run.py → ops/run.py} +159 -127
- etlplus/{transform.py → ops/transform.py} +75 -68
- etlplus/{validation → ops}/utils.py +53 -17
- etlplus/{validate.py → ops/validate.py} +22 -12
- etlplus/templates/README.md +46 -0
- etlplus/types.py +5 -4
- etlplus/utils.py +136 -2
- etlplus/workflow/README.md +52 -0
- etlplus/{config → workflow}/__init__.py +10 -23
- etlplus/{config → workflow}/connector.py +58 -44
- etlplus/workflow/dag.py +105 -0
- etlplus/{config → workflow}/jobs.py +105 -32
- etlplus/{config → workflow}/pipeline.py +59 -51
- etlplus/{config → workflow}/profile.py +8 -5
- etlplus/workflow/types.py +115 -0
- {etlplus-0.9.1.dist-info → etlplus-0.9.2.dist-info}/METADATA +210 -17
- etlplus-0.9.2.dist-info/RECORD +134 -0
- {etlplus-0.9.1.dist-info → etlplus-0.9.2.dist-info}/WHEEL +1 -1
- etlplus/config/types.py +0 -204
- etlplus/config/utils.py +0 -120
- etlplus/file.py +0 -657
- etlplus/validation/__init__.py +0 -44
- etlplus-0.9.1.dist-info/RECORD +0 -65
- {etlplus-0.9.1.dist-info → etlplus-0.9.2.dist-info}/entry_points.txt +0 -0
- {etlplus-0.9.1.dist-info → etlplus-0.9.2.dist-info}/licenses/LICENSE +0 -0
- {etlplus-0.9.1.dist-info → etlplus-0.9.2.dist-info}/top_level.txt +0 -0
etlplus/file/txt.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"""
|
|
2
|
+
:mod:`etlplus.file.txt` module.
|
|
3
|
+
|
|
4
|
+
Helpers for reading/writing text (TXT) files.
|
|
5
|
+
|
|
6
|
+
Notes
|
|
7
|
+
-----
|
|
8
|
+
- A TXT file is a plain text file that contains unformatted text.
|
|
9
|
+
- Common cases:
|
|
10
|
+
- Each line in the file represents a single piece of text.
|
|
11
|
+
- Lines may vary in length and content.
|
|
12
|
+
- Rule of thumb:
|
|
13
|
+
- If the file is a simple text file without specific formatting
|
|
14
|
+
requirements, use this module for reading and writing.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
19
|
+
from pathlib import Path
|
|
20
|
+
|
|
21
|
+
from ..types import JSONData
|
|
22
|
+
from ..types import JSONList
|
|
23
|
+
from ..utils import count_records
|
|
24
|
+
from ._io import normalize_records
|
|
25
|
+
|
|
26
|
+
# SECTION: EXPORTS ========================================================== #
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
__all__ = [
|
|
30
|
+
'read',
|
|
31
|
+
'write',
|
|
32
|
+
]
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
# SECTION: FUNCTIONS ======================================================== #
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def read(
|
|
39
|
+
path: Path,
|
|
40
|
+
) -> JSONList:
|
|
41
|
+
"""
|
|
42
|
+
Read TXT content from ``path``.
|
|
43
|
+
|
|
44
|
+
Parameters
|
|
45
|
+
----------
|
|
46
|
+
path : Path
|
|
47
|
+
Path to the TXT file on disk.
|
|
48
|
+
|
|
49
|
+
Returns
|
|
50
|
+
-------
|
|
51
|
+
JSONList
|
|
52
|
+
The list of dictionaries read from the TXT file.
|
|
53
|
+
"""
|
|
54
|
+
rows: JSONList = []
|
|
55
|
+
with path.open('r', encoding='utf-8') as handle:
|
|
56
|
+
for line in handle:
|
|
57
|
+
text = line.rstrip('\n')
|
|
58
|
+
if text == '':
|
|
59
|
+
continue
|
|
60
|
+
rows.append({'text': text})
|
|
61
|
+
return rows
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def write(
|
|
65
|
+
path: Path,
|
|
66
|
+
data: JSONData,
|
|
67
|
+
) -> int:
|
|
68
|
+
"""
|
|
69
|
+
Write ``data`` to TXT at ``path`` and return record count.
|
|
70
|
+
|
|
71
|
+
Parameters
|
|
72
|
+
----------
|
|
73
|
+
path : Path
|
|
74
|
+
Path to the TXT file on disk.
|
|
75
|
+
data : JSONData
|
|
76
|
+
Data to write. Expects ``{'text': '...'} `` or a list of those.
|
|
77
|
+
|
|
78
|
+
Returns
|
|
79
|
+
-------
|
|
80
|
+
int
|
|
81
|
+
Number of records written.
|
|
82
|
+
|
|
83
|
+
Raises
|
|
84
|
+
------
|
|
85
|
+
TypeError
|
|
86
|
+
If any item in ``data`` is not a dictionary or if any dictionary
|
|
87
|
+
does not contain a ``'text'`` key.
|
|
88
|
+
"""
|
|
89
|
+
rows = normalize_records(data, 'TXT')
|
|
90
|
+
|
|
91
|
+
if not rows:
|
|
92
|
+
return 0
|
|
93
|
+
|
|
94
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
95
|
+
with path.open('w', encoding='utf-8') as handle:
|
|
96
|
+
for row in rows:
|
|
97
|
+
if 'text' not in row:
|
|
98
|
+
raise TypeError('TXT payloads must include a "text" key')
|
|
99
|
+
handle.write(str(row['text']))
|
|
100
|
+
handle.write('\n')
|
|
101
|
+
|
|
102
|
+
return count_records(rows)
|
etlplus/file/vm.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"""
|
|
2
|
+
:mod:`etlplus.file.vm` module.
|
|
3
|
+
|
|
4
|
+
Helpers for reading/writing Apache Velocity (VM) template files.
|
|
5
|
+
|
|
6
|
+
Notes
|
|
7
|
+
-----
|
|
8
|
+
- A VM file is a text file used for generating HTML or other text formats
|
|
9
|
+
by combining templates with data.
|
|
10
|
+
- Common cases:
|
|
11
|
+
- HTML templates.
|
|
12
|
+
- Email templates.
|
|
13
|
+
- Configuration files.
|
|
14
|
+
- Rule of thumb:
|
|
15
|
+
- If you need to work with Apache Velocity template files, use this module
|
|
16
|
+
for reading and writing.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
from __future__ import annotations
|
|
20
|
+
|
|
21
|
+
from pathlib import Path
|
|
22
|
+
|
|
23
|
+
from ..types import JSONData
|
|
24
|
+
from ..types import JSONList
|
|
25
|
+
from . import stub
|
|
26
|
+
|
|
27
|
+
# SECTION: EXPORTS ========================================================== #
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
__all__ = [
|
|
31
|
+
'read',
|
|
32
|
+
'write',
|
|
33
|
+
]
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
# SECTION: FUNCTIONS ======================================================== #
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def read(
|
|
40
|
+
path: Path,
|
|
41
|
+
) -> JSONList:
|
|
42
|
+
"""
|
|
43
|
+
Read VM content from ``path``.
|
|
44
|
+
|
|
45
|
+
Parameters
|
|
46
|
+
----------
|
|
47
|
+
path : Path
|
|
48
|
+
Path to the VM file on disk.
|
|
49
|
+
|
|
50
|
+
Returns
|
|
51
|
+
-------
|
|
52
|
+
JSONList
|
|
53
|
+
The list of dictionaries read from the VM file.
|
|
54
|
+
"""
|
|
55
|
+
return stub.read(path, format_name='VM')
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def write(
|
|
59
|
+
path: Path,
|
|
60
|
+
data: JSONData,
|
|
61
|
+
) -> int:
|
|
62
|
+
"""
|
|
63
|
+
Write ``data`` to VM file at ``path`` and return record count.
|
|
64
|
+
|
|
65
|
+
Parameters
|
|
66
|
+
----------
|
|
67
|
+
path : Path
|
|
68
|
+
Path to the VM file on disk.
|
|
69
|
+
data : JSONData
|
|
70
|
+
Data to write as VM file. Should be a list of dictionaries or a single
|
|
71
|
+
dictionary.
|
|
72
|
+
|
|
73
|
+
Returns
|
|
74
|
+
-------
|
|
75
|
+
int
|
|
76
|
+
The number of rows written to the VM file.
|
|
77
|
+
"""
|
|
78
|
+
return stub.write(path, data, format_name='VM')
|
etlplus/file/wks.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"""
|
|
2
|
+
:mod:`etlplus.file.wks` module.
|
|
3
|
+
|
|
4
|
+
Helpers for reading/writing Lotus 1-2-3 (WKS) spreadsheet files.
|
|
5
|
+
|
|
6
|
+
Notes
|
|
7
|
+
-----
|
|
8
|
+
- A WKS file is a spreadsheet file created using the Lotus 1-2-3 format.
|
|
9
|
+
- Common cases:
|
|
10
|
+
- Reading data from legacy Lotus 1-2-3 spreadsheets.
|
|
11
|
+
- Writing data to Lotus 1-2-3 format for compatibility.
|
|
12
|
+
- Converting WKS files to more modern formats.
|
|
13
|
+
- Rule of thumb:
|
|
14
|
+
- If you need to work with Lotus 1-2-3 spreadsheet files, use this module
|
|
15
|
+
for reading and writing.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
from __future__ import annotations
|
|
19
|
+
|
|
20
|
+
from pathlib import Path
|
|
21
|
+
|
|
22
|
+
from ..types import JSONData
|
|
23
|
+
from ..types import JSONList
|
|
24
|
+
from . import stub
|
|
25
|
+
|
|
26
|
+
# SECTION: EXPORTS ========================================================== #
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
__all__ = [
|
|
30
|
+
'read',
|
|
31
|
+
'write',
|
|
32
|
+
]
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
# SECTION: FUNCTIONS ======================================================== #
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def read(
|
|
39
|
+
path: Path,
|
|
40
|
+
) -> JSONList:
|
|
41
|
+
"""
|
|
42
|
+
Read WKS content from ``path``.
|
|
43
|
+
|
|
44
|
+
Parameters
|
|
45
|
+
----------
|
|
46
|
+
path : Path
|
|
47
|
+
Path to the WKS file on disk.
|
|
48
|
+
|
|
49
|
+
Returns
|
|
50
|
+
-------
|
|
51
|
+
JSONList
|
|
52
|
+
The list of dictionaries read from the WKS file.
|
|
53
|
+
"""
|
|
54
|
+
return stub.read(path, format_name='WKS')
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def write(
|
|
58
|
+
path: Path,
|
|
59
|
+
data: JSONData,
|
|
60
|
+
) -> int:
|
|
61
|
+
"""
|
|
62
|
+
Write ``data`` to WKS file at ``path`` and return record count.
|
|
63
|
+
|
|
64
|
+
Parameters
|
|
65
|
+
----------
|
|
66
|
+
path : Path
|
|
67
|
+
Path to the WKS file on disk.
|
|
68
|
+
data : JSONData
|
|
69
|
+
Data to write as WKS file. Should be a list of dictionaries or a
|
|
70
|
+
single dictionary.
|
|
71
|
+
|
|
72
|
+
Returns
|
|
73
|
+
-------
|
|
74
|
+
int
|
|
75
|
+
The number of rows written to the WKS file.
|
|
76
|
+
"""
|
|
77
|
+
return stub.write(path, data, format_name='WKS')
|
etlplus/file/xls.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"""
|
|
2
|
+
:mod:`etlplus.file.xls` module.
|
|
3
|
+
|
|
4
|
+
Helpers for reading/writing Excel XLS files.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
from typing import cast
|
|
11
|
+
|
|
12
|
+
from ..types import JSONData
|
|
13
|
+
from ..types import JSONList
|
|
14
|
+
from ._imports import get_pandas
|
|
15
|
+
|
|
16
|
+
# SECTION: EXPORTS ========================================================== #
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
__all__ = [
|
|
20
|
+
'read',
|
|
21
|
+
'write',
|
|
22
|
+
]
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
# SECTION: FUNCTIONS ======================================================== #
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def read(
|
|
29
|
+
path: Path,
|
|
30
|
+
) -> JSONList:
|
|
31
|
+
"""
|
|
32
|
+
Read XLS content from ``path``.
|
|
33
|
+
|
|
34
|
+
Parameters
|
|
35
|
+
----------
|
|
36
|
+
path : Path
|
|
37
|
+
Path to the XLS file on disk.
|
|
38
|
+
|
|
39
|
+
Returns
|
|
40
|
+
-------
|
|
41
|
+
JSONList
|
|
42
|
+
The list of dictionaries read from the XLS file.
|
|
43
|
+
|
|
44
|
+
Raises
|
|
45
|
+
------
|
|
46
|
+
ImportError
|
|
47
|
+
If the optional dependency "xlrd" is not installed.
|
|
48
|
+
"""
|
|
49
|
+
pandas = get_pandas('XLS')
|
|
50
|
+
try:
|
|
51
|
+
frame = pandas.read_excel(path, engine='xlrd')
|
|
52
|
+
except ImportError as e: # pragma: no cover
|
|
53
|
+
raise ImportError(
|
|
54
|
+
'XLS support requires optional dependency "xlrd".\n'
|
|
55
|
+
'Install with: pip install xlrd',
|
|
56
|
+
) from e
|
|
57
|
+
return cast(JSONList, frame.to_dict(orient='records'))
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def write(
|
|
61
|
+
path: Path,
|
|
62
|
+
data: JSONData,
|
|
63
|
+
) -> int:
|
|
64
|
+
"""
|
|
65
|
+
Write ``data`` to XLS at ``path`` and return record count.
|
|
66
|
+
|
|
67
|
+
Notes
|
|
68
|
+
-----
|
|
69
|
+
XLS writing is not supported by pandas 2.x. Use XLSX for writes.
|
|
70
|
+
|
|
71
|
+
Parameters
|
|
72
|
+
----------
|
|
73
|
+
path : Path
|
|
74
|
+
Path to the XLS file on disk.
|
|
75
|
+
data : JSONData
|
|
76
|
+
Data to write.
|
|
77
|
+
|
|
78
|
+
Returns
|
|
79
|
+
-------
|
|
80
|
+
int
|
|
81
|
+
Number of records written.
|
|
82
|
+
|
|
83
|
+
Raises
|
|
84
|
+
------
|
|
85
|
+
RuntimeError
|
|
86
|
+
If XLS writing is attempted.
|
|
87
|
+
"""
|
|
88
|
+
raise RuntimeError('XLS write is not supported; use XLSX instead')
|
etlplus/file/xlsm.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"""
|
|
2
|
+
:mod:`etlplus.file.xlsm` module.
|
|
3
|
+
|
|
4
|
+
Helpers for reading/writing Microsoft Excel Macro-Enabled (XLSM) spreadsheet
|
|
5
|
+
files.
|
|
6
|
+
|
|
7
|
+
Notes
|
|
8
|
+
-----
|
|
9
|
+
- An XLSM file is a spreadsheet file created using the Microsoft Excel Macro-
|
|
10
|
+
Enabled (Open XML) format.
|
|
11
|
+
- Common cases:
|
|
12
|
+
- Reading data from Excel Macro-Enabled spreadsheets.
|
|
13
|
+
- Writing data to Excel Macro-Enabled format for compatibility.
|
|
14
|
+
- Converting XLSM files to more modern formats.
|
|
15
|
+
- Rule of thumb:
|
|
16
|
+
- If you need to work with Excel Macro-Enabled spreadsheet files, use this
|
|
17
|
+
module for reading and writing.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
from __future__ import annotations
|
|
21
|
+
|
|
22
|
+
from pathlib import Path
|
|
23
|
+
|
|
24
|
+
from ..types import JSONData
|
|
25
|
+
from ..types import JSONList
|
|
26
|
+
from . import stub
|
|
27
|
+
|
|
28
|
+
# SECTION: EXPORTS ========================================================== #
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
__all__ = [
|
|
32
|
+
'read',
|
|
33
|
+
'write',
|
|
34
|
+
]
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
# SECTION: FUNCTIONS ======================================================== #
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def read(
|
|
41
|
+
path: Path,
|
|
42
|
+
) -> JSONList:
|
|
43
|
+
"""
|
|
44
|
+
Read XLSM content from ``path``.
|
|
45
|
+
|
|
46
|
+
Parameters
|
|
47
|
+
----------
|
|
48
|
+
path : Path
|
|
49
|
+
Path to the XLSM file on disk.
|
|
50
|
+
|
|
51
|
+
Returns
|
|
52
|
+
-------
|
|
53
|
+
JSONList
|
|
54
|
+
The list of dictionaries read from the XLSM file.
|
|
55
|
+
"""
|
|
56
|
+
return stub.read(path, format_name='XLSM')
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def write(
|
|
60
|
+
path: Path,
|
|
61
|
+
data: JSONData,
|
|
62
|
+
) -> int:
|
|
63
|
+
"""
|
|
64
|
+
Write ``data`` to XLSM file at ``path`` and return record count.
|
|
65
|
+
|
|
66
|
+
Parameters
|
|
67
|
+
----------
|
|
68
|
+
path : Path
|
|
69
|
+
Path to the XLSM file on disk.
|
|
70
|
+
data : JSONData
|
|
71
|
+
Data to write as XLSM file. Should be a list of dictionaries or a
|
|
72
|
+
single dictionary.
|
|
73
|
+
|
|
74
|
+
Returns
|
|
75
|
+
-------
|
|
76
|
+
int
|
|
77
|
+
The number of rows written to the XLSM file.
|
|
78
|
+
"""
|
|
79
|
+
return stub.write(path, data, format_name='XLSM')
|
etlplus/file/xlsx.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"""
|
|
2
|
+
:mod:`etlplus.file.xlsx` module.
|
|
3
|
+
|
|
4
|
+
Helpers for reading/writing Excel XLSX files.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
from typing import cast
|
|
11
|
+
|
|
12
|
+
from ..types import JSONData
|
|
13
|
+
from ..types import JSONList
|
|
14
|
+
from ._imports import get_pandas
|
|
15
|
+
from ._io import normalize_records
|
|
16
|
+
|
|
17
|
+
# SECTION: EXPORTS ========================================================== #
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
__all__ = [
|
|
21
|
+
'read',
|
|
22
|
+
'write',
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
# SECTION: FUNCTIONS ======================================================== #
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def read(
|
|
30
|
+
path: Path,
|
|
31
|
+
) -> JSONList:
|
|
32
|
+
"""
|
|
33
|
+
Read XLSX content from ``path``.
|
|
34
|
+
|
|
35
|
+
Parameters
|
|
36
|
+
----------
|
|
37
|
+
path : Path
|
|
38
|
+
Path to the XLSX file on disk.
|
|
39
|
+
|
|
40
|
+
Returns
|
|
41
|
+
-------
|
|
42
|
+
JSONList
|
|
43
|
+
The list of dictionaries read from the XLSX file.
|
|
44
|
+
|
|
45
|
+
Raises
|
|
46
|
+
------
|
|
47
|
+
ImportError
|
|
48
|
+
If optional dependencies for XLSX support are missing.
|
|
49
|
+
"""
|
|
50
|
+
pandas = get_pandas('XLSX')
|
|
51
|
+
try:
|
|
52
|
+
frame = pandas.read_excel(path)
|
|
53
|
+
except ImportError as e: # pragma: no cover
|
|
54
|
+
raise ImportError(
|
|
55
|
+
'XLSX support requires optional dependency "openpyxl".\n'
|
|
56
|
+
'Install with: pip install openpyxl',
|
|
57
|
+
) from e
|
|
58
|
+
return cast(JSONList, frame.to_dict(orient='records'))
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def write(
|
|
62
|
+
path: Path,
|
|
63
|
+
data: JSONData,
|
|
64
|
+
) -> int:
|
|
65
|
+
"""
|
|
66
|
+
Write ``data`` to XLSX at ``path`` and return record count.
|
|
67
|
+
|
|
68
|
+
Parameters
|
|
69
|
+
----------
|
|
70
|
+
path : Path
|
|
71
|
+
Path to the XLSX file on disk.
|
|
72
|
+
data : JSONData
|
|
73
|
+
Data to write.
|
|
74
|
+
|
|
75
|
+
Returns
|
|
76
|
+
-------
|
|
77
|
+
int
|
|
78
|
+
Number of records written.
|
|
79
|
+
|
|
80
|
+
Raises
|
|
81
|
+
------
|
|
82
|
+
ImportError
|
|
83
|
+
If optional dependencies for XLSX support are missing.
|
|
84
|
+
"""
|
|
85
|
+
records = normalize_records(data, 'XLSX')
|
|
86
|
+
if not records:
|
|
87
|
+
return 0
|
|
88
|
+
|
|
89
|
+
pandas = get_pandas('XLSX')
|
|
90
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
91
|
+
frame = pandas.DataFrame.from_records(records)
|
|
92
|
+
try:
|
|
93
|
+
frame.to_excel(path, index=False)
|
|
94
|
+
except ImportError as e: # pragma: no cover
|
|
95
|
+
raise ImportError(
|
|
96
|
+
'XLSX support requires optional dependency "openpyxl".\n'
|
|
97
|
+
'Install with: pip install openpyxl',
|
|
98
|
+
) from e
|
|
99
|
+
return len(records)
|
etlplus/file/xml.py
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
"""
|
|
2
|
+
:mod:`etlplus.file.xml` module.
|
|
3
|
+
|
|
4
|
+
Helpers for reading/writing Extensible Markup Language (XML) files.
|
|
5
|
+
|
|
6
|
+
Notes
|
|
7
|
+
-----
|
|
8
|
+
- An XML file is a markup language file that uses tags to define elements.
|
|
9
|
+
- Common cases:
|
|
10
|
+
- Configuration files.
|
|
11
|
+
- Data interchange between systems.
|
|
12
|
+
- Document formatting.
|
|
13
|
+
- Rule of thumb:
|
|
14
|
+
- If the file follows the XML specification, use this module for
|
|
15
|
+
reading and writing.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
from __future__ import annotations
|
|
19
|
+
|
|
20
|
+
import xml.etree.ElementTree as ET
|
|
21
|
+
from pathlib import Path
|
|
22
|
+
from typing import Any
|
|
23
|
+
|
|
24
|
+
from ..types import JSONData
|
|
25
|
+
from ..types import JSONDict
|
|
26
|
+
from ..utils import count_records
|
|
27
|
+
|
|
28
|
+
# SECTION: EXPORTS ========================================================== #
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
__all__ = [
|
|
32
|
+
'read',
|
|
33
|
+
'write',
|
|
34
|
+
]
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
# SECTION: CONSTANTS ======================================================== #
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
DEFAULT_XML_ROOT = 'root'
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
# SECTION: INTERNAL FUNCTIONS =============================================== #
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def _dict_to_element(
|
|
47
|
+
name: str,
|
|
48
|
+
payload: Any,
|
|
49
|
+
) -> ET.Element:
|
|
50
|
+
"""
|
|
51
|
+
Convert a dictionary-like payload into an XML element.
|
|
52
|
+
|
|
53
|
+
Parameters
|
|
54
|
+
----------
|
|
55
|
+
name : str
|
|
56
|
+
Name of the XML element.
|
|
57
|
+
payload : Any
|
|
58
|
+
The data to include in the XML element.
|
|
59
|
+
|
|
60
|
+
Returns
|
|
61
|
+
-------
|
|
62
|
+
ET.Element
|
|
63
|
+
The constructed XML element.
|
|
64
|
+
"""
|
|
65
|
+
element = ET.Element(name)
|
|
66
|
+
|
|
67
|
+
if isinstance(payload, dict):
|
|
68
|
+
text = payload.get('text')
|
|
69
|
+
if text is not None:
|
|
70
|
+
element.text = str(text)
|
|
71
|
+
|
|
72
|
+
for key, value in payload.items():
|
|
73
|
+
if key == 'text':
|
|
74
|
+
continue
|
|
75
|
+
if key.startswith('@'):
|
|
76
|
+
element.set(key[1:], str(value))
|
|
77
|
+
continue
|
|
78
|
+
if isinstance(value, list):
|
|
79
|
+
for item in value:
|
|
80
|
+
element.append(_dict_to_element(key, item))
|
|
81
|
+
else:
|
|
82
|
+
element.append(_dict_to_element(key, value))
|
|
83
|
+
elif isinstance(payload, list):
|
|
84
|
+
for item in payload:
|
|
85
|
+
element.append(_dict_to_element('item', item))
|
|
86
|
+
elif payload is not None:
|
|
87
|
+
element.text = str(payload)
|
|
88
|
+
|
|
89
|
+
return element
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def _element_to_dict(
|
|
93
|
+
element: ET.Element,
|
|
94
|
+
) -> JSONDict:
|
|
95
|
+
"""
|
|
96
|
+
Convert an XML element into a nested dictionary.
|
|
97
|
+
|
|
98
|
+
Parameters
|
|
99
|
+
----------
|
|
100
|
+
element : ET.Element
|
|
101
|
+
XML element to convert.
|
|
102
|
+
|
|
103
|
+
Returns
|
|
104
|
+
-------
|
|
105
|
+
JSONDict
|
|
106
|
+
Nested dictionary representation of the XML element.
|
|
107
|
+
"""
|
|
108
|
+
result: JSONDict = {}
|
|
109
|
+
text = (element.text or '').strip()
|
|
110
|
+
if text:
|
|
111
|
+
result['text'] = text
|
|
112
|
+
|
|
113
|
+
for child in element:
|
|
114
|
+
child_data = _element_to_dict(child)
|
|
115
|
+
tag = child.tag
|
|
116
|
+
if tag in result:
|
|
117
|
+
existing = result[tag]
|
|
118
|
+
if isinstance(existing, list):
|
|
119
|
+
existing.append(child_data)
|
|
120
|
+
else:
|
|
121
|
+
result[tag] = [existing, child_data]
|
|
122
|
+
else:
|
|
123
|
+
result[tag] = child_data
|
|
124
|
+
|
|
125
|
+
for key, value in element.attrib.items():
|
|
126
|
+
if key in result:
|
|
127
|
+
result[f'@{key}'] = value
|
|
128
|
+
else:
|
|
129
|
+
result[key] = value
|
|
130
|
+
return result
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
# SECTION: FUNCTIONS ======================================================== #
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def read(
|
|
137
|
+
path: Path,
|
|
138
|
+
) -> JSONDict:
|
|
139
|
+
"""
|
|
140
|
+
Read XML content from ``path``.
|
|
141
|
+
|
|
142
|
+
Parameters
|
|
143
|
+
----------
|
|
144
|
+
path : Path
|
|
145
|
+
Path to the XML file on disk.
|
|
146
|
+
|
|
147
|
+
Returns
|
|
148
|
+
-------
|
|
149
|
+
JSONDict
|
|
150
|
+
Nested dictionary representation of the XML file.
|
|
151
|
+
"""
|
|
152
|
+
tree = ET.parse(path)
|
|
153
|
+
root = tree.getroot()
|
|
154
|
+
|
|
155
|
+
return {root.tag: _element_to_dict(root)}
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
def write(path: Path, data: JSONData, *, root_tag: str) -> int:
|
|
159
|
+
"""
|
|
160
|
+
Write ``data`` to XML at ``path`` and return record count.
|
|
161
|
+
|
|
162
|
+
Parameters
|
|
163
|
+
----------
|
|
164
|
+
path : Path
|
|
165
|
+
Path to the XML file on disk.
|
|
166
|
+
data : JSONData
|
|
167
|
+
Data to write as XML.
|
|
168
|
+
root_tag : str
|
|
169
|
+
Root tag name to use when writing XML files.
|
|
170
|
+
|
|
171
|
+
Returns
|
|
172
|
+
-------
|
|
173
|
+
int
|
|
174
|
+
The number of records written to the XML file.
|
|
175
|
+
"""
|
|
176
|
+
if isinstance(data, dict) and len(data) == 1:
|
|
177
|
+
root_name, payload = next(iter(data.items()))
|
|
178
|
+
root_element = _dict_to_element(str(root_name), payload)
|
|
179
|
+
else:
|
|
180
|
+
root_element = _dict_to_element(root_tag, data)
|
|
181
|
+
|
|
182
|
+
tree = ET.ElementTree(root_element)
|
|
183
|
+
tree.write(path, encoding='utf-8', xml_declaration=True)
|
|
184
|
+
|
|
185
|
+
return count_records(data)
|