This will read the content of the file selected by user Returns: Content of file
(file, enc)
| 126 | |
| 127 | |
| 128 | def read_file_generator(file, enc): |
| 129 | """ |
| 130 | This will read the content of the file selected by user |
| 131 | |
| 132 | Returns: |
| 133 | Content of file |
| 134 | """ |
| 135 | try: |
| 136 | with codecs.open(file, 'r', encoding=enc) as fileObj: |
| 137 | while True: |
| 138 | # 4MB chunk (4 * 1024 * 1024 Bytes) |
| 139 | data = fileObj.read(4194304) |
| 140 | if not data: |
| 141 | break |
| 142 | yield data |
| 143 | except UnicodeDecodeError: |
| 144 | # This is the closest equivalent Python 3 offers to the permissive |
| 145 | # Python 2 text handling model. The latin-1 encoding in Python |
| 146 | # implements ISO_8859-1:1987 which maps all possible byte values |
| 147 | # to the first 256 Unicode code points, and thus ensures decoding |
| 148 | # errors will never occur regardless of the configured error and |
| 149 | # handles most of the Windows encodings |
| 150 | # handler. |
| 151 | # Ref: https://tinyurl.com/yvj4u7fw |
| 152 | with codecs.open(file, 'r', encoding='latin-1') as fileObj: |
| 153 | while True: |
| 154 | # 4MB chunk (4 * 1024 * 1024 Bytes) |
| 155 | data = fileObj.read(4194304) |
| 156 | if not data: |
| 157 | break |
| 158 | yield data |
| 159 | except Exception: |
| 160 | # As a last resort we will use the provided encoding and then |
| 161 | # ignore the decoding errors |
| 162 | with codecs.open(file, 'r', encoding=enc, errors='ignore') as fileObj: |
| 163 | while True: |
| 164 | # 4MB chunk (4 * 1024 * 1024 Bytes) |
| 165 | data = fileObj.read(4194304) |
| 166 | if not data: |
| 167 | break |
| 168 | yield data |
| 169 | |
| 170 | |
| 171 | class FileManagerModule(PgAdminModule): |