Skip to content

Commit

Permalink
Reformat the codebase with ruff (#29)
Browse files Browse the repository at this point in the history
- also run ruff --check
- ignored assert ruff rule for the test cases
  • Loading branch information
ubmarco authored Jan 19, 2024
1 parent 73bbfca commit 5d08891
Show file tree
Hide file tree
Showing 36 changed files with 1,518 additions and 1,061 deletions.
15 changes: 8 additions & 7 deletions examples/lib_usage_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import sys

import libpdf

from tests.conftest import PDF_LOREM_IPSUM as TEST_PDF

LOG = logging.getLogger(__name__)
Expand All @@ -13,24 +12,26 @@

def main():
"""Show how the library is used via API."""
if 'tqdm' not in sys.modules:
if "tqdm" not in sys.modules:
# if tqdm is not available, only basic config for logging is initialized.
# if tqdm is installed the root logger is assigned a custom handler libpdf.log.TqdmLoggingHandler
# that writes all log messages through tqdm.write() to integrate progress bars with logging
logging.basicConfig(level='DEBUG', format='[%(levelname)5s] %(name)s - %(message)s')
logging.basicConfig(
level="DEBUG", format="[%(levelname)5s] %(name)s - %(message)s"
)

# constrain log levels of pdfminer and PIL to avoid log spam
logging.getLogger('pdfminer').level = logging.WARNING
logging.getLogger('PIL').level = logging.WARNING
logging.getLogger("pdfminer").level = logging.WARNING
logging.getLogger("PIL").level = logging.WARNING

objects = libpdf.load(
TEST_PDF,
verbose=3,
visual_debug=True,
visual_debug_output_dir='visual_debug',
visual_debug_output_dir="visual_debug",
)
LOG.info(objects)


if __name__ == '__main__':
if __name__ == "__main__":
main()
16 changes: 8 additions & 8 deletions libpdf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,27 @@
:copyright: © 2020 by team useblocks
:license: MIT, see LICENSE for more details
""" # noqa: RST399, RST201 # needed for autodoc
""" # needed for autodoc
try:
import importlib_metadata # Python 3.6 and 3.7
except ImportError:
import importlib.metadata as importlib_metadata # Python 3.8, 3.9

__version__: str = importlib_metadata.version('libpdf')
__summary__: str = importlib_metadata.metadata('libpdf')['Summary']
__version__: str = importlib_metadata.version("libpdf")
__summary__: str = importlib_metadata.metadata("libpdf")["Summary"]

# below imports from libpdf.core cannot be at the top avoid circular import errors in core.py when
# importing __version__ and __summary__
import libpdf._import_forks # noqa: E402, F401
from libpdf.core import main_api as load # noqa: E402
from libpdf.core import main_cli # noqa: E402
import libpdf._import_forks # noqa: F401
from libpdf.core import main_api as load
from libpdf.core import main_cli

# define importable objects
__all__ = ['load', '__version__', '__summary__']
__all__ = ["load", "__version__", "__summary__"]

# Enable running
# python -m libpdf.__init__
# python libpdf/__init__.py
# before installing the package itself
if __name__ == '__main__':
if __name__ == "__main__":
main_cli()
10 changes: 7 additions & 3 deletions libpdf/_import_forks.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,24 @@
import os
import sys

DEPS_DIR = os.path.join(os.path.dirname(__file__), '..', 'deps')
DEPS_DIR = os.path.join(os.path.dirname(__file__), "..", "deps")

# first try to import the dependencies so active venvs with direct Git dependencies are not overriden
try:
import pdfminer
except ModuleNotFoundError:
# make dependency available as wheel to sys.path as first entry
sys.path.insert(0, os.path.join(DEPS_DIR, 'pdfminer.six-20200517.dev1-py3-none-any.whl'))
sys.path.insert(
0, os.path.join(DEPS_DIR, "pdfminer.six-20200517.dev1-py3-none-any.whl")
)
else:
del pdfminer

try:
import pdfplumber
except ModuleNotFoundError:
sys.path.insert(0, os.path.join(DEPS_DIR, 'pdfplumber-0.5.21.dev1-py3-none-any.whl'))
sys.path.insert(
0, os.path.join(DEPS_DIR, "pdfplumber-0.5.21.dev1-py3-none-any.whl")
)
else:
del pdfplumber
11 changes: 6 additions & 5 deletions libpdf/apiobjects.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

from typing import List, NamedTuple

from pdfminer.pdfdocument import PDFDocument
from pdfplumber.pdf import PDF

from libpdf.models.chapter import Chapter
from libpdf.models.figure import Figure
from libpdf.models.paragraph import Paragraph
from libpdf.models.root import Root
from libpdf.models.table import Table

from pdfminer.pdfdocument import PDFDocument

from pdfplumber.pdf import PDF


# disable too-few-public-methods because this is a data storage class
# another option is using a dict but this does not feature IDE type hinting
Expand Down Expand Up @@ -45,7 +44,9 @@ def __init__( # pylint: disable=too-many-arguments # the parameters are needed
self.root = root

# attributes for API convenience
self.flattened = Flattened(chapters=chapters, paragraphs=paragraphs, tables=tables, figures=figures)
self.flattened = Flattened(
chapters=chapters, paragraphs=paragraphs, tables=tables, figures=figures
)

# exposing the pdfplumber PDF object
self.pdfplumber = pdfplumber
Expand Down
Loading

0 comments on commit 5d08891

Please sign in to comment.