meerschaum.connectors.parse

Utility functions for parsing connector keys.

  1#! /usr/bin/env python
  2# -*- coding: utf-8 -*-
  3# vim:fenc=utf-8
  4
  5"""
  6Utility functions for parsing connector keys.
  7"""
  8
  9from __future__ import annotations
 10
 11import meerschaum as mrsm
 12from meerschaum.utils.typing import Any, Union, Optional, Dict, Tuple
 13
 14
 15def parse_connector_keys(
 16    keys: str,
 17    construct: bool = True,
 18    as_tuple: bool = False,
 19    **kw:  Any
 20) -> (
 21    Union[
 22        mrsm.connectors.Connector,
 23        Dict[str, Any],
 24        Tuple[
 25            Union[
 26                mrsm.connectors.Connector,
 27                Dict[str, Any],
 28                None,
 29            ],
 30            str
 31        ],
 32        None
 33    ]
 34):
 35    """
 36    Convenience function for parsing connector keys and returning Connector objects.
 37
 38    Parameters
 39    ----------
 40    keys: str
 41        Keys are split by a colon (`':'`) into type and label. If the label is omitted,
 42        (e.g. 'sql'), pass it along to `meerschaum.connectors.get_connector` to parse.
 43
 44    construct: bool, default True
 45        If True, return a Connector. Otherwise return the configuration dictionary for a Connector.
 46        **NOTE:** This may include passwords, so be careful.
 47
 48    as_tuple: bool, default False
 49        If True, return a tuple of (conn, keys). `conn` may be a dict or connector.
 50
 51    Returns
 52    -------
 53    A connector or dictionary of attributes. If `as_tuple`, also return the connector's keys.
 54
 55    """
 56    import copy
 57    from meerschaum.connectors import get_connector
 58    from meerschaum.config import get_config
 59    from meerschaum.config.static import STATIC_CONFIG
 60    from meerschaum.utils.warnings import error
 61
 62    ### `get_connector()` handles the logic for falling back to 'main',
 63    ### so don't make any decisions here.
 64    vals = str(keys).split(':')
 65    _type = vals[0]
 66    _label = vals[1] if len(vals) > 1 else STATIC_CONFIG['connectors']['default_label']
 67    _get_connector_kw = {'type': _type, 'label': _label}
 68    _get_connector_kw.update(kw)
 69
 70    if construct:
 71        conn = get_connector(**_get_connector_kw)
 72        if conn is None:
 73            error(f"Unable to parse connector keys '{keys}'", stack=False)
 74    else:
 75        connectors_config = get_config('meerschaum', 'connectors', patch=True)
 76        ### invalid type
 77        if vals[0] not in connectors_config:
 78            return None
 79        type_config = get_config('meerschaum', 'connectors', _type)
 80
 81        default_config = copy.deepcopy(type_config.get('default', {}))
 82        conn = type_config.get(_label, None)
 83        if default_config is not None and conn is not None:
 84            default_config.update(conn)
 85            conn = default_config
 86
 87    if as_tuple:
 88        return conn, _type + ':' + _label
 89    return conn
 90
 91
 92def parse_instance_keys(
 93    keys: Optional[str],
 94    construct: bool = True,
 95    as_tuple: bool = False,
 96    **kw
 97):
 98    """
 99    Parse the Meerschaum instance value into a Connector object.
100    """
101    from meerschaum.config import get_config
102
103    if keys is None:
104        keys = get_config('meerschaum', 'instance')
105    keys = str(keys)
106
107    return parse_connector_keys(keys, construct=construct, as_tuple=as_tuple, **kw)
108
109
110def parse_repo_keys(keys: Optional[str] = None, **kw):
111    """Parse the Meerschaum repository value into an APIConnector."""
112    from meerschaum.config import get_config
113    if keys is None:
114        keys = get_config('meerschaum', 'default_repository', patch=True)
115    keys = str(keys)
116    if ':' not in keys:
117        keys = 'api:' + keys
118
119    return parse_connector_keys(keys, **kw)
120
121
122def parse_executor_keys(keys: Optional[str] = None, **kw):
123    """Parse the executor keys into an APIConnector or string."""
124    from meerschaum.jobs import get_executor_keys_from_context
125    if keys is None:
126        keys = get_executor_keys_from_context()
127
128    if keys is None or keys == 'local':
129        return 'local'
130
131    keys = str(keys)
132    return parse_connector_keys(keys, **kw)
133
134
135def is_valid_connector_keys(
136    keys: str
137) -> bool:
138    """
139    Verify a connector_keys string references a valid connector.
140    """
141    try:
142        success = parse_connector_keys(keys, construct=False) is not None
143    except Exception:
144        success = False
145    return success
def parse_connector_keys( keys: str, construct: bool = True, as_tuple: bool = False, **kw: Any) -> Union[meerschaum.Connector, Dict[str, Any], Tuple[Union[meerschaum.Connector, Dict[str, Any], NoneType], str], NoneType]:
16def parse_connector_keys(
17    keys: str,
18    construct: bool = True,
19    as_tuple: bool = False,
20    **kw:  Any
21) -> (
22    Union[
23        mrsm.connectors.Connector,
24        Dict[str, Any],
25        Tuple[
26            Union[
27                mrsm.connectors.Connector,
28                Dict[str, Any],
29                None,
30            ],
31            str
32        ],
33        None
34    ]
35):
36    """
37    Convenience function for parsing connector keys and returning Connector objects.
38
39    Parameters
40    ----------
41    keys: str
42        Keys are split by a colon (`':'`) into type and label. If the label is omitted,
43        (e.g. 'sql'), pass it along to `meerschaum.connectors.get_connector` to parse.
44
45    construct: bool, default True
46        If True, return a Connector. Otherwise return the configuration dictionary for a Connector.
47        **NOTE:** This may include passwords, so be careful.
48
49    as_tuple: bool, default False
50        If True, return a tuple of (conn, keys). `conn` may be a dict or connector.
51
52    Returns
53    -------
54    A connector or dictionary of attributes. If `as_tuple`, also return the connector's keys.
55
56    """
57    import copy
58    from meerschaum.connectors import get_connector
59    from meerschaum.config import get_config
60    from meerschaum.config.static import STATIC_CONFIG
61    from meerschaum.utils.warnings import error
62
63    ### `get_connector()` handles the logic for falling back to 'main',
64    ### so don't make any decisions here.
65    vals = str(keys).split(':')
66    _type = vals[0]
67    _label = vals[1] if len(vals) > 1 else STATIC_CONFIG['connectors']['default_label']
68    _get_connector_kw = {'type': _type, 'label': _label}
69    _get_connector_kw.update(kw)
70
71    if construct:
72        conn = get_connector(**_get_connector_kw)
73        if conn is None:
74            error(f"Unable to parse connector keys '{keys}'", stack=False)
75    else:
76        connectors_config = get_config('meerschaum', 'connectors', patch=True)
77        ### invalid type
78        if vals[0] not in connectors_config:
79            return None
80        type_config = get_config('meerschaum', 'connectors', _type)
81
82        default_config = copy.deepcopy(type_config.get('default', {}))
83        conn = type_config.get(_label, None)
84        if default_config is not None and conn is not None:
85            default_config.update(conn)
86            conn = default_config
87
88    if as_tuple:
89        return conn, _type + ':' + _label
90    return conn

Convenience function for parsing connector keys and returning Connector objects.

Parameters
  • keys (str): Keys are split by a colon (':') into type and label. If the label is omitted, (e.g. 'sql'), pass it along to meerschaum.connectors.get_connector to parse.
  • construct (bool, default True): If True, return a Connector. Otherwise return the configuration dictionary for a Connector. NOTE: This may include passwords, so be careful.
  • as_tuple (bool, default False): If True, return a tuple of (conn, keys). conn may be a dict or connector.
Returns
  • A connector or dictionary of attributes. If as_tuple, also return the connector's keys.
def parse_instance_keys( keys: Optional[str], construct: bool = True, as_tuple: bool = False, **kw):
 93def parse_instance_keys(
 94    keys: Optional[str],
 95    construct: bool = True,
 96    as_tuple: bool = False,
 97    **kw
 98):
 99    """
100    Parse the Meerschaum instance value into a Connector object.
101    """
102    from meerschaum.config import get_config
103
104    if keys is None:
105        keys = get_config('meerschaum', 'instance')
106    keys = str(keys)
107
108    return parse_connector_keys(keys, construct=construct, as_tuple=as_tuple, **kw)

Parse the Meerschaum instance value into a Connector object.

def parse_repo_keys(keys: Optional[str] = None, **kw):
111def parse_repo_keys(keys: Optional[str] = None, **kw):
112    """Parse the Meerschaum repository value into an APIConnector."""
113    from meerschaum.config import get_config
114    if keys is None:
115        keys = get_config('meerschaum', 'default_repository', patch=True)
116    keys = str(keys)
117    if ':' not in keys:
118        keys = 'api:' + keys
119
120    return parse_connector_keys(keys, **kw)

Parse the Meerschaum repository value into an APIConnector.

def parse_executor_keys(keys: Optional[str] = None, **kw):
123def parse_executor_keys(keys: Optional[str] = None, **kw):
124    """Parse the executor keys into an APIConnector or string."""
125    from meerschaum.jobs import get_executor_keys_from_context
126    if keys is None:
127        keys = get_executor_keys_from_context()
128
129    if keys is None or keys == 'local':
130        return 'local'
131
132    keys = str(keys)
133    return parse_connector_keys(keys, **kw)

Parse the executor keys into an APIConnector or string.

def is_valid_connector_keys(keys: str) -> bool:
136def is_valid_connector_keys(
137    keys: str
138) -> bool:
139    """
140    Verify a connector_keys string references a valid connector.
141    """
142    try:
143        success = parse_connector_keys(keys, construct=False) is not None
144    except Exception:
145        success = False
146    return success

Verify a connector_keys string references a valid connector.