Skip to content

Module dustpan.config

View Source
from __future__ import annotations

import argparse

import enum

import os

from dataclasses import dataclass

from pathlib import Path

from typing import Iterable, Set, Union

import toml

CWD = Path.cwd()

class Verbosity(enum.IntEnum):

    QUIET = 0

    NORMAL = 1

    VERBOSE = 2

    VERY_VERBOSE = 3

@dataclass

class Configuration:

    directories: Set[Path]

    include: Set[str]

    exclude: Set[str]

    remove_empty_directories: bool

    verbosity: Verbosity

    def __init__(

        self,

        directories: Iterable[Union[os.PathLike, str]] = {CWD},

        include: Iterable[str] = set(),

        exclude: Iterable[str] = set(),

        remove_empty_directories: bool = False,

        quiet: bool = False,

        verbose: bool = False,

        very_verbose: bool = False,

    ) -> None:

        self.directories = set(map(lambda p: Path(p).resolve(), directories))

        self.include = set(include)

        self.exclude = set(exclude)

        self.remove_empty_directories = remove_empty_directories

        if quiet:

            self.verbosity = Verbosity.QUIET

        elif verbose:

            self.verbosity = Verbosity.VERBOSE

        elif very_verbose:

            self.verbosity = Verbosity.VERY_VERBOSE

        else:

            self.verbosity = Verbosity.NORMAL

def parse_pyproject_toml() -> dict:

    pyproject_toml = toml.load(CWD / "pyproject.toml")

    config: dict = pyproject_toml.get("tool", {}).get("dustpan", {})

    return {k.replace("-", "_"): v for k, v in config.items()}

def parse_arguments() -> dict:

    parser = argparse.ArgumentParser(description="")

    parser.add_argument("directories", type=Path, nargs="+", help="Root directories to search")

    parser.add_argument("-p", "--patterns", type=str, nargs="+", help="Additional path patterns to queue for removal")

    parser.add_argument("-i", "--ignore", type=str, nargs="+", help="Path patterns to exclude from removal")

    parser.add_argument("--remove-empty-directories", action="store_true", help="Remove all childless directories")

    verbosity = parser.add_mutually_exclusive_group()

    verbosity.add_argument("-q", "--quiet", action="store_true", help="Be quiet")

    verbosity.add_argument("-v", "--verbose", action="store_true", help="Be more verbose")

    verbosity.add_argument("-vv", "--very-verbose", action="store_true", help="Be very verbose")

    args = parser.parse_args()

    return {k: v for k, v in vars(args).items() if bool(v)}

CONFIG = Configuration(**{**parse_pyproject_toml(), **parse_arguments()})

Variables

1
CONFIG
1
CWD

Functions

parse_arguments

1
2
3
def parse_arguments(

) -> 'dict'
View Source
def parse_arguments() -> dict:

    parser = argparse.ArgumentParser(description="")

    parser.add_argument("directories", type=Path, nargs="+", help="Root directories to search")

    parser.add_argument("-p", "--patterns", type=str, nargs="+", help="Additional path patterns to queue for removal")

    parser.add_argument("-i", "--ignore", type=str, nargs="+", help="Path patterns to exclude from removal")

    parser.add_argument("--remove-empty-directories", action="store_true", help="Remove all childless directories")

    verbosity = parser.add_mutually_exclusive_group()

    verbosity.add_argument("-q", "--quiet", action="store_true", help="Be quiet")

    verbosity.add_argument("-v", "--verbose", action="store_true", help="Be more verbose")

    verbosity.add_argument("-vv", "--very-verbose", action="store_true", help="Be very verbose")

    args = parser.parse_args()

    return {k: v for k, v in vars(args).items() if bool(v)}

parse_pyproject_toml

1
2
3
def parse_pyproject_toml(

) -> 'dict'
View Source
def parse_pyproject_toml() -> dict:

    pyproject_toml = toml.load(CWD / "pyproject.toml")

    config: dict = pyproject_toml.get("tool", {}).get("dustpan", {})

    return {k.replace("-", "_"): v for k, v in config.items()}

Classes

Configuration

1
2
3
4
5
6
7
8
9
class Configuration(
    directories: 'Iterable[Union[os.PathLike, str]]' = {PosixPath('/home/runner/work/dustpan/dustpan')},
    include: 'Iterable[str]' = set(),
    exclude: 'Iterable[str]' = set(),
    remove_empty_directories: 'bool' = False,
    quiet: 'bool' = False,
    verbose: 'bool' = False,
    very_verbose: 'bool' = False
)

Configuration(directories: 'Iterable[Union[os.PathLike, str]]' = {PosixPath('/home/runner/work/dustpan/dustpan')}, include: 'Iterable[str]' = set(), exclude: 'Iterable[str]' = set(), remove_empty_directories: 'bool' = False, quiet: 'bool' = False, verbose: 'bool' = False, very_verbose: 'bool' = False) -> 'None'

View Source
@dataclass

class Configuration:

    directories: Set[Path]

    include: Set[str]

    exclude: Set[str]

    remove_empty_directories: bool

    verbosity: Verbosity

    def __init__(

        self,

        directories: Iterable[Union[os.PathLike, str]] = {CWD},

        include: Iterable[str] = set(),

        exclude: Iterable[str] = set(),

        remove_empty_directories: bool = False,

        quiet: bool = False,

        verbose: bool = False,

        very_verbose: bool = False,

    ) -> None:

        self.directories = set(map(lambda p: Path(p).resolve(), directories))

        self.include = set(include)

        self.exclude = set(exclude)

        self.remove_empty_directories = remove_empty_directories

        if quiet:

            self.verbosity = Verbosity.QUIET

        elif verbose:

            self.verbosity = Verbosity.VERBOSE

        elif very_verbose:

            self.verbosity = Verbosity.VERY_VERBOSE

        else:

            self.verbosity = Verbosity.NORMAL

Verbosity

1
2
3
4
class Verbosity(
    *args,
    **kwds
)

Enum where members are also (and must be) ints

View Source
class Verbosity(enum.IntEnum):

    QUIET = 0

    NORMAL = 1

    VERBOSE = 2

    VERY_VERBOSE = 3

Ancestors (in MRO)

  • enum.IntEnum
  • builtins.int
  • enum.ReprEnum
  • enum.Enum

Class variables

1
NORMAL
1
QUIET
1
VERBOSE
1
VERY_VERBOSE
1
denominator
1
imag
1
numerator
1
real

Methods

as_integer_ratio

1
2
3
4
def as_integer_ratio(
    self,
    /
)

Return integer ratio.

Return a pair of integers, whose ratio is exactly equal to the original int and with a positive denominator.

(10).as_integer_ratio() (10, 1) (-10).as_integer_ratio() (-10, 1) (0).as_integer_ratio() (0, 1)

bit_count

1
2
3
4
def bit_count(
    self,
    /
)

Number of ones in the binary representation of the absolute value of self.

Also known as the population count.

bin(13) '0b1101' (13).bit_count() 3

bit_length

1
2
3
4
def bit_length(
    self,
    /
)

Number of bits necessary to represent self in binary.

bin(37) '0b100101' (37).bit_length() 6

conjugate

1
2
3
def conjugate(
    ...
)

Returns self, the complex conjugate of any int.

from_bytes

1
2
3
4
5
6
def from_bytes(
    bytes,
    byteorder='big',
    *,
    signed=False
)

Return the integer represented by the given array of bytes.

bytes Holds the array of bytes to convert. The argument must either support the buffer protocol or be an iterable object producing bytes. Bytes and bytearray are examples of built-in objects that support the buffer protocol. byteorder The byte order used to represent the integer. If byteorder is 'big', the most significant byte is at the beginning of the byte array. If byteorder is 'little', the most significant byte is at the end of the byte array. To request the native byte order of the host system, use `sys.byteorder' as the byte order value. Default is to use 'big'. signed Indicates whether two's complement is used to represent the integer.

to_bytes

1
2
3
4
5
6
7
8
def to_bytes(
    self,
    /,
    length=1,
    byteorder='big',
    *,
    signed=False
)

Return an array of bytes representing an integer.

length Length of bytes object to use. An OverflowError is raised if the integer is not representable with the given number of bytes. Default is length 1. byteorder The byte order used to represent the integer. If byteorder is 'big', the most significant byte is at the beginning of the byte array. If byteorder is 'little', the most significant byte is at the end of the byte array. To request the native byte order of the host system, use `sys.byteorder' as the byte order value. Default is to use 'big'. signed Determines whether two's complement is used to represent the integer. If signed is False and a negative integer is given, an OverflowError is raised.