Skip to content

Commit

Permalink
✨ v1.5.1 (#103) Added --params to edit pipes and register pipes
Browse files Browse the repository at this point in the history
…, and enforce chunksize limit in `SQLConnector.to_sql()`.
  • Loading branch information
bmeares authored Jan 11, 2023
1 parent 354c172 commit f0ede74
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 18 deletions.
54 changes: 39 additions & 15 deletions meerschaum/actions/edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"""

from __future__ import annotations
from meerschaum.utils.typing import List, Any, SuccessTuple, Optional
from meerschaum.utils.typing import List, Any, SuccessTuple, Optional, Dict

def edit(
action: Optional[List[str]] = None,
Expand Down Expand Up @@ -90,6 +90,10 @@ def _complete_edit_config(action: Optional[List[str]] = None, **kw : Any) -> Lis

def _edit_pipes(
action: Optional[List[str]] = None,
params: Optional[Dict[str, Any]] = None,
yes: bool = False,
force: bool = False,
noask: bool = False,
debug: bool = False,
**kw: Any
) -> SuccessTuple:
Expand All @@ -110,6 +114,9 @@ def _edit_pipes(
from meerschaum import get_pipes
from meerschaum.utils.prompt import prompt
from meerschaum.utils.misc import print_options
from meerschaum.utils.warnings import info
from meerschaum.utils.formatting import pprint
from meerschaum.config._patch import apply_patch_to_config

if action is None:
action = []
Expand All @@ -124,26 +131,43 @@ def _edit_pipes(

if len(pipes) > 1:
try:
prompt(
f"Press [Enter] to begin editing the above {len(pipes)} pipe" +
("s" if len(pipes) != 1 else "") +
" or [CTRL-C] to cancel:", icon=False
)
if not (yes or force or noask):
prompt(
f"Press [Enter] to begin editing the above {len(pipes)} pipe"
+ ("s" if len(pipes) != 1 else "")
+ " or [CTRL-C] to cancel:",
icon = False,
)
except KeyboardInterrupt:
return False, f"No pipes changed."

for p in pipes:
interactive = (not bool(params))
success, msg = True, ""
for pipe in pipes:
try:
text = prompt(f"Press [Enter] to edit {p} or [CTRL-C] to skip:", icon=False)
if text == 'pass':
continue
if not (yes or force or noask):
text = prompt(f"Press [Enter] to edit {pipe} or [CTRL-C] to skip:", icon=False)
if text == 'pass':
continue
except KeyboardInterrupt:
continue
if edit_definition:
p.edit_definition(debug=debug, **kw)
else:
p.edit(interactive=True, debug=debug, **kw)
return (True, "Success")

if params:
info(f"Will patch the following parameters into {pipe}:")
pprint(params)
pipe.parameters = apply_patch_to_config(pipe.parameters, params)

edit_success, edit_msg = (
pipe.edit_definition(debug=debug, **kw)
if edit_definition
else pipe.edit(interactive=interactive, debug=debug, **kw)
)
success = success and edit_success
if not edit_success:
msg += f"\n{pipe}: {edit_msg}"

msg = "Success" if success else msg[1:]
return success, msg


def _edit_definition(
Expand Down
8 changes: 8 additions & 0 deletions meerschaum/actions/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def _register_pipes(
from meerschaum.utils.debug import dprint
from meerschaum.utils.warnings import warn, info
from meerschaum.utils.misc import items_str
from meerschaum.config._patch import apply_patch_to_config

if connector_keys is None:
connector_keys = []
Expand Down Expand Up @@ -100,6 +101,13 @@ def _register_pipes(
**kw
)

if params:
for pipe in pipes:
pipe._attributes['parameters'] = apply_patch_to_config(
params,
pipe._attributes.get('parameters', {})
)

success, message = True, "Success"
failed_message = ""
failed_pipes = []
Expand Down
4 changes: 4 additions & 0 deletions meerschaum/config/_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import sys
import copy
from meerschaum.utils.typing import Dict, Any
from meerschaum.utils.warnings import warn

def apply_patch_to_config(
config: Dict[str, Any],
Expand All @@ -22,6 +23,9 @@ def apply_patch_to_config(
def update_dict(base, patch):
if base is None:
return {}
if not isinstance(base, dict):
warn(f"Overwriting the value {base} with a dictionary:\n{patch}")
base = {}
for key, value in patch.items():
if isinstance(value, dict):
base[key] = update_dict(base.get(key, {}), value)
Expand Down
2 changes: 1 addition & 1 deletion meerschaum/config/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
Specify the Meerschaum release version.
"""

__version__ = "1.5.0"
__version__ = "1.5.1"
16 changes: 14 additions & 2 deletions meerschaum/connectors/sql/_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ def to_sql(
Either a `bool` or a `SuccessTuple` (depends on `as_tuple`).
"""
import time
from meerschaum.utils.warnings import error
from meerschaum.utils.warnings import error, warn
import warnings
if name is None:
error(f"Name must not be `None` to insert data into {self}.")
Expand All @@ -535,7 +535,19 @@ def to_sql(
### Should resolve to 'multi' or `None`.
method = flavor_configs.get(self.flavor, {}).get('to_sql', {}).get('method', 'multi')
stats['method'] = method.__name__ if hasattr(method, '__name__') else str(method)
chunksize = chunksize if chunksize != -1 else self.sys_config.get('chunksize', None)

default_chunksize = self.sys_config.get('chunksize', None)
chunksize = chunksize if chunksize != -1 else default_chunksize
if chunksize is not None and self.flavor in _max_chunks_flavors:
if chunksize > _max_chunks_flavors[self.flavor]:
if chunksize != default_chunksize:
warn(
f"The specified chunksize of {chunksize} exceeds the maximum of "
+ f"{_max_chunks_flavors[self.flavor]} for flavor '{self.flavor}'.\n"
+ f" Falling back to a chunksize of {_max_chunks_flavors[self.flavor]}.",
stacklevel = 3,
)
chunksize = _max_chunks_flavors[self.flavor]
stats['chunksize'] = chunksize

success, msg = False, "Default to_sql message"
Expand Down

0 comments on commit f0ede74

Please sign in to comment.