Onionr/src/utils/sizeutils.py

45 lines
1.5 KiB
Python
Raw Normal View History

2020-03-24 08:17:22 +00:00
"""Onionr - Private P2P Communication.
size related utilities
"""
import sqlite3, os
from onionrutils import stringvalidators
2020-03-24 08:17:22 +00:00
"""
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'):
2020-03-24 08:17:22 +00:00
"""Convert from bytes to a human readable format."""
for unit in ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z']:
if abs(num) < 1024.0:
return "%.1f %s%s" % (num, unit, suffix)
num /= 1024.0
return "%.1f %s%s" % (num, 'Yi', suffix)
2020-03-24 08:17:22 +00:00
def size(path='.'):
2020-03-24 08:17:22 +00:00
"""Get size of a folder's contents in bytes."""
total = 0
if os.path.exists(path):
if os.path.isfile(path):
total = os.path.getsize(path)
else:
for entry in os.scandir(path):
if entry.is_file():
total += entry.stat().st_size
elif entry.is_dir():
total += size(entry.path)
2020-03-24 08:17:22 +00:00
return total