Skip to content
This repository has been archived by the owner on Oct 27, 2024. It is now read-only.

Commit

Permalink
Changed typing
Browse files Browse the repository at this point in the history
  • Loading branch information
tygoee committed Aug 15, 2023
1 parent bbaf81b commit e4c3e24
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 17 deletions.
16 changes: 8 additions & 8 deletions src/install/filesize.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

from typing import TypeAlias

traditional: list[tuple[int, str | tuple[str, str]]] = [
SizeSystem: TypeAlias = list[tuple[int, str | tuple[str, str]]]

traditional: SizeSystem = [
(1024 ** 5, 'P'),
(1024 ** 4, 'T'),
(1024 ** 3, 'G'),
Expand All @@ -11,7 +13,7 @@
(1024 ** 0, 'B'),
]

alternative: list[tuple[int, str | tuple[str, str]]] = [
alternative: SizeSystem = [
(1024 ** 5, ' PB'),
(1024 ** 4, ' TB'),
(1024 ** 3, ' GB'),
Expand All @@ -20,7 +22,7 @@
(1024 ** 0, (' byte', ' bytes')),
]

verbose: list[tuple[int, str | tuple[str, str]]] = [
verbose: SizeSystem = [
(1024 ** 5, (' petabyte', ' petabytes')),
(1024 ** 4, (' terabyte', ' terabytes')),
(1024 ** 3, (' gigabyte', ' gigabytes')),
Expand All @@ -29,7 +31,7 @@
(1024 ** 0, (' byte', ' bytes')),
]

iec: list[tuple[int, str | tuple[str, str]]] = [
iec: SizeSystem = [
(1024 ** 5, 'Pi'),
(1024 ** 4, 'Ti'),
(1024 ** 3, 'Gi'),
Expand All @@ -38,7 +40,7 @@
(1024 ** 0, ''),
]

si: list[tuple[int, str | tuple[str, str]]] = [
si: SizeSystem = [
(1000 ** 5, 'P'),
(1000 ** 4, 'T'),
(1000 ** 3, 'G'),
Expand All @@ -47,10 +49,8 @@
(1000 ** 0, 'B'),
]

size_system: TypeAlias = list[tuple[int, str | tuple[str, str]]]


def size(bytes: int, system: size_system) -> str:
def size(bytes: int, system: SizeSystem) -> str:
"""Human-readable file size.
Using the traditional system, where a factor of 1024 is used::
Expand Down
11 changes: 7 additions & 4 deletions src/install/media.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
from os import path, get_terminal_size
from tqdm import tqdm
from typing import Any
from typing import Any, TypeAlias
from urllib import parse, request, error
from install.headers import headers
from install.url_generator import generate_url

MediaList: TypeAlias = list[dict[str, Any]]
Media: TypeAlias = dict[str, Any]

def prepare_media(total_size: int, install_path: str, mods: list[dict[str, Any]], resourcepacks: list[dict[str, Any]]) -> int:

def prepare_media(total_size: int, install_path: str, mods: MediaList, resourcepacks: MediaList) -> int:
"""Get the file size and check media validity while listing all media"""

def check_media_validity(media_list: list[dict[str, Any]], media_type: str) -> None:
def check_media_validity(media_list: MediaList, media_type: str) -> None:
"""Check for the modpack file validity"""
for media in media_list:
for key in ['type', 'slug', 'name']:
Expand All @@ -20,7 +23,7 @@ def check_media_validity(media_list: list[dict[str, Any]], media_type: str) -> N
raise KeyError(
f"The type '{media['type']}' does not exist: {media}.")

def get_headers(media: dict[str, Any], total_size: int) -> int:
def get_headers(media: Media, total_size: int) -> int:
"""Recieve the content-length headers"""
try:
size = int(request.urlopen(
Expand Down
6 changes: 4 additions & 2 deletions src/install/url_generator.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from os import path
from typing import Any
from typing import Any, TypeAlias
from urllib import parse

Media: TypeAlias = dict[str, Any]

def generate_url(media: dict[str, Any], install_path: str, folder: str) -> tuple[str, tuple[str, str]]:

def generate_url(media: Media, install_path: str, folder: str) -> tuple[str, tuple[str, str]]:
"""Generate an url to download"""

match media['type']:
Expand Down
23 changes: 20 additions & 3 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from json import load
from os import path, get_terminal_size, mkdir
from tqdm import tqdm
from typing import Any
from typing import Any, TypeAlias
from install import filesize, media

MediaList: TypeAlias = list[dict[str, Any]]
Media: TypeAlias = dict[str, Any]

def download_files(total_size: int, install_path: str, mods: list[dict[str, Any]], resourcepacks: list[dict[str, Any]]):

def download_files(total_size: int, install_path: str, mods: MediaList, resourcepacks: MediaList):
"""Download all files with a tqdm loading bar"""
if not path.isdir(path.join(install_path, 'mods')):
mkdir(path.join(install_path, 'mods'))
Expand Down Expand Up @@ -80,6 +83,20 @@ def install(manifest_file: str, install_path: str = path.dirname(path.realpath(_
"slug": "(slug)",
"name": "(filename)"
}
],
"resourcepacks": [
{
"type": "(type)",
"slug": "(slug)",
"name": "(filename)"
}
],
"shaderpacks": [
{
"type": "(type)",
"slug": "(slug)",
"name": "(filename)"
}
]
}
```
Expand All @@ -90,7 +107,7 @@ def install(manifest_file: str, install_path: str = path.dirname(path.realpath(_

# Import the manifest file
with open(manifest_file) as json_file:
manifest: dict[str, Any] = load(json_file)
manifest: Media = load(json_file)

# Check for validity
if manifest.get('minecraft', None) is None:
Expand Down

0 comments on commit e4c3e24

Please sign in to comment.