Completed
Push — master ( 9ec4ff...84ac48 )
by P.R.
01:35
created

SqlLoaderWriter   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 7
c 3
b 0
f 0
dl 0
loc 81
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A register_handler() 0 10 1
A get_bulk_load_sql() 0 10 1
A __exit__() 0 2 1
B __init__() 0 29 1
A _write_field() 0 6 2
A __enter__() 0 2 1
1
"""
2
ETLT
3
4
Copyright 2016 Set Based IT Consultancy
5
6
Licence MIT
7
"""
8
import abc
9
10
from etlt.writer.Writer import Writer
11
12
13
class SqlLoaderWriter(Writer):
1 ignored issue
show
Unused Code introduced by
This abstract class does not seem to be used anywhere.
Loading history...
14
    """
15
    Abstract parent class for loading rows to a table in a database using a SQL statement for loading data from file.
16
    """
17
    handlers = {}
18
    """
19
    The handlers for writing objects as a field to a CSV file.
20
21
    :type dict[str,callable]:
22
    """
23
24
    # ------------------------------------------------------------------------------------------------------------------
25
    def __init__(self, filename, encoding='utf8'):
26
        """
27
        Object constructor.
28
29
        :param str filename: The destination file for the rows.
30
        :param str encoding: The encoding of the text of the destination file.
31
        """
32
        Writer.__init__(self)
33
34
        self._filename = filename
35
        """
36
        The name of the destination file.
37
38
        :type str:
39
        """
40
41
        self._encoding = encoding
42
        """
43
        The encoding of the text in the destination file.
44
45
        :type str:
46
        """
47
48
        self._file = None
49
        """
50
        The underling file object.
51
52
        :type T:
53
        """
54
55
    # ------------------------------------------------------------------------------------------------------------------
56
    def __enter__(self):
57
        self._file = open(self._filename, mode='wt', encoding=self._encoding)
58
59
    # ------------------------------------------------------------------------------------------------------------------
60
    def __exit__(self, exc_type, exc_value, traceback):
61
        self._file.close()
62
63
    # ------------------------------------------------------------------------------------------------------------------
64
    @abc.abstractmethod
65
    def get_bulk_load_sql(self, table_name):
66
        """
67
        Returns a SQL statement for bulk loading the data writen to the destination file into a table.
68
69
        :param str table_name: The name of the table.
70
71
        :rtype: str
72
        """
73
        raise NotImplementedError()
74
75
    # ------------------------------------------------------------------------------------------------------------------
76
    @staticmethod
77
    def register_handler(class_name, handler):
78
        """
79
        Registers a handler for writing instances of a class as a field to the destination file.
80
81
        :param str class_name: The name of the class.
82
        :param callable handler: The handler. This handler will be called with two arguments: the object which value
83
                                 must be writen to the destination file, the file handler.
84
        """
85
        SqlLoaderWriter.handlers[class_name] = handler
86
87
    # ------------------------------------------------------------------------------------------------------------------
88
    def _write_field(self, value):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
89
        class_name = str(value.__class__)
90
        if class_name not in self.handlers:
91
            raise ValueError('No handler has been registered for class: %s' % class_name)
92
        handler = self.handlers[class_name]
93
        handler(value, self._file)
94
95
# ----------------------------------------------------------------------------------------------------------------------
96