Switch to unified view

a b/ReadersWriters/_PickleSerialised.py
1
#!/usr/bin/env python
2
# -*- coding: UTF-8 -*-
3
#
4
# Copyright 2017 University of Westminster. All Rights Reserved.
5
#
6
# Licensed under the Apache License, Version 2.0 (the "License");
7
# you may not use this file except in compliance with the License.
8
# You may obtain a copy of the License at
9
#
10
#     http://www.apache.org/licenses/LICENSE-2.0
11
#
12
# Unless required by applicable law or agreed to in writing, software
13
# distributed under the License is distributed on an "AS IS" BASIS,
14
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
# See the License for the specific language governing permissions and
16
# limitations under the License.
17
# ==============================================================================
18
"""It is an interface for serialising python objects and optionally compressing them.
19
"""
20
21
from typing import TypeVar, Any
22
import pickle
23
import bz2
24
import sys
25
import os
26
import logging
27
from Configs.CONSTANTS import CONSTANTS
28
29
OsStatResult = TypeVar('stat_result')
30
31
__author__ = "Mohsen Mesgarpour"
32
__copyright__ = "Copyright 2016, https://github.com/mesgarpour"
33
__credits__ = ["Mohsen Mesgarpour"]
34
__license__ = "GPL"
35
__version__ = "1.1"
36
__maintainer__ = "Mohsen Mesgarpour"
37
__email__ = "mohsen.mesgarpour@gmail.com"
38
__status__ = "Release"
39
40
41
class PickleSerialised:
42
    def __init__(self):
43
        """Initialise the objects and constants."""
44
        self.__logger = logging.getLogger(CONSTANTS.app_name)
45
        self.__logger.debug(__name__)
46
        self.__path = None
47
48
    def set(self,
49
            path: str,
50
            title: str,
51
            ext: str):
52
        """Set the python serialiser configuration settings.
53
        :param path: the directory path of the serialised file.
54
        :param title: the title of the output file.
55
        :param ext: the extension of the output file.
56
        """
57
        self.__logger.debug("Set the pickle file.")
58
        self.__path = os.path.join(path, title + "." + ext)
59
        self.__logger.debug(self.__path)
60
61
    def exists(self) -> bool:
62
        """Check if the serialised object exists.
63
        :return: indicates if the file exists.
64
        """
65
        self.__logger.debug("Check if the pickle file exists.")
66
        return os.path.isfile(self.__path)
67
68
    def save(self,
69
             objects: Any):
70
        """Serialise the object (Pickle protocol=4), without compression.
71
        :param objects: the object to be saved.
72
        """
73
        self.__logger.debug("Save the Pickle file.")
74
        try:
75
            with open(self.__path, 'wb') as f:
76
                pickle.dump(objects, f, protocol=4)
77
        except (OSError, IOError) as e:
78
            self.__logger.error(__name__ + " - Can not open the file: \n" + self.__path + "\n")
79
            print(e)
80
            sys.exit()
81
        except Exception as e:
82
            self.__logger.error(__name__ + " - Unable to save data to: \n" + self.__path + "\n")
83
            print(e)
84
            sys.exit()
85
86
        file_stats = os.stat(self.__path)
87
        self.__logger.info("Pickle Size: " + str(file_stats.st_size) + ".")
88
89
    def save_bz2(self,
90
                 objects: Any):
91
        """Serialise the object (Pickle protocol=4), then compress (BZ2 compression).
92
        :param objects: the object to be saved.
93
        """
94
        self.__logger.debug("Save the Pickle file and compress.")
95
96
        try:
97
            with bz2.BZ2File(self.__path, 'wb') as f:
98
                pickle.dump(objects, f, protocol=4)
99
        except (OSError, IOError) as e:
100
            self.__logger.error(__name__ + " - Can not open the file: \n" + self.__path + "\n")
101
            print(e)
102
            sys.exit()
103
        except Exception as e:
104
            self.__logger.error(__name__ + " - Unable to save data to: \n" + self.__path + "\n")
105
            print(e)
106
            sys.exit()
107
108
        file_stats = os.stat(self.__path)
109
        self.__logger.info("Pickle Size: " + str(file_stats.st_size) + ".")
110
111
    def load(self) -> Any:
112
        """Load a serialised object, that was not compressed.
113
        :return: the loaded python object.
114
        """
115
        self.__logger.debug("Load a Pickle file.")
116
        try:
117
            with open(self.__path, 'rb') as f:
118
                objects = pickle.load(f)
119
        except (OSError, IOError) as e:
120
            self.__logger.error(__name__ + " - Can not open the file: \n" + self.__path + "\n")
121
            print(e)
122
            sys.exit()
123
        except Exception as e:
124
            self.__logger.error(__name__ + " - Unable to load data from: \n" + self.__path + "\n")
125
            print(e)
126
            sys.exit()
127
        return objects
128
129
    def load_bz2(self) -> Any:
130
        """Load a serialised object, that was compressed (BZ2 compression).
131
        :return: the loaded python object.
132
        """
133
        self.__logger.debug("Save a compressed Pickle file.")
134
        try:
135
            with bz2.BZ2File(self.__path, 'rb') as f:
136
                objects = pickle.load(f)
137
        except (OSError, IOError) as e:
138
            self.__logger.error(__name__ + " - Can not open the file: \n" + self.__path + "\n")
139
            print(e)
140
            sys.exit()
141
        except Exception as e:
142
            self.__logger.error(__name__ + " - Unable to load data from \n" + self.__path + "\n")
143
            print(e)
144
            sys.exit()
145
        return objects
146
147
    def size(self) -> OsStatResult:
148
        """Check the size of the saved file.
149
        :return: showing stat information of the file.
150
        """
151
        return os.stat(self.__path)