3D-tester/utils/utils.py

53 lines
1.7 KiB
Python
Raw Normal View History

2021-06-05 18:59:19 +02:00
"""
Copyright 2021 Hangar.org
This file is part of 3D-tester.
3D-tester is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero 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/>.
"""
import os
from pathlib import Path
MODEL_TYPES = ['gltf', 'obj', 'fbx', 'stl']
def get_directory_content(dir_path):
content = []
for p in Path(dir_path).iterdir():
file = Path(p)
model_type = (Path(file.name).suffix).lstrip('.').lower()
is_object_file = True if model_type in MODEL_TYPES else False
item = {
'name': os.path.basename(p),
'size': human_readable_bytes(os.path.getsize(p)),
'is_dir': True if p.is_dir() else False,
'is_object_file': is_object_file
}
content.append(item)
return content
def human_readable_bytes(bytes):
""" 1 KibiByte == 1024 Bytes
1 Mebibyte == 1024*1024 Bytes
1 GibiByte == 1024*1024*1024 Bytes
"""
if bytes == 0:
return "0 KB"
if bytes < 1024*1024:
return f"{int(round(bytes/(1024), 2))} KB"
if bytes < 1024*1024*1024:
return f"{int(round(bytes/(1024*1024), 2))} MB"
return f"{int(round(bytes/(1024*1024*1024), 2))} GB"