MCPcopy Create free account
hub / github.com/pgadmin-org/pgadmin4 / read_file_generator

Function read_file_generator

web/pgadmin/misc/file_manager/__init__.py:128–168  ·  view source on GitHub ↗

This will read the content of the file selected by user Returns: Content of file

(file, enc)

Source from the content-addressed store, hash-verified

126
127
128def 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
171class FileManagerModule(PgAdminModule):

Callers 2

runTestMethod · 0.90
load_fileFunction · 0.85

Calls 1

openMethod · 0.45

Tested by 1

runTestMethod · 0.72