Commit 26ac771a by Johannes vom Dorp Committed by GitHub

Merge pull request #1 from fkie-cad/bitmath

Bitmath
parents 3de2ab42 4c59895f
...@@ -5,12 +5,3 @@ File and filesystem related helper functions incl.: ...@@ -5,12 +5,3 @@ File and filesystem related helper functions incl.:
* Fail safe file operations: *They log errors but never throw an exception* * Fail safe file operations: *They log errors but never throw an exception*
* Git version string generator * Git version string generator
* Large file handling * Large file handling
## Known Issues
It seems that recent versions of setuptools can't handle a "." in the requirements list.
Therfore, [hurry.filesize](https://pypi.python.org/pypi/hurry.filesize) must be installed manually in advance.
```sh
$ sudo -EH pip3 install hurry.filesize
```
...@@ -2,9 +2,6 @@ from .fail_safe_file_operations import get_binary_from_file, get_string_list_fro ...@@ -2,9 +2,6 @@ from .fail_safe_file_operations import get_binary_from_file, get_string_list_fro
from .file_functions import read_in_chunks, get_directory_for_filename, create_dir_for_file, human_readable_file_size from .file_functions import read_in_chunks, get_directory_for_filename, create_dir_for_file, human_readable_file_size
from .git_functions import get_version_string_from_git from .git_functions import get_version_string_from_git
__version__ = '0.1.9'
__all__ = [ __all__ = [
'get_directory_for_filename', 'get_directory_for_filename',
'create_dir_for_file', 'create_dir_for_file',
......
import os import os
from hurry.filesize import size, alternative import bitmath
def read_in_chunks(file_object, chunk_size=1024): def read_in_chunks(file_object, chunk_size=1024):
""" '''
Helper function to read large file objects iteratively in smaller chunks. Can be used like this:: Helper function to read large file objects iteratively in smaller chunks. Can be used like this::
file_object = open('somelargefile.xyz', 'rb') file_object = open('somelargefile.xyz', 'rb')
...@@ -14,7 +14,7 @@ def read_in_chunks(file_object, chunk_size=1024): ...@@ -14,7 +14,7 @@ def read_in_chunks(file_object, chunk_size=1024):
:param chunk_size: Number of bytes to read per chunk. :param chunk_size: Number of bytes to read per chunk.
:type chunk_size: int :type chunk_size: int
:return: Returns a generator to iterate over all chunks, see above for usage. :return: Returns a generator to iterate over all chunks, see above for usage.
""" '''
while True: while True:
data = file_object.read(chunk_size) data = file_object.read(chunk_size)
if not data: if not data:
...@@ -23,34 +23,34 @@ def read_in_chunks(file_object, chunk_size=1024): ...@@ -23,34 +23,34 @@ def read_in_chunks(file_object, chunk_size=1024):
def get_directory_for_filename(filename): def get_directory_for_filename(filename):
""" '''
Convenience function which returns the absolute path to the directory that contains the given file name. Convenience function which returns the absolute path to the directory that contains the given file name.
:param filename: Path of the file. Can be absolute or relative to the current directory. :param filename: Path of the file. Can be absolute or relative to the current directory.
:type filename: str :type filename: str
:return: Absolute path of the directory :return: Absolute path of the directory
""" '''
return os.path.dirname(os.path.abspath(filename)) return os.path.dirname(os.path.abspath(filename))
def create_dir_for_file(file_path): def create_dir_for_file(file_path):
""" '''
Creates all directories of file path. File path may include the file as well. Creates all directories of file path. File path may include the file as well.
:param file_path: Path of the file. Can be absolute or relative to the current directory. :param file_path: Path of the file. Can be absolute or relative to the current directory.
:type file_path: str :type file_path: str
:return: None :return: None
""" '''
directory = os.path.dirname(os.path.abspath(file_path)) directory = os.path.dirname(os.path.abspath(file_path))
os.makedirs(directory, exist_ok=True) os.makedirs(directory, exist_ok=True)
def human_readable_file_size(size_in_bytes): def human_readable_file_size(size_in_bytes):
""" '''
Returns a nicly human readable file size Returns a nicly human readable file size
:param size_in_bytes: Size in Bytes :param size_in_bytes: Size in Bytes
:type size_in_bytes: int :type size_in_bytes: int
:return: str :return: str
""" '''
return size(size_in_bytes, system=alternative) return bitmath.Byte(bytes=size_in_bytes).best_prefix().format('{value:.2f} {unit}')
hurry.filesize
\ No newline at end of file
from setuptools import setup, find_packages from setuptools import setup, find_packages
from common_helper_files import __version__
VERSION = '0.2'
setup( setup(
name="common_helper_files", name='common_helper_files',
version=__version__, version=VERSION,
packages=find_packages(), packages=find_packages(),
install_requires=[ install_requires=[
'hurry.filesize' 'bitmath'
], ],
description="file operation helper functions", description='file operation helper functions',
author="Fraunhofer FKIE", author='Fraunhofer FKIE',
author_email="peter.weidenbach@fkie.fraunhofer.de", author_email='peter.weidenbach@fkie.fraunhofer.de',
url="http://www.fkie.fraunhofer.de", url='http://www.fkie.fraunhofer.de',
license="GPL-3.0" license='GPL-3.0'
) )
...@@ -5,8 +5,5 @@ from common_helper_files import human_readable_file_size ...@@ -5,8 +5,5 @@ from common_helper_files import human_readable_file_size
class Test_file_functions(unittest.TestCase): class Test_file_functions(unittest.TestCase):
def test_human_readable_file_size(self): def test_human_readable_file_size(self):
self.assertEqual(human_readable_file_size(1024), "1 KB") self.assertEqual(human_readable_file_size(1024), '1.00 KiB')
self.assertEqual(human_readable_file_size(5000), '4.88 KiB')
if __name__ == "__main__":
unittest.main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment