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:
- 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-tox-pytest:
when:

View file

@ -86,6 +86,9 @@ test_autoflake:
@python3 -m autoflake --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:
@python3 -m tox
@ -116,6 +119,7 @@ lint:
just test_isort
just test_mypy
just test_autoflake
just test_pylama
@echo -e "\n\033[0;32m=== ALL DONE ===\033[0m\n"
tests:
@ -126,6 +130,7 @@ tests:
just test_isort
just test_mypy
just test_autoflake
just test_pylama
just test_pytest
@echo -e "\n\033[0;32m=== ALL DONE ===\033[0m\n"
@ -136,6 +141,8 @@ tests_full:
just test_black
just test_isort
just test_mypy
just test_autoflake
just test_pylama
just test_build
just test_tox
just test_tox_coverage

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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