Пример #1
0
    def connect_kwargs(self) -> dict:
        """
        Returns effective kwargs to be passed to ``pyodbc.connect`` after merging between conn extra,
        ``connect_kwargs`` and hook init.

        Hook ``connect_kwargs`` precedes ``connect_kwargs`` from conn extra.

        String values for 'true' and 'false' are converted to bool type.

        If ``attrs_before`` provided, keys and values are converted to int, as required by pyodbc.
        """
        def clean_bool(val):  # pylint: disable=inconsistent-return-statements
            if hasattr(val, 'lower'):
                if val.lower() == 'true':
                    return True
                elif val.lower() == 'false':
                    return False
            else:
                return val

        conn_connect_kwargs = self.connection_extra_lower.get(
            'connect_kwargs', {})
        hook_connect_kwargs = self._connect_kwargs or {}
        merged_connect_kwargs = merge_dicts(conn_connect_kwargs,
                                            hook_connect_kwargs)

        if 'attrs_before' in merged_connect_kwargs:
            merged_connect_kwargs['attrs_before'] = {
                int(k): int(v)
                for k, v in merged_connect_kwargs['attrs_before'].items()
            }

        return {k: clean_bool(v) for k, v in merged_connect_kwargs.items()}
Пример #2
0
    def connect_kwargs(self) -> dict:
        """
        Returns effective kwargs to be passed to ``pyodbc.connect`` after merging between conn extra,
        ``connect_kwargs`` and hook init.

        Hook ``connect_kwargs`` precedes ``connect_kwargs`` from conn extra.

        String values for 'true' and 'false' are converted to bool type.

        If ``attrs_before`` provided, keys and values are converted to int, as required by pyodbc.
        """

        conn_connect_kwargs = self.connection_extra_lower.get(
            'connect_kwargs', {})
        hook_connect_kwargs = self._connect_kwargs or {}
        merged_connect_kwargs = merge_dicts(conn_connect_kwargs,
                                            hook_connect_kwargs)

        if 'attrs_before' in merged_connect_kwargs:
            merged_connect_kwargs['attrs_before'] = {
                int(k): int(v)
                for k, v in merged_connect_kwargs['attrs_before'].items()
            }

        return {k: v for k, v in merged_connect_kwargs.items()}
Пример #3
0
 def test_merge_dicts_recursive_right_only(self):
     """
     Test merge_dicts with recursive when dict1 doesn't have any nested dict
     """
     dict1 = {'a': 1}
     dict2 = {'a': 1, 'r': {'c': 3, 'b': 0}}
     merged = merge_dicts(dict1, dict2)
     self.assertDictEqual(merged, {'a': 1, 'r': {'b': 0, 'c': 3}})
Пример #4
0
 def test_merge_dicts_recursive_overlap_l1(self):
     """
     Test merge_dicts with recursive dict; one level of nesting
     """
     dict1 = {'a': 1, 'r': {'a': 1, 'b': 2}}
     dict2 = {'a': 1, 'r': {'c': 3, 'b': 0}}
     merged = merge_dicts(dict1, dict2)
     self.assertDictEqual(merged, {'a': 1, 'r': {'a': 1, 'b': 0, 'c': 3}})
Пример #5
0
 def test_merge_dicts(self):
     """
     Test _merge method from JSONFormatter
     """
     dict1 = {'a': 1, 'b': 2, 'c': 3}
     dict2 = {'a': 1, 'b': 3, 'd': 42}
     merged = merge_dicts(dict1, dict2)
     self.assertDictEqual(merged, {'a': 1, 'b': 3, 'c': 3, 'd': 42})
Пример #6
0
 def format(self, record):
     super().format(record)
     record_dict = {
         label: getattr(record, label, None)
         for label in self.json_fields
     }
     merged_record = merge_dicts(record_dict, self.extras)
     return json.dumps(merged_record)
Пример #7
0
    def test_merge_dicts_recursive_overlap_l2(self):
        """
        Test merge_dicts with recursive dict; two levels of nesting
        """

        dict1 = {'a': 1, 'r': {'a': 1, 'b': {'a': 1}}}
        dict2 = {'a': 1, 'r': {'c': 3, 'b': {'b': 1}}}
        merged = merge_dicts(dict1, dict2)
        assert merged == {'a': 1, 'r': {'a': 1, 'b': {'a': 1, 'b': 1}, 'c': 3}}
Пример #8
0
 def format(self, record):
     super().format(record)
     record_dict = {label: getattr(record, label, None) for label in self.json_fields}
     if "message" in self.json_fields:
         msg = record_dict["message"]
         if record.exc_text:
             if msg[-1:] != "\n":
                 msg = msg + "\n"
             msg = msg + record.exc_text
         if record.stack_info:
             if msg[-1:] != "\n":
                 msg = msg + "\n"
             msg = msg + self.formatStack(record.stack_info)
         record_dict["message"] = msg
     merged_record = merge_dicts(record_dict, self.extras)
     return json.dumps(merged_record)