Browse Source

black format

Fabian Peter Hammerle 4 years ago
parent
commit
498d5abc78
4 changed files with 84 additions and 88 deletions
  1. 1 0
      README.md
  2. 41 30
      free_disk/__init__.py
  3. 16 32
      setup.py
  4. 26 26
      tests/data_size_to_bytes_test.py

+ 1 - 0
README.md

@@ -1,5 +1,6 @@
 # free-disk 💾
 
+[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
 [![Build Status](https://travis-ci.org/fphammerle/free-disk.svg?branch=master)](https://travis-ci.org/fphammerle/free-disk)
 
 Delete file with the oldest modification date

+ 41 - 30
free_disk/__init__.py

@@ -12,29 +12,29 @@ import re
 
 # https://en.wikipedia.org/wiki/Template:Quantities_of_bytes
 DATA_SIZE_UNIT_BYTE_CONVERSION_FACTOR = {
-    'B': 1,
-    'kB': 10**3,
-    'KB': 10**3,
-    'MB': 10**6,
-    'GB': 10**9,
-    'TB': 10**12,
-    'KiB': 2**10,
-    'MiB': 2**20,
-    'GiB': 2**30,
-    'TiB': 2**40,
+    "B": 1,
+    "kB": 10 ** 3,
+    "KB": 10 ** 3,
+    "MB": 10 ** 6,
+    "GB": 10 ** 9,
+    "TB": 10 ** 12,
+    "KiB": 2 ** 10,
+    "MiB": 2 ** 20,
+    "GiB": 2 ** 30,
+    "TiB": 2 ** 40,
 }
 
 
 def data_size_to_bytes(size_with_unit: str) -> int:
-    match = re.match(r'^([\d\.]+)\s*([A-Za-z]+)?$', size_with_unit)
+    match = re.match(r"^([\d\.]+)\s*([A-Za-z]+)?$", size_with_unit)
     if not match:
-        raise ValueError('Unable to parse data size {!r}'.format(size_with_unit))
+        raise ValueError("Unable to parse data size {!r}".format(size_with_unit))
     unit_symbol = match.group(2)
     if unit_symbol:
         try:
             byte_conversion_factor = DATA_SIZE_UNIT_BYTE_CONVERSION_FACTOR[unit_symbol]
         except KeyError:
-            raise ValueError('Unknown data size unit symbol {!r}'.format(unit_symbol))
+            raise ValueError("Unknown data size unit symbol {!r}".format(unit_symbol))
     else:
         byte_conversion_factor = 1
     byte_size = float(match.group(1)) * byte_conversion_factor
@@ -43,23 +43,31 @@ def data_size_to_bytes(size_with_unit: str) -> int:
 
 def main():
     argparser = argparse.ArgumentParser(description=__doc__)
-    argparser.add_argument('-d', '--debug', action='store_true')
-    argparser.add_argument('--free-bytes', type=data_size_to_bytes, required=True,
-                           help='examples: 1024, 1024B, 4KiB, 4KB, 2TB')
-    argparser.add_argument('root_dir_path', metavar='ROOT_DIR')
+    argparser.add_argument("-d", "--debug", action="store_true")
+    argparser.add_argument(
+        "--free-bytes",
+        type=data_size_to_bytes,
+        required=True,
+        help="examples: 1024, 1024B, 4KiB, 4KB, 2TB",
+    )
+    argparser.add_argument("root_dir_path", metavar="ROOT_DIR")
     args = argparser.parse_args()
-    logging.basicConfig(level=logging.DEBUG if args.debug else logging.INFO,
-                        format='%(asctime)s:%(levelname)s:%(message)s',
-                        datefmt='%Y-%m-%dT%H:%M:%S%z')
-    logging.debug('Required free bytes: %d', args.free_bytes)
+    logging.basicConfig(
+        level=logging.DEBUG if args.debug else logging.INFO,
+        format="%(asctime)s:%(levelname)s:%(message)s",
+        datefmt="%Y-%m-%dT%H:%M:%S%z",
+    )
+    logging.debug("Required free bytes: %d", args.free_bytes)
     disk_usage = shutil.disk_usage(args.root_dir_path)
     logging.debug(disk_usage)
     if disk_usage.free >= args.free_bytes:
-        logging.debug('Requirement already fulfilled')
+        logging.debug("Requirement already fulfilled")
         return
-    file_paths = [os.path.join(dirpath, filename)
-                  for dirpath, _, filenames in os.walk(args.root_dir_path)
-                  for filename in filenames]
+    file_paths = [
+        os.path.join(dirpath, filename)
+        for dirpath, _, filenames in os.walk(args.root_dir_path)
+        for filename in filenames
+    ]
     file_mtime_paths = [(os.stat(p).st_mtime, p) for p in file_paths]
     file_mtime_paths.sort()
     removed_files_counter = 0
@@ -68,15 +76,18 @@ def main():
         if shutil.disk_usage(args.root_dir_path).free >= args.free_bytes:
             break
         os.remove(file_path)
-        logging.debug('Removed file %s', file_path)
+        logging.debug("Removed file %s", file_path)
         removed_files_counter += 1
         last_mtime = file_mtime
     if removed_files_counter == 0:
-        logging.warning('No files to remove')
+        logging.warning("No files to remove")
     else:
-        logging.info('Removed %d file(s) with modification date <= %sZ', removed_files_counter,
-                     datetime.datetime.utcfromtimestamp(last_mtime).isoformat('T'))
+        logging.info(
+            "Removed %d file(s) with modification date <= %sZ",
+            removed_files_counter,
+            datetime.datetime.utcfromtimestamp(last_mtime).isoformat("T"),
+        )
 
 
-if __name__ == '__main__':
+if __name__ == "__main__":
     main()

+ 16 - 32
setup.py

@@ -3,42 +3,26 @@ import setuptools
 import free_disk
 
 setuptools.setup(
-    name='free-disk',
+    name="free-disk",
     use_scm_version=True,
     description=free_disk.__doc__,
-    author='Fabian Peter Hammerle',
-    author_email='fabian@hammerle.me',
-    url='https://github.com/fphammerle/free-disk',
-    license='MIT',
-    keywords=[
-        'disk',
-        'files',
-        'cleanup',
-        'free',
-        'delete',
-        'old',
-    ],
+    author="Fabian Peter Hammerle",
+    author_email="fabian@hammerle.me",
+    url="https://github.com/fphammerle/free-disk",
+    license="MIT",
+    keywords=["disk", "files", "cleanup", "free", "delete", "old",],
     classifiers=[
-        'Development Status :: 3 - Alpha',
-        'Intended Audience :: System Administrators',
-        'License :: OSI Approved :: MIT License',
-        'Operating System :: POSIX',
-        'Programming Language :: Python :: 3',
-        'Topic :: System :: Filesystems',
-        'Topic :: Utilities',
+        "Development Status :: 3 - Alpha",
+        "Intended Audience :: System Administrators",
+        "License :: OSI Approved :: MIT License",
+        "Operating System :: POSIX",
+        "Programming Language :: Python :: 3",
+        "Topic :: System :: Filesystems",
+        "Topic :: Utilities",
     ],
     packages=setuptools.find_packages(),
-    entry_points={
-        'console_scripts': [
-            'free-disk = free_disk:main',
-        ],
-    },
+    entry_points={"console_scripts": ["free-disk = free_disk:main",],},
     install_requires=[],
-    setup_requires=[
-        'setuptools_scm',
-    ],
-    tests_require=[
-        'pylint>=2.3.0',
-        'pytest',
-    ],
+    setup_requires=["setuptools_scm",],
+    tests_require=["pylint>=2.3.0", "pytest",],
 )

+ 26 - 26
tests/data_size_to_bytes_test.py

@@ -3,36 +3,36 @@ import pytest
 import free_disk
 
 
-@pytest.mark.parametrize(('data_size_with_unit', 'expected_bytes'), [
-    ('123', 123),
-    ('123B', 123),
-    ('123.0B', 123),
-    ('1kB', 1000),
-    ('2kB', 2000),
-    ('2.5kB', 2500),
-    ('2KB', 2000),
-    ('8MB', 8 * (10**6)),
-    ('8.5MB', 8.5 * (10**6)),
-    ('32GB', 32 * (10**9)),
-    ('9TB', 9 * (10**12)),
-    ('3KiB', 3 * (1024**1)),
-    ('40MiB', 40 * (1024**2)),
-    ('512GiB', 512 * (1024**3)),
-    ('7TiB', 7 * (1024**4)),
-    ('123 B', 123),
-    ('123\tB', 123),
-    ('123.0  B', 123),
-    ('1  kB', 1000),
-    ('1  MiB', 1024**2),
-])
+@pytest.mark.parametrize(
+    ("data_size_with_unit", "expected_bytes"),
+    [
+        ("123", 123),
+        ("123B", 123),
+        ("123.0B", 123),
+        ("1kB", 1000),
+        ("2kB", 2000),
+        ("2.5kB", 2500),
+        ("2KB", 2000),
+        ("8MB", 8 * (10 ** 6)),
+        ("8.5MB", 8.5 * (10 ** 6)),
+        ("32GB", 32 * (10 ** 9)),
+        ("9TB", 9 * (10 ** 12)),
+        ("3KiB", 3 * (1024 ** 1)),
+        ("40MiB", 40 * (1024 ** 2)),
+        ("512GiB", 512 * (1024 ** 3)),
+        ("7TiB", 7 * (1024 ** 4)),
+        ("123 B", 123),
+        ("123\tB", 123),
+        ("123.0  B", 123),
+        ("1  kB", 1000),
+        ("1  MiB", 1024 ** 2),
+    ],
+)
 def test_data_size_to_bytes(data_size_with_unit, expected_bytes):
     assert expected_bytes == free_disk.data_size_to_bytes(data_size_with_unit)
 
 
-@pytest.mark.parametrize('data_size_with_unit', [
-    'abcdef',
-    '123G',
-])
+@pytest.mark.parametrize("data_size_with_unit", ["abcdef", "123G",])
 def test_data_size_to_bytes_fail(data_size_with_unit):
     with pytest.raises(ValueError):
         free_disk.data_size_to_bytes(data_size_with_unit)