@@ -120,6 +120,69 @@ def unquote_to_bytes(s):
120120PY2 = (sys .version_info [0 ] == 2 )
121121
122122
123+ # get_readable_size by Lipis
124+ # http://stackoverflow.com/posts/14998888/revisions
125+
126+
127+ def get_readable_size (bytes , precision = 1 , unit = "IEC" ):
128+ unit = unit .upper ()
129+ if (unit != "IEC" and unit != "SI" ):
130+ unit = "IEC"
131+ if (unit == "IEC" ):
132+ units = [" B" , " KiB" , " MiB" , " GiB" , " TiB" , " PiB" , " EiB" , " ZiB" ]
133+ unitswos = ["B" , "KiB" , "MiB" , "GiB" , "TiB" , "PiB" , "EiB" , "ZiB" ]
134+ unitsize = 1024.0
135+ if (unit == "SI" ):
136+ units = [" B" , " kB" , " MB" , " GB" , " TB" , " PB" , " EB" , " ZB" ]
137+ unitswos = ["B" , "kB" , "MB" , "GB" , "TB" , "PB" , "EB" , "ZB" ]
138+ unitsize = 1000.0
139+ return_val = {}
140+ orgbytes = bytes
141+ for unit in units :
142+ if abs (bytes ) < unitsize :
143+ strformat = "%3." + str (precision )+ "f%s"
144+ pre_return_val = (strformat % (bytes , unit ))
145+ pre_return_val = re .sub (
146+ r"([0]+) ([A-Za-z]+)" , r" \2" , pre_return_val )
147+ pre_return_val = re .sub (r"\. ([A-Za-z]+)" , r" \1" , pre_return_val )
148+ alt_return_val = pre_return_val .split ()
149+ return_val = {'Bytes' : orgbytes , 'ReadableWithSuffix' : pre_return_val ,
150+ 'ReadableWithoutSuffix' : alt_return_val [0 ], 'ReadableSuffix' : alt_return_val [1 ]}
151+ return return_val
152+ bytes /= unitsize
153+ strformat = "%." + str (precision )+ "f%s"
154+ pre_return_val = (strformat % (bytes , "YiB" ))
155+ pre_return_val = re .sub (r"([0]+) ([A-Za-z]+)" , r" \2" , pre_return_val )
156+ pre_return_val = re .sub (r"\. ([A-Za-z]+)" , r" \1" , pre_return_val )
157+ alt_return_val = pre_return_val .split ()
158+ return_val = {'Bytes' : orgbytes , 'ReadableWithSuffix' : pre_return_val ,
159+ 'ReadableWithoutSuffix' : alt_return_val [0 ], 'ReadableSuffix' : alt_return_val [1 ]}
160+ return return_val
161+
162+
163+ def get_readable_size_from_file (infile , precision = 1 , unit = "IEC" , usehashes = False , usehashtypes = "md5,sha1" ):
164+ unit = unit .upper ()
165+ usehashtypes = usehashtypes .lower ()
166+ getfilesize = os .path .getsize (infile )
167+ return_val = get_readable_size (getfilesize , precision , unit )
168+ if (usehashes ):
169+ hashtypelist = usehashtypes .split ("," )
170+ openfile = open (infile , "rb" )
171+ filecontents = openfile .read ()
172+ openfile .close ()
173+ listnumcount = 0
174+ listnumend = len (hashtypelist )
175+ while (listnumcount < listnumend ):
176+ hashtypelistlow = hashtypelist [listnumcount ].strip ()
177+ hashtypelistup = hashtypelistlow .upper ()
178+ filehash = hashlib .new (hashtypelistup )
179+ filehash .update (filecontents )
180+ filegethash = filehash .hexdigest ()
181+ return_val .update ({hashtypelistup : filegethash })
182+ listnumcount += 1
183+ return return_val
184+
185+
123186def _is_probably_text (data_bytes ):
124187 """
125188 Heuristic: treat as text if it decodes as UTF-8 and does not contain many
0 commit comments