reformated sizeutils

master
Kevin Froman 2020-03-24 03:17:22 -05:00
parent 0825f2dd01
commit 1be08e09ef
1 changed files with 24 additions and 7 deletions

View File

@ -1,19 +1,36 @@
"""Onionr - Private P2P Communication.
size related utilities
"""
import sqlite3, os import sqlite3, os
from onionrutils import stringvalidators from onionrutils import stringvalidators
"""
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
def human_size(num, suffix='B'): def human_size(num, suffix='B'):
''' """Convert from bytes to a human readable format."""
Converts from bytes to a human readable format.
'''
for unit in ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z']: for unit in ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z']:
if abs(num) < 1024.0: if abs(num) < 1024.0:
return "%.1f %s%s" % (num, unit, suffix) return "%.1f %s%s" % (num, unit, suffix)
num /= 1024.0 num /= 1024.0
return "%.1f %s%s" % (num, 'Yi', suffix) return "%.1f %s%s" % (num, 'Yi', suffix)
def size(path='.'): def size(path='.'):
''' """Get size of a folder's contents in bytes."""
Returns the size of a folder's contents in bytes
'''
total = 0 total = 0
if os.path.exists(path): if os.path.exists(path):
if os.path.isfile(path): if os.path.isfile(path):
@ -24,4 +41,4 @@ def size(path='.'):
total += entry.stat().st_size total += entry.stat().st_size
elif entry.is_dir(): elif entry.is_dir():
total += size(entry.path) total += size(entry.path)
return total return total