Skip to content

Commit f4388e2

Browse files
refactor
1 parent 0fcf3f5 commit f4388e2

3 files changed

Lines changed: 127 additions & 118 deletions

File tree

cmem_plugin_splitfile/plugin_splitfile.py

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from pathlib import Path
77
from shutil import move
88
from tempfile import TemporaryDirectory
9-
from warnings import simplefilter
109

1110
import requests
1211
from cmem.cmempy.api import config, get_access_token
@@ -29,12 +28,23 @@
2928
from cmem_plugin_base.dataintegration.utils import setup_cmempy_user_access
3029
from filesplit.split import Split
3130
from pathvalidate import is_valid_filepath
32-
from urllib3.exceptions import InsecureRequestWarning
3331

3432
from cmem_plugin_splitfile.doc import SPLITFILE_DOC
3533
from cmem_plugin_splitfile.resource_parameter_type import ResourceParameterType
3634

37-
simplefilter("ignore", category=InsecureRequestWarning)
35+
DEFAULT_PROJECT_DIR = "/data/datalake"
36+
SIZE_UNIT_KB = "kb"
37+
SIZE_UNIT_MB = "mb"
38+
SIZE_UNIT_GB = "gb"
39+
SIZE_UNIT_LINES = "lines"
40+
SIZE_UNIT_PARAMETER_CHOICES = OrderedDict(
41+
{
42+
SIZE_UNIT_KB: "KB",
43+
SIZE_UNIT_MB: "MB",
44+
SIZE_UNIT_GB: "GB",
45+
SIZE_UNIT_LINES: "lines",
46+
}
47+
)
3848

3949

4050
@Plugin(
@@ -56,9 +66,7 @@
5666
description="The maximum size of the chunk files.",
5767
),
5868
PluginParameter(
59-
param_type=ChoiceParameterType(
60-
OrderedDict({"KB": "KB", "MB": "MB", "GB": "GB", "lines": "Lines"})
61-
),
69+
param_type=ChoiceParameterType(SIZE_UNIT_PARAMETER_CHOICES),
6270
name="size_unit",
6371
label="Size unit",
6472
description="""The unit of the size value: kilobyte (KB), megabyte (MB), gigabyte (GB),
@@ -96,7 +104,7 @@
96104
label="Internal projects directory",
97105
description="""The path to the internal projects directory. If "Use internal projects
98106
directory" is disabled, this parameter has no effect.""",
99-
default_value="/data/datalake",
107+
default_value=DEFAULT_PROJECT_DIR,
100108
advanced=True,
101109
),
102110
],
@@ -108,26 +116,28 @@ def __init__( # noqa: C901 PLR0913
108116
self,
109117
input_filename: str,
110118
chunk_size: float,
111-
size_unit: str = "MB",
119+
size_unit: str = SIZE_UNIT_MB,
112120
include_header: bool = False,
113121
delete_file: bool = False,
114122
use_directory: bool = False,
115-
projects_path: str = "/data/datalake",
123+
projects_path: str = DEFAULT_PROJECT_DIR,
116124
) -> None:
117125
errors = ""
118126
if not is_valid_filepath(input_filename):
119127
errors += 'Invalid filename for parameter "Input filename". '
120128

121129
self.lines = False
122-
match size_unit.lower():
123-
case "kb":
124-
chunk_size *= 1024
125-
case "mb":
126-
chunk_size *= 1048576
127-
case "gb":
128-
chunk_size *= 1073741824
129-
case "lines":
130-
self.lines = True
130+
size_unit = size_unit.lower()
131+
if size_unit == SIZE_UNIT_KB:
132+
chunk_size *= 1024
133+
elif size_unit == SIZE_UNIT_MB:
134+
chunk_size *= 1048576
135+
elif size_unit == SIZE_UNIT_GB:
136+
chunk_size *= 1073741824
137+
elif size_unit == SIZE_UNIT_LINES:
138+
self.lines = True
139+
else:
140+
errors += "Invalid size unit. "
131141

132142
if self.lines:
133143
if int(chunk_size) != chunk_size or chunk_size < 1:

0 commit comments

Comments
 (0)