apply a few pylint/pycodestyle tips

This commit is contained in:
Ivan Schaller 2022-07-21 20:39:56 +02:00
parent fc3f1984a3
commit 7c3f83389a
9 changed files with 47 additions and 26 deletions

View file

@ -46,6 +46,13 @@ pipeline:
commands: commands:
- python3 -m mypy --install-types --non-interactive mangadlp/ - python3 -m mypy --install-types --non-interactive mangadlp/
# multiple linters with pylama
test-pylama:
image: cr.44net.ch/ci-plugins/tests
pull: true
commands:
- python3 -m pylama mangadlp/
# test code with different python versions # test code with different python versions
test-tox-pytest: test-tox-pytest:
when: when:

View file

@ -86,6 +86,9 @@ test_autoflake:
@python3 -m autoflake --remove-all-unused-imports -r -v mangadlp/ @python3 -m autoflake --remove-all-unused-imports -r -v mangadlp/
@python3 -m autoflake --check --remove-all-unused-imports -r -v mangadlp/ @python3 -m autoflake --check --remove-all-unused-imports -r -v mangadlp/
test_pylama:
@python3 -m pylama mangadlp/
test_tox: test_tox:
@python3 -m tox @python3 -m tox
@ -116,6 +119,7 @@ lint:
just test_isort just test_isort
just test_mypy just test_mypy
just test_autoflake just test_autoflake
just test_pylama
@echo -e "\n\033[0;32m=== ALL DONE ===\033[0m\n" @echo -e "\n\033[0;32m=== ALL DONE ===\033[0m\n"
tests: tests:
@ -126,6 +130,7 @@ tests:
just test_isort just test_isort
just test_mypy just test_mypy
just test_autoflake just test_autoflake
just test_pylama
just test_pytest just test_pytest
@echo -e "\n\033[0;32m=== ALL DONE ===\033[0m\n" @echo -e "\n\033[0;32m=== ALL DONE ===\033[0m\n"
@ -136,6 +141,8 @@ tests_full:
just test_black just test_black
just test_isort just test_isort
just test_mypy just test_mypy
just test_autoflake
just test_pylama
just test_build just test_build
just test_tox just test_tox
just test_tox_coverage just test_tox_coverage

View file

@ -46,7 +46,7 @@ class Mangadex:
manga_data = requests.get( manga_data = requests.get(
f"{self.api_base_url}/manga/{self.manga_uuid}" f"{self.api_base_url}/manga/{self.manga_uuid}"
) )
except: except Exception:
if counter >= 3: if counter >= 3:
log.error("Maybe the MangaDex API is down?") log.error("Maybe the MangaDex API is down?")
sys.exit(1) sys.exit(1)
@ -73,8 +73,8 @@ class Mangadex:
if not uuid_regex.search(self.url_uuid): if not uuid_regex.search(self.url_uuid):
log.error("No valid UUID found") log.error("No valid UUID found")
sys.exit(1) sys.exit(1)
manga_uuid = uuid_regex.search(self.url_uuid)[0]
return manga_uuid return uuid_regex.search(self.url_uuid)[0]
# get the title of the manga (and fix the filename) # get the title of the manga (and fix the filename)
def get_manga_title(self) -> str: def get_manga_title(self) -> str:
@ -82,16 +82,17 @@ class Mangadex:
manga_data = self.manga_data.json() manga_data = self.manga_data.json()
try: try:
title = manga_data["data"]["attributes"]["title"][self.language] title = manga_data["data"]["attributes"]["title"][self.language]
except: except Exception:
# search in alt titles # search in alt titles
try: try:
alt_titles = {} alt_titles = {}
for title in manga_data["data"]["attributes"]["altTitles"]: for title in manga_data["data"]["attributes"]["altTitles"]:
alt_titles.update(title) alt_titles.update(title)
title = alt_titles[self.language] title = alt_titles[self.language]
except: # no title on requested language found except Exception: # no title on requested language found
log.error("Chapter in requested language not found.") log.error("Chapter in requested language not found.")
sys.exit(1) sys.exit(1)
return utils.fix_name(title) return utils.fix_name(title)
# check if chapters are available in requested language # check if chapters are available in requested language
@ -104,7 +105,7 @@ class Mangadex:
) )
try: try:
total_chapters = r.json()["total"] total_chapters = r.json()["total"]
except: except Exception:
log.error( log.error(
"Error retrieving the chapters list. Did you specify a valid language code?" "Error retrieving the chapters list. Did you specify a valid language code?"
) )
@ -198,10 +199,10 @@ class Mangadex:
else: else:
api_error = False api_error = False
break break
except: except Exception:
if counter >= 3: if counter >= 3:
api_error = True api_error = True
log.error(f"Retrying in a few seconds") log.error("Retrying in a few seconds")
counter += 1 counter += 1
sleep(wait_time + 2) sleep(wait_time + 2)
# check if result is ok # check if result is ok
@ -218,6 +219,7 @@ class Mangadex:
image_urls.append(f"{self.img_base_url}/data/{chapter_hash}/{image}") image_urls.append(f"{self.img_base_url}/data/{chapter_hash}/{image}")
sleep(wait_time) sleep(wait_time)
return image_urls return image_urls
# create list of chapters # create list of chapters

View file

@ -106,7 +106,7 @@ class MangaDLP:
if api_mangadex.search(url_uuid) or api_mangadex2.search(url_uuid): if api_mangadex.search(url_uuid) or api_mangadex2.search(url_uuid):
return Mangadex return Mangadex
# this is only for testing multiple apis # this is only for testing multiple apis
elif api_test.search(url_uuid): if api_test.search(url_uuid):
log.critical("Not supported yet") log.critical("Not supported yet")
sys.exit(1) sys.exit(1)
@ -252,7 +252,7 @@ class MangaDLP:
except KeyboardInterrupt: except KeyboardInterrupt:
log.critical("Stopping") log.critical("Stopping")
sys.exit(1) sys.exit(1)
except: except Exception:
log.error(f"Cant download: '{chapter_filename}'. Skipping") log.error(f"Cant download: '{chapter_filename}'. Skipping")
# add to skipped chapters list # add to skipped chapters list
return ( return (
@ -281,8 +281,8 @@ class MangaDLP:
utils.make_pdf(chapter_path) utils.make_pdf(chapter_path)
else: else:
utils.make_archive(chapter_path, self.file_format) utils.make_archive(chapter_path, self.file_format)
except: except Exception:
log.error(f"Archive error. Skipping chapter") log.error("Archive error. Skipping chapter")
# add to skipped chapters list # add to skipped chapters list
return { return {
"error": chapter_path, "error": chapter_path,

View file

@ -13,6 +13,7 @@ from mangadlp.logger import Logger
# prepare logger # prepare logger
log = Logger(__name__) log = Logger(__name__)
# download images # download images
def download_chapter( def download_chapter(
image_urls: list, image_urls: list,
@ -40,7 +41,7 @@ def download_chapter(
except KeyboardInterrupt: except KeyboardInterrupt:
log.critical("Stopping") log.critical("Stopping")
sys.exit(1) sys.exit(1)
except: except Exception:
if counter >= 3: if counter >= 3:
log.error("Maybe the MangaDex Servers are down?") log.error("Maybe the MangaDex Servers are down?")
raise ConnectionError raise ConnectionError
@ -54,7 +55,7 @@ def download_chapter(
with image_path.open("wb") as file: with image_path.open("wb") as file:
r.raw.decode_content = True r.raw.decode_content = True
shutil.copyfileobj(r.raw, file) shutil.copyfileobj(r.raw, file)
except: except Exception:
log.error("Can't write file") log.error("Can't write file")
raise IOError raise IOError

View file

@ -38,7 +38,7 @@ def readin_list(readlist: str) -> list:
try: try:
url_str = list_file.read_text() url_str = list_file.read_text()
url_list = url_str.splitlines() url_list = url_str.splitlines()
except: except Exception:
raise IOError raise IOError
# filter empty lines and remove them # filter empty lines and remove them
@ -72,11 +72,11 @@ def get_input():
readlist = str(input("List with links (optional): ")) readlist = str(input("List with links (optional): "))
language = str(input("Language: ")) or "en" language = str(input("Language: ")) or "en"
list_chapters = str(input("List chapters? y/N: ")) list_chapters = str(input("List chapters? y/N: "))
if list_chapters.lower() != "y" or list_chapters.lower() != "yes": if list_chapters.lower() in {"y", "yes"}:
chapters = str(input("Chapters: ")) chapters = str(input("Chapters: "))
except KeyboardInterrupt: except KeyboardInterrupt:
sys.exit(1) sys.exit(1)
except: except Exception:
continue continue
else: else:
break break
@ -88,12 +88,10 @@ def get_input():
chapters, chapters,
] ]
if url_uuid: if url_uuid:
args.append("-u") args.extend(("-u", url_uuid))
args.append(url_uuid)
if readlist: if readlist:
args.append("--read") args.extend(("--read", readlist))
args.append(readlist) if list_chapters.lower() in {"y", "yes"}:
if list_chapters.lower() == "y" or list_chapters.lower() == "yes":
args.append("--list") args.append("--list")
# start script again with the arguments # start script again with the arguments

View file

@ -9,6 +9,7 @@ from mangadlp.logger import Logger
# prepare logger # prepare logger
log = Logger(__name__) log = Logger(__name__)
# create an archive of the chapter images # create an archive of the chapter images
def make_archive(chapter_path: Path, file_format: str) -> None: def make_archive(chapter_path: Path, file_format: str) -> None:
zip_path = Path(f"{chapter_path}.zip") zip_path = Path(f"{chapter_path}.zip")
@ -19,14 +20,14 @@ def make_archive(chapter_path: Path, file_format: str) -> None:
zipfile.write(file, file.name) zipfile.write(file, file.name)
# rename zip to file format requested # rename zip to file format requested
zip_path.rename(zip_path.with_suffix(file_format)) zip_path.rename(zip_path.with_suffix(file_format))
except: except Exception:
raise IOError raise IOError
def make_pdf(chapter_path: Path) -> None: def make_pdf(chapter_path: Path) -> None:
try: try:
import img2pdf import img2pdf
except: except Exception:
log.error("Cant import img2pdf. Please install it first") log.error("Cant import img2pdf. Please install it first")
raise ImportError raise ImportError
@ -36,7 +37,7 @@ def make_pdf(chapter_path: Path) -> None:
images.append(str(file)) images.append(str(file))
try: try:
pdf_path.write_bytes(img2pdf.convert(images)) pdf_path.write_bytes(img2pdf.convert(images))
except: except Exception:
log.error("Can't create '.pdf' archive") log.error("Can't create '.pdf' archive")
raise IOError raise IOError

View file

@ -103,4 +103,4 @@ py-version = "3.9"
[tool.pylint.logging] [tool.pylint.logging]
logging-modules = ["logging"] logging-modules = ["logging"]
logging-format-style = "fstr" #logging-format-style = "fstr"

View file

@ -24,3 +24,8 @@ commands =
coverage erase coverage erase
coverage run coverage run
coverage xml -i coverage xml -i
[pylama]
format = pycodestyle
linters = mccabe,pycodestyle,pyflakes
ignore = E501,C901,C0301